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.util.InfrastructureUtil;
029 import com.liferay.portal.kernel.util.MethodHandler;
030 import com.liferay.portal.kernel.util.MethodKey;
031 import com.liferay.portal.kernel.util.PropertiesUtil;
032 import com.liferay.portal.kernel.util.PropsKeys;
033 import com.liferay.portal.kernel.util.Validator;
034 import com.liferay.portal.util.PrefsPropsUtil;
035 import com.liferay.portal.util.PropsValues;
036
037 import java.io.IOException;
038
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, IdentifiableBean {
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 String getBeanIdentifier() {
122 return _beanIdentifier;
123 }
124
125 public Session getSession() throws SystemException {
126 if (_session != null) {
127 return _session;
128 }
129
130 Session session = InfrastructureUtil.getMailSession();
131
132 if (!PrefsPropsUtil.getBoolean(PropsKeys.MAIL_SESSION_MAIL)) {
133 _session = session;
134
135 return _session;
136 }
137
138 String advancedPropertiesString = PrefsPropsUtil.getString(
139 PropsKeys.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES,
140 PropsValues.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES);
141 String pop3Host = PrefsPropsUtil.getString(
142 PropsKeys.MAIL_SESSION_MAIL_POP3_HOST,
143 PropsValues.MAIL_SESSION_MAIL_POP3_HOST);
144 String pop3Password = PrefsPropsUtil.getString(
145 PropsKeys.MAIL_SESSION_MAIL_POP3_PASSWORD,
146 PropsValues.MAIL_SESSION_MAIL_POP3_PASSWORD);
147 int pop3Port = PrefsPropsUtil.getInteger(
148 PropsKeys.MAIL_SESSION_MAIL_POP3_PORT,
149 PropsValues.MAIL_SESSION_MAIL_POP3_PORT);
150 String pop3User = PrefsPropsUtil.getString(
151 PropsKeys.MAIL_SESSION_MAIL_POP3_USER,
152 PropsValues.MAIL_SESSION_MAIL_POP3_USER);
153 String smtpHost = PrefsPropsUtil.getString(
154 PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST,
155 PropsValues.MAIL_SESSION_MAIL_SMTP_HOST);
156 String smtpPassword = PrefsPropsUtil.getString(
157 PropsKeys.MAIL_SESSION_MAIL_SMTP_PASSWORD,
158 PropsValues.MAIL_SESSION_MAIL_SMTP_PASSWORD);
159 int smtpPort = PrefsPropsUtil.getInteger(
160 PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT,
161 PropsValues.MAIL_SESSION_MAIL_SMTP_PORT);
162 String smtpUser = PrefsPropsUtil.getString(
163 PropsKeys.MAIL_SESSION_MAIL_SMTP_USER,
164 PropsValues.MAIL_SESSION_MAIL_SMTP_USER);
165 String storeProtocol = PrefsPropsUtil.getString(
166 PropsKeys.MAIL_SESSION_MAIL_STORE_PROTOCOL,
167 PropsValues.MAIL_SESSION_MAIL_STORE_PROTOCOL);
168 String transportProtocol = PrefsPropsUtil.getString(
169 PropsKeys.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL,
170 PropsValues.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL);
171
172 Properties properties = session.getProperties();
173
174
175
176 if (!storeProtocol.equals(Account.PROTOCOL_POPS)) {
177 storeProtocol = Account.PROTOCOL_POP;
178 }
179
180 properties.setProperty("mail.store.protocol", storeProtocol);
181
182 String storePrefix = "mail." + storeProtocol + ".";
183
184 properties.setProperty(storePrefix + "host", pop3Host);
185 properties.setProperty(storePrefix + "password", pop3Password);
186 properties.setProperty(storePrefix + "port", String.valueOf(pop3Port));
187 properties.setProperty(storePrefix + "user", pop3User);
188
189
190
191 if (!transportProtocol.equals(Account.PROTOCOL_SMTPS)) {
192 transportProtocol = Account.PROTOCOL_SMTP;
193 }
194
195 properties.setProperty("mail.transport.protocol", transportProtocol);
196
197 String transportPrefix = "mail." + transportProtocol + ".";
198
199 boolean smtpAuth = false;
200
201 if (Validator.isNotNull(smtpPassword) ||
202 Validator.isNotNull(smtpUser)) {
203
204 smtpAuth = true;
205 }
206
207 properties.setProperty(
208 transportPrefix + "auth", String.valueOf(smtpAuth));
209 properties.setProperty(transportPrefix + "host", smtpHost);
210 properties.setProperty(transportPrefix + "password", smtpPassword);
211 properties.setProperty(
212 transportPrefix + "port", String.valueOf(smtpPort));
213 properties.setProperty(transportPrefix + "user", smtpUser);
214
215
216
217 try {
218 if (Validator.isNotNull(advancedPropertiesString)) {
219 Properties advancedProperties = PropertiesUtil.load(
220 advancedPropertiesString);
221
222 for (Map.Entry<Object, Object> entry :
223 advancedProperties.entrySet()) {
224
225 String key = (String)entry.getKey();
226 String value = (String)entry.getValue();
227
228 properties.setProperty(key, value);
229 }
230 }
231 }
232 catch (IOException ioe) {
233 if (_log.isWarnEnabled()) {
234 _log.warn(ioe, ioe);
235 }
236 }
237
238 _session = Session.getInstance(properties);
239
240 return _session;
241 }
242
243 public void sendEmail(MailMessage mailMessage) {
244 if (_log.isDebugEnabled()) {
245 _log.debug("sendEmail");
246 }
247
248 MessageBusUtil.sendMessage(DestinationNames.MAIL, mailMessage);
249 }
250
251 public void setBeanIdentifier(String beanIdentifier) {
252 _beanIdentifier = beanIdentifier;
253 }
254
255 public void updateBlocked(
256 long companyId, long userId, List<String> blocked) {
257
258 if (_log.isDebugEnabled()) {
259 _log.debug("updateBlocked");
260 }
261
262 MethodHandler methodHandler = new MethodHandler(
263 _updateBlockedMethodKey, companyId, userId, blocked);
264
265 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
266 }
267
268 public void updateEmailAddress(
269 long companyId, long userId, String emailAddress) {
270
271 if (_log.isDebugEnabled()) {
272 _log.debug("updateEmailAddress");
273 }
274
275 MethodHandler methodHandler = new MethodHandler(
276 _updateEmailAddressMethodKey, companyId, userId, emailAddress);
277
278 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
279 }
280
281 public void updatePassword(long companyId, long userId, String password) {
282 if (_log.isDebugEnabled()) {
283 _log.debug("updatePassword");
284 }
285
286 MethodHandler methodHandler = new MethodHandler(
287 _updatePasswordMethodKey, companyId, userId, password);
288
289 MessageBusUtil.sendMessage(DestinationNames.MAIL, methodHandler);
290 }
291
292 private static Log _log = LogFactoryUtil.getLog(MailServiceImpl.class);
293
294 private static MethodKey _addForwardMethodKey = new MethodKey(
295 Hook.class.getName(), "addForward", long.class, long.class, List.class,
296 List.class, boolean.class);
297 private static MethodKey _addUserMethodKey = new MethodKey(
298 Hook.class.getName(), "addUser", long.class, long.class, String.class,
299 String.class, String.class, String.class, String.class);
300 private static MethodKey _addVacationMessageMethodKey = new MethodKey(
301 Hook.class.getName(), "addVacationMessage", long.class, long.class,
302 String.class, String.class);
303 private static MethodKey _deleteEmailAddressMethodKey = new MethodKey(
304 Hook.class.getName(), "deleteEmailAddress", long.class, long.class);
305 private static MethodKey _deleteUserMethodKey = new MethodKey(
306 Hook.class.getName(), "deleteUser", long.class, long.class);
307 private static MethodKey _updateBlockedMethodKey = new MethodKey(
308 Hook.class.getName(), "updateBlocked", long.class, long.class,
309 List.class);
310 private static MethodKey _updateEmailAddressMethodKey = new MethodKey(
311 Hook.class.getName(), "updateEmailAddress", long.class, long.class,
312 String.class);
313 private static MethodKey _updatePasswordMethodKey = new MethodKey(
314 Hook.class.getName(), "updatePassword", long.class, long.class,
315 String.class);
316
317 private String _beanIdentifier;
318 private Session _session;
319
320 }