001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.pop.messaging;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.mail.Account;
020    import com.liferay.portal.kernel.pop.MessageListener;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.pop.POPServerUtil;
025    import com.liferay.util.mail.MailEngine;
026    
027    import java.util.Iterator;
028    import java.util.List;
029    
030    import javax.mail.Address;
031    import javax.mail.Flags;
032    import javax.mail.Folder;
033    import javax.mail.Message.RecipientType;
034    import javax.mail.Message;
035    import javax.mail.MessagingException;
036    import javax.mail.Session;
037    import javax.mail.Store;
038    import javax.mail.internet.InternetAddress;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class POPNotificationsMessageListener
044            extends com.liferay.portal.kernel.messaging.BaseMessageListener {
045    
046            @Override
047            protected void doReceive(
048                            com.liferay.portal.kernel.messaging.Message message)
049                    throws Exception {
050    
051                    try {
052                            pollPopServer();
053                    }
054                    finally {
055                            _store = null;
056                            _inboxFolder = null;
057                    }
058            }
059    
060            protected String getEmailAddress(Address[] addresses) {
061                    if ((addresses == null) || (addresses.length == 0)) {
062                            return StringPool.BLANK;
063                    }
064    
065                    InternetAddress internetAddress = (InternetAddress)addresses[0];
066    
067                    return internetAddress.getAddress();
068            }
069    
070            protected void initInboxFolder() throws Exception {
071                    if ((_inboxFolder == null) || !_inboxFolder.isOpen()) {
072                            initStore();
073    
074                            Folder defaultFolder = _store.getDefaultFolder();
075    
076                            Folder[] folders = defaultFolder.list();
077    
078                            if (folders.length == 0) {
079                                    throw new MessagingException("Inbox not found");
080                            }
081                            else {
082                                    _inboxFolder = folders[0];
083    
084                                    _inboxFolder.open(Folder.READ_WRITE);
085                            }
086                    }
087            }
088    
089            protected void initStore() throws Exception {
090                    if ((_store != null) && _store.isConnected()) {
091                            return;
092                    }
093    
094                    Session session = MailEngine.getSession();
095    
096                    String storeProtocol = GetterUtil.getString(
097                            session.getProperty("mail.store.protocol"));
098    
099                    if (!storeProtocol.equals(Account.PROTOCOL_POPS)) {
100                            storeProtocol = Account.PROTOCOL_POP;
101                    }
102    
103                    _store = session.getStore(storeProtocol);
104    
105                    String prefix = "mail." + storeProtocol + ".";
106    
107                    String host = session.getProperty(prefix + "host");
108    
109                    String user = session.getProperty(prefix + "user");
110    
111                    if (Validator.isNull(user)) {
112                            user = session.getProperty("mail.smtp.user");
113                    }
114    
115                    String password = session.getProperty(prefix + "password");
116    
117                    if (Validator.isNull(password)) {
118                            password = session.getProperty("mail.smtp.password");
119                    }
120    
121                    _store.connect(host, user, password);
122            }
123    
124            protected void nostifyListeners(
125                            List<MessageListener> listeners, Message message)
126                    throws Exception {
127    
128                    String from = getEmailAddress(message.getFrom());
129                    String recipient = getEmailAddress(
130                            message.getRecipients(RecipientType.TO));
131    
132                    if (_log.isDebugEnabled()) {
133                            _log.debug("From " + from);
134                            _log.debug("Recipient " + recipient);
135                    }
136    
137                    Iterator<MessageListener> itr = listeners.iterator();
138    
139                    while (itr.hasNext()) {
140                            MessageListener messageListener = itr.next();
141    
142                            try {
143                                    if (messageListener.accept(from, recipient, message)) {
144                                            messageListener.deliver(from, recipient, message);
145                                    }
146                            }
147                            catch (Exception e) {
148                                    _log.error(e, e);
149                            }
150                    }
151            }
152    
153            protected void nostifyListeners(Message[] messages) throws Exception {
154                    if (_log.isDebugEnabled()) {
155                            _log.debug("Messages " + messages.length);
156                    }
157    
158                    List<MessageListener> listeners = POPServerUtil.getListeners();
159    
160                    for (int i = 0; i < messages.length; i++) {
161                            Message message = messages[i];
162    
163                            if (_log.isDebugEnabled()) {
164                                    _log.debug("Message " + message);
165                            }
166    
167                            nostifyListeners(listeners, message);
168                    }
169            }
170    
171            protected void pollPopServer() throws Exception {
172                    initInboxFolder();
173    
174                    Message[] messages = _inboxFolder.getMessages();
175    
176                    try {
177                            nostifyListeners(messages);
178                    }
179                    finally {
180                            if (_log.isDebugEnabled()) {
181                                    _log.debug("Deleting messages");
182                            }
183    
184                            _inboxFolder.setFlags(
185                                    messages, new Flags(Flags.Flag.DELETED), true);
186    
187                            _inboxFolder.close(true);
188                    }
189            }
190    
191            private static Log _log = LogFactoryUtil.getLog(
192                    POPNotificationsMessageListener.class);
193    
194            private Folder _inboxFolder;
195            private Store _store;
196    
197    }