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