INT-1475, addressed the issue. Made 'should-delete-messages' required by the schema and added tests to validate it. Added more tests for Pop3MailReceiever. Modified documentation to explain the change and reasoning behind the change
This commit is contained in:
@@ -60,7 +60,7 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
|
||||
|
||||
private volatile Folder folder;
|
||||
|
||||
private volatile boolean shouldDeleteMessages = false;
|
||||
private volatile boolean shouldDeleteMessages;
|
||||
|
||||
protected volatile int folderOpenMode = Folder.READ_ONLY;
|
||||
|
||||
@@ -80,13 +80,13 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
|
||||
public AbstractMailReceiver(URLName urlName) {
|
||||
Assert.notNull(urlName, "urlName must not be null");
|
||||
this.url = urlName;
|
||||
this.shouldDeleteMessages = urlName.getProtocol().startsWith("pop3");
|
||||
//this.shouldDeleteMessages = urlName.getProtocol().startsWith("pop3");
|
||||
}
|
||||
|
||||
public AbstractMailReceiver(String url) {
|
||||
if (url != null) {
|
||||
this.url = new URLName(url);
|
||||
this.shouldDeleteMessages = this.url.getProtocol().startsWith("pop3");
|
||||
//this.shouldDeleteMessages = this.url.getProtocol().startsWith("pop3");
|
||||
}
|
||||
else {
|
||||
this.url = null;
|
||||
|
||||
@@ -188,7 +188,7 @@
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="should-delete-messages" type="xsd:string" use="optional" default="false">
|
||||
<xsd:attribute name="should-delete-messages" type="xsd:string" use="required">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Specify whether mail messages should be deleted after retrieval.
|
||||
|
||||
@@ -15,18 +15,12 @@
|
||||
*/
|
||||
package org.springframework.integration.mail;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.mail.Flags;
|
||||
import javax.mail.Flags.Flag;
|
||||
import javax.mail.Folder;
|
||||
import javax.mail.Message;
|
||||
@@ -34,18 +28,9 @@ import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.core.PollableChannel;
|
||||
import org.springframework.integration.history.MessageHistory;
|
||||
import org.springframework.integration.mail.config.ImapIdleChannelAdapterParserTests;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
|
||||
import com.sun.mail.imap.IMAPFolder;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -124,8 +109,8 @@ public class Pop3MailReceiverTests {
|
||||
verify(msg1, times(0)).setFlag(Flag.DELETED, true);
|
||||
verify(msg2, times(0)).setFlag(Flag.DELETED, true);
|
||||
}
|
||||
@Test @Ignore
|
||||
public void receieveAndDontSetDeleteFlagWithUrl() throws Exception{
|
||||
@Test
|
||||
public void receieveAndDontSetDeleteWithUrl() throws Exception{
|
||||
AbstractMailReceiver receiver = new Pop3MailReceiver("pop3://some.host");
|
||||
receiver = spy(receiver);
|
||||
receiver.afterPropertiesSet();
|
||||
@@ -160,7 +145,7 @@ public class Pop3MailReceiverTests {
|
||||
verify(msg2, times(0)).setFlag(Flag.DELETED, true);
|
||||
}
|
||||
@Test
|
||||
public void receieveAndDontSetDeleteFlagWithoutUrl() throws Exception{
|
||||
public void receieveAndDontSetDeleteWithoutUrl() throws Exception{
|
||||
AbstractMailReceiver receiver = new Pop3MailReceiver();
|
||||
receiver = spy(receiver);
|
||||
receiver.afterPropertiesSet();
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.mail.config;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class FailedMailConfigurationTests {
|
||||
/**
|
||||
* validates that if 'should-delete-messages' is not set the context fails
|
||||
*/
|
||||
@Test(expected=XmlBeanDefinitionStoreException.class)
|
||||
public void testImapIdleWithNoDeleteMessageAttribute(){
|
||||
new ClassPathXmlApplicationContext("failed-imap-config.xml", this.getClass());
|
||||
}
|
||||
/**
|
||||
* validates that if 'should-delete-messages' is not set the context fails
|
||||
*/
|
||||
@Test(expected=XmlBeanDefinitionStoreException.class)
|
||||
public void testAdapterWithNoDeleteMessageAttribute(){
|
||||
new ClassPathXmlApplicationContext("failed-adapter-config.xml", this.getClass());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
|
||||
http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail-2.0.xsd"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mail="http://www.springframework.org/schema/integration/mail">
|
||||
|
||||
<int:channel id="recieveChannel"/>
|
||||
|
||||
<int-mail:inbound-channel-adapter store-uri="imaps://[username]:[password]@imap.gmail.com/INBOX"
|
||||
channel="recieveChannel">
|
||||
<int:poller max-messages-per-poll="1">
|
||||
<int:interval-trigger interval="5000"/>
|
||||
</int:poller>
|
||||
</int-mail:inbound-channel-adapter>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
|
||||
http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail-2.0.xsd"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-mail="http://www.springframework.org/schema/integration/mail">
|
||||
|
||||
<int:channel id="recieveChannel"/>
|
||||
|
||||
<int-mail:imap-idle-channel-adapter store-uri="imaps://[username]:[password]@imap.gmail.com/INBOX"
|
||||
channel="recieveChannel"/>
|
||||
|
||||
</beans>
|
||||
@@ -72,9 +72,9 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:mail="http://www.springframework.org/schema/integration/mail"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/integration/mail
|
||||
http://www.springframework.org/schema/integration/mail/spring-integration-mail-1.0.xsd">]]></programlisting>
|
||||
http://www.springframework.org/schema/integration/mail/spring-integration-mail-2.0.xsd">]]></programlisting>
|
||||
</para>
|
||||
<para>
|
||||
To configure an outbound Channel Adapter, provide the channel to receive from, and the MailSender:
|
||||
@@ -122,8 +122,28 @@
|
||||
</util:properties>]]></programlisting>
|
||||
</para>
|
||||
|
||||
<note>When using <emphasis>should-mark-messages-as-read</emphasis> be aware of the protocol you are using ti retrieve messages. For example POP3 does not support this flag
|
||||
which means setting it to 'true' will have no effect.</note>
|
||||
<important>
|
||||
In both configurations <code>channel</code> and <code>should-delete-messages</code> are the <emphasis>REQUIRED</emphasis>
|
||||
attributes. The important thing to understand is why <code>should-delete-messages</code> is required?
|
||||
The issue is with POP3 protocol, which does NOT have any knowlege of messages that were READ. It can only know what's been read
|
||||
within a single session. This means that when your POP3 mail adapter is running emails are successfully consumed as as they become available during each poll
|
||||
and no single email message will be delivered more then once. However, as soon as you restart your adapter and begin a new session
|
||||
all the email messages that might have been retreeved in the previous session will be retrieved again. That is the nature of POP3. Some might argue
|
||||
that why not set <code>should-delete-messages</code> to TRUE by default? Becouse there are two valid amd mutually exclusive use cases
|
||||
which makes it very hard pick the right default. You may want to configure your adapter as the only email receiever in which
|
||||
case you want to be able to restart such adapter without fear that messages that were delivered before will not be redelivered again.
|
||||
In this case setting <code>should-delete-messages</code> to TRUE would make most sence. However, you may have anoher use case where
|
||||
you may want to have multiple adapters that simply monitor email servers and their content. In other words you just want to 'peek but not touch'.
|
||||
Then setting <code>should-delete-messages</code> to FALSE would be much more appropriate. So since it is hard to choose what should be
|
||||
the right default value for <code>should-delete-messages</code> attribute we simply made it required to be set - leaving it up to you
|
||||
while also not letting you to forget that you must set it.
|
||||
</important>
|
||||
|
||||
<note>When configuring a polling adapter (e.g., inbound-channel-adapter) <emphasis>should-mark-messages-as-read</emphasis>
|
||||
be aware of the protocol you are configuring to retrieve messages. For example POP3 does not support this flag
|
||||
which means setting it to either value will have no effect as messages will NOT be marked as read</note>
|
||||
|
||||
|
||||
<para>
|
||||
When using the namespace support, a <emphasis>header-enricher</emphasis> Message Transformer is also available.
|
||||
This simplifies the application of the headers mentioned above to any Message prior to sending to the
|
||||
|
||||
Reference in New Issue
Block a user