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