1
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.mail.util.Hook;
28 import com.liferay.portal.SystemException;
29 import com.liferay.portal.kernel.log.Log;
30 import com.liferay.portal.kernel.log.LogFactoryUtil;
31 import com.liferay.portal.kernel.mail.Account;
32 import com.liferay.portal.kernel.mail.MailMessage;
33 import com.liferay.portal.kernel.messaging.DestinationNames;
34 import com.liferay.portal.kernel.messaging.MessageBusUtil;
35 import com.liferay.portal.kernel.util.BooleanWrapper;
36 import com.liferay.portal.kernel.util.InfrastructureUtil;
37 import com.liferay.portal.kernel.util.LongWrapper;
38 import com.liferay.portal.kernel.util.MethodWrapper;
39 import com.liferay.portal.kernel.util.PropertiesUtil;
40 import com.liferay.portal.kernel.util.PropsKeys;
41 import com.liferay.portal.kernel.util.Validator;
42 import com.liferay.portal.util.PrefsPropsUtil;
43 import com.liferay.portal.util.PropsValues;
44
45 import java.io.IOException;
46
47 import java.util.Iterator;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.Properties;
51
52 import javax.mail.Session;
53
54
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 Hook.class.getName(), "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 Hook.class.getName(), "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 Hook.class.getName(), "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 Hook.class.getName(), "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 Hook.class.getName(), "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
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
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
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 Hook.class.getName(), "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 Hook.class.getName(), "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 Hook.class.getName(), "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 }