use of org.springframework.integration.mail.ImapMailReceiver in project nifi by apache.
the class ConsumeIMAP method buildMessageReceiver.
/**
*/
@Override
protected ImapMailReceiver buildMessageReceiver(ProcessContext processContext) {
ImapMailReceiver receiver = new ImapMailReceiver(this.buildUrl(processContext));
boolean shouldMarkAsRead = processContext.getProperty(SHOULD_MARK_READ).asBoolean();
receiver.setShouldMarkMessagesAsRead(shouldMarkAsRead);
receiver.setShouldDeleteMessages(processContext.getProperty(AbstractEmailProcessor.SHOULD_DELETE_MESSAGES).asBoolean());
return receiver;
}
use of org.springframework.integration.mail.ImapMailReceiver in project spring-integration by spring-projects.
the class MailReceiverFactoryBean method createReceiver.
private MailReceiver createReceiver() {
this.verifyProtocol();
boolean isPop3 = this.protocol.toLowerCase().startsWith("pop3");
boolean isImap = this.protocol.toLowerCase().startsWith("imap");
Assert.isTrue(isPop3 || isImap, "the store URI must begin with 'pop3' or 'imap'");
AbstractMailReceiver receiver = isPop3 ? new Pop3MailReceiver(this.storeUri) : new ImapMailReceiver(this.storeUri);
if (this.session != null) {
Assert.isNull(this.javaMailProperties, "JavaMail Properties are not allowed when a Session has been provided.");
Assert.isNull(this.authenticator, "A JavaMail Authenticator is not allowed when a Session has been provided.");
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);
}
if (this.authenticator != null) {
receiver.setJavaMailAuthenticator(this.authenticator);
}
if (this.shouldDeleteMessages != null) {
// always set the value if configured explicitly
// otherwise, the default is true for POP3 but false for IMAP
receiver.setShouldDeleteMessages(this.shouldDeleteMessages);
}
receiver.setMaxFetchSize(this.maxFetchSize);
receiver.setSelectorExpression(this.selectorExpression);
if (StringUtils.hasText(this.userFlag)) {
receiver.setUserFlag(this.userFlag);
}
if (isPop3) {
if (this.isShouldMarkMessagesAsRead() && this.logger.isWarnEnabled()) {
this.logger.warn("Setting 'should-mark-messages-as-read' to 'true' while using POP3 has no effect");
}
} else if (isImap) {
((ImapMailReceiver) receiver).setShouldMarkMessagesAsRead(this.shouldMarkMessagesAsRead);
}
if (this.beanFactory != null) {
receiver.setBeanFactory(this.beanFactory);
}
if (this.headerMapper != null) {
receiver.setHeaderMapper(this.headerMapper);
}
if (this.embeddedPartsAsBytes != null) {
receiver.setEmbeddedPartsAsBytes(this.embeddedPartsAsBytes);
}
if (this.simpleContent != null) {
receiver.setSimpleContent(this.simpleContent);
}
receiver.afterPropertiesSet();
return receiver;
}
use of org.springframework.integration.mail.ImapMailReceiver in project spring-integration by spring-projects.
the class ImapIdleIntegrationTests method testWithTransactionSynchronization.
@SuppressWarnings("resource")
@Test
public void testWithTransactionSynchronization() throws Exception {
final AtomicBoolean block = new AtomicBoolean(false);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("imap-idle-mock-integration-config.xml", this.getClass());
PostTransactionProcessor processor = context.getBean("syncProcessor", PostTransactionProcessor.class);
ImapIdleChannelAdapter adapter = context.getBean("customAdapter", ImapIdleChannelAdapter.class);
assertNotNull(TestUtils.getPropertyValue(adapter, "applicationEventPublisher"));
ImapMailReceiver receiver = TestUtils.getPropertyValue(adapter, "mailReceiver", ImapMailReceiver.class);
// setup mock scenario
receiver = spy(receiver);
doAnswer(invocation -> {
// to emulate the behavior of IDLE
if (block.get()) {
Thread.sleep(5000);
}
block.set(true);
return null;
}).when(receiver).waitForNewMessages();
Message m1 = mock(Message.class);
doReturn(new Message[] { m1 }).when(receiver).receive();
Folder folder = mock(Folder.class);
when(folder.isOpen()).thenReturn(true);
Field folderField = ReflectionUtils.findField(ImapMailReceiver.class, "folder");
folderField.setAccessible(true);
folderField.set(receiver, folder);
Field mrField = ImapIdleChannelAdapter.class.getDeclaredField("mailReceiver");
mrField.setAccessible(true);
mrField.set(adapter, receiver);
// end mock setup
final CountDownLatch txProcessorLatch = new CountDownLatch(1);
doAnswer(invocation -> {
txProcessorLatch.countDown();
return null;
}).when(processor).process(any(Message.class));
adapter.start();
assertTrue(txProcessorLatch.await(10, TimeUnit.SECONDS));
adapter.stop();
context.close();
}
use of org.springframework.integration.mail.ImapMailReceiver in project lavagna by digitalfondue.
the class MailTicketService method getImapMailReceiver.
private MailReceiver getImapMailReceiver(ProjectMailTicketConfigData config) {
String sanitizedUsername = sanitizeUsername(config.getInboundUser());
String inboxFolder = getInboxFolder(config);
String url = config.getInboundProtocol() + "://" + sanitizedUsername + ":" + config.getInboundPassword() + "@" + config.getInboundServer() + ":" + config.getInboundPort() + "/" + inboxFolder.toLowerCase();
ImapMailReceiver receiver = new ImapMailReceiver(url);
receiver.setShouldMarkMessagesAsRead(true);
receiver.setShouldDeleteMessages(false);
Properties mailProperties = new Properties();
if (config.getInboundProtocol().equals("imaps")) {
mailProperties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
mailProperties.setProperty("mail.pop3.socketFactory.fallback", "false");
}
mailProperties.setProperty("mail.store.protocol", config.getInboundProtocol());
mailProperties.putAll(config.generateInboundProperties());
receiver.setJavaMailProperties(mailProperties);
receiver.afterPropertiesSet();
return receiver;
}
Aggregations