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