pop3importer.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 403 行

JAVA
403
字号
/** * $RCSfile: Pop3Importer.java,v $ * $Revision: 1.4 $ * $Date: 2002/07/16 15:35:18 $ * * Copyright (C) 1999-2002 CoolServlets, Inc. All Rights Reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.gateway;import com.jivesoftware.forum.Forum;import com.jivesoftware.forum.ForumFactory;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.Store;import javax.mail.URLName;import java.security.Security;import java.util.*;/** * A gateway to import to a forum with the contents of an email account or * a mailing list. For example, say you want to import the mailing * list <tt>mail-list@example.com</tt>. To accomplish this you'd create and * use a POP3 account that was subscribed to the list, for example * <tt>mail-list-robot@example.com</tt>.<p> * * The following properties must be set with valid values before importing can * work: <ul> *      <li> <tt>Host</tt> *      <li> <tt>Username</tt> *      <li> <tt>Password</tt></ul> * * All other properties start with reasonable default values, but can be * changed as necessary. * * @author Bruce Ritchie */public class Pop3Importer implements GatewayImporter {    private POP3Gateway gateway;    /**     * Used to flag messages in the forum with a message id     * Specific to this gateway     */    public static final String GATEWAY_MESSAGE_ID = "Email-Message-ID";    /**     * Used to flag messages in the forum with a parent id     * Specific to this gateway     */    public static final String GATEWAY_PARENT_ID = "Email-Parent-ID";    private static Map forumLock = Collections.synchronizedMap(new HashMap());    /**     * Create a new Pop3Importer instance.     */    public Pop3Importer(ForumFactory factory, Forum forum) {        // Configure POP3 Gateway        gateway = new POP3Gateway(factory, forum);        synchronized(forumLock) {            if (forumLock.get(new Long(gateway.forumID)) == null) {                forumLock.put(new Long(gateway.forumID), new Long(gateway.forumID));            }        }    }    //FROM THE GATEWAYIMPORTER INTERFACE//    public void importData(Date afterDate) throws GatewayException    {        synchronized (forumLock.get(new Long(gateway.forumID))) {            gateway.importData(afterDate);        }    }    public void stop()            throws GatewayException    {        gateway.stop();    }    //GATEWAY PROPERTIES//    /**     * Returns the POP3 host (eg mail.example.com). The host is null by     * default, but must be set before gateway imports can execute.     *     * @return the POP3 host.     */    public String getHost() {        return gateway.getHost();    }    /**     * Sets the POP3 host (eg mail.example.com). The host is null by     * default, but must be set before gateway imports can execute.     *     * @param host the POP3 host.     */    public void setHost(String host) {        gateway.setHost(host);    }    /**     * Returns the port number that will be used when connecting to the POP3     * server. The default is 110, the standard POP3 port number.     *     * @return the POP3 port number.     */    public int getPort() {        return gateway.getPort();    }    /**     * Sets the port number that will be used when connecting to the POP3     * server. The default is 110, the standard POP3 port number.     *     * @param port the POP3 port number.     */    public void setPort(int port) {        gateway.setPort(port);    }    /**     * Returns the username that will be used when connection to the POP3 server.     * By default the value is null, which means that no username will be used.     *     * @return the POP3 username.     */    public String getUsername() {        return gateway.getUsername();    }    /**     * Sets the username that will be used when connecting to the POP3 server.     * By default the value is null, which means that no username will be used.     *     * @param username the POP3 username.     */    public void setUsername(String username) {        gateway.setUsername(username);    }    /**     * Returns the password that will be used when connection to the POP3 server.     * By default the value is null, which means that no password will be used.     *     * @return the POP3 password.     */    public String getPassword() {        return gateway.getPassword();    }    /**     * Sets the password that will be used when connecting to the POP3 server.     * By default the value is null, which means that no password will be used.     *     * @param password the POP3 password.     */    public void setPassword(String password) {        gateway.setPassword(password);    }    /**     * Returns true if the POP3 delete flag is set. When true, messages will be     * deleted from the server after being downloaded. The default value is     * false.     *     * @return true if messages will be deleted from the POP3 server after     *      being downloaded.     */    public boolean isDeleteEnabled() {        return gateway.isDeleteEnabled();    }    /**     * Toggles the POP3 delete flag. When true, messages will be     * deleted from the server after being downloaded. The default value is     * false.     *     * @param POP3DeleteEnabled true if messages should be deleted from the     *      POP3 server after being downloaded.     */    public void setDeleteEnabled(boolean POP3DeleteEnabled) {        gateway.setDeleteEnabled(POP3DeleteEnabled);    }    /**     * Returns true if debugging is turned on for the email transport layers.     * Debug information is written to <tt>System.out</tt> by the underlying     * JavaMail provider.     *     * @return true if debugging is turned on.     */    public boolean isDebugEnabled() {        return gateway.isDebugEnabled();    }    /**     * Toggles SMTP transport layer debugging on or off. Debug information is     * written to <tt>System.out</tt> by the underlying JavaMail provider.     *     * @param debugEnabled true if SMTP debugging should be enabled.     */    public void setDebugEnabled(boolean debugEnabled) {        gateway.setDebugEnabled(debugEnabled);    }    /**     * Returns true if attachments are allowed for the email transport layers.     *     * @return true if attachments are allowed.     */    public boolean isAttachmentsEnabled() {        return gateway.isAttachmentsEnabled();    }    /**     * Toggles attachments on or off.     *     * @param attachmentEnabled true if attachments should be enabled.     */    public void setAttachmentsEnabled(boolean attachmentEnabled) {        gateway.setAttachmentsEnabled(attachmentEnabled);    }    /**     * Returns the body that will be used when creating temporary parent     * messages. It's possible with email accounts and mailing     * lists to get a response to a message before getting the original message.     * If this happens and only the child message is available at the time of     * the gateway import, Jive must still have a way to establish a correct     * parent/child relationship. Therefore, a temporary fake parent message is     * created using an extrapolated subject from the child, and a body with the     * value <tt>temporaryParentBody</tt>. By default, this value will be     * the empty String. However, you may wish to create a short message that     * explains the nature of the fake parent message to the user.<p>     *     * On subsequent imports when a real parent message is found, the fake     * data will be replaced with the correct subject and body.<p>     *     * @return the message body that will be used for temporary fake parent     *      messages.     */    public String getTemporaryParentBody() {        return gateway.getTemporaryParentBody();    }    /**     * Sets the body that will be used when creating temporary parent     * messages. It's possible with email accounts and mailing     * lists to get a response to a message before getting the original message.     * If this happens and only the child message is available at the time of     * the gateway import, Jive must still have a way to establish a correct     * parent/child relationship. Therefore, a temporary fake parent message is     * created using an extrapolated subject from the child, and a body with the     * value <tt>temporaryParentBody</tt>. By default, this value will be     * the empty String. However, you may wish to create a short message that     * explains the nature of the fake parent message to the user.<p>     *     * On subsequent imports when a real parent message is found, the fake     * data will be replaced with the correct subject and body.<p>     *     * @param temporaryParentBody the message body that will be used for     *      temporary fake parent messages.     */    public void setTemporaryParentBody(String temporaryParentBody) {        gateway.setTemporaryParentBody(temporaryParentBody);    }    /**     * Returns true if this gateway is configured to connect to the POP3 server     * using an SSL encrypted connection. Currently, if an encrypted connection     * cannot be made an unencrypted connection will be attempted.     *     * @return true if an SSL connection to the POP3 server is enabled.     */    public boolean isSSLEnabled() {        return gateway.isSSLEnabled();    }    /**     * Toggles SSL connections to the POP3 server on or off.     *     * @param enabled true if SSL connections to the POP3 server should be enabled.     */    public void setSSLEnabled(boolean enabled) {        gateway.setSSLEnabled(enabled);    }    /**     * Returns whether parentage checks will be done using subject line matching or not.     *     * @return whether parentage checks will be done using subject line matching or not.     */    public boolean isSubjectParentageCheckEnabled() {        return gateway.subjectParentageCheckEnabled;    }    /**     * Sets whether parentage checks will be done using subject line matching or not.     *     * @param subjectParentageCheckEnabled true if parentage checks will be done using     * subject line matching, false otherwise.     */    public void setSubjectParentageCheckEnabled(boolean subjectParentageCheckEnabled) {        gateway.subjectParentageCheckEnabled = subjectParentageCheckEnabled;    }    //OTHER//    /**     * An extension of the JavaMailGateway class that performs imports     * through POP3.     */    private class POP3Gateway extends JavaMailGateway {        boolean SSLEnabled = false;        Session session = null;        private static final String SSL_FACTORY =                "com.jivesoftware.forum.gateway.ssl.DummySSLSocketFactory";        public POP3Gateway(ForumFactory factory, Forum forum) {            super(factory, forum);            this.setPort(110);            this.setProtocol("pop3");            this.setMailbox("INBOX"); // only choice for Sun's POP3 provider.            gatewayMessageId = GATEWAY_MESSAGE_ID;            gatewayParentId  = GATEWAY_PARENT_ID;        }        /**         * get the JavaMail store         *         * @param afterDate the date after which messages will be imported         * @return a connected Store object         * @throws MessagingException if error occurred establishing the connection         */        protected Store getStore(Date afterDate)                throws MessagingException        {            getSession();            // Turn on debugging if it's enabled.            session.setDebug(debugEnabled);            // Create a Store            URLName url = new URLName(protocol, host, port, mailbox, username, password);            Store store = session.getStore(url);            // Connect the store            store.connect();            return store;        }        public boolean isSSLEnabled() {            return SSLEnabled;        }        public void setSSLEnabled(boolean enabled) {            this.SSLEnabled = enabled;            // JavaMail Session no longer valid so set to null; it will get            // recreated as needed.            session = null;            // enabled SSL            if (enabled) {                Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());                Security.setProperty("ssl.SocketFactory.provider", SSL_FACTORY);            }        }        /**         *  Retrieve a javamail session.         */        private void getSession() {            if (session == null && SSLEnabled) {                Properties mailProps = new Properties();                // Methology from an article on www.javaworld.com (Java Tip 115)                mailProps.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);                mailProps.setProperty("mail.pop3.socketFactory.fallback", "false");                session = Session.getInstance(mailProps, null);            }            else if (session == null) {                session = Session.getInstance(System.getProperties(), null);            }        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?