📄 javamailgateway.java
字号:
new InsertCacheItem(forum, null, pMessage, 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 {
existsCheck = new ResultFilter();
existsCheck.setModerationRangeMin(Integer.MIN_VALUE+1);
existsCheck.addProperty(gatewayMessageId, messageId);
existsCheck.addProperty(DUMMY_PARENT_HEADER, "true");
// modify existing parent message since we created it
if (forum.getMessageCount(existsCheck) > 0) {
Iterator iter = forum.messages(existsCheck);
ForumMessage msg = (ForumMessage) iter.next();
msg.setSubject(message.getSubject());
msg.setProperty(SUBJECT_EXTENDED_PROPERTY,
StringUtils.hash(message.getSubject()));
msg.setCreationDate(message.getCreationDate());
msg.setModifiedDate(message.getModifiedDate());
msg.setProperty(gatewayMessageId,
message.getProperty(gatewayMessageId));
if (message.getProperty(gatewayParentId) != null) {
msg.setProperty(gatewayParentId,
message.getProperty(gatewayParentId));
}
msg.deleteProperty(DUMMY_PARENT_HEADER);
msg.setProperty("name", message.getProperty("name"));
msg.setProperty("email", message.getProperty("email"));
msg.setBody(message.getBody());
}
else { /* do nothing */ }
}
}
catch (UnauthorizedException uae) {
return;
}
}
// Now insert all the messages
Iterator cacheIterator = insertCache.iterator();
InsertCacheItem cacheItem = null;
while (cacheIterator.hasNext()) {
cacheItem = (InsertCacheItem) cacheIterator.next();
cacheItem.insert();
}
// explicitly nullify to help gc
cacheIterator = null;
insertCache = null;
}
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 key property key to use in the query
* @param value property value to use in the query
* @return true if the message was found in the db, false otherwise
*/
protected boolean isMessageInDb(Forum forum, String key, String value) {
if (key == null || key.equals("") || value == null || value.equals("")) {
return false;
}
ResultFilter existsCheck = new ResultFilter();
existsCheck.setModerationRangeMin(Integer.MIN_VALUE+1);
existsCheck.addProperty(key, value);
long messageCount = forum.getMessageCount(existsCheck);
if (messageCount > 0) {
return true;
}
else {
return false;
}
}
/**
* Returns the subject of a JavaMail message.
*
* @param message the JavaMail message.
* @return String the subject of the message.
* @throws MessagingException if the subject could not be found.
*/
protected String getSubject(Message message) throws MessagingException {
String subject = message.getSubject();
if (subject != null) {
try {
return getRFC2047DecodedString(subject);
}
catch (Exception e) {
return subject;
}
}
else {
String err = "Subject header is unavailable or its value is absent";
throw new MessagingException(err);
}
}
/**
* Utility method to decode strings that may have rfc 2047 encoded words
* in them
*/
protected String getRFC2047DecodedString(String str) {
if (str == null) {
return null;
}
if (str.indexOf("=?") != -1 && str.indexOf("?=") != -1) {
StringBuffer temp = new StringBuffer(str.length());
int i = 0, first = 0, last = 0;
String encodedWord = "";
try {
while (str.indexOf("=?", i) != -1 && i < str.length()) {
first = str.indexOf("=?", i);
if (first != i) {
// White space between adjacent 'encoded-word's
// is not displayed.
for (int x = last; x < first; x++) {
if (str.charAt(x) != ' ' || i == 0) {
temp.append(str.substring(last, first));
break;
}
}
}
int c = 0;
i = first + 2;
while (c < 2 && str.indexOf("?", i) != -1) {
i = str.indexOf("?", i) + 1;
c++;
}
last = str.indexOf("?=", i);
if (last > 0 && last <= str.length()) {
last += 2;
encodedWord = str.substring(first, last);
temp.append(MimeUtility.decodeWord(encodedWord));
i = last;
}
else {
// broken encoding
throw new ParseException();
}
}
if (last < str.length()) {
temp.append(str.substring(last));
}
str = temp.toString();
}
catch (ParseException e) {
System.err.println("pe thrown, encodedWord was " + encodedWord +
", string was " + str);
}
catch (UnsupportedEncodingException e) {
System.err.println("uee thrown, encodedWord was " + encodedWord);
}
}
return str;
}
/**
* Returns the date a message was created or received, or the current date
* if date parsing fails. POP3 is notorious for improperly formatted date
* fields and doesn't provide a separate received date field. In many cases
* JavaMail fails to parse these dates, so we attempt to use our own
* parsers as backup.
*
* @param message a JavaMail message.
* @return Date the sent (or received) date of the message.
* @throws MessagingException if an error occurred trying to retrieve the date.
*/
protected Date getDate(Message message) throws MessagingException {
Date date = null;
message = (MimeMessage) message;
// First, let JavaMail try to parse the date.
try {
date = message.getSentDate();
// If it failed, try the received date
if (date == null) {
date = message.getReceivedDate();
}
}
catch (MessagingException me) { /* ignore */ }
// If JavaMail was able to parse the date, return it.
if (date != null) {
return date;
}
// Rarely, there will be a date that JavaMail fails to parse. In this
// case, we'll attempt to parse the date ourselves using some of the
// more common date formats that JavaMail doesn't recognize.
else {
String[] headers = message.getHeader("Date");
// If no headers exist, there is no chance we can parse ourselves so
// return the current date.
if (headers == null || headers[0] == null) {
return new Date();
}
String header = headers[0].trim();
// Parse date in form: Tues Jan 17 21:10:14 1978
try {
if (format1 == null) {
format1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
format1.setLenient(true);
}
return format1.parse(header);
} catch (java.text.ParseException e1) { }
// Parse date in form: Wednesday, June 27, 2001
try {
if (format2 == null) {
format2 = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
format2.setLenient(true);
}
return format2.parse(header);
} catch (java.text.ParseException e2) { }
// Parse date in form: 11/23/97 11:20 PM
try {
if (format3 == null) {
format3 = new SimpleDateFormat("dd/MM/yy hh:mm a");
format3.setLenient(true);
}
return format3.parse(header);
} catch (java.text.ParseException e3) { }
// Parse date in form: 1999-03-06 17:22:05
try {
if (format4 == null) {
format4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format4.setLenient(true);
}
return format4.parse(header);
} catch (java.text.ParseException e4) { }
// Parse date in form: Fri, 17 May, 1974 09:13:57
try {
if (format5 == null) {
format5 = new SimpleDateFormat("EEE, dd MMM, yyyy HH:mm:ss");
format5.setLenient(true);
}
return format5.parse(header);
} catch (java.text.ParseException e5) { }
// Parse date in form: November 15, 1978 08:13 PM
try {
if (format6 == null) {
format6 = new SimpleDateFormat("MMMM dd, yyyy HH:mm a");
format6.setLenient(true);
}
return format6.parse(header);
} catch (java.text.ParseException e6) { }
// All parsing failed so return current date.
return new Date();
}
}
/**
* Retrieve the messageID using the JavaMail method
*
* @param message the JavaMail message
* @return String the messageID
* @throws MessagingException if an error occurred trying to retrieve the
* messageid
*/
protected String getMessageID(Message message) throws MessagingException {
String messageID = ((MimeMessage) message).getMessageID();
if (messageID != null && !messageID.equals("")) {
return messageID;
}
else {
String err = "Message-ID header is unavailable or its value is absent";
throw new MessagingException(err);
}
}
/**
* Returns the the parent message id's of a message. Currently, two headers
* are given for threading purposes, In-Reply-To: and References:
* In-Reply-To contains the msg id's of all the messages for which
* this message is a reply (And for which is almost universally only one),
* References is used to identify a thread of conversation.
* <p>
* See RFC 822/2822 for a full explanation. Note that this implementation
* doesn't handle CFWS, particularly the Comment part. Shoot me.
* (RFC 2822 Section 3.6.4)
*
* @param message the JavaMail message
* @return String the parent message id (empty if not found)
* @throws MessagingException if something goes wrong
*/
protected String[] getParentMessageID(Message message)
throws MessagingException {
String headers[] = new String[] {"In-Reply-To", "References"};
String[] parents = getParentMessageID(message, headers[0]);
if (parents == null) {
parents = getParentMessageID(message, headers[1]);
}
if (parents == null) {
return new String[] {""};
}
return parents;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -