1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.mail.messaging;
16  
17  import com.liferay.mail.util.HookFactory;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.mail.MailMessage;
21  import com.liferay.portal.kernel.messaging.Message;
22  import com.liferay.portal.kernel.messaging.MessageListener;
23  import com.liferay.portal.kernel.util.ArrayUtil;
24  import com.liferay.portal.kernel.util.MethodInvoker;
25  import com.liferay.portal.kernel.util.MethodWrapper;
26  import com.liferay.portal.security.auth.EmailAddressGenerator;
27  import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
28  import com.liferay.portal.util.PropsValues;
29  import com.liferay.util.mail.MailEngine;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import javax.mail.internet.InternetAddress;
35  
36  /**
37   * <a href="MailMessageListener.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Wesley Gong
41   */
42  public class MailMessageListener implements MessageListener {
43  
44      public void receive(Message message) {
45          try {
46              doReceive(message);
47          }
48          catch (Exception e) {
49              _log.error("Unable to process message " + message, e);
50          }
51      }
52  
53      protected void doMailMessage(MailMessage mailMessage) throws Exception {
54          InternetAddress[] auditTrail = InternetAddress.parse(
55              PropsValues.MAIL_AUDIT_TRAIL);
56  
57          if (auditTrail.length > 0) {
58              InternetAddress[] bcc = mailMessage.getBCC();
59  
60              if (bcc != null) {
61                  InternetAddress[] allBCC = new InternetAddress[
62                      bcc.length + auditTrail.length];
63  
64                  ArrayUtil.combine(bcc, auditTrail, allBCC);
65  
66                  mailMessage.setBCC(allBCC);
67              }
68              else {
69                  mailMessage.setBCC(auditTrail);
70              }
71          }
72  
73          InternetAddress from = filterInternetAddress(mailMessage.getFrom());
74  
75          if (from == null) {
76              return;
77          }
78          else {
79              mailMessage.setFrom(from);
80          }
81  
82          InternetAddress[] to = filterInternetAddresses(mailMessage.getTo());
83  
84          mailMessage.setTo(to);
85  
86          InternetAddress[] cc = filterInternetAddresses(mailMessage.getCC());
87  
88          mailMessage.setCC(cc);
89  
90          InternetAddress[] bcc = filterInternetAddresses(mailMessage.getBCC());
91  
92          mailMessage.setBCC(bcc);
93  
94          InternetAddress[] bulkAddresses = filterInternetAddresses(
95              mailMessage.getBulkAddresses());
96  
97          mailMessage.setBulkAddresses(bulkAddresses);
98  
99          if (((to != null) && (to.length > 0)) ||
100             ((cc != null) && (cc.length > 0)) ||
101             ((bcc != null) && (bcc.length > 0)) ||
102             ((bulkAddresses != null) && (bulkAddresses.length > 0))) {
103 
104             MailEngine.send(mailMessage);
105         }
106     }
107 
108     protected void doMethodWrapper(MethodWrapper methodWrapper)
109         throws Exception {
110 
111         MethodInvoker.invoke(methodWrapper, HookFactory.getInstance());
112     }
113 
114     protected void doReceive(Message message) throws Exception {
115         Object payload = message.getPayload();
116 
117         if (payload instanceof MailMessage) {
118             doMailMessage((MailMessage)payload);
119         }
120         else if (payload instanceof MethodWrapper) {
121             doMethodWrapper((MethodWrapper)payload);
122         }
123     }
124 
125     protected InternetAddress filterInternetAddress(
126         InternetAddress internetAddress) {
127 
128         EmailAddressGenerator emailAddressGenerator =
129             EmailAddressGeneratorFactory.getInstance();
130 
131         if (emailAddressGenerator.isFake(internetAddress.getAddress())) {
132             return null;
133         }
134 
135         return internetAddress;
136     }
137 
138     protected InternetAddress[] filterInternetAddresses(
139         InternetAddress[] internetAddresses) {
140 
141         if (internetAddresses == null) {
142             return null;
143         }
144 
145         List<InternetAddress> filteredInternetAddresses =
146             new ArrayList<InternetAddress>(internetAddresses.length);
147 
148         for (InternetAddress internetAddress : internetAddresses) {
149             InternetAddress filteredInternetAddress = filterInternetAddress(
150                 internetAddress);
151 
152             if (filteredInternetAddress != null) {
153                 filteredInternetAddresses.add(filteredInternetAddress);
154             }
155         }
156 
157         return filteredInternetAddresses.toArray(
158             new InternetAddress[filteredInternetAddresses.size()]);
159     }
160 
161     private static Log _log = LogFactoryUtil.getLog(MailMessageListener.class);
162 
163 }