newsgroupimporter.java

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

JAVA
421
字号
/** * $RCSfile: NewsgroupImporter.java,v $ * $Revision: 1.5 $ * $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 com.jivesoftware.forum.ForumNotFoundException;import com.jivesoftware.forum.UnauthorizedException;import dog.mail.nntp.NNTPStore;import javax.mail.*;import java.text.SimpleDateFormat;import java.util.*;/** * A gateway for the import of messages from a newsgroup. * * @author Bruce Ritchie */public class NewsgroupImporter implements GatewayImporter {    private NNTPGateway gateway;    private int lastMessageNumberSeen = -1;    /**     * Used to flag messages in the forum with a message id     * specific to this gateway.     */    public static final String GATEWAY_MESSAGE_ID = "NNTP-Message-ID";    /**     * Used to flag messages in the forum with a parent id     * specific to this gateway.     */    public static final String GATEWAY_PARENT_ID = "NNTP-Parent-ID";    /**     * Used for defining for the NNTP gateway from when to do the import.     */    private static final SimpleDateFormat DATEFORMATTER =            new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");    private static Map forumLock = Collections.synchronizedMap(new HashMap());    /**     * Create an instance     */    public NewsgroupImporter(ForumFactory factory, Forum forum) {        gateway = new NNTPGateway(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//    /**     * Retrieves the username set for this gateway. Default is null.     *     * @return username the username to be used in connecting to the NNTP server.     */    public String getUsername() {        return gateway.getUsername();    }    /**     * Sets the username to be used in connecting to the NNTP server.     *     * @param username the username to be used in connecting to the NNTP server.     */    public void setUsername(String username) {        if ("".equals(username)) {            username = null;        }        gateway.setUsername(username);    }    /**     * Retrieves the password to be used in connecting to the NNTP server.     * Default is null.     *     * @return the password to be used in connecting to the NNTP server.     */    public String getPassword() {        return gateway.getPassword();    }    /**     * Sets the password to be used in connecting to the NNTP server.     *     * @param password the password to use be used in connecting to the     *        NNTP server.     */    public void setPassword(String password) {        if ("".equals(password)) {            password = null;        }        gateway.setPassword(password);    }    /**     * Returns the port number that will be used when connecting to the NNTP     * server. The default is 119, the standard NNTP port number.     *     * @return the port number that will be used when connecting to the NNTP     *         server.     */    public int getPort() {        return gateway.getPort();    }    /**     * Set the port to use when connecting to the NNTP server. The default is     * port 119; for ssl the normal port to use is 993.     *     * @param port the port to use when connecting to the NNTP server. The     *        default is port 119.     */    public void setPort(int port) {        gateway.setPort(port);        // Old session no longer valid so set to null.        gateway.session = null;    }    /**     * Returns the NNTP host (eg news.example.com). The host is null by     * default, but must be set before gateway imports can execute.     *     * @return the NNTP host (eg news.example.com).     */    public String getHost() {        return gateway.getHost();    }    /**     * Sets the NNTP host (eg news.example.com). The host is null by     * default, but must be set before gateway imports can execute.     *     * @param host the NNTP host (eg news.example.com) to use.     */    public void setHost(String host) {        gateway.setHost(host);        // Old session no longer valid so set to null.        gateway.session = null;    }    /**     * Retrieves the newsgroup that the gateway is going to use.     *     * @return the newsgroup that the gateway will use.     */    public String getNewsgroup() {        return gateway.getMailbox();    }    /**     * Sets the newsgroup that this gateway is going to use.     *     * @param newsgroup the newsgroup the gateway is going to use.     */    public void setNewsgroup(String newsgroup) {        gateway.setMailbox(newsgroup);        // Old session no longer valid so set to null.        gateway.session = null;    }    /**     * 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 debugging is turned on for the NNTP transport layer.     * Debug information is written to <tt>System.out.err</tt>.     *     * @return true if NNTP debugging is turned on, false otherwise.     */    public boolean isDebugEnabled() {        return gateway.isDebugEnabled();    }    /**     * Toggles NNTP transport layer debugging on or off. Debug information is     * written to <tt>System.out<p>     *     * @param debugEnabled true if NNTP debugging should be enabled, false otherwise.     */    public void setDebugEnabled(boolean debugEnabled) {        gateway.setDebugEnabled(debugEnabled);        // Old session no longer valid so set to null.        gateway.session = null;    }    /**     * Used by the gateway to keep track of the last message seen.     * This allows for fast updates since we can ask the NNTP     * server for only the messages newer than the message associated     * with this message number.<p>     *     * It is *highly* recommended not to set this yourself.     *     * @param messageNumber the last message number seen by this gateway.     */    public void setLastMessageNumberSeen(int messageNumber) {        if (messageNumber > 0) {            lastMessageNumberSeen = messageNumber;        }    }    /**     * Used by the gateway to keep track of the last message seen.     * This allows for fast updates since we can ask the NNTP     * server for only the messages newer than the message associated     * with this message number.<p>     *     * It is *highly* recommended not to set this but rather let the     * gateway handle it itself.     *     * @return the last message number seen by this gateway.     */    public int getLastMessageNumberSeen() {        return lastMessageNumberSeen;    }    /**     * By default attachments are allowed by this gateway.     *     * @return whether this gateway will allow attachments or not.     */    public boolean isAttachmentsEnabled() {        return gateway.isAttachmentsEnabled();    }    /**     * Sets whether this gateway allows the import of attachments or not.     * If true attachments are allowed, otherwise attachments will be     * stripped before importing a message.     *     * @param attachmentsEnabled true is attachments are allowed,     *        false otherwise.     */    public void setAttachmentsEnabled(boolean attachmentsEnabled) {        gateway.setAttachmentsEnabled(attachmentsEnabled);    }    /**     * 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;    }    /**     * An extension of the JavaMailGateway class     */    private class NNTPGateway extends JavaMailGateway {        protected Session session = null;        public NNTPGateway(ForumFactory factory, Forum forum) {            super(factory, forum);            this.setPort(119); // use the default port of 119            setProtocol("nntp");            gatewayMessageId = GATEWAY_MESSAGE_ID;            gatewayParentId  = GATEWAY_PARENT_ID;        }        /**         * Override base class to set a system property and then         * get the JavaMail store.         *         * @param afterDate the date after which we'll import a message.         * @return Store a connected store object.         * @throws MessagingException if error occurred establishing the connection.         */        protected Store getStore(Date afterDate) throws MessagingException {            if (afterDate == null) {                afterDate = new Date(0L);            }            Properties props = new Properties();            props.put("mail.nntp.useNewNews", "false");            props.put("mail.nntp.SimpleDateFormat", DATEFORMATTER.toPattern());            props.put("mail.nntp.afterDate", DATEFORMATTER.format(afterDate));            props.put("mail.nntp.memoryOptimisation", Boolean.TRUE.toString());            props.put("mail.nntp.checkNewTimeout", "10");            props.put("mail.store.protocol", "nntp");            props.put("mail.transport.protocol", "nntp-post");            if (lastMessageNumberSeen > 0) {                props.put("mail.nntp.lastArticleIdSeen", String.valueOf(lastMessageNumberSeen));            }            URLName url = new URLName(protocol, host, port, mailbox, username, password);            session = Session.getInstance(props, null);            session.setDebug(debugEnabled);            Store store = new NNTPStore(session, url);            store.connect();            return store;        }        /**         * Overridden so that we can grab the article number from the provider.         */        protected String getMessageID(Message message) throws MessagingException {            if (message.getMessageNumber() > 0) {                int messageNumber = message.getMessageNumber();                if (messageNumber != lastMessageNumberSeen) {                    lastMessageNumberSeen = messageNumber;                }            }            return super.getMessageID(message);        }        /**         * Overridden so that we save the latest article number for the next         * run.         */        protected void cleanup() {            try {                GatewayManager gatewayManager = factory.getForum(forumID).getGatewayManager();                for (int i=0; i < gatewayManager.getGatewayCount(); i++) {                    GatewayImporter gateway = gatewayManager.getGateway(i).getGatewayImporter();                    if (gateway != null && gateway instanceof NewsgroupImporter) {                        ((NewsgroupImporter) gateway).setLastMessageNumberSeen(lastMessageNumberSeen);                    }                }                gatewayManager.saveGateways(false);            }            catch (ForumNotFoundException e) { /* do nothing */ }            catch (UnauthorizedException e) { /* do nothing */ }        }    }}

⌨️ 快捷键说明

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