1
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.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.InfrastructureUtil;
28 import com.liferay.portal.kernel.util.MethodHandler;
29 import com.liferay.portal.kernel.util.MethodKey;
30 import com.liferay.portal.kernel.util.PropertiesUtil;
31 import com.liferay.portal.kernel.util.PropsKeys;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.util.PrefsPropsUtil;
34 import com.liferay.portal.util.PropsValues;
35
36 import java.io.IOException;
37
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Properties;
42
43 import javax.mail.Session;
44
45
50 public class MailServiceImpl implements MailService {
51
52 public void addForward(
53 long companyId, long userId, List<Filter> filters,
54 List<String> emailAddresses, boolean leaveCopy) {
55
56 if (_log.isDebugEnabled()) {
57 _log.debug("addForward");
58 }
59
60 MethodHandler methodHandler = new MethodHandler(
61 _addForwardMethodKey, companyId, userId, filters, emailAddresses,
62 leaveCopy);
63
64 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
65 }
66
67 public void addUser(
68 long companyId, long userId, String password, String firstName,
69 String middleName, String lastName, String emailAddress) {
70
71 if (_log.isDebugEnabled()) {
72 _log.debug("addUser");
73 }
74
75 MethodHandler methodHandler = new MethodHandler(
76 _addUserMethodKey, companyId, userId, password, firstName,
77 middleName, lastName, emailAddress);
78
79 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
80 }
81
82 public void addVacationMessage(
83 long companyId, long userId, String emailAddress,
84 String vacationMessage) {
85
86 if (_log.isDebugEnabled()) {
87 _log.debug("addVacationMessage");
88 }
89
90 MethodHandler methodHandler = new MethodHandler(
91 _addVacationMessageMethodKey, companyId, userId, emailAddress,
92 vacationMessage);
93
94 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
95 }
96
97 public void clearSession() {
98 _session = null;
99 }
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 Session getSession() throws SystemException {
124 if (_session != null) {
125 return _session;
126 }
127
128 Session session = InfrastructureUtil.getMailSession();
129
130 if (!PrefsPropsUtil.getBoolean(PropsKeys.MAIL_SESSION_MAIL)) {
131 _session = session;
132
133 return _session;
134 }
135
136 String advancedPropertiesString = PrefsPropsUtil.getString(
137 PropsKeys.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES,
138 PropsValues.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES);
139 String pop3Host = PrefsPropsUtil.getString(
140 PropsKeys.MAIL_SESSION_MAIL_POP3_HOST,
141 PropsValues.MAIL_SESSION_MAIL_POP3_HOST);
142 String pop3Password = PrefsPropsUtil.getString(
143 PropsKeys.MAIL_SESSION_MAIL_POP3_PASSWORD,
144 PropsValues.MAIL_SESSION_MAIL_POP3_PASSWORD);
145 int pop3Port = PrefsPropsUtil.getInteger(
146 PropsKeys.MAIL_SESSION_MAIL_POP3_PORT,
147 PropsValues.MAIL_SESSION_MAIL_POP3_PORT);
148 String pop3User = PrefsPropsUtil.getString(
149 PropsKeys.MAIL_SESSION_MAIL_POP3_USER,
150 PropsValues.MAIL_SESSION_MAIL_POP3_USER);
151 String smtpHost = PrefsPropsUtil.getString(
152 PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST,
153 PropsValues.MAIL_SESSION_MAIL_SMTP_HOST);
154 String smtpPassword = PrefsPropsUtil.getString(
155 PropsKeys.MAIL_SESSION_MAIL_SMTP_PASSWORD,
156 PropsValues.MAIL_SESSION_MAIL_SMTP_PASSWORD);
157 int smtpPort = PrefsPropsUtil.getInteger(
158 PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT,
159 PropsValues.MAIL_SESSION_MAIL_SMTP_PORT);
160 String smtpUser = PrefsPropsUtil.getString(
161 PropsKeys.MAIL_SESSION_MAIL_SMTP_USER,
162 PropsValues.MAIL_SESSION_MAIL_SMTP_USER);
163 String storeProtocol = PrefsPropsUtil.getString(
164 PropsKeys.MAIL_SESSION_MAIL_STORE_PROTOCOL,
165 PropsValues.MAIL_SESSION_MAIL_STORE_PROTOCOL);
166 String transportProtocol = PrefsPropsUtil.getString(
167 PropsKeys.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL,
168 PropsValues.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL);
169
170 Properties properties = session.getProperties();
171
172
174 if (!storeProtocol.equals(Account.PROTOCOL_POPS)) {
175 storeProtocol = Account.PROTOCOL_POP;
176 }
177
178 properties.setProperty("mail.store.protocol", storeProtocol);
179
180 String storePrefix = "mail." + storeProtocol + ".";
181
182 properties.setProperty(storePrefix + "host", pop3Host);
183 properties.setProperty(storePrefix + "password", pop3Password);
184 properties.setProperty(storePrefix + "port", String.valueOf(pop3Port));
185 properties.setProperty(storePrefix + "user", pop3User);
186
187
189 if (!transportProtocol.equals(Account.PROTOCOL_SMTPS)) {
190 transportProtocol = Account.PROTOCOL_SMTP;
191 }
192
193 properties.setProperty("mail.transport.protocol", transportProtocol);
194
195 String transportPrefix = "mail." + transportProtocol + ".";
196
197 boolean smtpAuth = false;
198
199 if (Validator.isNotNull(smtpPassword) ||
200 Validator.isNotNull(smtpUser)) {
201
202 smtpAuth = true;
203 }
204
205 properties.setProperty(
206 transportPrefix + "auth", String.valueOf(smtpAuth));
207 properties.setProperty(transportPrefix + "host", smtpHost);
208 properties.setProperty(transportPrefix + "password", smtpPassword);
209 properties.setProperty(
210 transportPrefix + "port", String.valueOf(smtpPort));
211 properties.setProperty(transportPrefix + "user", smtpUser);
212
213
215 try {
216 if (Validator.isNotNull(advancedPropertiesString)) {
217 Properties advancedProperties = PropertiesUtil.load(
218 advancedPropertiesString);
219
220 Iterator<Map.Entry<Object, Object>> itr =
221 advancedProperties.entrySet().iterator();
222
223 while (itr.hasNext()) {
224 Map.Entry<Object, Object> entry = itr.next();
225
226 String key = (String)entry.getKey();
227 String value = (String)entry.getValue();
228
229 properties.setProperty(key, value);
230 }
231 }
232 }
233 catch (IOException ioe) {
234 if (_log.isWarnEnabled()) {
235 _log.warn(ioe, ioe);
236 }
237 }
238
239 _session = Session.getInstance(properties);
240
241 return _session;
242 }
243
244 public void sendEmail(MailMessage mailMessage) {
245 if (_log.isDebugEnabled()) {
246 _log.debug("sendEmail");
247 }
248
249 MessageBusUtil.sendMessage(DestinationNames.MAIL, mailMessage);
250 }
251
252 public void updateBlocked(
253 long companyId, long userId, List<String> blocked) {
254
255 if (_log.isDebugEnabled()) {
256 _log.debug("updateBlocked");
257 }
258
259 MethodHandler methodHandler = new MethodHandler(
260 _updateBlockedMethodKey, companyId, userId, blocked);
261
262 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
263 }
264
265 public void updateEmailAddress(
266 long companyId, long userId, String emailAddress) {
267
268 if (_log.isDebugEnabled()) {
269 _log.debug("updateEmailAddress");
270 }
271
272 MethodHandler methodHandler = new MethodHandler(
273 _updateEmailAddressMethodKey, companyId, userId, emailAddress);
274
275 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
276 }
277
278 public void updatePassword(long companyId, long userId, String password) {
279 if (_log.isDebugEnabled()) {
280 _log.debug("updatePassword");
281 }
282
283 MethodHandler methodHandler = new MethodHandler(
284 _updatePasswordMethodKey, companyId, userId, password);
285
286 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
287 }
288
289 private static Log _log = LogFactoryUtil.getLog(MailServiceImpl.class);
290
291 private static MethodKey _addForwardMethodKey = new MethodKey(
292 Hook.class.getName(), "addForward", long.class, long.class, List.class,
293 List.class, boolean.class);
294 private static MethodKey _addUserMethodKey = new MethodKey(
295 Hook.class.getName(), "addUser", long.class, long.class, String.class,
296 String.class, String.class, String.class, String.class);
297 private static MethodKey _addVacationMessageMethodKey = new MethodKey(
298 Hook.class.getName(), "addVacationMessage", long.class, long.class,
299 String.class, String.class);
300 private static MethodKey _deleteEmailAddressMethodKey = new MethodKey(
301 Hook.class.getName(), "deleteEmailAddress", long.class, long.class);
302 private static MethodKey _deleteUserMethodKey = new MethodKey(
303 Hook.class.getName(), "deleteUser", long.class, long.class);
304 private static MethodKey _updateBlockedMethodKey = new MethodKey(
305 Hook.class.getName(), "updateBlocked", long.class, long.class,
306 List.class);
307 private static MethodKey _updateEmailAddressMethodKey = new MethodKey(
308 Hook.class.getName(), "updateEmailAddress", long.class, long.class,
309 String.class);
310 private static MethodKey _updatePasswordMethodKey = new MethodKey(
311 Hook.class.getName(), "updatePassword", long.class, long.class,
312 String.class);
313
314 private Session _session;
315
316 }