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 from = filterInternetAddress(mailMessage.getFrom());
042    
043                    if (from == null) {
044                            return;
045                    }
046    
047                    mailMessage.setFrom(from);
048    
049                    InternetAddress[] auditTrail = InternetAddress.parse(
050                            PropsValues.MAIL_AUDIT_TRAIL);
051    
052                    if (auditTrail.length > 0) {
053                            InternetAddress[] bcc = mailMessage.getBCC();
054    
055                            if (bcc != null) {
056                                    InternetAddress[] allBCC = new InternetAddress[
057                                            bcc.length + auditTrail.length];
058    
059                                    ArrayUtil.combine(bcc, auditTrail, allBCC);
060    
061                                    mailMessage.setBCC(allBCC);
062                            }
063                            else {
064                                    mailMessage.setBCC(auditTrail);
065                            }
066                    }
067    
068                    InternetAddress[] to = filterInternetAddresses(mailMessage.getTo());
069    
070                    mailMessage.setTo(to);
071    
072                    InternetAddress[] cc = filterInternetAddresses(mailMessage.getCC());
073    
074                    mailMessage.setCC(cc);
075    
076                    InternetAddress[] bcc = filterInternetAddresses(mailMessage.getBCC());
077    
078                    mailMessage.setBCC(bcc);
079    
080                    InternetAddress[] bulkAddresses = filterInternetAddresses(
081                            mailMessage.getBulkAddresses());
082    
083                    mailMessage.setBulkAddresses(bulkAddresses);
084    
085                    InternetAddress[] replyTo = filterInternetAddresses(
086                            mailMessage.getReplyTo());
087    
088                    mailMessage.setReplyTo(replyTo);
089    
090                    if (ArrayUtil.isNotEmpty(to) || ArrayUtil.isNotEmpty(cc) ||
091                            ArrayUtil.isNotEmpty(bcc) || ArrayUtil.isNotEmpty(bulkAddresses)) {
092    
093                            MailEngine.send(mailMessage);
094                    }
095            }
096    
097            protected void doMethodHandler(MethodHandler methodHandler)
098                    throws Exception {
099    
100                    methodHandler.invoke(HookFactory.getInstance());
101            }
102    
103            @Override
104            protected void doReceive(Message message) throws Exception {
105                    Object payload = message.getPayload();
106    
107                    if (payload instanceof MailMessage) {
108                            doMailMessage((MailMessage)payload);
109                    }
110                    else if (payload instanceof MethodHandler) {
111                            doMethodHandler((MethodHandler)payload);
112                    }
113            }
114    
115            protected InternetAddress filterInternetAddress(
116                    InternetAddress internetAddress) {
117    
118                    EmailAddressGenerator emailAddressGenerator =
119                            EmailAddressGeneratorFactory.getInstance();
120    
121                    if (emailAddressGenerator.isFake(internetAddress.getAddress())) {
122                            return null;
123                    }
124    
125                    return internetAddress;
126            }
127    
128            protected InternetAddress[] filterInternetAddresses(
129                    InternetAddress[] internetAddresses) {
130    
131                    if (internetAddresses == null) {
132                            return null;
133                    }
134    
135                    List<InternetAddress> filteredInternetAddresses =
136                            new ArrayList<InternetAddress>(internetAddresses.length);
137    
138                    for (InternetAddress internetAddress : internetAddresses) {
139                            InternetAddress filteredInternetAddress = filterInternetAddress(
140                                    internetAddress);
141    
142                            if (filteredInternetAddress != null) {
143                                    filteredInternetAddresses.add(filteredInternetAddress);
144                            }
145                    }
146    
147                    return filteredInternetAddresses.toArray(
148                            new InternetAddress[filteredInternetAddresses.size()]);
149            }
150    
151    }