javamailgateway.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,639 行 · 第 1/5 页
JAVA
1,639 行
// 1 millisecond earlier than the child's ForumMessage dMessage = factory.createMessage(); long time = message.getCreationDate().getTime(); Date parentDate = new Date(time-1); String messageSubject = message.getSubject(); String[] parentIDs = (String[]) parentMessageIDs.get(messageID); // find the subject of the parent that we want to create int replyIndex = messageSubject.toLowerCase().indexOf(replyPrefix); if (replyIndex == 0 && messageSubject.length() > 4) { messageSubject = messageSubject.trim().substring(3).trim(); if (messageSubject.length() > 255) { messageSubject = messageSubject.substring(0, 254); } } // set parent message properties dMessage.setSubject(messageSubject); dMessage.setProperty(SUBJECT_EXTENDED_PROPERTY, StringUtils.hash(messageSubject)); dMessage.setCreationDate(parentDate); dMessage.setModifiedDate(parentDate); dMessage.setProperty(gatewayMessageId, pMessageID); dMessage.setProperty(DUMMY_PARENT_HEADER, "true"); dMessage.setBody(temporaryParentBody); // determine if this parent is itself a child // if so, set it's parent if (parentIDs != null && parentIDs.length > 1) { // verify that some dumb mailer didn't set the // references header incorrectly if (!pMessageID.equals(parentIDs[1])) { dMessage.setProperty(gatewayParentId, parentIDs[1]); dMessage.setSubject(message.getSubject()); } } // add this message into the list just before the child messages.add(messages.indexOf(message), dMessage); parentDateCheckComplete = false; // restart loop since we modified the messages array // otherwise we'll see a ConcurrentModificationException // The CME will be thrown because of the messages.add() // call above and how the ArrayList class works break; } Date messageDate = message.getCreationDate(); Date pMessageDate = pMessage.getCreationDate(); long cTime = messageDate.getTime(); long pTime = pMessageDate.getTime(); String sTime = String.valueOf(cTime); if (pTime >= cTime) { // store the original date as a property // set the child's creation date 1 millisecond later // than the parent's message.setProperty(MESSAGE_DATE_HEADER, sTime); message.setCreationDate(new Date(pTime+1)); message.setModifiedDate(new Date(pTime+1)); parentDateCheckComplete = false; } } } } /** * Parse the JavaMail message object and return a ForumMessage object. If * parsing fails the message won't be marked for deletion if message deletion * is enabled. * * @param message the JavaMail Message object. * @param afterDate the date we use to check to see if we should import this * message or not. * @return a ForumMessage object. */ protected ForumMessage parseMessage(Message message, Date afterDate) { // Create a message (anonymous author) ForumMessage forumMessage = factory.createMessage(); try { // too old if (getDate(message).compareTo(afterDate) < 0) { // Some providers require a delete flag to be set message.setFlag(Flags.Flag.DELETED, deleteEnabled); return null; } // set ForumMessage fields // we need to strip out the null character (\u0000) because // some database backends (such as PostgreSQL) can't handle them String subject = StringUtils.replace(getSubject(message), "\u0000", ""); Date date = getDate(message); // trim subject if it's too big subject = (subject.length() > 255) ? subject.substring(0, 254) : subject; forumMessage.setSubject(subject); forumMessage.setProperty(SUBJECT_EXTENDED_PROPERTY, StringUtils.hash(subject)); forumMessage.setCreationDate(date); forumMessage.setModifiedDate(date); forumMessage.setProperty(gatewayMessageId, getMessageID(message)); forumMessage.setProperty("email", getFromEmail(message)); if (!"".equals(getFromName(message))) { forumMessage.setProperty("name", getFromName(message)); } StringBuffer body = new StringBuffer(4096); getBody(body, message); String bodyString = StringUtils.replace(body.toString(), "\u0000", ""); if (bodyString == null) { bodyString = " "; } forumMessage.setBody(bodyString); if (!getParentMessageID(message)[0].equals("")) { forumMessage.setProperty(gatewayParentId, getParentMessageID(message)[0]); } // store all the parent message id's so we can use for threading // later. Only done if message parses ok, otherwise we'll mess up // threading. parentMessageIDs.put(getMessageID(message), getParentMessageID(message)); // Some providers require a delete flag to be set message.setFlag(Flags.Flag.DELETED, deleteEnabled); } catch (MessagingException me) { me.printStackTrace(); // fail this message return null; } catch (NullPointerException npe) { npe.printStackTrace(); // fail this message return null; } catch (IOException ioe) { ioe.printStackTrace(); // fail this message return null; } catch (UnauthorizedException uae) { uae.printStackTrace(); // fail this message return null; } return forumMessage; } /** * Import the List of messages into the forum specified.<p> * * One of the things we need to do in this method is check to see if this * message or it's parent already exist (or doesn't exist) in this forum.<p> * * If a parent doesn't exist, we create a dummy message in its place. If a * parent does exist, we attach the message to the parent's thread.<p> * * If a message already exists in the forum, we first check to see if it's * one of the dummy messages we previously created. If so, we modify it. * Otherwise, we don't insert the message. <p> * * If a message doesn't already exist in the forum and it has no parent(s), * we create a new thread with this message as the root message<p> * * @param messages a list of messages to import into the forum. * @throws ForumNotFoundException * @throws UnauthorizedException */ protected void importMessages(List messages) throws ForumNotFoundException, UnauthorizedException { Iterator messageIterator = messages.listIterator(); ArrayList insertCache = new ArrayList(); // go through each message in the list while (messageIterator.hasNext()) { ForumMessage message = (ForumMessage) messageIterator.next(); Forum forum = factory.getForum(forumID); // failed message - likely couldn't parse a header if (message == null) { System.err.println("Unabled to import empty message"); continue; } String messageID = message.getProperty(gatewayMessageId); ForumMessage dbMessage = lookupMessageByID(forum, messageID, false); // test to see if this message already exists in this forum if (dbMessage == null) { // Now, see if the message states that it has a parent String parentID = message.getProperty(gatewayParentId); // we have a parent if (parentID != null && !parentID.equals("")) { InsertCacheItem parentItem; // first, check the cache if ((parentItem = isMessageinInsertCache(insertCache, parentID)) != null) { // cache insert InsertCacheItem insertCacheItem = new InsertCacheItem(forum, null, null, message); insertCache.add(insertCacheItem); // update parent so it knows to set the // thread and parent for the child parentItem.addChild(insertCacheItem); } else { // verify that the parent exists in this forum ForumMessage parent = lookupMessageByID(forum, parentID, false); // yes if (parent != null) { ForumThread thread = parent.getForumThread(); // cache insert InsertCacheItem insertCacheItem = new InsertCacheItem(forum, thread, parent, message); insertCache.add(insertCacheItem); } // no else { // shouldn't happen since correctMessageDates() will // create dummy parent messages // of course, that mean that the correctMessageDates() // method should be called prior to calling this method // pretend that there isn't a parent InsertCacheItem insertCacheItem = new InsertCacheItem(forum, null, null, message); insertCache.add(insertCacheItem); } } } // we do not have a parent else { // cache insert InsertCacheItem insertCacheItem = new InsertCacheItem(forum, null, null, message); insertCache.add(insertCacheItem); } } // message does exist, check to make sure it's not a lost parent else { if ("true".equalsIgnoreCase(dbMessage.getProperty(DUMMY_PARENT_HEADER))) { // modify existing parent message since we created it dbMessage.setCreationDate(message.getCreationDate()); dbMessage.setSubject(message.getSubject()); dbMessage.setProperty(SUBJECT_EXTENDED_PROPERTY, StringUtils.hash(message.getSubject())); dbMessage.setProperty(gatewayMessageId, message.getProperty(gatewayMessageId)); if (message.getProperty(gatewayParentId) != null) { dbMessage.setProperty(gatewayParentId, message.getProperty(gatewayParentId)); } dbMessage.deleteProperty(DUMMY_PARENT_HEADER); dbMessage.setProperty("name", message.getProperty("name")); dbMessage.setProperty("email", message.getProperty("email")); dbMessage.setBody(message.getBody()); } else { /* do nothing */ } } } // Now insert all the messages Iterator cacheIterator = insertCache.iterator(); InsertCacheItem cacheItem; while (cacheIterator.hasNext()) { cacheItem = (InsertCacheItem) cacheIterator.next(); cacheItem.insert(); } } protected InsertCacheItem isMessageinInsertCache(List cache, String messageID) { Iterator iter = cache.iterator(); while (iter.hasNext()) { InsertCacheItem cacheItem = (InsertCacheItem) iter.next(); if (cacheItem.containsMessage(messageID)) { return cacheItem; } } return null; } /** * Utility method to determine if a message exists in the forum already. * * @param forum [required] * @param messageID Message-ID mail header value. Checked against null and empty. * @param checkDummy whether to mandate that it is a "dummy" or not. * @return the forumMessage if a match is found, null otherwise. */ protected ForumMessage lookupMessageByID(Forum forum, String messageID, boolean checkDummy) { if (messageID == null || messageID.length() == 0) { return null; } ResultFilter existsCheck = new ResultFilter(); existsCheck.setModerationRangeMin(Integer.MIN_VALUE+1); existsCheck.addProperty(gatewayMessageId, messageID); if (checkDummy) { existsCheck.addProperty(DUMMY_PARENT_HEADER, "true"); } shortTermQueryCacheHack(existsCheck); Iterator iter = forum.messages(existsCheck); if (!iter.hasNext()) { return null; } return (ForumMessage) iter.next(); } /** * Like the name says, it's a hack. Ask Bruce if you need an explanation.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?