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.pop.MessageListenerException;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.pop.POPServerUtil;
027    import com.liferay.util.mail.MailEngine;
028    
029    import javax.mail.Address;
030    import javax.mail.Flags;
031    import javax.mail.Folder;
032    import javax.mail.Message;
033    import javax.mail.Message.RecipientType;
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 MessagingException {
049    
050                    Store store = null;
051    
052                    try {
053                            store = getStore();
054    
055                            Folder inboxFolder = getInboxFolder(store);
056    
057                            if (inboxFolder == null) {
058                                    return;
059                            }
060    
061                            try {
062                                    Message[] messages = inboxFolder.getMessages();
063    
064                                    if (messages == null) {
065                                            return;
066                                    }
067    
068                                    if (_log.isDebugEnabled()) {
069                                            _log.debug("Deleting messages");
070                                    }
071    
072                                    inboxFolder.setFlags(
073                                            messages, new Flags(Flags.Flag.DELETED), true);
074    
075                                    notifyMessageListeners(messages);
076                            }
077                            finally {
078                                    inboxFolder.close(true);
079                            }
080                    }
081                    finally {
082                            if (store != null) {
083                                    store.close();
084                            }
085                    }
086            }
087    
088            protected String getEmailAddress(Address[] addresses) {
089                    if (ArrayUtil.isEmpty(addresses)) {
090                            return StringPool.BLANK;
091                    }
092    
093                    InternetAddress internetAddress = (InternetAddress)addresses[0];
094    
095                    return internetAddress.getAddress();
096            }
097    
098            protected Folder getInboxFolder(Store store) throws MessagingException {
099                    Folder defaultFolder = store.getDefaultFolder();
100    
101                    Folder[] folders = defaultFolder.list();
102    
103                    if (folders.length == 0) {
104                            throw new MessagingException("Inbox not found");
105                    }
106    
107                    Folder inboxFolder = folders[0];
108    
109                    inboxFolder.open(Folder.READ_WRITE);
110    
111                    return inboxFolder;
112            }
113    
114            protected Store getStore() throws MessagingException {
115                    Session session = MailEngine.getSession();
116    
117                    String storeProtocol = GetterUtil.getString(
118                            session.getProperty("mail.store.protocol"));
119    
120                    if (!storeProtocol.equals(Account.PROTOCOL_POPS)) {
121                            storeProtocol = Account.PROTOCOL_POP;
122                    }
123    
124                    Store store = session.getStore(storeProtocol);
125    
126                    String prefix = "mail." + storeProtocol + ".";
127    
128                    String host = session.getProperty(prefix + "host");
129    
130                    String user = session.getProperty(prefix + "user");
131    
132                    if (Validator.isNull(user)) {
133                            user = session.getProperty("mail.smtp.user");
134                    }
135    
136                    String password = session.getProperty(prefix + "password");
137    
138                    if (Validator.isNull(password)) {
139                            password = session.getProperty("mail.smtp.password");
140                    }
141    
142                    store.connect(host, user, password);
143    
144                    return store;
145            }
146    
147            protected void notifyMessageListeners(Message[] messages)
148                    throws MessagingException {
149    
150                    if (_log.isDebugEnabled()) {
151                            _log.debug("Messages " + messages.length);
152                    }
153    
154                    for (Message message : messages) {
155                            if (_log.isDebugEnabled()) {
156                                    _log.debug("Message " + message);
157                            }
158    
159                            String from = getEmailAddress(message.getFrom());
160                            String recipient = getEmailAddress(
161                                    message.getRecipients(RecipientType.TO));
162    
163                            if (_log.isDebugEnabled()) {
164                                    _log.debug("From " + from);
165                                    _log.debug("Recipient " + recipient);
166                            }
167    
168                            for (MessageListener messageListener :
169                                            POPServerUtil.getListeners()) {
170    
171                                    try {
172                                            if (messageListener.accept(from, recipient, message)) {
173                                                    messageListener.deliver(from, recipient, message);
174                                            }
175                                    }
176                                    catch (MessageListenerException mle) {
177                                            _log.error(mle, mle);
178                                    }
179                            }
180                    }
181            }
182    
183            private static Log _log = LogFactoryUtil.getLog(
184                    POPNotificationsMessageListener.class);
185    
186    }