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