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