001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.MethodInvoker;
025    import com.liferay.portal.kernel.util.MethodWrapper;
026    import com.liferay.portal.security.auth.EmailAddressGenerator;
027    import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
028    import com.liferay.portal.util.PropsValues;
029    import com.liferay.util.mail.MailEngine;
030    
031    import java.util.ArrayList;
032    import java.util.List;
033    
034    import javax.mail.internet.InternetAddress;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Wesley Gong
039     */
040    public class MailMessageListener implements MessageListener {
041    
042            public void receive(Message message) {
043                    try {
044                            doReceive(message);
045                    }
046                    catch (Exception e) {
047                            _log.error("Unable to process message " + message, e);
048                    }
049            }
050    
051            protected void doMailMessage(MailMessage mailMessage) throws Exception {
052                    InternetAddress[] auditTrail = InternetAddress.parse(
053                            PropsValues.MAIL_AUDIT_TRAIL);
054    
055                    if (auditTrail.length > 0) {
056                            InternetAddress[] bcc = mailMessage.getBCC();
057    
058                            if (bcc != null) {
059                                    InternetAddress[] allBCC = new InternetAddress[
060                                            bcc.length + auditTrail.length];
061    
062                                    ArrayUtil.combine(bcc, auditTrail, allBCC);
063    
064                                    mailMessage.setBCC(allBCC);
065                            }
066                            else {
067                                    mailMessage.setBCC(auditTrail);
068                            }
069                    }
070    
071                    InternetAddress from = filterInternetAddress(mailMessage.getFrom());
072    
073                    if (from == null) {
074                            return;
075                    }
076                    else {
077                            mailMessage.setFrom(from);
078                    }
079    
080                    InternetAddress[] to = filterInternetAddresses(mailMessage.getTo());
081    
082                    mailMessage.setTo(to);
083    
084                    InternetAddress[] cc = filterInternetAddresses(mailMessage.getCC());
085    
086                    mailMessage.setCC(cc);
087    
088                    InternetAddress[] bcc = filterInternetAddresses(mailMessage.getBCC());
089    
090                    mailMessage.setBCC(bcc);
091    
092                    InternetAddress[] bulkAddresses = filterInternetAddresses(
093                            mailMessage.getBulkAddresses());
094    
095                    mailMessage.setBulkAddresses(bulkAddresses);
096    
097                    if (((to != null) && (to.length > 0)) ||
098                            ((cc != null) && (cc.length > 0)) ||
099                            ((bcc != null) && (bcc.length > 0)) ||
100                            ((bulkAddresses != null) && (bulkAddresses.length > 0))) {
101    
102                            MailEngine.send(mailMessage);
103                    }
104            }
105    
106            protected void doMethodWrapper(MethodWrapper methodWrapper)
107                    throws Exception {
108    
109                    MethodInvoker.invoke(methodWrapper, HookFactory.getInstance());
110            }
111    
112            protected void doReceive(Message message) throws Exception {
113                    Object payload = message.getPayload();
114    
115                    if (payload instanceof MailMessage) {
116                            doMailMessage((MailMessage)payload);
117                    }
118                    else if (payload instanceof MethodWrapper) {
119                            doMethodWrapper((MethodWrapper)payload);
120                    }
121            }
122    
123            protected InternetAddress filterInternetAddress(
124                    InternetAddress internetAddress) {
125    
126                    EmailAddressGenerator emailAddressGenerator =
127                            EmailAddressGeneratorFactory.getInstance();
128    
129                    if (emailAddressGenerator.isFake(internetAddress.getAddress())) {
130                            return null;
131                    }
132    
133                    return internetAddress;
134            }
135    
136            protected InternetAddress[] filterInternetAddresses(
137                    InternetAddress[] internetAddresses) {
138    
139                    if (internetAddresses == null) {
140                            return null;
141                    }
142    
143                    List<InternetAddress> filteredInternetAddresses =
144                            new ArrayList<InternetAddress>(internetAddresses.length);
145    
146                    for (InternetAddress internetAddress : internetAddresses) {
147                            InternetAddress filteredInternetAddress = filterInternetAddress(
148                                    internetAddress);
149    
150                            if (filteredInternetAddress != null) {
151                                    filteredInternetAddresses.add(filteredInternetAddress);
152                            }
153                    }
154    
155                    return filteredInternetAddresses.toArray(
156                            new InternetAddress[filteredInternetAddresses.size()]);
157            }
158    
159            private static Log _log = LogFactoryUtil.getLog(MailMessageListener.class);
160    
161    }