]> source.dussan.org Git - archiva.git/blob
273e07f29332153167cc960845b9356338448b92
[archiva.git] /
1 package org.apache.archiva.redback.integration.mail;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import java.io.UnsupportedEncodingException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.mail.Address;
30 import javax.mail.Message;
31 import javax.mail.MessagingException;
32 import javax.mail.internet.AddressException;
33 import javax.mail.internet.InternetAddress;
34 import javax.mail.internet.MimeMessage;
35
36 import org.apache.archiva.redback.configuration.UserConfiguration;
37 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
38 import org.apache.archiva.redback.keys.AuthenticationKey;
39 import org.apache.archiva.redback.policy.UserSecurityPolicy;
40 import org.apache.archiva.redback.policy.UserValidationSettings;
41 import org.apache.archiva.redback.system.SecuritySystem;
42 import org.apache.commons.lang.StringUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.mail.javamail.JavaMailSender;
46 import org.springframework.stereotype.Service;
47
48 /**
49  * Mailer
50  *
51  * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
52  */
53 @Service( "mailer" )
54 public class MailerImpl
55     implements Mailer
56 {
57     protected Logger log = LoggerFactory.getLogger( getClass() );
58
59     @Inject
60     @Named( value = "mailGenerator#velocity" )
61     private MailGenerator generator;
62
63     @Inject
64     @Named( value = "mailSender" )
65     private JavaMailSender javaMailSender;
66
67     @Inject
68     private SecuritySystem securitySystem;
69
70     @Inject
71     @Named( value = "userConfiguration" )
72     private UserConfiguration config;
73
74     public void sendAccountValidationEmail( Collection<String> recipients, AuthenticationKey authkey, String baseUrl )
75     {
76         String content = generator.generateMail( "newAccountValidationEmail", authkey, baseUrl );
77
78         UserSecurityPolicy policy = securitySystem.getPolicy();
79         UserValidationSettings validation = policy.getUserValidationSettings();
80         sendMessage( recipients, validation.getEmailSubject(), content );
81     }
82
83     public void sendPasswordResetEmail( Collection<String> recipients, AuthenticationKey authkey, String baseUrl )
84     {
85         String content = generator.generateMail( "passwordResetEmail", authkey, baseUrl );
86
87         UserSecurityPolicy policy = securitySystem.getPolicy();
88         UserValidationSettings validation = policy.getUserValidationSettings();
89         sendMessage( recipients, validation.getEmailSubject(), content );
90     }
91
92     public void sendMessage( Collection<String> recipients, String subject, String content )
93     {
94         if ( recipients.isEmpty() )
95         {
96             log.warn( "Mail Not Sent - No mail recipients for email. subject [" + subject + "]" );
97             return;
98         }
99
100         String fromAddress = config.getString( UserConfigurationKeys.EMAIL_FROM_ADDRESS );
101         String fromName = config.getString( UserConfigurationKeys.EMAIL_FROM_NAME );
102
103         if ( StringUtils.isEmpty( fromAddress ) )
104         {
105             fromAddress = System.getProperty( "user.name" ) + "@localhost";
106         }
107
108         // TODO: Allow for configurable message headers.
109
110         try
111         {
112
113             MimeMessage message = javaMailSender.createMimeMessage();
114
115             message.setSubject( subject );
116             message.setText( content );
117
118             InternetAddress from = new InternetAddress( fromAddress, fromName );
119
120             message.setFrom( from );
121
122             List<Address> tos = new ArrayList<Address>();
123
124             for ( String mailbox : recipients )
125             {
126                 InternetAddress to = new InternetAddress( mailbox.trim() );
127
128                 tos.add( to );
129             }
130
131             message.setRecipients( Message.RecipientType.TO, tos.toArray( new Address[tos.size()] ) );
132
133             log.debug( "mail content {}", content );
134
135             javaMailSender.send( message );
136         }
137         catch ( AddressException e )
138         {
139             log.error( "Unable to send message, subject [" + subject + "]", e );
140         }
141         catch ( MessagingException e )
142         {
143             log.error( "Unable to send message, subject [" + subject + "]", e );
144         }
145         catch ( UnsupportedEncodingException e )
146         {
147             log.error( "Unable to send message, subject [" + subject + "]", e );
148         }
149     }
150 }