1
22
23 package com.liferay.portlet.messageboards.pop;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.pop.MessageListener;
28 import com.liferay.portal.kernel.pop.MessageListenerException;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portal.kernel.util.StringUtil;
32 import com.liferay.portal.model.Company;
33 import com.liferay.portal.model.User;
34 import com.liferay.portal.security.auth.PrincipalException;
35 import com.liferay.portal.security.permission.PermissionCheckerUtil;
36 import com.liferay.portal.service.CompanyLocalServiceUtil;
37 import com.liferay.portal.service.UserLocalServiceUtil;
38 import com.liferay.portlet.messageboards.NoSuchMessageException;
39 import com.liferay.portlet.messageboards.model.MBCategory;
40 import com.liferay.portlet.messageboards.model.MBMessage;
41 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
42 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
43 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
44 import com.liferay.portlet.messageboards.util.MBMailMessage;
45 import com.liferay.portlet.messageboards.util.MBUtil;
46
47 import javax.mail.Message;
48
49 import org.apache.commons.lang.time.StopWatch;
50
51
58 public class MessageListenerImpl implements MessageListener {
59
60 public boolean accept(String from, String recipient, Message message) {
61 try {
62 String messageId = getMessageId(recipient, message);
63
64 if ((messageId == null) ||
65 (!messageId.startsWith(
66 MBUtil.POP_PORTLET_PREFIX, getOffset()))) {
67
68 return false;
69 }
70
71 Company company = getCompany(recipient);
72 long categoryId = getCategoryId(messageId);
73
74 MBCategory category = MBCategoryLocalServiceUtil.getCategory(
75 categoryId);
76
77 if (category.getCompanyId() != company.getCompanyId()) {
78 return false;
79 }
80
81 if (_log.isDebugEnabled()) {
82 _log.debug("Check to see if user " + from + " exists");
83 }
84
85 UserLocalServiceUtil.getUserByEmailAddress(
86 company.getCompanyId(), from);
87
88 return true;
89 }
90 catch (Exception e) {
91 if (_log.isErrorEnabled()) {
92 _log.error("Unable to process message: " + message, e);
93 }
94
95 return false;
96 }
97 }
98
99 public void deliver(String from, String recipient, Message message)
100 throws MessageListenerException {
101
102 try {
103 StopWatch stopWatch = null;
104
105 if (_log.isDebugEnabled()) {
106 stopWatch = new StopWatch();
107
108 stopWatch.start();
109
110 _log.debug("Deliver message from " + from + " to " + recipient);
111 }
112
113 Company company = getCompany(recipient);
114
115 String messageId = getMessageId(recipient, message);
116
117 if (_log.isDebugEnabled()) {
118 _log.debug("Message id " + messageId);
119 }
120
121 long categoryId = getCategoryId(messageId);
122
123 if (_log.isDebugEnabled()) {
124 _log.debug("Category id " + categoryId);
125 }
126
127 User user = UserLocalServiceUtil.getUserByEmailAddress(
128 company.getCompanyId(), from);
129
130 long parentMessageId = getParentMessageId(recipient, message);
131
132 if (_log.isDebugEnabled()) {
133 _log.debug("Parent message id " + parentMessageId);
134 }
135
136 MBMessage parentMessage = null;
137
138 try {
139 if (parentMessageId > 0) {
140 parentMessage = MBMessageLocalServiceUtil.getMessage(
141 parentMessageId);
142 }
143 }
144 catch (NoSuchMessageException nsme) {
145
146
149 }
150
151 if (_log.isDebugEnabled()) {
152 _log.debug("Parent message " + parentMessage);
153 }
154
155 String subject = MBUtil.getSubjectWithoutMessageId(message);
156
157 MBMailMessage collector = new MBMailMessage();
158
159 MBUtil.collectPartContent(message, collector);
160
161 PermissionCheckerUtil.setThreadValues(user);
162
163 if (parentMessage == null) {
164 MBMessageServiceUtil.addMessage(
165 categoryId, subject, collector.getBody(),
166 collector.getFiles(), false, 0.0, null, true, true);
167 }
168 else {
169 MBMessageServiceUtil.addMessage(
170 categoryId, parentMessage.getThreadId(),
171 parentMessage.getMessageId(), subject, collector.getBody(),
172 collector.getFiles(), false, 0.0, null, true, true);
173 }
174
175 if (_log.isDebugEnabled()) {
176 _log.debug(
177 "Delivering message takes " + stopWatch.getTime() + " ms");
178 }
179 }
180 catch (PrincipalException pe) {
181 if (_log.isDebugEnabled()) {
182 _log.debug("Prevented unauthorized post from " + from);
183 }
184
185 throw new MessageListenerException(pe);
186 }
187 catch (Exception e) {
188 _log.error(e, e);
189
190 throw new MessageListenerException(e);
191 }
192 }
193
194 public String getId() {
195 return MessageListenerImpl.class.getName();
196 }
197
198 protected long getCategoryId(String recipient) {
199 int pos = recipient.indexOf(StringPool.AT);
200
201 String target = recipient.substring(
202 MBUtil.POP_PORTLET_PREFIX.length() + getOffset(), pos);
203
204 String[] parts = StringUtil.split(target, StringPool.PERIOD);
205
206 return GetterUtil.getLong(parts[0]);
207 }
208
209 protected Company getCompany(String recipient) throws Exception {
210 int pos =
211 recipient.indexOf(StringPool.AT) +
212 MBUtil.POP_SERVER_SUBDOMAIN_LENGTH + 1;
213
214 if (MBUtil.POP_SERVER_SUBDOMAIN_LENGTH > 0) {
215 pos++;
216 }
217
218 String mx = recipient.substring(pos);
219
220 return CompanyLocalServiceUtil.getCompanyByMx(mx);
221 }
222
223 protected String getMessageId(String recipient, Message message)
224 throws Exception {
225
226 if (MBUtil.POP_SERVER_SUBDOMAIN_LENGTH > 0) {
227 return recipient;
228 }
229 else {
230 return MBUtil.getParentMessageIdString(message);
231 }
232 }
233
234 protected int getOffset() {
235 if (MBUtil.POP_SERVER_SUBDOMAIN_LENGTH == 0) {
236 return 1;
237 }
238 return 0;
239 }
240
241 protected long getParentMessageId(String recipient, Message message)
242 throws Exception {
243
244
246 int pos = recipient.indexOf(StringPool.AT);
247
248 String target = recipient.substring(
249 MBUtil.POP_PORTLET_PREFIX.length(), pos);
250
251 String[] parts = StringUtil.split(target, StringPool.PERIOD);
252
253 long parentMessageId = 0;
254
255 if (parts.length == 2) {
256 parentMessageId = GetterUtil.getLong(parts[1]);
257 }
258
259 if (parentMessageId > 0) {
260 return parentMessageId;
261 }
262 else {
263 return MBUtil.getParentMessageId(message);
264 }
265 }
266
267 private static Log _log = LogFactoryUtil.getLog(MessageListenerImpl.class);
268
269 }