001package org.apache.archiva.redback.integration.mail;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.redback.configuration.UserConfiguration;
023import org.apache.archiva.redback.configuration.UserConfigurationKeys;
024import org.apache.archiva.redback.keys.AuthenticationKey;
025import org.apache.archiva.redback.policy.UserSecurityPolicy;
026import org.apache.archiva.redback.policy.UserValidationSettings;
027import org.apache.archiva.redback.system.SecuritySystem;
028import org.apache.commons.lang3.StringUtils;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031import org.springframework.mail.javamail.JavaMailSender;
032import org.springframework.stereotype.Service;
033
034import javax.inject.Inject;
035import javax.inject.Named;
036import javax.mail.Address;
037import javax.mail.Message;
038import javax.mail.MessagingException;
039import javax.mail.internet.AddressException;
040import javax.mail.internet.InternetAddress;
041import javax.mail.internet.MimeMessage;
042import java.io.UnsupportedEncodingException;
043import java.util.ArrayList;
044import java.util.Collection;
045import java.util.List;
046
047/**
048 * Mailer
049 *
050 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
051 */
052@Service( "mailer" )
053public class MailerImpl
054    implements Mailer
055{
056    protected Logger log = LoggerFactory.getLogger( getClass() );
057
058    @Inject
059    @Named( value = "mailGenerator#default" )
060    private MailGenerator generator;
061
062    @Inject
063    @Named( value = "mailSender" )
064    private JavaMailSender javaMailSender;
065
066    @Inject
067    private SecuritySystem securitySystem;
068
069    @Inject
070    @Named( value = "userConfiguration#default" )
071    private UserConfiguration config;
072
073    public void sendAccountValidationEmail( Collection<String> recipients, AuthenticationKey authkey, String baseUrl )
074    {
075        String content = generator.generateMail( "newAccountValidationEmail", authkey, baseUrl );
076
077        UserSecurityPolicy policy = securitySystem.getPolicy();
078        UserValidationSettings validation = policy.getUserValidationSettings();
079        sendMessage( recipients, validation.getEmailSubject(), content );
080    }
081
082    public void sendPasswordResetEmail( Collection<String> recipients, AuthenticationKey authkey, String baseUrl )
083    {
084        String content = generator.generateMail( "passwordResetEmail", authkey, baseUrl );
085
086        UserSecurityPolicy policy = securitySystem.getPolicy();
087        UserValidationSettings validation = policy.getUserValidationSettings();
088        sendMessage( recipients, validation.getEmailSubject(), content );
089    }
090
091    public void sendMessage( Collection<String> recipients, String subject, String content )
092    {
093        if ( recipients.isEmpty() )
094        {
095            log.warn( "Mail Not Sent - No mail recipients for email. subject [{}]", subject );
096            return;
097        }
098
099        String fromAddress = config.getString( UserConfigurationKeys.EMAIL_FROM_ADDRESS );
100        String fromName = config.getString( UserConfigurationKeys.EMAIL_FROM_NAME );
101
102        if ( StringUtils.isEmpty( fromAddress ) )
103        {
104            fromAddress = System.getProperty( "user.name" ) + "@localhost";
105        }
106
107        // TODO: Allow for configurable message headers.
108
109        try
110        {
111
112            MimeMessage message = javaMailSender.createMimeMessage();
113
114            message.setSubject( subject );
115            message.setText( content );
116
117            InternetAddress from = new InternetAddress( fromAddress, fromName );
118
119            message.setFrom( from );
120
121            List<Address> tos = new ArrayList<Address>();
122
123            for ( String mailbox : recipients )
124            {
125                InternetAddress to = new InternetAddress( mailbox.trim() );
126
127                tos.add( to );
128            }
129
130            message.setRecipients( Message.RecipientType.TO, tos.toArray( new Address[tos.size()] ) );
131
132            log.debug( "mail content {}", content );
133
134            javaMailSender.send( message );
135        }
136        catch ( AddressException e )
137        {
138            log.error( "Unable to send message, subject [{}]", subject, e );
139        }
140        catch ( MessagingException e )
141        {
142            log.error( "Unable to send message, subject [{}]", subject, e );
143        }
144        catch ( UnsupportedEncodingException e )
145        {
146            log.error( "Unable to send message, subject [{}]", subject, e );
147        }
148    }
149}