1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
52   * <a href="MessageListenerImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Jorge Ferrer
56   * @author Michael C. Han
57   *
58   */
59  public class MessageListenerImpl implements MessageListener {
60  
61      public boolean accept(String from, String recipient, Message message) {
62          try {
63              String messageId = getMessageId(recipient, message);
64  
65              if ((messageId == null) ||
66                  (!messageId.startsWith(
67                      MBUtil.POP_PORTLET_PREFIX, getOffset()))) {
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                 // If the parent message does not exist we ignore it and post
148                 // the message as a new thread.
149 
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             if (parentMessage == null) {
165                 MBMessageServiceUtil.addMessage(
166                     categoryId, subject, collector.getBody(),
167                     collector.getFiles(), false, 0.0, null, true, true);
168             }
169             else {
170                 MBMessageServiceUtil.addMessage(
171                     categoryId, parentMessage.getThreadId(),
172                     parentMessage.getMessageId(), subject, collector.getBody(),
173                     collector.getFiles(), false, 0.0, null, true, true);
174             }
175 
176             if (_log.isDebugEnabled()) {
177                 _log.debug(
178                     "Delivering message takes " + stopWatch.getTime() + " ms");
179             }
180         }
181         catch (PrincipalException pe) {
182             if (_log.isDebugEnabled()) {
183                 _log.debug("Prevented unauthorized post from " + from);
184             }
185 
186             throw new MessageListenerException(pe);
187         }
188         catch (Exception e) {
189             _log.error(e, e);
190 
191             throw new MessageListenerException(e);
192         }
193     }
194 
195     public String getId() {
196         return MessageListenerImpl.class.getName();
197     }
198 
199     protected long getCategoryId(String recipient) {
200         int pos = recipient.indexOf(StringPool.AT);
201 
202         String target = recipient.substring(
203             MBUtil.POP_PORTLET_PREFIX.length() + getOffset(), pos);
204 
205         String[] parts = StringUtil.split(target, StringPool.PERIOD);
206 
207         return GetterUtil.getLong(parts[0]);
208     }
209 
210     protected Company getCompany(String recipient) throws Exception {
211         int pos =
212             recipient.indexOf(StringPool.AT) +
213                 MBUtil.POP_SERVER_SUBDOMAIN_LENGTH + 1;
214 
215         if (MBUtil.POP_SERVER_SUBDOMAIN_LENGTH > 0) {
216             pos++;
217         }
218 
219         String mx = recipient.substring(pos);
220 
221         return CompanyLocalServiceUtil.getCompanyByMx(mx);
222     }
223 
224     protected String getMessageId(String recipient, Message message)
225         throws Exception {
226 
227         if (MBUtil.POP_SERVER_SUBDOMAIN_LENGTH > 0) {
228             return recipient;
229         }
230         else {
231             return MBUtil.getParentMessageIdString(message);
232         }
233     }
234 
235     protected int getOffset() {
236         if (MBUtil.POP_SERVER_SUBDOMAIN_LENGTH == 0) {
237             return 1;
238         }
239         return 0;
240     }
241 
242     protected long getParentMessageId(String recipient, Message message)
243         throws Exception {
244 
245         // Get the parent message ID from the recipient address
246 
247         int pos = recipient.indexOf(StringPool.AT);
248 
249         String target = recipient.substring(
250             MBUtil.POP_PORTLET_PREFIX.length(), pos);
251 
252         String[] parts = StringUtil.split(target, StringPool.PERIOD);
253 
254         long parentMessageId = 0;
255 
256         if (parts.length == 2) {
257             parentMessageId = GetterUtil.getLong(parts[1]);
258         }
259 
260         if (parentMessageId > 0) {
261             return parentMessageId;
262         }
263         else {
264             return MBUtil.getParentMessageId(message);
265         }
266     }
267 
268     private static Log _log = LogFactoryUtil.getLog(MessageListenerImpl.class);
269 
270 }