newsgroupexporter.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 713 行 · 第 1/2 页
JAVA
713 行
* By default, the messageID is stored in Jive so that the * message won't be reexported accidentally.<p> * * Note: This setting has no effect unless accompanied by * setAllowExportAgain(true). * * @param updateMessageID true if Jive should be updated with the messageID * of exported messages, false otherwise. */ public void setUpdateMessageIDOnExport(boolean updateMessageID) { gateway.setUpdateMessageID(updateMessageID); } /** * Return whether this gateway will allow reexporting of messages * or not. Default is false. * * @return true if reexporting a message is allowed, false otherwise. */ public boolean isAllowExportAgain() { return gateway.isExportIgnoreMessageID(); } /** * By default this gateway refuses to re-export a message which has * previously been exported or imported or which was automatically * created for threading purposes by an import gateway.<p> * * However this can be overridden so that messages can be re-exported * again if absolutely needed. Definite care is needed if you set * the property to true.<p> * * Please note however that dummy messages will *never* be exported. * * @param allowExportAgain true if messages should be allowed to be * reexported, false otherwise. */ public void setAllowExportAgain(boolean allowExportAgain) { gateway.setExportIgnoreMessageID(allowExportAgain); } /** * By default attachments are disallowed by this gateway. * * @return true if attachments are allowed, false otherwise. */ public boolean isAttachmentsEnabled() { return gateway.isAttachmentsEnabled(); } /** * Sets whether this gateway allows the export of attachments or not. * If true attachments are allowed, * otherwise attachments will be stripped before exporting a message. * * @param attachmentsEnabled true is attachments are allowed, * false otherwise. */ public void setAttachmentsEnabled(boolean attachmentsEnabled) { gateway.setAttachmentsEnabled(attachmentsEnabled); } /** * An extension of the JavaMailGateway class */ private class NNTPGateway extends JavaMailGateway implements GatewayExporter { protected Session session = null; protected String organization = null; protected String defaultFromAddress = null; protected boolean emailPrefEnabled = true; protected boolean updateMessageID = true; protected boolean exportIgnoreMessageID = false; public String getOrganization() { return organization; } public void setOrganization(String organization) { this.organization = organization; } public String getDefaultFromAddress() { return defaultFromAddress; } public void setDefaultFromAddress(String address) { this.defaultFromAddress = address; } public boolean isEmailPrefEnabled() { return emailPrefEnabled; } public void setEmailPrefEnabled(boolean enabled) { this.emailPrefEnabled = enabled; } public boolean isUpdateMessageID() { return updateMessageID; } public void setUpdateMessageID(boolean updateMessageID) { this.updateMessageID = updateMessageID; } public boolean isExportIgnoreMessageID() { return exportIgnoreMessageID; } public void setExportIgnoreMessageID(boolean exportIgnoreMessageID) { this.exportIgnoreMessageID = exportIgnoreMessageID; } 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; } 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 // // this can be overridden so that messages can be re-exported // however dummy messages will never be exported if ((forumMessage.getProperty(GATEWAY_MESSAGE_ID) != null && !exportIgnoreMessageID) || forumMessage.getProperty(DUMMY_PARENT_HEADER) != null) { // ignore return; } try { // Create the session if necessary. if (session == null) { session = Session.getDefaultInstance(new Properties(), null); session.setDebug(debugEnabled); } MimeMessage message = new MimeMessage(session); NewsAddress to = new NewsAddress(mailbox); String subject = (!"".equals(forumMessage.getSubject())) ? forumMessage.getSubject() : " "; message.setFrom(getFromAddress(forumMessage)); message.setRecipient(MimeMessage.RecipientType.NEWSGROUPS, to); message.setSubject(subject, "8859_1"); // add organization header 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) { // no parent, ignore } // add body addContent(forumMessage, message); // 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)); if (!updateMessageID && exportIgnoreMessageID) { // don't set only if we have been told not to, one time // gateway export task may choose to override } else { // set the message ID so that we don't inadvertently cause a // loop to happen forumMessage.setProperty(GATEWAY_MESSAGE_ID, message.getMessageID()); } trans.close(); session = null; } catch (Exception e) { throw new GatewayException(e); } } public void exportData(ForumMessage[] forumMessage) throws GatewayException { for (int x=0; x < forumMessage.length; x++) { exportData(forumMessage[x]); } } /** * 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 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, "8859_1"); } // 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, "8859_1"); } // Message was posted anonymously. However, a name might // be defined through extended properties. else if (forumMessage.getProperty("name") != null) { String name = forumMessage.getProperty("name"); fromAddress = new InternetAddress(defaultFromAddress, name, "8859_1"); } // Message was posted anonymously. However, an email might // be defined through extended properties. else if (forumMessage.getProperty("email") != null) { String email = forumMessage.getProperty("email"); fromAddress = new InternetAddress(email, "8859_1"); } // 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; } /** * Utility method to add ForumMessage content and attachments * to a MimeMessage. * * @throws GatewayException If anything goes wrong. */ private void addContent(ForumMessage forumMessage, MimeMessage message) throws GatewayException { try { // 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)); // check for attachments, handle differently if we have them if (attachmentsEnabled && forumMessage.getAttachmentCount() > 0) { // Create the message body part, add text content Multipart multipart = new MimeMultipart(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(body.toString(), "8859_1"); messageBodyPart.setDisposition(Part.INLINE); multipart.addBodyPart(messageBodyPart); Iterator iter = forumMessage.attachments(); // add each attachment to the message while (iter.hasNext()) { Attachment attachment = (Attachment) iter.next(); AttachmentDataSource source = new AttachmentDataSource(attachment); messageBodyPart = new MimeBodyPart(); messageBodyPart.setDisposition(Part.ATTACHMENT); messageBodyPart.setFileName(attachment.getName()); messageBodyPart.setDataHandler(new DataHandler(source)); multipart.addBodyPart(messageBodyPart); } // Put parts in message message.setContent(multipart); } else { message.setText(body.toString(), "8859_1"); } } catch (ForumNotFoundException e) { throw new GatewayException("ForumNotFoundException thrown", e); } catch (MessagingException e) { throw new GatewayException("MessagingException thrown", e); } catch (UnauthorizedException e) { throw new GatewayException("UnauthorizedException thrown", e); } catch (IOException e) { throw new GatewayException("IOException thrown", e); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?