INT-1374, INT-1375 added support for 'shouldMarkMessagesAsRead', fix to ensure message deletion happens after message processing
This commit is contained in:
@@ -27,6 +27,7 @@ import javax.mail.MessagingException;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Store;
|
||||
import javax.mail.URLName;
|
||||
import javax.mail.Flags.Flag;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -42,6 +43,7 @@ import org.springframework.util.Assert;
|
||||
* @author Jonas Partner
|
||||
* @author Mark Fisher
|
||||
* @author Iwein Fuld
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public abstract class AbstractMailReceiver implements MailReceiver, DisposableBean {
|
||||
|
||||
@@ -60,6 +62,8 @@ public abstract class AbstractMailReceiver implements MailReceiver, DisposableBe
|
||||
private volatile Folder folder;
|
||||
|
||||
private volatile boolean shouldDeleteMessages = false;
|
||||
|
||||
private volatile boolean shouldMarkMessagesAsRead = false;
|
||||
|
||||
private volatile Properties javaMailProperties = new Properties();
|
||||
|
||||
@@ -143,8 +147,21 @@ public abstract class AbstractMailReceiver implements MailReceiver, DisposableBe
|
||||
* Specify whether mail messages should be deleted after retrieval.
|
||||
*/
|
||||
public void setShouldDeleteMessages(boolean shouldDeleteMessages) {
|
||||
if (this.shouldMarkMessagesAsRead && shouldDeleteMessages){
|
||||
throw new IllegalArgumentException("setting both 'shouldDeleteMessages' and 'shouldMarkMessagesAsRead' to true is not allowed");
|
||||
}
|
||||
this.shouldDeleteMessages = shouldDeleteMessages;
|
||||
}
|
||||
public boolean isShouldMarkMessagesAsRead() {
|
||||
return shouldMarkMessagesAsRead;
|
||||
}
|
||||
|
||||
public void setShouldMarkMessagesAsRead(boolean shouldMarkMessagesAsRead) {
|
||||
if (this.shouldDeleteMessages && shouldMarkMessagesAsRead){
|
||||
throw new IllegalArgumentException("setting both 'shouldDeleteMessages' and 'shouldMarkMessagesAsRead' to true is not allowed");
|
||||
}
|
||||
this.shouldMarkMessagesAsRead = shouldMarkMessagesAsRead;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the mail messages should be deleted after being received.
|
||||
@@ -204,10 +221,10 @@ public abstract class AbstractMailReceiver implements MailReceiver, DisposableBe
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("opening folder [" + MailTransportUtils.toPasswordProtectedString(this.url) + "]");
|
||||
}
|
||||
if (this.shouldDeleteMessages()) {
|
||||
if (this.shouldDeleteMessages() || this.shouldMarkMessagesAsRead) {
|
||||
this.folder.open(Folder.READ_WRITE);
|
||||
}
|
||||
else {
|
||||
else {
|
||||
this.folder.open(Folder.READ_ONLY);
|
||||
}
|
||||
}
|
||||
@@ -230,13 +247,17 @@ public abstract class AbstractMailReceiver implements MailReceiver, DisposableBe
|
||||
if (messages.length > 0) {
|
||||
this.fetchMessages(messages);
|
||||
}
|
||||
if (this.shouldDeleteMessages()) {
|
||||
this.deleteMessages(messages);
|
||||
}
|
||||
|
||||
Message[] copiedMessages = new Message[messages.length];
|
||||
for (int i = 0; i < messages.length; i++) {
|
||||
if (this.isShouldMarkMessagesAsRead()){
|
||||
messages[i].setFlag(Flag.SEEN, true);
|
||||
}
|
||||
copiedMessages[i] = new MimeMessage((MimeMessage) messages[i]);
|
||||
}
|
||||
if (this.shouldDeleteMessages()) {
|
||||
this.deleteMessages(messages);
|
||||
}
|
||||
return copiedMessages;
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
@@ -25,6 +25,7 @@ import javax.mail.event.MessageCountEvent;
|
||||
import javax.mail.event.MessageCountListener;
|
||||
import javax.mail.search.AndTerm;
|
||||
import javax.mail.search.FlagTerm;
|
||||
import javax.mail.search.NotTerm;
|
||||
import javax.mail.search.SearchTerm;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
@@ -41,6 +42,7 @@ import com.sun.mail.imap.IMAPFolder;
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Mark Fisher
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class ImapMailReceiver extends AbstractMailReceiver {
|
||||
|
||||
@@ -99,6 +101,11 @@ public class ImapMailReceiver extends AbstractMailReceiver {
|
||||
@Override
|
||||
protected Message[] searchForNewMessages() throws MessagingException {
|
||||
Flags supportedFlags = this.getFolder().getPermanentFlags();
|
||||
SearchTerm searchTerm = this.compileSearchTerms(supportedFlags);
|
||||
return searchTerm != null ? this.getFolder().search(searchTerm) : this.getFolder().getMessages();
|
||||
}
|
||||
|
||||
private SearchTerm compileSearchTerms(Flags supportedFlags){
|
||||
SearchTerm searchTerm = null;
|
||||
if (supportedFlags != null) {
|
||||
if (supportedFlags.contains(Flags.Flag.RECENT)) {
|
||||
@@ -123,7 +130,16 @@ public class ImapMailReceiver extends AbstractMailReceiver {
|
||||
}
|
||||
}
|
||||
}
|
||||
return searchTerm != null ? this.getFolder().search(searchTerm) : this.getFolder().getMessages();
|
||||
if (searchTerm == null){
|
||||
if (this.isShouldMarkMessagesAsRead()){
|
||||
searchTerm = new NotTerm( new FlagTerm(new Flags(Flags.Flag.SEEN), true) );
|
||||
}
|
||||
} else {
|
||||
if (this.isShouldMarkMessagesAsRead()){
|
||||
searchTerm = new AndTerm(searchTerm, new NotTerm( new FlagTerm(new Flags(Flags.Flag.SEEN), true) ));
|
||||
}
|
||||
}
|
||||
return searchTerm;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user