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