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.GetterUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.model.Company;
025 import com.liferay.portal.model.User;
026 import com.liferay.portal.security.auth.PrincipalException;
027 import com.liferay.portal.security.permission.PermissionCheckerUtil;
028 import com.liferay.portal.service.CompanyLocalServiceUtil;
029 import com.liferay.portal.service.ServiceContext;
030 import com.liferay.portal.service.UserLocalServiceUtil;
031 import com.liferay.portal.util.PortalUtil;
032 import com.liferay.portal.util.PortletKeys;
033 import com.liferay.portlet.messageboards.NoSuchCategoryException;
034 import com.liferay.portlet.messageboards.NoSuchMessageException;
035 import com.liferay.portlet.messageboards.model.MBCategory;
036 import com.liferay.portlet.messageboards.model.MBCategoryConstants;
037 import com.liferay.portlet.messageboards.model.MBMessage;
038 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
039 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
040 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
041 import com.liferay.portlet.messageboards.util.MBMailMessage;
042 import com.liferay.portlet.messageboards.util.MBUtil;
043
044 import javax.mail.Message;
045
046 import org.apache.commons.lang.time.StopWatch;
047
048
053 public class MessageListenerImpl implements MessageListener {
054
055 public boolean accept(String from, String recipient, Message message) {
056 try {
057 String messageId = getMessageId(recipient, message);
058
059 if ((messageId == null) ||
060 (!messageId.startsWith(
061 MBUtil.POP_PORTLET_PREFIX, getOffset()))) {
062
063 return false;
064 }
065
066 Company company = getCompany(recipient);
067 long categoryId = getCategoryId(messageId);
068
069 MBCategory category = MBCategoryLocalServiceUtil.getCategory(
070 categoryId);
071
072 if (category.getCompanyId() != company.getCompanyId()) {
073 return false;
074 }
075
076 if (_log.isDebugEnabled()) {
077 _log.debug("Check to see if user " + from + " exists");
078 }
079
080 UserLocalServiceUtil.getUserByEmailAddress(
081 company.getCompanyId(), from);
082
083 return true;
084 }
085 catch (Exception e) {
086 if (_log.isErrorEnabled()) {
087 _log.error("Unable to process message: " + message, e);
088 }
089
090 return false;
091 }
092 }
093
094 public void deliver(String from, String recipient, Message message)
095 throws MessageListenerException {
096
097 try {
098 StopWatch stopWatch = null;
099
100 if (_log.isDebugEnabled()) {
101 stopWatch = new StopWatch();
102
103 stopWatch.start();
104
105 _log.debug("Deliver message from " + from + " to " + recipient);
106 }
107
108 Company company = getCompany(recipient);
109
110 String messageId = getMessageId(recipient, message);
111
112 if (_log.isDebugEnabled()) {
113 _log.debug("Message id " + messageId);
114 }
115
116 long groupId = 0;
117 long categoryId = getCategoryId(messageId);
118
119 try {
120 MBCategory category = MBCategoryLocalServiceUtil.getCategory(
121 categoryId);
122
123 groupId = category.getGroupId();
124 }
125 catch (NoSuchCategoryException nsce) {
126 groupId = categoryId;
127 categoryId = MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
128 }
129
130 if (_log.isDebugEnabled()) {
131 _log.debug("Group id " + groupId);
132 _log.debug("Category id " + categoryId);
133 }
134
135 User user = UserLocalServiceUtil.getUserByEmailAddress(
136 company.getCompanyId(), from);
137
138 long parentMessageId = getParentMessageId(recipient, message);
139
140 if (_log.isDebugEnabled()) {
141 _log.debug("Parent message id " + parentMessageId);
142 }
143
144 MBMessage parentMessage = null;
145
146 try {
147 if (parentMessageId > 0) {
148 parentMessage = MBMessageLocalServiceUtil.getMessage(
149 parentMessageId);
150 }
151 }
152 catch (NoSuchMessageException nsme) {
153
154
155
156
157 }
158
159 if (_log.isDebugEnabled()) {
160 _log.debug("Parent message " + parentMessage);
161 }
162
163 String subject = MBUtil.getSubjectWithoutMessageId(message);
164
165 MBMailMessage collector = new MBMailMessage();
166
167 MBUtil.collectPartContent(message, collector);
168
169 PermissionCheckerUtil.setThreadValues(user);
170
171 ServiceContext serviceContext = new ServiceContext();
172
173 serviceContext.setAddCommunityPermissions(true);
174 serviceContext.setAddGuestPermissions(true);
175 serviceContext.setLayoutFullURL(
176 PortalUtil.getLayoutFullURL(
177 groupId, PortletKeys.MESSAGE_BOARDS));
178 serviceContext.setScopeGroupId(groupId);
179
180 if (parentMessage == null) {
181 MBMessageServiceUtil.addMessage(
182 groupId, categoryId, subject, collector.getBody(),
183 collector.getFiles(), false, 0.0, true, serviceContext);
184 }
185 else {
186 MBMessageServiceUtil.addMessage(
187 groupId, categoryId, parentMessage.getThreadId(),
188 parentMessage.getMessageId(), subject, collector.getBody(),
189 collector.getFiles(), false, 0.0, true, serviceContext);
190 }
191
192 if (_log.isDebugEnabled()) {
193 _log.debug(
194 "Delivering message takes " + stopWatch.getTime() + " ms");
195 }
196 }
197 catch (PrincipalException pe) {
198 if (_log.isDebugEnabled()) {
199 _log.debug("Prevented unauthorized post from " + from);
200 }
201
202 throw new MessageListenerException(pe);
203 }
204 catch (Exception e) {
205 _log.error(e, e);
206
207 throw new MessageListenerException(e);
208 }
209 finally {
210 PermissionCheckerUtil.setThreadValues(null);
211 }
212 }
213
214 public String getId() {
215 return MessageListenerImpl.class.getName();
216 }
217
218 protected long getCategoryId(String recipient) {
219 int pos = recipient.indexOf(StringPool.AT);
220
221 String target = recipient.substring(
222 MBUtil.POP_PORTLET_PREFIX.length() + getOffset(), pos);
223
224 String[] parts = StringUtil.split(target, StringPool.PERIOD);
225
226 return GetterUtil.getLong(parts[0]);
227 }
228
229 protected Company getCompany(String recipient) throws Exception {
230 int pos =
231 recipient.indexOf(StringPool.AT) +
232 MBUtil.POP_SERVER_SUBDOMAIN_LENGTH + 1;
233
234 if (MBUtil.POP_SERVER_SUBDOMAIN_LENGTH > 0) {
235 pos++;
236 }
237
238 String mx = recipient.substring(pos);
239
240 return CompanyLocalServiceUtil.getCompanyByMx(mx);
241 }
242
243 protected String getMessageId(String recipient, Message message)
244 throws Exception {
245
246 if (MBUtil.POP_SERVER_SUBDOMAIN_LENGTH > 0) {
247 return recipient;
248 }
249 else {
250 return MBUtil.getParentMessageIdString(message);
251 }
252 }
253
254 protected int getOffset() {
255 if (MBUtil.POP_SERVER_SUBDOMAIN_LENGTH == 0) {
256 return 1;
257 }
258 return 0;
259 }
260
261 protected long getParentMessageId(String recipient, Message message)
262 throws Exception {
263
264
265
266 int pos = recipient.indexOf(StringPool.AT);
267
268 String target = recipient.substring(
269 MBUtil.POP_PORTLET_PREFIX.length(), pos);
270
271 String[] parts = StringUtil.split(target, StringPool.PERIOD);
272
273 long parentMessageId = 0;
274
275 if (parts.length == 2) {
276 parentMessageId = GetterUtil.getLong(parts[1]);
277 }
278
279 if (parentMessageId > 0) {
280 return parentMessageId;
281 }
282 else {
283 return MBUtil.getParentMessageId(message);
284 }
285 }
286
287 private static Log _log = LogFactoryUtil.getLog(MessageListenerImpl.class);
288
289 }