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 (((to != null) && (to.length > 0)) ||
087                            ((cc != null) && (cc.length > 0)) ||
088                            ((bcc != null) && (bcc.length > 0)) ||
089                            ((bulkAddresses != null) && (bulkAddresses.length > 0))) {
090    
091                            MailEngine.send(mailMessage);
092                    }
093            }
094    
095            protected void doMethodHandler(MethodHandler methodHandler)
096                    throws Exception {
097    
098                    methodHandler.invoke(HookFactory.getInstance());
099            }
100    
101            @Override
102            protected void doReceive(Message message) throws Exception {
103                    Object payload = message.getPayload();
104    
105                    if (payload instanceof MailMessage) {
106                            doMailMessage((MailMessage)payload);
107                    }
108                    else if (payload instanceof MethodHandler) {
109                            doMethodHandler((MethodHandler)payload);
110                    }
111            }
112    
113            protected InternetAddress filterInternetAddress(
114                    InternetAddress internetAddress) {
115    
116                    EmailAddressGenerator emailAddressGenerator =
117                            EmailAddressGeneratorFactory.getInstance();
118    
119                    if (emailAddressGenerator.isFake(internetAddress.getAddress())) {
120                            return null;
121                    }
122    
123                    return internetAddress;
124            }
125    
126            protected InternetAddress[] filterInternetAddresses(
127                    InternetAddress[] internetAddresses) {
128    
129                    if (internetAddresses == null) {
130                            return null;
131                    }
132    
133                    List<InternetAddress> filteredInternetAddresses =
134                            new ArrayList<InternetAddress>(internetAddresses.length);
135    
136                    for (InternetAddress internetAddress : internetAddresses) {
137                            InternetAddress filteredInternetAddress = filterInternetAddress(
138                                    internetAddress);
139    
140                            if (filteredInternetAddress != null) {
141                                    filteredInternetAddresses.add(filteredInternetAddress);
142                            }
143                    }
144    
145                    return filteredInternetAddresses.toArray(
146                            new InternetAddress[filteredInternetAddresses.size()]);
147            }
148    
149    }