001    /**
002     * Copyright (c) 2000-2012 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.BaseMessageListener;
022    import com.liferay.portal.kernel.messaging.Message;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.CharPool;
025    import com.liferay.portal.kernel.util.MethodHandler;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.security.auth.EmailAddressGenerator;
028    import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
029    import com.liferay.portal.util.PropsValues;
030    import com.liferay.util.mail.MailEngine;
031    
032    import java.util.ArrayList;
033    import java.util.List;
034    
035    import javax.mail.internet.InternetAddress;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Wesley Gong
040     * @author Zsolt Balogh
041     */
042    public class MailMessageListener extends BaseMessageListener {
043    
044            protected void doMailMessage(MailMessage mailMessage) throws Exception {
045                    InternetAddress[] auditTrail = InternetAddress.parse(
046                            PropsValues.MAIL_AUDIT_TRAIL);
047    
048                    if (auditTrail.length > 0) {
049                            InternetAddress[] bcc = mailMessage.getBCC();
050    
051                            if (bcc != null) {
052                                    InternetAddress[] allBCC = new InternetAddress[
053                                            bcc.length + auditTrail.length];
054    
055                                    ArrayUtil.combine(bcc, auditTrail, allBCC);
056    
057                                    mailMessage.setBCC(allBCC);
058                            }
059                            else {
060                                    mailMessage.setBCC(auditTrail);
061                            }
062                    }
063    
064                    InternetAddress from = filterInternetAddress(mailMessage.getFrom());
065    
066                    if (from == null) {
067                            return;
068                    }
069                    else {
070                            mailMessage.setFrom(from);
071                    }
072    
073                    InternetAddress[] to = filterInternetAddresses(mailMessage.getTo());
074    
075                    mailMessage.setTo(to);
076    
077                    InternetAddress[] cc = filterInternetAddresses(mailMessage.getCC());
078    
079                    mailMessage.setCC(cc);
080    
081                    InternetAddress[] bcc = filterInternetAddresses(mailMessage.getBCC());
082    
083                    mailMessage.setBCC(bcc);
084    
085                    InternetAddress[] bulkAddresses = filterInternetAddresses(
086                            mailMessage.getBulkAddresses());
087    
088                    mailMessage.setBulkAddresses(bulkAddresses);
089    
090                    if (((to != null) && (to.length > 0)) ||
091                            ((cc != null) && (cc.length > 0)) ||
092                            ((bcc != null) && (bcc.length > 0)) ||
093                            ((bulkAddresses != null) && (bulkAddresses.length > 0))) {
094    
095                            MailEngine.send(mailMessage);
096                    }
097            }
098    
099            protected void doMethodHandler(MethodHandler methodHandler)
100                    throws Exception {
101    
102                    methodHandler.invoke(HookFactory.getInstance());
103            }
104    
105            @Override
106            protected void doReceive(Message message) throws Exception {
107                    Object payload = message.getPayload();
108    
109                    if (payload instanceof MailMessage) {
110                            doMailMessage((MailMessage)payload);
111                    }
112                    else if (payload instanceof MethodHandler) {
113                            doMethodHandler((MethodHandler)payload);
114                    }
115            }
116    
117            protected InternetAddress filterInternetAddress(
118                    InternetAddress internetAddress) {
119    
120                    EmailAddressGenerator emailAddressGenerator =
121                            EmailAddressGeneratorFactory.getInstance();
122    
123                    if (emailAddressGenerator.isFake(internetAddress.getAddress())) {
124                            return null;
125                    }
126    
127                    String address = internetAddress.toString();
128    
129                    for (char c : address.toCharArray()) {
130                            if ((c == CharPool.NEW_LINE) || (c == CharPool.RETURN)) {
131                                    if (_log.isWarnEnabled()) {
132                                            StringBundler sb = new StringBundler(4);
133    
134                                            sb.append("Email address ");
135                                            sb.append(address);
136                                            sb.append(" contains line break characters and will be ");
137                                            sb.append("excluded from the email");
138    
139                                            _log.warn(sb.toString());
140                                    }
141    
142                                    return null;
143                            }
144                    }
145    
146                    return internetAddress;
147            }
148    
149            protected InternetAddress[] filterInternetAddresses(
150                    InternetAddress[] internetAddresses) {
151    
152                    if (internetAddresses == null) {
153                            return null;
154                    }
155    
156                    List<InternetAddress> filteredInternetAddresses =
157                            new ArrayList<InternetAddress>(internetAddresses.length);
158    
159                    for (InternetAddress internetAddress : internetAddresses) {
160                            InternetAddress filteredInternetAddress = filterInternetAddress(
161                                    internetAddress);
162    
163                            if (filteredInternetAddress != null) {
164                                    filteredInternetAddresses.add(filteredInternetAddress);
165                            }
166                    }
167    
168                    return filteredInternetAddresses.toArray(
169                            new InternetAddress[filteredInternetAddresses.size()]);
170            }
171    
172            private static Log _log = LogFactoryUtil.getLog(MailMessageListener.class);
173    
174    }