001
014
015 package com.liferay.mail.service.impl;
016
017 import com.liferay.mail.model.Filter;
018 import com.liferay.mail.service.MailService;
019 import com.liferay.mail.util.Hook;
020 import com.liferay.portal.kernel.exception.SystemException;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.mail.Account;
024 import com.liferay.portal.kernel.mail.MailMessage;
025 import com.liferay.portal.kernel.messaging.DestinationNames;
026 import com.liferay.portal.kernel.messaging.MessageBusUtil;
027 import com.liferay.portal.kernel.util.InfrastructureUtil;
028 import com.liferay.portal.kernel.util.MethodHandler;
029 import com.liferay.portal.kernel.util.MethodKey;
030 import com.liferay.portal.kernel.util.PropertiesUtil;
031 import com.liferay.portal.kernel.util.PropsKeys;
032 import com.liferay.portal.kernel.util.Validator;
033 import com.liferay.portal.util.PrefsPropsUtil;
034 import com.liferay.portal.util.PropsValues;
035
036 import java.io.IOException;
037
038 import java.util.Iterator;
039 import java.util.List;
040 import java.util.Map;
041 import java.util.Properties;
042
043 import javax.mail.Session;
044
045
048 public class MailServiceImpl implements MailService {
049
050 public void addForward(
051 long companyId, long userId, List<Filter> filters,
052 List<String> emailAddresses, boolean leaveCopy) {
053
054 if (_log.isDebugEnabled()) {
055 _log.debug("addForward");
056 }
057
058 MethodHandler methodHandler = new MethodHandler(
059 _addForwardMethodKey, companyId, userId, filters, emailAddresses,
060 leaveCopy);
061
062 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
063 }
064
065 public void addUser(
066 long companyId, long userId, String password, String firstName,
067 String middleName, String lastName, String emailAddress) {
068
069 if (_log.isDebugEnabled()) {
070 _log.debug("addUser");
071 }
072
073 MethodHandler methodHandler = new MethodHandler(
074 _addUserMethodKey, companyId, userId, password, firstName,
075 middleName, lastName, emailAddress);
076
077 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
078 }
079
080 public void addVacationMessage(
081 long companyId, long userId, String emailAddress,
082 String vacationMessage) {
083
084 if (_log.isDebugEnabled()) {
085 _log.debug("addVacationMessage");
086 }
087
088 MethodHandler methodHandler = new MethodHandler(
089 _addVacationMessageMethodKey, companyId, userId, emailAddress,
090 vacationMessage);
091
092 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
093 }
094
095 public void clearSession() {
096 _session = null;
097 }
098
099 public void deleteEmailAddress(long companyId, long userId) {
100 if (_log.isDebugEnabled()) {
101 _log.debug("deleteEmailAddress");
102 }
103
104 MethodHandler methodHandler = new MethodHandler(
105 _deleteEmailAddressMethodKey, companyId, userId);
106
107 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
108 }
109
110 public void deleteUser(long companyId, long userId) {
111 if (_log.isDebugEnabled()) {
112 _log.debug("deleteUser");
113 }
114
115 MethodHandler methodHandler = new MethodHandler(
116 _deleteUserMethodKey, companyId, userId);
117
118 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
119 }
120
121 public Session getSession() throws SystemException {
122 if (_session != null) {
123 return _session;
124 }
125
126 Session session = InfrastructureUtil.getMailSession();
127
128 if (!PrefsPropsUtil.getBoolean(PropsKeys.MAIL_SESSION_MAIL)) {
129 _session = session;
130
131 return _session;
132 }
133
134 String advancedPropertiesString = PrefsPropsUtil.getString(
135 PropsKeys.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES,
136 PropsValues.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES);
137 String pop3Host = PrefsPropsUtil.getString(
138 PropsKeys.MAIL_SESSION_MAIL_POP3_HOST,
139 PropsValues.MAIL_SESSION_MAIL_POP3_HOST);
140 String pop3Password = PrefsPropsUtil.getString(
141 PropsKeys.MAIL_SESSION_MAIL_POP3_PASSWORD,
142 PropsValues.MAIL_SESSION_MAIL_POP3_PASSWORD);
143 int pop3Port = PrefsPropsUtil.getInteger(
144 PropsKeys.MAIL_SESSION_MAIL_POP3_PORT,
145 PropsValues.MAIL_SESSION_MAIL_POP3_PORT);
146 String pop3User = PrefsPropsUtil.getString(
147 PropsKeys.MAIL_SESSION_MAIL_POP3_USER,
148 PropsValues.MAIL_SESSION_MAIL_POP3_USER);
149 String smtpHost = PrefsPropsUtil.getString(
150 PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST,
151 PropsValues.MAIL_SESSION_MAIL_SMTP_HOST);
152 String smtpPassword = PrefsPropsUtil.getString(
153 PropsKeys.MAIL_SESSION_MAIL_SMTP_PASSWORD,
154 PropsValues.MAIL_SESSION_MAIL_SMTP_PASSWORD);
155 int smtpPort = PrefsPropsUtil.getInteger(
156 PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT,
157 PropsValues.MAIL_SESSION_MAIL_SMTP_PORT);
158 String smtpUser = PrefsPropsUtil.getString(
159 PropsKeys.MAIL_SESSION_MAIL_SMTP_USER,
160 PropsValues.MAIL_SESSION_MAIL_SMTP_USER);
161 String storeProtocol = PrefsPropsUtil.getString(
162 PropsKeys.MAIL_SESSION_MAIL_STORE_PROTOCOL,
163 PropsValues.MAIL_SESSION_MAIL_STORE_PROTOCOL);
164 String transportProtocol = PrefsPropsUtil.getString(
165 PropsKeys.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL,
166 PropsValues.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL);
167
168 Properties properties = session.getProperties();
169
170
171
172 if (!storeProtocol.equals(Account.PROTOCOL_POPS)) {
173 storeProtocol = Account.PROTOCOL_POP;
174 }
175
176 properties.setProperty("mail.store.protocol", storeProtocol);
177
178 String storePrefix = "mail." + storeProtocol + ".";
179
180 properties.setProperty(storePrefix + "host", pop3Host);
181 properties.setProperty(storePrefix + "password", pop3Password);
182 properties.setProperty(storePrefix + "port", String.valueOf(pop3Port));
183 properties.setProperty(storePrefix + "user", pop3User);
184
185
186
187 if (!transportProtocol.equals(Account.PROTOCOL_SMTPS)) {
188 transportProtocol = Account.PROTOCOL_SMTP;
189 }
190
191 properties.setProperty("mail.transport.protocol", transportProtocol);
192
193 String transportPrefix = "mail." + transportProtocol + ".";
194
195 boolean smtpAuth = false;
196
197 if (Validator.isNotNull(smtpPassword) ||
198 Validator.isNotNull(smtpUser)) {
199
200 smtpAuth = true;
201 }
202
203 properties.setProperty(
204 transportPrefix + "auth", String.valueOf(smtpAuth));
205 properties.setProperty(transportPrefix + "host", smtpHost);
206 properties.setProperty(transportPrefix + "password", smtpPassword);
207 properties.setProperty(
208 transportPrefix + "port", String.valueOf(smtpPort));
209 properties.setProperty(transportPrefix + "user", smtpUser);
210
211
212
213 try {
214 if (Validator.isNotNull(advancedPropertiesString)) {
215 Properties advancedProperties = PropertiesUtil.load(
216 advancedPropertiesString);
217
218 Iterator<Map.Entry<Object, Object>> itr =
219 advancedProperties.entrySet().iterator();
220
221 while (itr.hasNext()) {
222 Map.Entry<Object, Object> entry = itr.next();
223
224 String key = (String)entry.getKey();
225 String value = (String)entry.getValue();
226
227 properties.setProperty(key, value);
228 }
229 }
230 }
231 catch (IOException ioe) {
232 if (_log.isWarnEnabled()) {
233 _log.warn(ioe, ioe);
234 }
235 }
236
237 _session = Session.getInstance(properties);
238
239 return _session;
240 }
241
242 public void sendEmail(MailMessage mailMessage) {
243 if (_log.isDebugEnabled()) {
244 _log.debug("sendEmail");
245 }
246
247 MessageBusUtil.sendMessage(DestinationNames.MAIL, mailMessage);
248 }
249
250 public void updateBlocked(
251 long companyId, long userId, List<String> blocked) {
252
253 if (_log.isDebugEnabled()) {
254 _log.debug("updateBlocked");
255 }
256
257 MethodHandler methodHandler = new MethodHandler(
258 _updateBlockedMethodKey, companyId, userId, blocked);
259
260 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
261 }
262
263 public void updateEmailAddress(
264 long companyId, long userId, String emailAddress) {
265
266 if (_log.isDebugEnabled()) {
267 _log.debug("updateEmailAddress");
268 }
269
270 MethodHandler methodHandler = new MethodHandler(
271 _updateEmailAddressMethodKey, companyId, userId, emailAddress);
272
273 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
274 }
275
276 public void updatePassword(long companyId, long userId, String password) {
277 if (_log.isDebugEnabled()) {
278 _log.debug("updatePassword");
279 }
280
281 MethodHandler methodHandler = new MethodHandler(
282 _updatePasswordMethodKey, companyId, userId, password);
283
284 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
285 }
286
287 private static Log _log = LogFactoryUtil.getLog(MailServiceImpl.class);
288
289 private static MethodKey _addForwardMethodKey = new MethodKey(
290 Hook.class.getName(), "addForward", long.class, long.class, List.class,
291 List.class, boolean.class);
292 private static MethodKey _addUserMethodKey = new MethodKey(
293 Hook.class.getName(), "addUser", long.class, long.class, String.class,
294 String.class, String.class, String.class, String.class);
295 private static MethodKey _addVacationMessageMethodKey = new MethodKey(
296 Hook.class.getName(), "addVacationMessage", long.class, long.class,
297 String.class, String.class);
298 private static MethodKey _deleteEmailAddressMethodKey = new MethodKey(
299 Hook.class.getName(), "deleteEmailAddress", long.class, long.class);
300 private static MethodKey _deleteUserMethodKey = new MethodKey(
301 Hook.class.getName(), "deleteUser", long.class, long.class);
302 private static MethodKey _updateBlockedMethodKey = new MethodKey(
303 Hook.class.getName(), "updateBlocked", long.class, long.class,
304 List.class);
305 private static MethodKey _updateEmailAddressMethodKey = new MethodKey(
306 Hook.class.getName(), "updateEmailAddress", long.class, long.class,
307 String.class);
308 private static MethodKey _updatePasswordMethodKey = new MethodKey(
309 Hook.class.getName(), "updatePassword", long.class, long.class,
310 String.class);
311
312 private Session _session;
313
314 }