1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.mail.service.impl;
24  
25  import com.liferay.mail.model.Filter;
26  import com.liferay.mail.service.MailService;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.mail.Account;
31  import com.liferay.portal.kernel.mail.MailMessage;
32  import com.liferay.portal.kernel.messaging.DestinationNames;
33  import com.liferay.portal.kernel.messaging.MessageBusUtil;
34  import com.liferay.portal.kernel.util.BooleanWrapper;
35  import com.liferay.portal.kernel.util.InfrastructureUtil;
36  import com.liferay.portal.kernel.util.LongWrapper;
37  import com.liferay.portal.kernel.util.MethodWrapper;
38  import com.liferay.portal.kernel.util.PropertiesUtil;
39  import com.liferay.portal.kernel.util.Validator;
40  import com.liferay.portal.util.PrefsPropsUtil;
41  import com.liferay.portal.util.PropsKeys;
42  import com.liferay.portal.util.PropsValues;
43  
44  import java.io.IOException;
45  
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.Map;
49  import java.util.Properties;
50  
51  import javax.mail.Session;
52  
53  /**
54   * <a href="MailServiceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   *
58   */
59  public class MailServiceImpl implements MailService {
60  
61      public void addForward(
62          long companyId, long userId, List<Filter> filters,
63          List<String> emailAddresses, boolean leaveCopy) {
64  
65          if (_log.isDebugEnabled()) {
66              _log.debug("addForward");
67          }
68  
69          MethodWrapper methodWrapper = new MethodWrapper(
70              PropsValues.MAIL_HOOK_IMPL, "addForward",
71              new Object[] {
72                  new LongWrapper(companyId), new LongWrapper(userId), filters,
73                  emailAddresses, new BooleanWrapper(leaveCopy)
74              });
75  
76          MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
77      }
78  
79      public void addUser(
80          long companyId, long userId, String password, String firstName,
81          String middleName, String lastName, String emailAddress) {
82  
83          if (_log.isDebugEnabled()) {
84              _log.debug("addUser");
85          }
86  
87          MethodWrapper methodWrapper = new MethodWrapper(
88              PropsValues.MAIL_HOOK_IMPL, "addUser",
89              new Object[] {
90                  new LongWrapper(companyId), new LongWrapper(userId), password,
91                  firstName, middleName, lastName, emailAddress
92              });
93  
94          MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
95      }
96  
97      public void addVacationMessage(
98          long companyId, long userId, String emailAddress,
99          String vacationMessage) {
100 
101         if (_log.isDebugEnabled()) {
102             _log.debug("addVacationMessage");
103         }
104 
105         MethodWrapper methodWrapper = new MethodWrapper(
106             PropsValues.MAIL_HOOK_IMPL, "addVacationMessage",
107             new Object[] {
108                 new LongWrapper(companyId), new LongWrapper(userId),
109                 emailAddress, vacationMessage
110             });
111 
112         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
113     }
114 
115     public void clearSession() {
116         _session = null;
117     }
118 
119     public void deleteEmailAddress(long companyId, long userId) {
120         if (_log.isDebugEnabled()) {
121             _log.debug("deleteEmailAddress");
122         }
123 
124         MethodWrapper methodWrapper = new MethodWrapper(
125             PropsValues.MAIL_HOOK_IMPL, "deleteEmailAddress",
126             new Object[] {new LongWrapper(companyId), new LongWrapper(userId)});
127 
128         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
129     }
130 
131     public void deleteUser(long companyId, long userId) {
132         if (_log.isDebugEnabled()) {
133             _log.debug("deleteUser");
134         }
135 
136         MethodWrapper methodWrapper = new MethodWrapper(
137             PropsValues.MAIL_HOOK_IMPL, "deleteUser",
138             new Object[] {new LongWrapper(companyId), new LongWrapper(userId)});
139 
140         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
141     }
142 
143     public Session getSession() throws SystemException {
144         if (_session != null) {
145             return _session;
146         }
147 
148         Session session = InfrastructureUtil.getMailSession();
149 
150         if (!PrefsPropsUtil.getBoolean(PropsKeys.MAIL_SESSION_MAIL)) {
151             _session = session;
152 
153             return _session;
154         }
155 
156         String advancedPropertiesString = PrefsPropsUtil.getString(
157             PropsKeys.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES,
158             PropsValues.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES);
159         String pop3Host = PrefsPropsUtil.getString(
160             PropsKeys.MAIL_SESSION_MAIL_POP3_HOST,
161             PropsValues.MAIL_SESSION_MAIL_POP3_HOST);
162         String pop3Password = PrefsPropsUtil.getString(
163             PropsKeys.MAIL_SESSION_MAIL_POP3_PASSWORD,
164             PropsValues.MAIL_SESSION_MAIL_POP3_PASSWORD);
165         int pop3Port = PrefsPropsUtil.getInteger(
166             PropsKeys.MAIL_SESSION_MAIL_POP3_PORT,
167             PropsValues.MAIL_SESSION_MAIL_POP3_PORT);
168         String pop3User = PrefsPropsUtil.getString(
169             PropsKeys.MAIL_SESSION_MAIL_POP3_USER,
170             PropsValues.MAIL_SESSION_MAIL_POP3_USER);
171         String smtpHost = PrefsPropsUtil.getString(
172             PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST,
173             PropsValues.MAIL_SESSION_MAIL_SMTP_HOST);
174         String smtpPassword = PrefsPropsUtil.getString(
175             PropsKeys.MAIL_SESSION_MAIL_SMTP_PASSWORD,
176             PropsValues.MAIL_SESSION_MAIL_SMTP_PASSWORD);
177         int smtpPort = PrefsPropsUtil.getInteger(
178             PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT,
179             PropsValues.MAIL_SESSION_MAIL_SMTP_PORT);
180         String smtpUser = PrefsPropsUtil.getString(
181             PropsKeys.MAIL_SESSION_MAIL_SMTP_USER,
182             PropsValues.MAIL_SESSION_MAIL_SMTP_USER);
183         String storeProtocol = PrefsPropsUtil.getString(
184             PropsKeys.MAIL_SESSION_MAIL_STORE_PROTOCOL,
185             PropsValues.MAIL_SESSION_MAIL_STORE_PROTOCOL);
186         String transportProtocol = PrefsPropsUtil.getString(
187             PropsKeys.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL,
188             PropsValues.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL);
189 
190         Properties properties = session.getProperties();
191 
192         // Incoming
193 
194         if (!storeProtocol.equals(Account.PROTOCOL_POPS)) {
195             storeProtocol = Account.PROTOCOL_POP;
196         }
197 
198         properties.setProperty("mail.store.protocol", storeProtocol);
199 
200         String storePrefix = "mail." + storeProtocol + ".";
201 
202         properties.setProperty(storePrefix + "host", pop3Host);
203         properties.setProperty(storePrefix + "password", pop3Password);
204         properties.setProperty(storePrefix + "port", String.valueOf(pop3Port));
205         properties.setProperty(storePrefix + "user", pop3User);
206 
207         // Outgoing
208 
209         if (!transportProtocol.equals(Account.PROTOCOL_SMTPS)) {
210             transportProtocol = Account.PROTOCOL_SMTP;
211         }
212 
213         properties.setProperty("mail.transport.protocol", transportProtocol);
214 
215         String transportPrefix = "mail." + transportProtocol + ".";
216 
217         boolean smtpAuth = false;
218 
219         if (Validator.isNotNull(smtpPassword) ||
220             Validator.isNotNull(smtpUser)) {
221 
222             smtpAuth = true;
223         }
224 
225         properties.setProperty(
226             transportPrefix + "auth", String.valueOf(smtpAuth));
227         properties.setProperty(transportPrefix + "host", smtpHost);
228         properties.setProperty(transportPrefix + "password", smtpPassword);
229         properties.setProperty(
230             transportPrefix + "port", String.valueOf(smtpPort));
231         properties.setProperty(transportPrefix + "user", smtpUser);
232 
233         // Advanced
234 
235         try {
236             if (Validator.isNotNull(advancedPropertiesString)) {
237                 Properties advancedProperties = PropertiesUtil.load(
238                     advancedPropertiesString);
239 
240                 Iterator<Map.Entry<Object, Object>> itr =
241                     advancedProperties.entrySet().iterator();
242 
243                 while (itr.hasNext()) {
244                     Map.Entry<Object, Object> entry = itr.next();
245 
246                     String key = (String)entry.getKey();
247                     String value = (String)entry.getValue();
248 
249                     properties.setProperty(key, value);
250                 }
251             }
252         }
253         catch (IOException ioe) {
254             if (_log.isWarnEnabled()) {
255                 _log.warn(ioe, ioe);
256             }
257         }
258 
259         _session = Session.getInstance(properties);
260 
261         return _session;
262     }
263 
264     public void sendEmail(MailMessage mailMessage) {
265         if (_log.isDebugEnabled()) {
266             _log.debug("sendEmail");
267         }
268 
269         MessageBusUtil.sendMessage(DestinationNames.MAIL, mailMessage);
270     }
271 
272     public void updateBlocked(
273         long companyId, long userId, List<String> blocked) {
274 
275         if (_log.isDebugEnabled()) {
276             _log.debug("updateBlocked");
277         }
278 
279         MethodWrapper methodWrapper = new MethodWrapper(
280             PropsValues.MAIL_HOOK_IMPL, "updateBlocked",
281             new Object[] {
282                 new LongWrapper(companyId), new LongWrapper(userId), blocked
283             });
284 
285         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
286     }
287 
288     public void updateEmailAddress(
289         long companyId, long userId, String emailAddress) {
290 
291         if (_log.isDebugEnabled()) {
292             _log.debug("updateEmailAddress");
293         }
294 
295         MethodWrapper methodWrapper = new MethodWrapper(
296             PropsValues.MAIL_HOOK_IMPL, "updateEmailAddress",
297             new Object[] {
298                 new LongWrapper(companyId), new LongWrapper(userId),
299                 emailAddress
300             });
301 
302         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
303     }
304 
305     public void updatePassword(long companyId, long userId, String password) {
306         if (_log.isDebugEnabled()) {
307             _log.debug("updatePassword");
308         }
309 
310         MethodWrapper methodWrapper = new MethodWrapper(
311             PropsValues.MAIL_HOOK_IMPL, "updatePassword",
312             new Object[] {
313                 new LongWrapper(companyId), new LongWrapper(userId), password
314             });
315 
316         MessageBusUtil.sendMessage(DestinationNames.MAIL, methodWrapper);
317     }
318 
319     private static Log _log = LogFactoryUtil.getLog(MailServiceImpl.class);
320 
321     private Session _session;
322 
323 }