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