001
014
015 package com.liferay.portlet.messageboards.pop;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.pop.MessageListener;
020 import com.liferay.portal.kernel.pop.MessageListenerException;
021 import com.liferay.portal.kernel.util.ArrayUtil;
022 import com.liferay.portal.kernel.util.CharPool;
023 import com.liferay.portal.kernel.util.Http;
024 import com.liferay.portal.kernel.util.ObjectValuePair;
025 import com.liferay.portal.kernel.util.PrefsPropsUtil;
026 import com.liferay.portal.kernel.util.PropsKeys;
027 import com.liferay.portal.kernel.util.StreamUtil;
028 import com.liferay.portal.kernel.util.StringUtil;
029 import com.liferay.portal.model.Company;
030 import com.liferay.portal.model.User;
031 import com.liferay.portal.security.auth.PrincipalException;
032 import com.liferay.portal.security.permission.PermissionCheckerUtil;
033 import com.liferay.portal.service.CompanyLocalServiceUtil;
034 import com.liferay.portal.service.ServiceContext;
035 import com.liferay.portal.service.UserLocalServiceUtil;
036 import com.liferay.portal.util.PortalUtil;
037 import com.liferay.portal.util.PortletKeys;
038 import com.liferay.portal.util.PropsValues;
039 import com.liferay.portlet.messageboards.model.MBCategory;
040 import com.liferay.portlet.messageboards.model.MBCategoryConstants;
041 import com.liferay.portlet.messageboards.model.MBMessage;
042 import com.liferay.portlet.messageboards.model.MBMessageConstants;
043 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
044 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
045 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
046 import com.liferay.portlet.messageboards.util.MBMailMessage;
047 import com.liferay.portlet.messageboards.util.MBUtil;
048
049 import java.io.InputStream;
050
051 import java.util.List;
052
053 import javax.mail.Message;
054 import javax.mail.MessagingException;
055
056 import org.apache.commons.lang.time.StopWatch;
057
058
063 public class MessageListenerImpl implements MessageListener {
064
065 @Override
066 public boolean accept(String from, String recipient, Message message) {
067 try {
068 if (isAutoReply(message)) {
069 return false;
070 }
071
072 String messageIdString = getMessageIdString(recipient, message);
073
074 if ((messageIdString == null) ||
075 !messageIdString.startsWith(
076 MBUtil.MESSAGE_POP_PORTLET_PREFIX,
077 MBUtil.getMessageIdStringOffset())) {
078
079 return false;
080 }
081
082 Company company = getCompany(messageIdString);
083 long categoryId = MBUtil.getCategoryId(messageIdString);
084
085 MBCategory category = MBCategoryLocalServiceUtil.getCategory(
086 categoryId);
087
088 if ((category.getCompanyId() != company.getCompanyId()) &&
089 !category.isRoot()) {
090
091 return false;
092 }
093
094 if (_log.isDebugEnabled()) {
095 _log.debug("Check to see if user " + from + " exists");
096 }
097
098 String pop3User = PrefsPropsUtil.getString(
099 PropsKeys.MAIL_SESSION_MAIL_POP3_USER,
100 PropsValues.MAIL_SESSION_MAIL_POP3_USER);
101
102 if (StringUtil.equalsIgnoreCase(from, pop3User)) {
103 return false;
104 }
105
106 UserLocalServiceUtil.getUserByEmailAddress(
107 company.getCompanyId(), from);
108
109 return true;
110 }
111 catch (Exception e) {
112 if (_log.isErrorEnabled()) {
113 _log.error("Unable to process message: " + message, e);
114 }
115
116 return false;
117 }
118 }
119
120 @Override
121 public void deliver(String from, String recipient, Message message)
122 throws MessageListenerException {
123
124 List<ObjectValuePair<String, InputStream>> inputStreamOVPs = null;
125
126 try {
127 StopWatch stopWatch = new StopWatch();
128
129 stopWatch.start();
130
131 if (_log.isDebugEnabled()) {
132 _log.debug("Deliver message from " + from + " to " + recipient);
133 }
134
135 String messageIdString = getMessageIdString(recipient, message);
136
137 Company company = getCompany(messageIdString);
138
139 if (_log.isDebugEnabled()) {
140 _log.debug("Message id " + messageIdString);
141 }
142
143 long parentMessageId = MBUtil.getMessageId(messageIdString);
144
145 if (_log.isDebugEnabled()) {
146 _log.debug("Parent message id " + parentMessageId);
147 }
148
149 MBMessage parentMessage = null;
150
151 if (parentMessageId > 0) {
152 parentMessage = MBMessageLocalServiceUtil.fetchMBMessage(
153 parentMessageId);
154 }
155
156 if (_log.isDebugEnabled()) {
157 _log.debug("Parent message " + parentMessage);
158 }
159
160 long groupId = 0;
161 long categoryId = MBUtil.getCategoryId(messageIdString);
162
163 MBCategory category = MBCategoryLocalServiceUtil.fetchMBCategory(
164 categoryId);
165
166 if (category == null) {
167 categoryId = MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
168
169 if (parentMessage != null) {
170 groupId = parentMessage.getGroupId();
171 }
172 }
173 else {
174 groupId = category.getGroupId();
175 }
176
177 if (_log.isDebugEnabled()) {
178 _log.debug("Group id " + groupId);
179 _log.debug("Category id " + categoryId);
180 }
181
182 User user = UserLocalServiceUtil.getUserByEmailAddress(
183 company.getCompanyId(), from);
184
185 String subject = null;
186
187 if (parentMessage != null) {
188 subject = MBUtil.getSubjectForEmail(parentMessage);
189 }
190
191 MBMailMessage mbMailMessage = new MBMailMessage();
192
193 MBUtil.collectPartContent(message, mbMailMessage);
194
195 inputStreamOVPs = mbMailMessage.getInputStreamOVPs();
196
197 PermissionCheckerUtil.setThreadValues(user);
198
199 ServiceContext serviceContext = new ServiceContext();
200
201 serviceContext.setAttribute("propagatePermissions", Boolean.TRUE);
202 serviceContext.setLayoutFullURL(
203 PortalUtil.getLayoutFullURL(
204 groupId, PortletKeys.MESSAGE_BOARDS,
205 StringUtil.equalsIgnoreCase(
206 Http.HTTPS, PropsValues.WEB_SERVER_PROTOCOL)));
207 serviceContext.setScopeGroupId(groupId);
208
209 if (parentMessage == null) {
210 MBMessageServiceUtil.addMessage(
211 groupId, categoryId, subject, mbMailMessage.getBody(),
212 MBMessageConstants.DEFAULT_FORMAT, inputStreamOVPs, false,
213 0.0, true, serviceContext);
214 }
215 else {
216 MBMessageServiceUtil.addMessage(
217 parentMessage.getMessageId(), subject,
218 mbMailMessage.getBody(), MBMessageConstants.DEFAULT_FORMAT,
219 inputStreamOVPs, false, 0.0, true, serviceContext);
220 }
221
222 if (_log.isDebugEnabled()) {
223 _log.debug(
224 "Delivering message takes " + stopWatch.getTime() + " ms");
225 }
226 }
227 catch (PrincipalException pe) {
228 if (_log.isDebugEnabled()) {
229 _log.debug("Prevented unauthorized post from " + from);
230 }
231
232 throw new MessageListenerException(pe);
233 }
234 catch (Exception e) {
235 _log.error(e, e);
236
237 throw new MessageListenerException(e);
238 }
239 finally {
240 if (inputStreamOVPs != null) {
241 for (ObjectValuePair<String, InputStream> inputStreamOVP :
242 inputStreamOVPs) {
243
244 InputStream inputStream = inputStreamOVP.getValue();
245
246 StreamUtil.cleanUp(inputStream);
247 }
248 }
249
250 PermissionCheckerUtil.setThreadValues(null);
251 }
252 }
253
254 @Override
255 public String getId() {
256 return MessageListenerImpl.class.getName();
257 }
258
259 protected Company getCompany(String messageIdString) throws Exception {
260 int pos =
261 messageIdString.indexOf(CharPool.AT) +
262 PropsValues.POP_SERVER_SUBDOMAIN.length() + 1;
263
264 if (PropsValues.POP_SERVER_SUBDOMAIN.length() > 0) {
265 pos++;
266 }
267
268 int endPos = messageIdString.indexOf(CharPool.GREATER_THAN, pos);
269
270 if (endPos == -1) {
271 endPos = messageIdString.length();
272 }
273
274 String mx = messageIdString.substring(pos, endPos);
275
276 return CompanyLocalServiceUtil.getCompanyByMx(mx);
277 }
278
279 protected String getMessageIdString(String recipient, Message message)
280 throws Exception {
281
282 if (PropsValues.POP_SERVER_SUBDOMAIN.length() > 0) {
283 return recipient;
284 }
285 else {
286 return MBUtil.getParentMessageIdString(message);
287 }
288 }
289
290 protected boolean isAutoReply(Message message) throws MessagingException {
291 String[] autoReply = message.getHeader("X-Autoreply");
292
293 if (ArrayUtil.isNotEmpty(autoReply)) {
294 return true;
295 }
296
297 String[] autoReplyFrom = message.getHeader("X-Autoreply-From");
298
299 if (ArrayUtil.isNotEmpty(autoReplyFrom)) {
300 return true;
301 }
302
303 String[] mailAutoReply = message.getHeader("X-Mail-Autoreply");
304
305 if (ArrayUtil.isNotEmpty(mailAutoReply)) {
306 return true;
307 }
308
309 return false;
310 }
311
312 private static Log _log = LogFactoryUtil.getLog(MessageListenerImpl.class);
313
314 }