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