⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newsgroupgateway.java

📁 jive 2.1.1 的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            address = null;
        }
        gateway.setDefaultFromAddress(address);
    }

    /**
     * Retrieves the Organization header for outbound
     * nntp messages. Default is null.
     *
     * @return the current organization header
     */
    public String getOrganization() {
        return gateway.getOrganization();
    }

    /**
     * Sets the Organization header for outbound
     * nntp messages
     *
     * @param organization the string to set the
     *   organization header to
     */
    public void setOrganization(String organization) {
        if (organization != null) {
            gateway.setOrganization(organization);
        }
    }

    public void setLastMessageNumberSeen(int messageNumber) {
        if (messageNumber > 0) {
            lastMessageNumberSeen = messageNumber;
        }
    }

    public int getLastMessageNumberSeen() {
        return lastMessageNumberSeen;
    }

    /**
     * An extension of the JavaMailGateway class
     */
    private class NNTPGateway extends JavaMailGateway {

        protected Session session = null;
        protected String organization = null;

        public String getOrganization() {
            return organization;
        }

        public void setOrganization(String organization) {
            this.organization = organization;
        }

        public NNTPGateway(ForumFactory factory, Forum forum) {
            super(factory, forum);
            setPort(119); // use the default port of 119
            setProtocol("nntp");

            gatewayMessageId = GATEWAY_MESSAGE_ID;
            gatewayParentId  = GATEWAY_PARENT_ID;
        }

        public void exportData(ForumMessage forumMessage) throws GatewayException {

           // check to make sure that we have been properly setup
            if (host == null || mailbox == null || defaultFromAddress == null) {
                throw new GatewayException("Required properties are not all set.");
            }

            // refuse to re-export a message which has previously been exported
            // or imported or which was automatically created for threading
            // purposes by an import gateway
            if (forumMessage.getProperty(GATEWAY_MESSAGE_ID) != null ||
                    forumMessage.getProperty(DUMMY_PARENT_HEADER) != null)
            {
                return;
            }

            try {
                // Create the session if necessary.
                if (session == null) {
                    session = Session.getDefaultInstance(new Properties(), null);
                    session.setDebug(debugEnabled);
                }

                InternetAddress fromAddress = getFromAddress(forumMessage);
                MimeMessage message = new MimeMessage(session);

                message.setFrom(fromAddress);
                message.setRecipient(MimeMessage.RecipientType.NEWSGROUPS,
                    new NewsAddress(mailbox));
                // The body is the message + the footer.
                StringBuffer body = new StringBuffer(forumMessage.getUnfilteredBody());
                Forum forum = gateway.factory.getForum(gateway.forumID);
                GatewayManager manager = forum.getGatewayManager();
                body.append(manager.getTranslatedFooter(forumMessage));
                message.setContent(body.toString(), "text/plain");
                message.setSubject(forumMessage.getSubject());

                if (organization != null) {
                    message.setHeader("Organization", organization);
                }

                // Attempt to get a parent message id so we can set
                // the references header properly for threading to work
                // good netique :)
                try {
                    ForumThread thread = forumMessage.getForumThread();
                    TreeWalker tree = thread.treeWalker();
                    ForumMessage parent = tree.getParent(forumMessage);
                    String parentId = parent.getProperty(GATEWAY_MESSAGE_ID);
                    if (parentId != null && !parentId.equals("")) {
                        message.setHeader("References", parentId);
                    }
                }
                catch (ForumMessageNotFoundException fmnfe) { /* ignore */ }

                // save all the changes to the message
                message.saveChanges();

                URLName url = new URLName(protocol, host, port, mailbox,
                        username, password);
                NNTPTransport trans = new NNTPTransport(session, url);

                trans.connect(host, port, username, password);
                trans.sendMessage(message, message.getRecipients(
                        MimeMessage.RecipientType.NEWSGROUPS));
                // set the message ID so that we don't inadvertently cause a
                // loop to happen
                forumMessage.setProperty(GATEWAY_MESSAGE_ID, message.getMessageID());
            }
            catch (Exception e) {
                throw new GatewayException(e);
            }
        }

        /**
         * override base class to set a system property and then
         * get the JavaMail store
         *
         * @return Store     a connected store object
         * @param afterDate the date after which we'll import a message
         * @param store a JavaMail 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);
        }

        /**
         * Overrideen 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++) {
                    Gateway gateway = gatewayManager.getGateway(i);
                    if (gateway != null && gateway instanceof NewsgroupGateway) {
                        ((NewsgroupGateway) gateway).setLastMessageNumberSeen(lastMessageNumberSeen);
                    }
                }

                gatewayManager.saveGateways(false);
            }
            catch (ForumNotFoundException e) { /* do nothing */ }
            catch (UnauthorizedException e) { /* do nothing */ }
        }

        /**
         * Determine the "from" address, which will normally be
         * the message author's name and email address. However, we have to
         * use a default address in some circumstances when that info isn't
         * available.
         *
         * @param forumMessage the message for which we are trying to extract
         *                     the from address
         * @return InternetAddress the from address
         */
        private InternetAddress getFromAddress(ForumMessage forumMessage) {

            InternetAddress fromAddress = null;

            try {
                if (!forumMessage.isAnonymous()) {
                    User user = forumMessage.getUser();
                    String name, email = null;
                    // Get the name, or use their username if the name is hidden.
                    if (user.isNameVisible()) {
                        name = user.getName();
                    }
                    else {
                        name = user.getUsername();
                    }
                    // Get their email address, or use the dummy address if their
                    // email is hidden and the admin hasn't overriden that.
                    if (!user.isEmailVisible() && emailPrefEnabled) {
                        email = defaultFromAddress;
                    }
                    else {
                        email = user.getEmail();
                    }
                    fromAddress = new InternetAddress(email, name);
                }
                // Message was posted anonymously. However, a name and email might
                // be defined through extended properties.
                else if (forumMessage.getProperty("name") != null &&
                         forumMessage.getProperty("email") != null)
                {
                    String name  = forumMessage.getProperty("name");
                    String email = forumMessage.getProperty("email");
                    fromAddress = new InternetAddress(email, name);
                }
                // Getting "from" address failed. Use the default address.
                else {
                    fromAddress = new InternetAddress(defaultFromAddress);
                }
            }
            catch (UnsupportedEncodingException e) {
                fromAddress = null;
            }
            catch (AddressException e) {
                fromAddress = null;
            }

            return fromAddress;
        }
    }
}

⌨️ 快捷键说明

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