1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.mail.messaging;
24  
25  import com.liferay.mail.util.HookFactory;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.mail.MailMessage;
29  import com.liferay.portal.kernel.messaging.Message;
30  import com.liferay.portal.kernel.messaging.MessageListener;
31  import com.liferay.portal.kernel.util.ArrayUtil;
32  import com.liferay.portal.kernel.util.MethodInvoker;
33  import com.liferay.portal.kernel.util.MethodWrapper;
34  import com.liferay.portal.security.auth.EmailAddressGenerator;
35  import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
36  import com.liferay.portal.util.PropsValues;
37  import com.liferay.util.mail.MailEngine;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  import javax.mail.internet.InternetAddress;
43  
44  /**
45   * <a href="MailMessageListener.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Wesley Gong
49   */
50  public class MailMessageListener implements MessageListener {
51  
52      public void receive(Message message) {
53          try {
54              doReceive(message);
55          }
56          catch (Exception e) {
57              _log.error("Unable to process message " + message, e);
58          }
59      }
60  
61      protected void doMailMessage(MailMessage mailMessage) throws Exception {
62          InternetAddress[] auditTrail = InternetAddress.parse(
63              PropsValues.MAIL_AUDIT_TRAIL);
64  
65          if (auditTrail.length > 0) {
66              InternetAddress[] bcc = mailMessage.getBCC();
67  
68              if (bcc != null) {
69                  InternetAddress[] allBCC = new InternetAddress[
70                      bcc.length + auditTrail.length];
71  
72                  ArrayUtil.combine(bcc, auditTrail, allBCC);
73  
74                  mailMessage.setBCC(allBCC);
75              }
76              else {
77                  mailMessage.setBCC(auditTrail);
78              }
79          }
80  
81          InternetAddress from = filterInternetAddress(mailMessage.getFrom());
82  
83          if (from == null) {
84              return;
85          }
86          else {
87              mailMessage.setFrom(from);
88          }
89  
90          InternetAddress[] to = filterInternetAddresses(mailMessage.getTo());
91  
92          mailMessage.setTo(to);
93  
94          InternetAddress[] cc = filterInternetAddresses(mailMessage.getCC());
95  
96          mailMessage.setCC(cc);
97  
98          InternetAddress[] bcc = filterInternetAddresses(mailMessage.getBCC());
99  
100         mailMessage.setBCC(bcc);
101 
102         InternetAddress[] bulkAddresses = filterInternetAddresses(
103             mailMessage.getBulkAddresses());
104 
105         mailMessage.setBulkAddresses(bulkAddresses);
106 
107         if (((to != null) && (to.length > 0)) ||
108             ((cc != null) && (cc.length > 0)) ||
109             ((bcc != null) && (bcc.length > 0)) ||
110             ((bulkAddresses != null) && (bulkAddresses.length > 0))) {
111 
112             MailEngine.send(mailMessage);
113         }
114     }
115 
116     protected void doMethodWrapper(MethodWrapper methodWrapper)
117         throws Exception {
118 
119         MethodInvoker.invoke(methodWrapper, HookFactory.getInstance());
120     }
121 
122     protected void doReceive(Message message) throws Exception {
123         Object payload = message.getPayload();
124 
125         if (payload instanceof MailMessage) {
126             doMailMessage((MailMessage)payload);
127         }
128         else if (payload instanceof MethodWrapper) {
129             doMethodWrapper((MethodWrapper)payload);
130         }
131     }
132 
133     protected InternetAddress filterInternetAddress(
134         InternetAddress internetAddress) {
135 
136         EmailAddressGenerator emailAddressGenerator =
137             EmailAddressGeneratorFactory.getInstance();
138 
139         if (emailAddressGenerator.isFake(internetAddress.getAddress())) {
140             return null;
141         }
142 
143         return internetAddress;
144     }
145 
146     protected InternetAddress[] filterInternetAddresses(
147         InternetAddress[] internetAddresses) {
148 
149         if (internetAddresses == null) {
150             return null;
151         }
152 
153         List<InternetAddress> filteredInternetAddresses =
154             new ArrayList<InternetAddress>(internetAddresses.length);
155 
156         for (InternetAddress internetAddress : internetAddresses) {
157             InternetAddress filteredInternetAddress = filterInternetAddress(
158                 internetAddress);
159 
160             if (filteredInternetAddress != null) {
161                 filteredInternetAddresses.add(filteredInternetAddress);
162             }
163         }
164 
165         return filteredInternetAddresses.toArray(
166             new InternetAddress[filteredInternetAddresses.size()]);
167     }
168 
169     private static Log _log = LogFactoryUtil.getLog(MailMessageListener.class);
170 
171 }