1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.mail.service.impl;
16  
17  import com.liferay.mail.model.Filter;
18  import com.liferay.mail.service.MailService;
19  import com.liferay.mail.util.Hook;
20  import com.liferay.portal.kernel.exception.SystemException;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.mail.Account;
24  import com.liferay.portal.kernel.mail.MailMessage;
25  import com.liferay.portal.kernel.messaging.DestinationNames;
26  import com.liferay.portal.kernel.messaging.MessageBusUtil;
27  import com.liferay.portal.kernel.util.BooleanWrapper;
28  import com.liferay.portal.kernel.util.InfrastructureUtil;
29  import com.liferay.portal.kernel.util.LongWrapper;
30  import com.liferay.portal.kernel.util.MethodWrapper;
31  import com.liferay.portal.kernel.util.PropertiesUtil;
32  import com.liferay.portal.kernel.util.PropsKeys;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.util.PrefsPropsUtil;
35  import com.liferay.portal.util.PropsValues;
36  
37  import java.io.IOException;
38  
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.Map;
42  import java.util.Properties;
43  
44  import javax.mail.Session;
45  
46  /**
47   * <a href="MailServiceImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   */
51  public class MailServiceImpl implements MailService {
52  
53      public void addForward(
54          long companyId, long userId, List<Filter> filters,
55          List<String> emailAddresses, boolean leaveCopy) {
56  
57          if (_log.isDebugEnabled()) {
58              _log.debug("addForward");
59          }
60  
61          MethodWrapper methodWrapper = new MethodWrapper(
62              Hook.class.getName(), "addForward",
63              new Object[] {
64                  new LongWrapper(companyId), new LongWrapper(userId), filters,
65                  emailAddresses, new BooleanWrapper(leaveCopy)
66              });
67  
68          MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
69      }
70  
71      public void addUser(
72          long companyId, long userId, String password, String firstName,
73          String middleName, String lastName, String emailAddress) {
74  
75          if (_log.isDebugEnabled()) {
76              _log.debug("addUser");
77          }
78  
79          MethodWrapper methodWrapper = new MethodWrapper(
80              Hook.class.getName(), "addUser",
81              new Object[] {
82                  new LongWrapper(companyId), new LongWrapper(userId), password,
83                  firstName, middleName, lastName, emailAddress
84              });
85  
86          MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
87      }
88  
89      public void addVacationMessage(
90          long companyId, long userId, String emailAddress,
91          String vacationMessage) {
92  
93          if (_log.isDebugEnabled()) {
94              _log.debug("addVacationMessage");
95          }
96  
97          MethodWrapper methodWrapper = new MethodWrapper(
98              Hook.class.getName(), "addVacationMessage",
99              new Object[] {
100                 new LongWrapper(companyId), new LongWrapper(userId),
101                 emailAddress, vacationMessage
102             });
103 
104         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
105     }
106 
107     public void clearSession() {
108         _session = null;
109     }
110 
111     public void deleteEmailAddress(long companyId, long userId) {
112         if (_log.isDebugEnabled()) {
113             _log.debug("deleteEmailAddress");
114         }
115 
116         MethodWrapper methodWrapper = new MethodWrapper(
117             Hook.class.getName(), "deleteEmailAddress",
118             new Object[] {new LongWrapper(companyId), new LongWrapper(userId)});
119 
120         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
121     }
122 
123     public void deleteUser(long companyId, long userId) {
124         if (_log.isDebugEnabled()) {
125             _log.debug("deleteUser");
126         }
127 
128         MethodWrapper methodWrapper = new MethodWrapper(
129             Hook.class.getName(), "deleteUser",
130             new Object[] {new LongWrapper(companyId), new LongWrapper(userId)});
131 
132         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
133     }
134 
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                 Iterator<Map.Entry<Object, Object>> itr =
233                     advancedProperties.entrySet().iterator();
234 
235                 while (itr.hasNext()) {
236                     Map.Entry<Object, Object> entry = itr.next();
237 
238                     String key = (String)entry.getKey();
239                     String value = (String)entry.getValue();
240 
241                     properties.setProperty(key, value);
242                 }
243             }
244         }
245         catch (IOException ioe) {
246             if (_log.isWarnEnabled()) {
247                 _log.warn(ioe, ioe);
248             }
249         }
250 
251         _session = Session.getInstance(properties);
252 
253         return _session;
254     }
255 
256     public void sendEmail(MailMessage mailMessage) {
257         if (_log.isDebugEnabled()) {
258             _log.debug("sendEmail");
259         }
260 
261         MessageBusUtil.sendMessage(DestinationNames.MAIL, mailMessage);
262     }
263 
264     public void updateBlocked(
265         long companyId, long userId, List<String> blocked) {
266 
267         if (_log.isDebugEnabled()) {
268             _log.debug("updateBlocked");
269         }
270 
271         MethodWrapper methodWrapper = new MethodWrapper(
272             Hook.class.getName(), "updateBlocked",
273             new Object[] {
274                 new LongWrapper(companyId), new LongWrapper(userId), blocked
275             });
276 
277         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
278     }
279 
280     public void updateEmailAddress(
281         long companyId, long userId, String emailAddress) {
282 
283         if (_log.isDebugEnabled()) {
284             _log.debug("updateEmailAddress");
285         }
286 
287         MethodWrapper methodWrapper = new MethodWrapper(
288             Hook.class.getName(), "updateEmailAddress",
289             new Object[] {
290                 new LongWrapper(companyId), new LongWrapper(userId),
291                 emailAddress
292             });
293 
294         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
295     }
296 
297     public void updatePassword(long companyId, long userId, String password) {
298         if (_log.isDebugEnabled()) {
299             _log.debug("updatePassword");
300         }
301 
302         MethodWrapper methodWrapper = new MethodWrapper(
303             Hook.class.getName(), "updatePassword",
304             new Object[] {
305                 new LongWrapper(companyId), new LongWrapper(userId), password
306             });
307 
308         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
309     }
310 
311     private static Log _log = LogFactoryUtil.getLog(MailServiceImpl.class);
312 
313     private Session _session;
314 
315 }