001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.mail.messaging;
016    
017    import com.liferay.mail.util.HookFactory;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.mail.MailMessage;
021    import com.liferay.portal.kernel.messaging.Message;
022    import com.liferay.portal.kernel.messaging.MessageListener;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.MethodHandler;
025    import com.liferay.portal.security.auth.EmailAddressGenerator;
026    import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.util.mail.MailEngine;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    import javax.mail.internet.InternetAddress;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Wesley Gong
038     */
039    public class MailMessageListener implements MessageListener {
040    
041            public void receive(Message message) {
042                    try {
043                            doReceive(message);
044                    }
045                    catch (Exception e) {
046                            _log.error("Unable to process message " + message, e);
047                    }
048            }
049    
050            protected void doMailMessage(MailMessage mailMessage) throws Exception {
051                    InternetAddress[] auditTrail = InternetAddress.parse(
052                            PropsValues.MAIL_AUDIT_TRAIL);
053    
054                    if (auditTrail.length > 0) {
055                            InternetAddress[] bcc = mailMessage.getBCC();
056    
057                            if (bcc != null) {
058                                    InternetAddress[] allBCC = new InternetAddress[
059                                            bcc.length + auditTrail.length];
060    
061                                    ArrayUtil.combine(bcc, auditTrail, allBCC);
062    
063                                    mailMessage.setBCC(allBCC);
064                            }
065                            else {
066                                    mailMessage.setBCC(auditTrail);
067                            }
068                    }
069    
070                    InternetAddress from = filterInternetAddress(mailMessage.getFrom());
071    
072                    if (from == null) {
073                            return;
074                    }
075                    else {
076                            mailMessage.setFrom(from);
077                    }
078    
079                    InternetAddress[] to = filterInternetAddresses(mailMessage.getTo());
080    
081                    mailMessage.setTo(to);
082    
083                    InternetAddress[] cc = filterInternetAddresses(mailMessage.getCC());
084    
085                    mailMessage.setCC(cc);
086    
087                    InternetAddress[] bcc = filterInternetAddresses(mailMessage.getBCC());
088    
089                    mailMessage.setBCC(bcc);
090    
091                    InternetAddress[] bulkAddresses = filterInternetAddresses(
092                            mailMessage.getBulkAddresses());
093    
094                    mailMessage.setBulkAddresses(bulkAddresses);
095    
096                    if (((to != null) && (to.length > 0)) ||
097                            ((cc != null) && (cc.length > 0)) ||
098                            ((bcc != null) && (bcc.length > 0)) ||
099                            ((bulkAddresses != null) && (bulkAddresses.length > 0))) {
100    
101                            MailEngine.send(mailMessage);
102                    }
103            }
104    
105            protected void doMethodHandler(MethodHandler methodHandler)
106                    throws Exception {
107    
108                    methodHandler.invoke(HookFactory.getInstance());
109            }
110    
111            protected void doReceive(Message message) throws Exception {
112                    Object payload = message.getPayload();
113    
114                    if (payload instanceof MailMessage) {
115                            doMailMessage((MailMessage)payload);
116                    }
117                    else if (payload instanceof MethodHandler) {
118                            doMethodHandler((MethodHandler)payload);
119                    }
120            }
121    
122            protected InternetAddress filterInternetAddress(
123                    InternetAddress internetAddress) {
124    
125                    EmailAddressGenerator emailAddressGenerator =
126                            EmailAddressGeneratorFactory.getInstance();
127    
128                    if (emailAddressGenerator.isFake(internetAddress.getAddress())) {
129                            return null;
130                    }
131    
132                    return internetAddress;
133            }
134    
135            protected InternetAddress[] filterInternetAddresses(
136                    InternetAddress[] internetAddresses) {
137    
138                    if (internetAddresses == null) {
139                            return null;
140                    }
141    
142                    List<InternetAddress> filteredInternetAddresses =
143                            new ArrayList<InternetAddress>(internetAddresses.length);
144    
145                    for (InternetAddress internetAddress : internetAddresses) {
146                            InternetAddress filteredInternetAddress = filterInternetAddress(
147                                    internetAddress);
148    
149                            if (filteredInternetAddress != null) {
150                                    filteredInternetAddresses.add(filteredInternetAddress);
151                            }
152                    }
153    
154                    return filteredInternetAddresses.toArray(
155                            new InternetAddress[filteredInternetAddresses.size()]);
156            }
157    
158            private static Log _log = LogFactoryUtil.getLog(MailMessageListener.class);
159    
160    }