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