001    /**
002     * Copyright (c) 2000-2012 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.List;
028    
029    import javax.mail.Address;
030    import javax.mail.Flags;
031    import javax.mail.Folder;
032    import javax.mail.Message.RecipientType;
033    import javax.mail.Message;
034    import javax.mail.MessagingException;
035    import javax.mail.Session;
036    import javax.mail.Store;
037    import javax.mail.internet.InternetAddress;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class POPNotificationsMessageListener
043            extends com.liferay.portal.kernel.messaging.BaseMessageListener {
044    
045            @Override
046            protected void doReceive(
047                            com.liferay.portal.kernel.messaging.Message message)
048                    throws Exception {
049    
050                    try {
051                            pollPopServer();
052                    }
053                    finally {
054                            _store = null;
055                            _inboxFolder = null;
056                    }
057            }
058    
059            protected String getEmailAddress(Address[] addresses) {
060                    if ((addresses == null) || (addresses.length == 0)) {
061                            return StringPool.BLANK;
062                    }
063    
064                    InternetAddress internetAddress = (InternetAddress)addresses[0];
065    
066                    return internetAddress.getAddress();
067            }
068    
069            protected void initInboxFolder() throws Exception {
070                    if ((_inboxFolder == null) || !_inboxFolder.isOpen()) {
071                            initStore();
072    
073                            Folder defaultFolder = _store.getDefaultFolder();
074    
075                            Folder[] folders = defaultFolder.list();
076    
077                            if (folders.length == 0) {
078                                    throw new MessagingException("Inbox not found");
079                            }
080                            else {
081                                    _inboxFolder = folders[0];
082    
083                                    _inboxFolder.open(Folder.READ_WRITE);
084                            }
085                    }
086            }
087    
088            protected void initStore() throws Exception {
089                    if ((_store == null) || !_store.isConnected()) {
090                            Session session = MailEngine.getSession();
091    
092                            String storeProtocol = GetterUtil.getString(
093                                    session.getProperty("mail.store.protocol"));
094    
095                            if (!storeProtocol.equals(Account.PROTOCOL_POPS)) {
096                                    storeProtocol = Account.PROTOCOL_POP;
097                            }
098    
099                            _store = session.getStore(storeProtocol);
100    
101                            String prefix = "mail." + storeProtocol + ".";
102    
103                            String host = session.getProperty(prefix + "host");
104    
105                            String user = session.getProperty(prefix + "user");
106    
107                            if (Validator.isNull(user)) {
108                                    user = session.getProperty("mail.smtp.user");
109                            }
110    
111                            String password = session.getProperty(prefix + "password");
112    
113                            if (Validator.isNull(password)) {
114                                    password = session.getProperty("mail.smtp.password");
115                            }
116    
117                            _store.connect(host, user, password);
118                    }
119            }
120    
121            protected void notifyMessageListeners(
122                            List<MessageListener> messageListeners, Message message)
123                    throws Exception {
124    
125                    String from = getEmailAddress(message.getFrom());
126                    String recipient = getEmailAddress(
127                            message.getRecipients(RecipientType.TO));
128    
129                    if (_log.isDebugEnabled()) {
130                            _log.debug("From " + from);
131                            _log.debug("Recipient " + recipient);
132                    }
133    
134                    for (MessageListener messageListener : messageListeners) {
135                            try {
136                                    if (messageListener.accept(from, recipient, message)) {
137                                            messageListener.deliver(from, recipient, message);
138                                    }
139                            }
140                            catch (Exception e) {
141                                    _log.error(e, e);
142                            }
143                    }
144            }
145    
146            protected void notifyMessageListeners(Message[] messages) throws Exception {
147                    if (_log.isDebugEnabled()) {
148                            _log.debug("Messages " + messages.length);
149                    }
150    
151                    List<MessageListener> messageListeners = POPServerUtil.getListeners();
152    
153                    for (int i = 0; i < messages.length; i++) {
154                            Message message = messages[i];
155    
156                            if (_log.isDebugEnabled()) {
157                                    _log.debug("Message " + message);
158                            }
159    
160                            notifyMessageListeners(messageListeners, message);
161                    }
162            }
163    
164            protected void pollPopServer() throws Exception {
165                    initInboxFolder();
166    
167                    Message[] messages = _inboxFolder.getMessages();
168    
169                    try {
170                            notifyMessageListeners(messages);
171                    }
172                    finally {
173                            if (_log.isDebugEnabled()) {
174                                    _log.debug("Deleting messages");
175                            }
176    
177                            _inboxFolder.setFlags(
178                                    messages, new Flags(Flags.Flag.DELETED), true);
179    
180                            _inboxFolder.close(true);
181                    }
182            }
183    
184            private static Log _log = LogFactoryUtil.getLog(
185                    POPNotificationsMessageListener.class);
186    
187            private Folder _inboxFolder;
188            private Store _store;
189    
190    }