1   /**
2    * Copyright (c) 2000-2008 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.pop.MessageListener;
26  import com.liferay.portal.kernel.pop.MessageListenerException;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.model.Company;
31  import com.liferay.portal.model.User;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.security.permission.PermissionCheckerUtil;
34  import com.liferay.portal.service.CompanyLocalServiceUtil;
35  import com.liferay.portal.service.UserLocalServiceUtil;
36  import com.liferay.portlet.messageboards.NoSuchMessageException;
37  import com.liferay.portlet.messageboards.model.MBCategory;
38  import com.liferay.portlet.messageboards.model.MBMessage;
39  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
40  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
41  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
42  import com.liferay.portlet.messageboards.util.MBMailMessage;
43  import com.liferay.portlet.messageboards.util.MBUtil;
44  
45  import javax.mail.Message;
46  
47  import org.apache.commons.lang.time.StopWatch;
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
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(MBUtil.POP_PORTLET_PREFIX))) {
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                 // If the parent message does not exist we ignore it and post
147                 // the message as a new thread.
148 
149             }
150 
151             if (_log.isDebugEnabled()) {
152                 _log.debug("Parent message " + parentMessage);
153             }
154 
155             MBMailMessage collector = new MBMailMessage();
156 
157             MBUtil.collectPartContent(message, collector);
158 
159             PermissionCheckerUtil.setThreadValues(user);
160 
161             if (parentMessage == null) {
162                 MBMessageServiceUtil.addMessage(
163                     categoryId, message.getSubject(), collector.getBody(),
164                     collector.getFiles(), false, 0.0, null, true, true);
165             }
166             else {
167                 MBMessageServiceUtil.addMessage(
168                     categoryId, parentMessage.getThreadId(),
169                     parentMessage.getMessageId(), message.getSubject(),
170                     collector.getBody(), collector.getFiles(), false, 0.0, null,
171                     true, true);
172             }
173 
174             if (_log.isDebugEnabled()) {
175                 _log.debug(
176                     "Delivering message takes " + stopWatch.getTime() + " ms");
177             }
178         }
179         catch (PrincipalException pe) {
180             if (_log.isDebugEnabled()) {
181                 _log.debug("Prevented unauthorized post from " + from);
182             }
183 
184             throw new MessageListenerException(pe);
185         }
186         catch (Exception e) {
187             _log.error(e, e);
188 
189             throw new MessageListenerException(e);
190         }
191     }
192 
193     public String getId() {
194         return MessageListenerImpl.class.getName();
195     }
196 
197     protected long getCategoryId(String recipient) {
198         int pos = recipient.indexOf(StringPool.AT);
199 
200         String target = recipient.substring(
201             MBUtil.POP_PORTLET_PREFIX.length(), pos);
202 
203         String[] parts = StringUtil.split(target, StringPool.PERIOD);
204 
205         return GetterUtil.getLong(parts[0]);
206     }
207 
208     protected Company getCompany(String recipient) throws Exception {
209         int pos =
210             recipient.indexOf(StringPool.AT) +
211                 MBUtil.POP_SERVER_SUBDOMAIN_LENGTH + 1;
212 
213         if (MBUtil.POP_SERVER_SUBDOMAIN_LENGTH > 0) {
214             pos++;
215         }
216 
217         String mx = recipient.substring(pos);
218 
219         return CompanyLocalServiceUtil.getCompanyByMx(mx);
220     }
221 
222     protected String getMessageId(String recipient, Message message)
223         throws Exception {
224 
225         if (MBUtil.POP_SERVER_SUBDOMAIN_LENGTH > 0) {
226             return recipient;
227         }
228         else {
229             return MBUtil.getParentMessageIdString(message);
230         }
231     }
232 
233     protected long getParentMessageId(String recipient, Message message)
234         throws Exception {
235 
236         // Get the parent message ID from the recipient address
237 
238         int pos = recipient.indexOf(StringPool.AT);
239 
240         String target = recipient.substring(
241             MBUtil.POP_PORTLET_PREFIX.length(), pos);
242 
243         String[] parts = StringUtil.split(target, StringPool.PERIOD);
244 
245         long parentMessageId = 0;
246 
247         if (parts.length == 2) {
248             parentMessageId = GetterUtil.getLong(parts[1]);
249         }
250 
251         if (parentMessageId > 0) {
252             return parentMessageId;
253         }
254         else {
255             return MBUtil.getParentMessageId(message);
256         }
257     }
258 
259     private static Log _log = LogFactory.getLog(MessageListenerImpl.class);
260 
261 }