javamailgateway.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,639 行 · 第 1/5 页
JAVA
1,639 行
addAttachments(forumMessage, message); } } else { System.err.println("Message which should be in the db wasn't: " + getMessageID(message)); } } } } catch (MessagingException e) { System.err.println("Messaging exception thrown, stack trace follows"); e.printStackTrace(); } } processMessagesAndImport(messages); } catch (ForumNotFoundException e) { throw new GatewayException(e); } catch (UnauthorizedException e) { throw new GatewayException(e); } finally { // TODO: Can we do anything to make sure these messages are there for // next time? (don't delete on email server?) if (!messages.isEmpty()) { System.err.println("Unable to import " + messages.size() + " messages because of errors."); } } } /** * Resolves parentage, corrects dates, sorts, imports, then clears messages. * * @param messages a list of ForumMessage objects to import * @throws ForumNotFoundException * @throws UnauthorizedException */ protected void processMessagesAndImport(List messages) throws ForumNotFoundException, UnauthorizedException { if (messages.isEmpty()) { return; } resolveParentage(messages); correctMessageDates(messages); // Sort messages (oldest first by creation date). This // is needed so that we don't insert a child before a // parent's thread is created, which would be // 'A Bad Thing' (tm) Collections.sort(messages, new Comparator() { public int compare(Object object1, Object object2) { ForumMessage msg1 = (ForumMessage)object1; ForumMessage msg2 = (ForumMessage)object2; return msg1.getCreationDate().compareTo(msg2.getCreationDate()); } }); importMessages(messages); messages.clear(); parentMessageIDs.clear(); } /** * Resolve parentage of messages.<p> * * We need to go through all the messages and determine * parentage from subject if the parent id property isn't already * set, which is almost certain in the case of POP3 and sometimes * the case with other providers. * * @param messages a List of ForumMessage objects. * @throws ForumNotFoundException * @throws UnauthorizedException */ protected void resolveParentage(List messages) throws ForumNotFoundException, UnauthorizedException { Iterator iter = messages.listIterator(); while (iter.hasNext()) { ForumMessage message = (ForumMessage) iter.next(); String messageID = message.getProperty(gatewayMessageId); Forum forum = factory.getForum(forumID); boolean found = false; ForumMessage dbMessage = lookupMessageByID(forum, messageID, true); // jive created message, don't allow parentage checks if (dbMessage != null) { continue; } // has a parent, ignore if (message.getProperty(gatewayParentId) != null && message.getProperty(gatewayParentId).length() > 0) { continue; } // if we don't want to match on subjects at all, ignore if (!subjectParentageCheckEnabled) { continue; } // find the subject of the parent that we want to look for // and check the database for a parent message anytime in the // last 60 days String messageSubject = message.getSubject(); int replyIndex = messageSubject.toLowerCase().indexOf(replyPrefix); String parentSubject = (replyIndex > -1) ? messageSubject.substring(3).trim() : messageSubject.trim(); Date min = new Date(message.getCreationDate().getTime() - 60*JiveGlobals.DAY); Date max = new Date(message.getCreationDate().getTime() - 1); ResultFilter parentCheck = new ResultFilter(); parentCheck.setModerationRangeMin(Integer.MIN_VALUE+1); parentCheck.setCreationDateRangeMin(min); parentCheck.setCreationDateRangeMax(max); parentCheck.addProperty(SUBJECT_EXTENDED_PROPERTY, StringUtils.hash(parentSubject)); shortTermQueryCacheHack(parentCheck); if (forum.getMessageCount(parentCheck) > 0) { // we found a parent, set the parent messageID Iterator parentIterator = forum.messages(parentCheck); ForumMessage pMessage = (ForumMessage) parentIterator.next(); String pMessageID = pMessage.getProperty(gatewayMessageId); // verify that this isn't the same message being imported yet again // if it isn't, update the parent header if (!pMessageID.equals(messageID)) { // if we are playing with the resultFilter to work around the short // term cache, results that come back may be in the future in // reference to the child message. Therefore verify that the parent // is older than the child if (pMessage.getCreationDate().compareTo(message.getCreationDate()) < 0) { found = true; message.setProperty(gatewayParentId, pMessageID); } } } // if we didn't find in the db, check the messages that // we are importing if (!found) { Iterator parentIterator = messages.listIterator(); while (parentIterator.hasNext() && !found) { ForumMessage pMessage = (ForumMessage) parentIterator.next(); // subject fields don't match, next parent if (!pMessage.getSubject().trim().equals(parentSubject)) { continue; } // we found a parent, set the parent messageID String pMessageID = pMessage.getProperty(gatewayMessageId); String pParentID = pMessage.getProperty(gatewayParentId); // check to make sure we're not talking about the same // message here if (pMessageID.equals(messageID)) { continue; } // also check to verify that the parent message doesn't // have this message as it's parent if (pParentID != null && pParentID.equals(messageID)) { continue; } // don't allow parent to be younger than child - could // cause circular parentage problems if (pMessage.getCreationDate().compareTo(message.getCreationDate()) >= 0) { continue; } found = true; message.setProperty(gatewayParentId, pMessageID); } } } } /** * Corrects the dates on the provided list of messages.<p> * * We need to check dates on all the messages to verify * that we don't have a child the same age or older than a parent, * which Jive was explicitly designed not to handle. Our solution * to this problem is to change the child messages' creation date * to 1 millisecond later the parent's and store the original date * in an extended property. In the case where we are inserting a * message that was already created as a dummy parent message, we * check to verify that the message is also older than any children * it has. If it isn't we correct the current messages' date to be * 1 millisecond older than it's oldest child. * * @param messages a List of ForumMessage objects. * @throws ForumNotFoundException * @throws UnauthorizedException */ protected void correctMessageDates(List messages) throws ForumNotFoundException, UnauthorizedException { boolean parentDateCheckComplete = false; // repeatedly go through all the messages checking for // parents younger than children. while (!parentDateCheckComplete) { Iterator iter = messages.listIterator(); Forum forum = factory.getForum(forumID); parentDateCheckComplete = true; while (iter.hasNext()) { ForumMessage message = (ForumMessage) iter.next(); String messageID = message.getProperty(gatewayMessageId); String pMessageID = message.getProperty(gatewayParentId); Iterator parentIterator = messages.listIterator(); // no parent, check to see if this is a jive created // parent message. If so, we need to check children // messages to make sure that the parent message is // older than any children messages. if (pMessageID == null || pMessageID.length() < 1) { // is this message in the db and is it a // jive created message? ForumMessage cMessage = lookupMessageByID(forum, messageID, true); if (cMessage == null) { continue; } ForumMessage cMessage2 = null; ForumThread thread = cMessage.getForumThread(); TreeWalker tw = thread.treeWalker(); Iterator children = tw.recursiveChildren(cMessage); // first get the oldest child message from the db while (children.hasNext()) { cMessage = (ForumMessage) children.next(); if (cMessage2 == null) { cMessage2 = cMessage; } else if (cMessage.getCreationDate().compareTo(cMessage2.getCreationDate()) < 0) { cMessage2 = cMessage; } } if (cMessage2 != null) { cMessage = cMessage2; cMessage2 = null; } else { System.err.println("Search for child message of dummy parent failed!! "); continue; } // next get oldest child message from current import list while (parentIterator.hasNext()) { cMessage2 = (ForumMessage) parentIterator.next(); String temp = cMessage2.getProperty(gatewayParentId); if (!messageID.equals(temp)) { cMessage2 = null; } else { break; } } // find oldest child if (cMessage2 != null) { Date d1 = cMessage.getCreationDate(); Date d2 = cMessage2.getCreationDate(); if (d2.compareTo(d1) < 0) { cMessage = cMessage2; } } // now compare the dates of the current message and it's // older child Date pDate = message.getCreationDate(); Date cDate = cMessage.getCreationDate(); if (pDate.compareTo(cDate) >= 0) { long cTime = cDate.getTime(); String sTime = String.valueOf(cTime); // store the original date as a property // set the parent's creation date 1 millisecond older // than the child's message.setProperty(MESSAGE_DATE_HEADER, sTime); message.setCreationDate(new Date(cTime-1)); parentDateCheckComplete = false; } continue; } ForumMessage pMessage = null; // get the parent message while (parentIterator.hasNext()) { pMessage = (ForumMessage) parentIterator.next(); String temp = pMessage.getProperty(gatewayMessageId); if (!pMessageID.equals(temp)) { pMessage = null; } else { break; } } // couldn't find the parent in the current import, check db if (pMessage == null) { pMessage = lookupMessageByID(forum, pMessageID, false); } // couldn't find the parent at all, create a dummy parent if (pMessage == null) { // create the dummy message with a creation date
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?