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