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.service.impl;
016    
017    import com.liferay.mail.model.Filter;
018    import com.liferay.mail.service.MailService;
019    import com.liferay.mail.util.Hook;
020    import com.liferay.portal.kernel.bean.IdentifiableBean;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.mail.Account;
024    import com.liferay.portal.kernel.mail.MailMessage;
025    import com.liferay.portal.kernel.messaging.DestinationNames;
026    import com.liferay.portal.kernel.messaging.MessageBusUtil;
027    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
028    import com.liferay.portal.kernel.util.InfrastructureUtil;
029    import com.liferay.portal.kernel.util.MethodHandler;
030    import com.liferay.portal.kernel.util.MethodKey;
031    import com.liferay.portal.kernel.util.PropertiesUtil;
032    import com.liferay.portal.kernel.util.PropsKeys;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.util.PrefsPropsUtil;
035    import com.liferay.portal.util.PropsValues;
036    
037    import java.io.IOException;
038    
039    import java.util.List;
040    import java.util.Map;
041    import java.util.Properties;
042    
043    import javax.mail.Session;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    @DoPrivileged
049    public class MailServiceImpl implements MailService, IdentifiableBean {
050    
051            @Override
052            public void addForward(
053                    long companyId, long userId, List<Filter> filters,
054                    List<String> emailAddresses, boolean leaveCopy) {
055    
056                    if (_log.isDebugEnabled()) {
057                            _log.debug("addForward");
058                    }
059    
060                    MethodHandler methodHandler = new MethodHandler(
061                            _addForwardMethodKey, companyId, userId, filters, emailAddresses,
062                            leaveCopy);
063    
064                    MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
065            }
066    
067            @Override
068            public void addUser(
069                    long companyId, long userId, String password, String firstName,
070                    String middleName, String lastName, String emailAddress) {
071    
072                    if (_log.isDebugEnabled()) {
073                            _log.debug("addUser");
074                    }
075    
076                    MethodHandler methodHandler = new MethodHandler(
077                            _addUserMethodKey, companyId, userId, password, firstName,
078                            middleName, lastName, emailAddress);
079    
080                    MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
081            }
082    
083            @Override
084            public void addVacationMessage(
085                    long companyId, long userId, String emailAddress,
086                    String vacationMessage) {
087    
088                    if (_log.isDebugEnabled()) {
089                            _log.debug("addVacationMessage");
090                    }
091    
092                    MethodHandler methodHandler = new MethodHandler(
093                            _addVacationMessageMethodKey, companyId, userId, emailAddress,
094                            vacationMessage);
095    
096                    MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
097            }
098    
099            @Override
100            public void clearSession() {
101                    _session = null;
102            }
103    
104            @Override
105            public void deleteEmailAddress(long companyId, long userId) {
106                    if (_log.isDebugEnabled()) {
107                            _log.debug("deleteEmailAddress");
108                    }
109    
110                    MethodHandler methodHandler = new MethodHandler(
111                            _deleteEmailAddressMethodKey, companyId, userId);
112    
113                    MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
114            }
115    
116            @Override
117            public void deleteUser(long companyId, long userId) {
118                    if (_log.isDebugEnabled()) {
119                            _log.debug("deleteUser");
120                    }
121    
122                    MethodHandler methodHandler = new MethodHandler(
123                            _deleteUserMethodKey, companyId, userId);
124    
125                    MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
126            }
127    
128            @Override
129            public String getBeanIdentifier() {
130                    return _beanIdentifier;
131            }
132    
133            @Override
134            public Session getSession() {
135                    if (_session != null) {
136                            return _session;
137                    }
138    
139                    Session session = InfrastructureUtil.getMailSession();
140    
141                    if (!PrefsPropsUtil.getBoolean(PropsKeys.MAIL_SESSION_MAIL)) {
142                            _session = session;
143    
144                            return _session;
145                    }
146    
147                    String advancedPropertiesString = PrefsPropsUtil.getString(
148                            PropsKeys.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES,
149                            PropsValues.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES);
150                    String pop3Host = PrefsPropsUtil.getString(
151                            PropsKeys.MAIL_SESSION_MAIL_POP3_HOST,
152                            PropsValues.MAIL_SESSION_MAIL_POP3_HOST);
153                    String pop3Password = PrefsPropsUtil.getString(
154                            PropsKeys.MAIL_SESSION_MAIL_POP3_PASSWORD,
155                            PropsValues.MAIL_SESSION_MAIL_POP3_PASSWORD);
156                    int pop3Port = PrefsPropsUtil.getInteger(
157                            PropsKeys.MAIL_SESSION_MAIL_POP3_PORT,
158                            PropsValues.MAIL_SESSION_MAIL_POP3_PORT);
159                    String pop3User = PrefsPropsUtil.getString(
160                            PropsKeys.MAIL_SESSION_MAIL_POP3_USER,
161                            PropsValues.MAIL_SESSION_MAIL_POP3_USER);
162                    String smtpHost = PrefsPropsUtil.getString(
163                            PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST,
164                            PropsValues.MAIL_SESSION_MAIL_SMTP_HOST);
165                    String smtpPassword = PrefsPropsUtil.getString(
166                            PropsKeys.MAIL_SESSION_MAIL_SMTP_PASSWORD,
167                            PropsValues.MAIL_SESSION_MAIL_SMTP_PASSWORD);
168                    int smtpPort = PrefsPropsUtil.getInteger(
169                            PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT,
170                            PropsValues.MAIL_SESSION_MAIL_SMTP_PORT);
171                    String smtpUser = PrefsPropsUtil.getString(
172                            PropsKeys.MAIL_SESSION_MAIL_SMTP_USER,
173                            PropsValues.MAIL_SESSION_MAIL_SMTP_USER);
174                    String storeProtocol = PrefsPropsUtil.getString(
175                            PropsKeys.MAIL_SESSION_MAIL_STORE_PROTOCOL,
176                            PropsValues.MAIL_SESSION_MAIL_STORE_PROTOCOL);
177                    String transportProtocol = PrefsPropsUtil.getString(
178                            PropsKeys.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL,
179                            PropsValues.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL);
180    
181                    Properties properties = session.getProperties();
182    
183                    // Incoming
184    
185                    if (!storeProtocol.equals(Account.PROTOCOL_POPS)) {
186                            storeProtocol = Account.PROTOCOL_POP;
187                    }
188    
189                    properties.setProperty("mail.store.protocol", storeProtocol);
190    
191                    String storePrefix = "mail." + storeProtocol + ".";
192    
193                    properties.setProperty(storePrefix + "host", pop3Host);
194                    properties.setProperty(storePrefix + "password", pop3Password);
195                    properties.setProperty(storePrefix + "port", String.valueOf(pop3Port));
196                    properties.setProperty(storePrefix + "user", pop3User);
197    
198                    // Outgoing
199    
200                    if (!transportProtocol.equals(Account.PROTOCOL_SMTPS)) {
201                            transportProtocol = Account.PROTOCOL_SMTP;
202                    }
203    
204                    properties.setProperty("mail.transport.protocol", transportProtocol);
205    
206                    String transportPrefix = "mail." + transportProtocol + ".";
207    
208                    boolean smtpAuth = false;
209    
210                    if (Validator.isNotNull(smtpPassword) ||
211                            Validator.isNotNull(smtpUser)) {
212    
213                            smtpAuth = true;
214                    }
215    
216                    properties.setProperty(
217                            transportPrefix + "auth", String.valueOf(smtpAuth));
218                    properties.setProperty(transportPrefix + "host", smtpHost);
219                    properties.setProperty(transportPrefix + "password", smtpPassword);
220                    properties.setProperty(
221                            transportPrefix + "port", String.valueOf(smtpPort));
222                    properties.setProperty(transportPrefix + "user", smtpUser);
223    
224                    // Advanced
225    
226                    try {
227                            if (Validator.isNotNull(advancedPropertiesString)) {
228                                    Properties advancedProperties = PropertiesUtil.load(
229                                            advancedPropertiesString);
230    
231                                    for (Map.Entry<Object, Object> entry :
232                                                    advancedProperties.entrySet()) {
233    
234                                            String key = (String)entry.getKey();
235                                            String value = (String)entry.getValue();
236    
237                                            properties.setProperty(key, value);
238                                    }
239                            }
240                    }
241                    catch (IOException ioe) {
242                            if (_log.isWarnEnabled()) {
243                                    _log.warn(ioe, ioe);
244                            }
245                    }
246    
247                    _session = Session.getInstance(properties);
248    
249                    return _session;
250            }
251    
252            @Override
253            public void sendEmail(MailMessage mailMessage) {
254                    if (_log.isDebugEnabled()) {
255                            _log.debug("sendEmail");
256                    }
257    
258                    MessageBusUtil.sendMessage(DestinationNames.MAIL, mailMessage);
259            }
260    
261            @Override
262            public void setBeanIdentifier(String beanIdentifier) {
263                    _beanIdentifier = beanIdentifier;
264            }
265    
266            @Override
267            public void updateBlocked(
268                    long companyId, long userId, List<String> blocked) {
269    
270                    if (_log.isDebugEnabled()) {
271                            _log.debug("updateBlocked");
272                    }
273    
274                    MethodHandler methodHandler = new MethodHandler(
275                            _updateBlockedMethodKey, companyId, userId, blocked);
276    
277                    MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
278            }
279    
280            @Override
281            public void updateEmailAddress(
282                    long companyId, long userId, String emailAddress) {
283    
284                    if (_log.isDebugEnabled()) {
285                            _log.debug("updateEmailAddress");
286                    }
287    
288                    MethodHandler methodHandler = new MethodHandler(
289                            _updateEmailAddressMethodKey, companyId, userId, emailAddress);
290    
291                    MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
292            }
293    
294            @Override
295            public void updatePassword(long companyId, long userId, String password) {
296                    if (_log.isDebugEnabled()) {
297                            _log.debug("updatePassword");
298                    }
299    
300                    MethodHandler methodHandler = new MethodHandler(
301                            _updatePasswordMethodKey, companyId, userId, password);
302    
303                    MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
304            }
305    
306            private static final Log _log = LogFactoryUtil.getLog(
307                    MailServiceImpl.class);
308    
309            private static final MethodKey _addForwardMethodKey = new MethodKey(
310                    Hook.class, "addForward", long.class, long.class, List.class,
311                    List.class, boolean.class);
312            private static final MethodKey _addUserMethodKey = new MethodKey(
313                    Hook.class, "addUser", long.class, long.class, String.class,
314                    String.class, String.class, String.class, String.class);
315            private static final MethodKey _addVacationMessageMethodKey = new MethodKey(
316                    Hook.class, "addVacationMessage", long.class, long.class, String.class,
317                    String.class);
318            private static final MethodKey _deleteEmailAddressMethodKey = new MethodKey(
319                    Hook.class, "deleteEmailAddress", long.class, long.class);
320            private static final MethodKey _deleteUserMethodKey = new MethodKey(
321                    Hook.class, "deleteUser", long.class, long.class);
322            private static final MethodKey _updateBlockedMethodKey = new MethodKey(
323                    Hook.class, "updateBlocked", long.class, long.class, List.class);
324            private static final MethodKey _updateEmailAddressMethodKey = new MethodKey(
325                    Hook.class, "updateEmailAddress", long.class, long.class, String.class);
326            private static final MethodKey _updatePasswordMethodKey = new MethodKey(
327                    Hook.class, "updatePassword", long.class, long.class, String.class);
328    
329            private String _beanIdentifier;
330            private Session _session;
331    
332    }