1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.MethodHandler;
25  import com.liferay.portal.security.auth.EmailAddressGenerator;
26  import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
27  import com.liferay.portal.util.PropsValues;
28  import com.liferay.util.mail.MailEngine;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import javax.mail.internet.InternetAddress;
34  
35  /**
36   * <a href="MailMessageListener.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   * @author Wesley Gong
40   */
41  public class MailMessageListener implements MessageListener {
42  
43      public void receive(Message message) {
44          try {
45              doReceive(message);
46          }
47          catch (Exception e) {
48              _log.error("Unable to process message " + message, e);
49          }
50      }
51  
52      protected void doMailMessage(MailMessage mailMessage) throws Exception {
53          InternetAddress[] auditTrail = InternetAddress.parse(
54              PropsValues.MAIL_AUDIT_TRAIL);
55  
56          if (auditTrail.length > 0) {
57              InternetAddress[] bcc = mailMessage.getBCC();
58  
59              if (bcc != null) {
60                  InternetAddress[] allBCC = new InternetAddress[
61                      bcc.length + auditTrail.length];
62  
63                  ArrayUtil.combine(bcc, auditTrail, allBCC);
64  
65                  mailMessage.setBCC(allBCC);
66              }
67              else {
68                  mailMessage.setBCC(auditTrail);
69              }
70          }
71  
72          InternetAddress from = filterInternetAddress(mailMessage.getFrom());
73  
74          if (from == null) {
75              return;
76          }
77          else {
78              mailMessage.setFrom(from);
79          }
80  
81          InternetAddress[] to = filterInternetAddresses(mailMessage.getTo());
82  
83          mailMessage.setTo(to);
84  
85          InternetAddress[] cc = filterInternetAddresses(mailMessage.getCC());
86  
87          mailMessage.setCC(cc);
88  
89          InternetAddress[] bcc = filterInternetAddresses(mailMessage.getBCC());
90  
91          mailMessage.setBCC(bcc);
92  
93          InternetAddress[] bulkAddresses = filterInternetAddresses(
94              mailMessage.getBulkAddresses());
95  
96          mailMessage.setBulkAddresses(bulkAddresses);
97  
98          if (((to != null) && (to.length > 0)) ||
99              ((cc != null) && (cc.length > 0)) ||
100             ((bcc != null) && (bcc.length > 0)) ||
101             ((bulkAddresses != null) && (bulkAddresses.length > 0))) {
102 
103             MailEngine.send(mailMessage);
104         }
105     }
106 
107     protected void doMethodHandler(MethodHandler methodHandler)
108         throws Exception {
109 
110         methodHandler.invoke(HookFactory.getInstance());
111     }
112 
113     protected void doReceive(Message message) throws Exception {
114         Object payload = message.getPayload();
115 
116         if (payload instanceof MailMessage) {
117             doMailMessage((MailMessage)payload);
118         }
119         else if (payload instanceof MethodHandler) {
120             doMethodHandler((MethodHandler)payload);
121         }
122     }
123 
124     protected InternetAddress filterInternetAddress(
125         InternetAddress internetAddress) {
126 
127         EmailAddressGenerator emailAddressGenerator =
128             EmailAddressGeneratorFactory.getInstance();
129 
130         if (emailAddressGenerator.isFake(internetAddress.getAddress())) {
131             return null;
132         }
133 
134         return internetAddress;
135     }
136 
137     protected InternetAddress[] filterInternetAddresses(
138         InternetAddress[] internetAddresses) {
139 
140         if (internetAddresses == null) {
141             return null;
142         }
143 
144         List<InternetAddress> filteredInternetAddresses =
145             new ArrayList<InternetAddress>(internetAddresses.length);
146 
147         for (InternetAddress internetAddress : internetAddresses) {
148             InternetAddress filteredInternetAddress = filterInternetAddress(
149                 internetAddress);
150 
151             if (filteredInternetAddress != null) {
152                 filteredInternetAddresses.add(filteredInternetAddress);
153             }
154         }
155 
156         return filteredInternetAddresses.toArray(
157             new InternetAddress[filteredInternetAddresses.size()]);
158     }
159 
160     private static Log _log = LogFactoryUtil.getLog(MailMessageListener.class);
161 
162 }