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             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(), 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 long getParentMessageId(String recipient, Message message)
235         throws Exception {
236 
237         // Get the parent message ID from the recipient address
238 
239         int pos = recipient.indexOf(StringPool.AT);
240 
241         String target = recipient.substring(
242             MBUtil.POP_PORTLET_PREFIX.length(), pos);
243 
244         String[] parts = StringUtil.split(target, StringPool.PERIOD);
245 
246         long parentMessageId = 0;
247 
248         if (parts.length == 2) {
249             parentMessageId = GetterUtil.getLong(parts[1]);
250         }
251 
252         if (parentMessageId > 0) {
253             return parentMessageId;
254         }
255         else {
256             return MBUtil.getParentMessageId(message);
257         }
258     }
259 
260     private static Log _log = LogFactory.getLog(MessageListenerImpl.class);
261 
262 }