INT-2800 Add search-term-strategy to IMAP Adapter

Previously, the ability to customize the search term
strategy was added to the IMAP Idle adapter; it should
also be available to the polled IMAP adapter.

Add the attribute.

Remove some obsolete code from the parser.
This commit is contained in:
Gary Russell
2012-11-02 08:50:49 -04:00
committed by Mark Fisher
parent 73faefa22c
commit d8d77a9bbc
6 changed files with 95 additions and 17 deletions

View File

@@ -51,6 +51,7 @@ public class MailInboundChannelAdapterParser extends AbstractPollingInboundChann
Object source = parserContext.extractSource(element);
IntegrationNamespaceUtils.setValueIfAttributeDefined(receiverBuilder, element, "store-uri");
IntegrationNamespaceUtils.setValueIfAttributeDefined(receiverBuilder, element, "protocol");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(receiverBuilder, element, "search-term-strategy");
String session = element.getAttribute("session");
if (StringUtils.hasText(session)) {
if (element.hasAttribute("java-mail-properties") || element.hasAttribute("authenticator")) {
@@ -74,9 +75,6 @@ public class MailInboundChannelAdapterParser extends AbstractPollingInboundChann
if (StringUtils.hasText(mmpp)) {
receiverBuilder.addPropertyValue("maxFetchSize", mmpp);
}
if (StringUtils.hasText(pollerElement.getAttribute("synchronized"))) {
receiverBuilder.addPropertyValue("synchronized", true);
}
}
}
receiverBuilder.addPropertyValue("shouldDeleteMessages", element.getAttribute("should-delete-messages"));

View File

@@ -31,6 +31,7 @@ import org.springframework.integration.mail.AbstractMailReceiver;
import org.springframework.integration.mail.ImapMailReceiver;
import org.springframework.integration.mail.MailReceiver;
import org.springframework.integration.mail.Pop3MailReceiver;
import org.springframework.integration.mail.SearchTermStrategy;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -55,8 +56,6 @@ public class MailReceiverFactoryBean implements FactoryBean<MailReceiver>, Dispo
private volatile Authenticator authenticator;
private volatile boolean synchronizedTx;
/**
* Indicates whether retrieved messages should be deleted from the server.
* This value will be <code>null</code> <i>unless</i> explicitly configured.
@@ -69,6 +68,8 @@ public class MailReceiverFactoryBean implements FactoryBean<MailReceiver>, Dispo
private volatile Expression selectorExpression;
private volatile SearchTermStrategy searchTermStrategy;
public void setStoreUri(String storeUri) {
this.storeUri = storeUri;
@@ -110,8 +111,8 @@ public class MailReceiverFactoryBean implements FactoryBean<MailReceiver>, Dispo
this.selectorExpression = selectorExpression;
}
public void setSynchronized(boolean synchronizedTx) {
this.synchronizedTx = synchronizedTx;
public void setSearchTermStrategy(SearchTermStrategy searchTermStrategy) {
this.searchTermStrategy = searchTermStrategy;
}
public MailReceiver getObject() throws Exception {
@@ -157,6 +158,10 @@ public class MailReceiverFactoryBean implements FactoryBean<MailReceiver>, Dispo
Assert.isNull(this.authenticator, "A JavaMail Authenticator is not allowed when a Session has been provied.");
receiver.setSession(this.session);
}
if (this.searchTermStrategy != null) {
Assert.isTrue(isImap, "searchTermStrategy is only allowed with imap");
((ImapMailReceiver) receiver).setSearchTermStrategy(this.searchTermStrategy);
}
if (this.javaMailProperties != null) {
receiver.setJavaMailProperties(this.javaMailProperties);
}
@@ -168,12 +173,6 @@ public class MailReceiverFactoryBean implements FactoryBean<MailReceiver>, Dispo
// otherwise, the default is true for POP3 but false for IMAP
receiver.setShouldDeleteMessages(this.shouldDeleteMessages);
}
if (this.synchronizedTx && this.maxFetchSize != 1) {
if (logger.isWarnEnabled()) {
logger.warn("Max Fetch Size is set to 1 because the poller is synchronized; was " + this.maxFetchSize);
}
this.maxFetchSize = 1;
}
receiver.setMaxFetchSize(this.maxFetchSize);
receiver.setSelectorExpression(selectorExpression);

View File

@@ -94,6 +94,23 @@
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="search-term-strategy" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type
type="org.springframework.integration.mail.SearchTermStrategy" />
</tool:annotation>
</xsd:appinfo>
<xsd:documentation>
Reference to a custom implementation of org.springframework.integration.mail.SearchTermStrategy
to use when retrieving email. Only permitted with 'imap' protocol or an 'imap' uri.
By default, the ImapMailReceiver will search for Messages based on the default SearchTerm
which is "All mails that are RECENT (if supported), that are NOT ANSWERED, that are NOT DELETED, that are NOT SEEN and have not
been processed by this mail receiver (enabled by the use of the custom USER flag or simply NOT FLAGGED if not supported)".
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -137,9 +154,9 @@
<xsd:documentation>
Reference to a custom implementation of org.springframework.integration.mail.SearchTermStrategy
to use when retrieving email.
By default ImapMailReceiver will search for Messages based in the default SearchTerm
which is "All mails that are RECENT (if supported), that are NOT ANSWERED, that are NOT DELETED, that are NOT SEEN and have not
been processed by this mail receiver (enabled by the use of the custom USER flag or simply NOT FLAGGED if not supported)".
By default, the ImapMailReceiver will search for Messages based on the default SearchTerm
which is "All mails that are RECENT (if supported), that are NOT ANSWERED, that are NOT DELETED, that are NOT SEEN and have not
been processed by this mail receiver (enabled by the use of the custom USER flag or simply NOT FLAGGED if not supported)".
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>

View File

@@ -104,6 +104,15 @@
<si:bridge input-channel="autoChannel" output-channel="nullChannel"/>
<!-- INT-2800 -->
<mail:inbound-channel-adapter id="imapWithSearch" channel="testChannel" protocol="imap" should-delete-messages="false" auto-startup="false"
search-term-strategy="searchTermStrategy"/>
<bean id="searchTermStrategy" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.integration.mail.SearchTermStrategy"/>
</bean>
<!-- COMMON CONFIGURATION -->
<si:channel id="testChannel"/>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mail="http://www.springframework.org/schema/integration/mail"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- INT-2800 -->
<mail:inbound-channel-adapter id="pop3WithSearch" channel="testChannel" protocol="pop3" should-delete-messages="false" auto-startup="false"
search-term-strategy="searchTermStrategy"/>
<bean id="searchTermStrategy" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.integration.mail.SearchTermStrategy"/>
</bean>
<!-- COMMON CONFIGURATION -->
<si:channel id="testChannel"/>
<si:poller default="true" max-messages-per-poll="1" fixed-rate="60000"/>
</beans>

View File

@@ -29,6 +29,7 @@ import javax.mail.Authenticator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -40,6 +41,7 @@ import org.springframework.integration.mail.AbstractMailReceiver;
import org.springframework.integration.mail.ImapIdleChannelAdapter;
import org.springframework.integration.mail.ImapMailReceiver;
import org.springframework.integration.mail.Pop3MailReceiver;
import org.springframework.integration.mail.SearchTermStrategy;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -72,7 +74,7 @@ public class InboundChannelAdapterParserTests {
Boolean value = (Boolean) new DirectFieldAccessor(receiver).getPropertyValue("shouldDeleteMessages");
assertTrue(value);
}
@Test
public void imapShouldMarkMessagesAsRead() {
AbstractMailReceiver receiver = this.getReceiver("imapShouldMarkAsReadTrue");
@@ -283,6 +285,30 @@ public class InboundChannelAdapterParserTests {
}
//==================== INT-2800 ====================
@Test
public void imapWithSearchTermStrategy() {
AbstractMailReceiver receiver = this.getReceiver("imapWithSearch");
assertEquals(ImapMailReceiver.class, receiver.getClass());
Object sts = new DirectFieldAccessor(receiver).getPropertyValue("searchTermStrategy");
assertNotNull(sts);
assertSame(context.getBean(SearchTermStrategy.class), sts);
}
@Test
public void pop3WithSearchTermStrategy() {
try {
new ClassPathXmlApplicationContext(
"org/springframework/integration/mail/config/InboundChannelAdapterParserTests-pop3Search-context.xml");
fail("expected a parser error");
}
catch(BeanCreationException e) {
assertTrue(e.getMessage().contains("searchTermStrategy is only allowed with imap"));
}
}
//===================== COMMON =====================
private AbstractMailReceiver getReceiver(String name) {