001    /**
002     * Copyright (c) 2000-2013 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.mail.MailMessage;
019    import com.liferay.portal.kernel.messaging.BaseMessageListener;
020    import com.liferay.portal.kernel.messaging.Message;
021    import com.liferay.portal.kernel.util.ArrayUtil;
022    import com.liferay.portal.kernel.util.MethodHandler;
023    import com.liferay.portal.security.auth.EmailAddressGenerator;
024    import com.liferay.portal.security.auth.EmailAddressGeneratorFactory;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.util.mail.MailEngine;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    import javax.mail.internet.InternetAddress;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Wesley Gong
036     * @author Zsolt Balogh
037     */
038    public class MailMessageListener extends BaseMessageListener {
039    
040            protected void doMailMessage(MailMessage mailMessage) throws Exception {
041                    InternetAddress[] auditTrail = InternetAddress.parse(
042                            PropsValues.MAIL_AUDIT_TRAIL);
043    
044                    if (auditTrail.length > 0) {
045                            InternetAddress[] bcc = mailMessage.getBCC();
046    
047                            if (bcc != null) {
048                                    InternetAddress[] allBCC = new InternetAddress[
049                                            bcc.length + auditTrail.length];
050    
051                                    ArrayUtil.combine(bcc, auditTrail, allBCC);
052    
053                                    mailMessage.setBCC(allBCC);
054                            }
055                            else {
056                                    mailMessage.setBCC(auditTrail);
057                            }
058                    }
059    
060                    InternetAddress from = filterInternetAddress(mailMessage.getFrom());
061    
062                    if (from == null) {
063                            return;
064                    }
065                    else {
066                            mailMessage.setFrom(from);
067                    }
068    
069                    InternetAddress[] to = filterInternetAddresses(mailMessage.getTo());
070    
071                    mailMessage.setTo(to);
072    
073                    InternetAddress[] cc = filterInternetAddresses(mailMessage.getCC());
074    
075                    mailMessage.setCC(cc);
076    
077                    InternetAddress[] bcc = filterInternetAddresses(mailMessage.getBCC());
078    
079                    mailMessage.setBCC(bcc);
080    
081                    InternetAddress[] bulkAddresses = filterInternetAddresses(
082                            mailMessage.getBulkAddresses());
083    
084                    mailMessage.setBulkAddresses(bulkAddresses);
085    
086                    if (ArrayUtil.isNotEmpty(to) || ArrayUtil.isNotEmpty(cc) ||
087                            ArrayUtil.isNotEmpty(bcc) || ArrayUtil.isNotEmpty(bulkAddresses)) {
088    
089                            MailEngine.send(mailMessage);
090                    }
091            }
092    
093            protected void doMethodHandler(MethodHandler methodHandler)
094                    throws Exception {
095    
096                    methodHandler.invoke(HookFactory.getInstance());
097            }
098    
099            @Override
100            protected void doReceive(Message message) throws Exception {
101                    Object payload = message.getPayload();
102    
103                    if (payload instanceof MailMessage) {
104                            doMailMessage((MailMessage)payload);
105                    }
106                    else if (payload instanceof MethodHandler) {
107                            doMethodHandler((MethodHandler)payload);
108                    }
109            }
110    
111            protected InternetAddress filterInternetAddress(
112                    InternetAddress internetAddress) {
113    
114                    EmailAddressGenerator emailAddressGenerator =
115                            EmailAddressGeneratorFactory.getInstance();
116    
117                    if (emailAddressGenerator.isFake(internetAddress.getAddress())) {
118                            return null;
119                    }
120    
121                    return internetAddress;
122            }
123    
124            protected InternetAddress[] filterInternetAddresses(
125                    InternetAddress[] internetAddresses) {
126    
127                    if (internetAddresses == null) {
128                            return null;
129                    }
130    
131                    List<InternetAddress> filteredInternetAddresses =
132                            new ArrayList<InternetAddress>(internetAddresses.length);
133    
134                    for (InternetAddress internetAddress : internetAddresses) {
135                            InternetAddress filteredInternetAddress = filterInternetAddress(
136                                    internetAddress);
137    
138                            if (filteredInternetAddress != null) {
139                                    filteredInternetAddresses.add(filteredInternetAddress);
140                            }
141                    }
142    
143                    return filteredInternetAddresses.toArray(
144                            new InternetAddress[filteredInternetAddresses.size()]);
145            }
146    
147    }