1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.pop.messaging;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.mail.Account;
20  import com.liferay.portal.kernel.pop.MessageListener;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.pop.POPServerUtil;
25  import com.liferay.util.mail.MailEngine;
26  
27  import java.util.Iterator;
28  import java.util.List;
29  
30  import javax.mail.Address;
31  import javax.mail.Flags;
32  import javax.mail.Folder;
33  import javax.mail.Message.RecipientType;
34  import javax.mail.Message;
35  import javax.mail.MessagingException;
36  import javax.mail.Session;
37  import javax.mail.Store;
38  import javax.mail.internet.InternetAddress;
39  
40  /**
41   * <a href="POPNotificationsMessageListener.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class POPNotificationsMessageListener
47      implements com.liferay.portal.kernel.messaging.MessageListener {
48  
49      public void receive(com.liferay.portal.kernel.messaging.Message message) {
50          try {
51              doReceive(message);
52          }
53          catch (Exception e) {
54              _log.error("Unable to process message " + message, e);
55          }
56      }
57  
58      protected void doReceive(
59              com.liferay.portal.kernel.messaging.Message message)
60          throws Exception {
61  
62          try {
63              pollPopServer();
64          }
65          finally {
66              _store = null;
67              _inboxFolder = null;
68          }
69      }
70  
71      protected String getEmailAddress(Address[] addresses) {
72          if ((addresses == null) || (addresses.length == 0)) {
73              return StringPool.BLANK;
74          }
75  
76          InternetAddress internetAddress = (InternetAddress)addresses[0];
77  
78          return internetAddress.getAddress();
79      }
80  
81      protected void initInboxFolder() throws Exception {
82          if ((_inboxFolder == null) || !_inboxFolder.isOpen()) {
83              initStore();
84  
85              Folder defaultFolder = _store.getDefaultFolder();
86  
87              Folder[] folders = defaultFolder.list();
88  
89              if (folders.length == 0) {
90                  throw new MessagingException("Inbox not found");
91              }
92              else {
93                  _inboxFolder = folders[0];
94  
95                  _inboxFolder.open(Folder.READ_WRITE);
96              }
97          }
98      }
99  
100     protected void initStore() throws Exception {
101         if ((_store == null) || !_store.isConnected()) {
102             Session session = MailEngine.getSession();
103 
104             String storeProtocol = GetterUtil.getString(
105                 session.getProperty("mail.store.protocol"));
106 
107             if (!storeProtocol.equals(Account.PROTOCOL_POPS)) {
108                 storeProtocol = Account.PROTOCOL_POP;
109             }
110 
111             _store = session.getStore(storeProtocol);
112 
113             String prefix = "mail." + storeProtocol + ".";
114 
115             String host = session.getProperty(prefix + "host");
116 
117             String user = session.getProperty(prefix + "user");
118 
119             if (Validator.isNull(user)) {
120                 user = session.getProperty("mail.smtp.user");
121             }
122 
123             String password = session.getProperty(prefix + "password");
124 
125             if (Validator.isNull(password)) {
126                 password = session.getProperty("mail.smtp.password");
127             }
128 
129             _store.connect(host, user, password);
130         }
131     }
132 
133     protected void nostifyListeners(
134             List<MessageListener> listeners, Message message)
135         throws Exception {
136 
137         String from = getEmailAddress(message.getFrom());
138         String recipient = getEmailAddress(
139             message.getRecipients(RecipientType.TO));
140 
141         if (_log.isDebugEnabled()) {
142             _log.debug("From " + from);
143             _log.debug("Recipient " + recipient);
144         }
145 
146         Iterator<MessageListener> itr = listeners.iterator();
147 
148         while (itr.hasNext()) {
149             MessageListener messageListener = itr.next();
150 
151             try {
152                 if (messageListener.accept(from, recipient, message)) {
153                     messageListener.deliver(from, recipient, message);
154                 }
155             }
156             catch (Exception e) {
157                 _log.error(e, e);
158             }
159         }
160     }
161 
162     protected void nostifyListeners(Message[] messages) throws Exception {
163         if (_log.isDebugEnabled()) {
164             _log.debug("Messages " + messages.length);
165         }
166 
167         List<MessageListener> listeners = POPServerUtil.getListeners();
168 
169         for (int i = 0; i < messages.length; i++) {
170             Message message = messages[i];
171 
172             if (_log.isDebugEnabled()) {
173                 _log.debug("Message " + message);
174             }
175 
176             nostifyListeners(listeners, message);
177         }
178     }
179 
180     protected void pollPopServer() throws Exception {
181         initInboxFolder();
182 
183         Message[] messages = _inboxFolder.getMessages();
184 
185         try {
186             nostifyListeners(messages);
187         }
188         finally {
189             if (_log.isDebugEnabled()) {
190                 _log.debug("Deleting messages");
191             }
192 
193             _inboxFolder.setFlags(
194                 messages, new Flags(Flags.Flag.DELETED), true);
195 
196             _inboxFolder.close(true);
197         }
198     }
199 
200     private static Log _log = LogFactoryUtil.getLog(
201         POPNotificationsMessageListener.class);
202 
203     private Folder _inboxFolder;
204     private Store _store;
205 
206 }