INT-3744: Fix IMAPMessage receivedDate

JIRA: https://jira.spring.io/browse/INT-3744

MimeMessages are copied to eagerly fetch IMAP messages, this process loses
the `receivedDate` property.

Keep a reference to the source Message and delegate to its `receiveDate()`.

Cover more "void" getters
This commit is contained in:
Gary Russell
2015-06-16 09:29:09 -04:00
committed by Artem Bilan
parent 6ccb00b330
commit 9f5d717bc9
2 changed files with 48 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.integration.mail;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
@@ -120,6 +121,8 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
* Set the {@link Session}. Otherwise, the Session will be created by invocation of
* {@link Session#getInstance(Properties)} or {@link Session#getInstance(Properties, Authenticator)}.
*
* @param session The session.
*
* @see #setJavaMailProperties(Properties)
* @see #setJavaMailAuthenticator(Authenticator)
*/
@@ -132,6 +135,8 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
* A new {@link Session} will be created with these properties (and the JavaMailAuthenticator if provided).
* Use either this method or {@link #setSession}, but not both.
*
* @param javaMailProperties The javamail properties.
*
* @see #setJavaMailAuthenticator(Authenticator)
* @see #setSession(Session)
*/
@@ -143,6 +148,8 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
* Optional, sets the Authenticator to be used to obtain a session. This will not be used if
* {@link AbstractMailReceiver#setSession} has been used to configure the {@link Session} directly.
*
* @param javaMailAuthenticator The javamail authenticator.
*
* @see #setSession(Session)
*/
public void setJavaMailAuthenticator(Authenticator javaMailAuthenticator) {
@@ -151,6 +158,8 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
/**
* Specify the maximum number of Messages to fetch per call to {@link #receive()}.
*
* @param maxFetchSize The max fetch size.
*/
public void setMaxFetchSize(int maxFetchSize) {
this.maxFetchSize = maxFetchSize;
@@ -158,12 +167,16 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
/**
* Specify whether mail messages should be deleted after retrieval.
*
* @param shouldDeleteMessages true to delete messages.
*/
public void setShouldDeleteMessages(boolean shouldDeleteMessages) {
this.shouldDeleteMessages = shouldDeleteMessages;
}
/**
* Indicates whether the mail messages should be deleted after being received.
*
* @return true when messages will be deleted.
*/
protected boolean shouldDeleteMessages() {
return this.shouldDeleteMessages;
@@ -175,6 +188,9 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
/**
* Subclasses must implement this method to return new mail messages.
*
* @return An array of messages.
* @throws MessagingException Any MessagingException.
*/
protected abstract Message[] searchForNewMessages() throws MessagingException;
@@ -234,6 +250,7 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
return this.store.getFolder(this.url);
}
@Override
public Message[] receive() throws javax.mail.MessagingException {
synchronized (this.folderMonitor) {
try {
@@ -370,12 +387,13 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
* Optional method allowing you to set additional flags.
* Currently only implemented in IMapMailReceiver.
*
* @param message
* @throws MessagingException
* @param message The message.
* @throws MessagingException A MessagingException.
*/
protected void setAdditionalFlags(Message message) throws MessagingException {
}
@Override
public void destroy() throws Exception {
synchronized (this.folderMonitor) {
MailTransportUtils.closeFolder(this.folder, this.shouldDeleteMessages);
@@ -413,8 +431,11 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
*/
private class IntegrationMimeMessage extends MimeMessage {
private final MimeMessage source;
public IntegrationMimeMessage(MimeMessage source) throws MessagingException {
super(source);
this.source = source;
}
@Override
@@ -427,5 +448,22 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
}
}
@Override
public Date getReceivedDate() throws MessagingException {
/*
* Basic MimeMessage always returns null; delegate to the original.
*/
return this.source.getReceivedDate();
}
@Override
public int getLineCount() throws MessagingException {
/*
* Basic MimeMessage always returns '-1'; delegate to the original.
*/
return this.source.getLineCount();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -157,7 +157,12 @@ public class ImapMailReceiverTests {
adapter.setOutputChannel(channel);
adapter.setTaskScheduler(taskScheduler);
adapter.start();
assertNotNull(channel.receive(6000));
@SuppressWarnings("unchecked")
org.springframework.messaging.Message<MimeMessage> received =
(org.springframework.messaging.Message<MimeMessage>) channel.receive(6000);
assertNotNull(received);
assertNotNull(received.getPayload().getReceivedDate());
assertTrue(received.getPayload().getLineCount() > -1);
assertNotNull(channel.receive(6000)); // new message after idle
assertNull(channel.receive(10000)); // no new message after second and third idle
verify(logger).debug("Canceling IDLE");