Search in sources :

Example 56 with Store

use of javax.mail.Store in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testIdleReconnects.

@Test
public void testIdleReconnects() throws Exception {
    ImapMailReceiver receiver = spy(new ImapMailReceiver("imap:foo"));
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();
    IMAPFolder folder = mock(IMAPFolder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    given(folder.isOpen()).willReturn(false).willReturn(true);
    given(folder.exists()).willReturn(true);
    given(folder.hasNewMessages()).willReturn(true);
    Field storeField = AbstractMailReceiver.class.getDeclaredField("store");
    storeField.setAccessible(true);
    Store store = mock(Store.class);
    given(store.isConnected()).willReturn(false);
    given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
    storeField.set(receiver, store);
    ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(receiver);
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    willDoNothing().given(logger).warn(anyString(), any(Throwable.class));
    willAnswer(i -> {
        i.callRealMethod();
        throw new FolderClosedException(folder, "test");
    }).given(receiver).waitForNewMessages();
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    adapter.setTaskScheduler(taskScheduler);
    adapter.setReconnectDelay(50);
    adapter.afterPropertiesSet();
    final CountDownLatch latch = new CountDownLatch(3);
    adapter.setApplicationEventPublisher(e -> {
        latch.countDown();
    });
    adapter.start();
    assertTrue(latch.await(60, TimeUnit.SECONDS));
    verify(store, atLeast(3)).connect();
    taskScheduler.shutdown();
}
Also used : Log(org.apache.commons.logging.Log) IMAPFolder(com.sun.mail.imap.IMAPFolder) URLName(javax.mail.URLName) FolderClosedException(javax.mail.FolderClosedException) Store(javax.mail.Store) Flags(javax.mail.Flags) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Field(java.lang.reflect.Field) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 57 with Store

use of javax.mail.Store in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testImapLifecycleForRaceCondition.

// see INT-1801
@Test
public void testImapLifecycleForRaceCondition() throws Exception {
    for (int i = 0; i < 1000; i++) {
        final ImapMailReceiver receiver = new ImapMailReceiver("imap://foo");
        Store store = mock(Store.class);
        Folder folder = mock(Folder.class);
        given(folder.exists()).willReturn(true);
        given(folder.isOpen()).willReturn(true);
        given(folder.search((SearchTerm) Mockito.any())).willReturn(new Message[] {});
        given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
        given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
        DirectFieldAccessor df = new DirectFieldAccessor(receiver);
        df.setPropertyValue("store", store);
        receiver.setBeanFactory(mock(BeanFactory.class));
        receiver.afterPropertiesSet();
        new Thread(() -> {
            try {
                receiver.receive();
            } catch (javax.mail.MessagingException e) {
                if (e.getCause() instanceof NullPointerException) {
                    failed.getAndIncrement();
                }
            }
        }).start();
        new Thread(() -> {
            try {
                receiver.destroy();
            } catch (Exception ignore) {
            // ignore
            }
        }).start();
    }
    assertEquals(0, failed.get());
}
Also used : MessagingException(javax.mail.MessagingException) URLName(javax.mail.URLName) Store(javax.mail.Store) Flags(javax.mail.Flags) Folder(javax.mail.Folder) IMAPFolder(com.sun.mail.imap.IMAPFolder) MessagingException(javax.mail.MessagingException) FolderClosedException(javax.mail.FolderClosedException) AddressException(javax.mail.internet.AddressException) IOException(java.io.IOException) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 58 with Store

use of javax.mail.Store in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testNoInitialIdleDelayWhenRecentNotSupported.

@SuppressWarnings("resource")
@Test
public void testNoInitialIdleDelayWhenRecentNotSupported() throws Exception {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("ImapIdleChannelAdapterParserTests-context.xml", ImapIdleChannelAdapterParserTests.class);
    ImapIdleChannelAdapter adapter = context.getBean("simpleAdapter", ImapIdleChannelAdapter.class);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    ImapMailReceiver receiver = new ImapMailReceiver("imap:foo");
    receiver = spy(receiver);
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();
    final IMAPFolder folder = mock(IMAPFolder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    given(folder.isOpen()).willReturn(false).willReturn(true);
    given(folder.exists()).willReturn(true);
    DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
    adapterAccessor.setPropertyValue("mailReceiver", receiver);
    Field storeField = AbstractMailReceiver.class.getDeclaredField("store");
    storeField.setAccessible(true);
    Store store = mock(Store.class);
    given(store.isConnected()).willReturn(true);
    given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
    storeField.set(receiver, store);
    willAnswer(invocation -> folder).given(receiver).getFolder();
    MimeMessage mailMessage = mock(MimeMessage.class);
    Flags flags = mock(Flags.class);
    given(mailMessage.getFlags()).willReturn(flags);
    final Message[] messages = new Message[] { mailMessage };
    final AtomicInteger shouldFindMessagesCounter = new AtomicInteger(2);
    willAnswer(invocation -> {
        /*
			 * Return the message from first invocation of waitForMessages()
			 * and in receive(); then return false in the next call to
			 * waitForMessages() so we enter idle(); counter will be reset
			 * to 1 in the mocked idle().
			 */
        if (shouldFindMessagesCounter.decrementAndGet() >= 0) {
            return messages;
        } else {
            return new Message[0];
        }
    }).given(receiver).searchForNewMessages();
    willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
    willAnswer(invocation -> {
        Thread.sleep(5000);
        shouldFindMessagesCounter.set(1);
        return null;
    }).given(folder).idle();
    adapter.start();
    /*
		 * Idle takes 5 seconds; if all is well, we should receive the first message
		 * before then.
		 */
    assertNotNull(channel.receive(3000));
    // We should not receive any more until the next idle elapses
    assertNull(channel.receive(3000));
    assertNotNull(channel.receive(6000));
    adapter.stop();
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) IMAPFolder(com.sun.mail.imap.IMAPFolder) URLName(javax.mail.URLName) Store(javax.mail.Store) Flags(javax.mail.Flags) Field(java.lang.reflect.Field) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MimeMessage(javax.mail.internet.MimeMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 59 with Store

use of javax.mail.Store in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testInitialIdleDelayWhenRecentIsSupported.

@SuppressWarnings("resource")
@Test
public void testInitialIdleDelayWhenRecentIsSupported() throws Exception {
    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("ImapIdleChannelAdapterParserTests-context.xml", ImapIdleChannelAdapterParserTests.class);
    ImapIdleChannelAdapter adapter = context.getBean("simpleAdapter", ImapIdleChannelAdapter.class);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    ImapMailReceiver receiver = new ImapMailReceiver("imap:foo");
    receiver = spy(receiver);
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();
    final IMAPFolder folder = mock(IMAPFolder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.RECENT));
    given(folder.isOpen()).willReturn(false).willReturn(true);
    given(folder.exists()).willReturn(true);
    DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
    adapterAccessor.setPropertyValue("mailReceiver", receiver);
    Field storeField = AbstractMailReceiver.class.getDeclaredField("store");
    storeField.setAccessible(true);
    Store store = mock(Store.class);
    given(store.isConnected()).willReturn(true);
    given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
    storeField.set(receiver, store);
    willAnswer(invocation -> folder).given(receiver).getFolder();
    MimeMessage mailMessage = mock(MimeMessage.class);
    Flags flags = mock(Flags.class);
    given(mailMessage.getFlags()).willReturn(flags);
    final Message[] messages = new Message[] { mailMessage };
    willAnswer(invocation -> messages).given(receiver).searchForNewMessages();
    willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
    final CountDownLatch idles = new CountDownLatch(2);
    willAnswer(invocation -> {
        idles.countDown();
        Thread.sleep(5000);
        return null;
    }).given(folder).idle();
    adapter.start();
    /*
		 * Idle takes 5 seconds; since this server supports RECENT, we should
		 * not receive any early messages.
		 */
    assertNull(channel.receive(3000));
    assertNotNull(channel.receive(5000));
    assertTrue(idles.await(5, TimeUnit.SECONDS));
    adapter.stop();
    context.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) IMAPFolder(com.sun.mail.imap.IMAPFolder) URLName(javax.mail.URLName) Store(javax.mail.Store) Flags(javax.mail.Flags) CountDownLatch(java.util.concurrent.CountDownLatch) Field(java.lang.reflect.Field) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MimeMessage(javax.mail.internet.MimeMessage) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 60 with Store

use of javax.mail.Store in project opentest by mcdcorp.

the class ReadEmailImap method run.

@Override
public void run() {
    super.run();
    final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);
    String defaultSentAfterTime = LocalDateTime.now().minusDays(365).format(dateTimeFormatter);
    String imapServer = this.readStringArgument("server");
    Integer port = this.readIntArgument("port", null);
    String subjectContains = this.readStringArgument("subjectContains", this.readStringArgument("subject", null));
    Pattern subjectRegex = this.readRegexArgument("subjectRegex", null);
    String bodyContains = this.readStringArgument("bodyContains", null);
    Pattern bodyRegex = this.readRegexArgument("bodyRegex", null);
    String fromAddress = this.readStringArgument("from", null);
    String sentAfter = this.readStringArgument("sentAfter", defaultSentAfterTime);
    String userName = this.readStringArgument("userName");
    String password = this.readStringArgument("password");
    Integer maxResults = this.readIntArgument("maxResults", 1);
    Integer minResults = this.readIntArgument("minResults", 1);
    LocalDateTime sentAfterDate = LocalDateTime.parse(sentAfter, dateTimeFormatter);
    ZonedDateTime sentAfterZonedDate = ZonedDateTime.of(sentAfterDate, ZoneId.systemDefault());
    Folder inbox = null;
    Store store = null;
    try {
        Session session = Session.getDefaultInstance(System.getProperties());
        store = session.getStore("imaps");
        if (port == null) {
            store.connect(imapServer, userName, password);
        } else {
            store.connect(imapServer, port, userName, password);
        }
        inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        SentDateTerm sentDateTerm = new SentDateTerm(ComparisonTerm.GE, Date.from(sentAfterZonedDate.toInstant()));
        SearchTerm searchTerm = sentDateTerm;
        if (subjectContains != null) {
            SubjectTerm subjectTerm = new SubjectTerm(subjectContains);
            searchTerm = new AndTerm(searchTerm, subjectTerm);
        }
        if (bodyContains != null) {
            BodyTerm bodyTerm = new BodyTerm(bodyContains);
            searchTerm = new AndTerm(searchTerm, bodyTerm);
        }
        if (fromAddress != null) {
            FromStringTerm fromTerm = new FromStringTerm(fromAddress);
            searchTerm = new AndTerm(searchTerm, fromTerm);
        }
        List<Message> allMessages = Arrays.asList(inbox.search(searchTerm));
        int resultsIndex = 0;
        ArrayList<HashMap<String, String>> resultMessages = new ArrayList<>();
        while (resultMessages.size() < maxResults) {
            resultsIndex++;
            if (resultsIndex > allMessages.size()) {
                break;
            }
            Message currentMessage = allMessages.get(allMessages.size() - resultsIndex);
            // Make sure the sent date and time is after the specified
            // one. Since javax.mail only takes the date into consideration
            // and ignores the time, we might end up with messages sent earlier
            // than the time specified, so we must check this ourselves
            boolean sentDateIsInRange = currentMessage.getSentDate().after(Date.from(sentAfterZonedDate.toInstant()));
            if (!sentDateIsInRange) {
                continue;
            }
            String subject = currentMessage.getSubject();
            if (subjectRegex != null && !subjectRegex.matcher(subject).find()) {
                continue;
            }
            String body = this.getTextFromMessage(currentMessage);
            if (bodyRegex != null && !bodyRegex.matcher(body).find()) {
                continue;
            }
            HashMap resultMessage = new HashMap<>();
            resultMessage.put("subject", subject);
            resultMessage.put("body", body);
            resultMessage.put("from", getEmailAddresses(currentMessage.getFrom()));
            resultMessage.put("to", getEmailAddresses(currentMessage.getAllRecipients()));
            LocalDateTime sentDate = LocalDateTime.ofInstant(currentMessage.getSentDate().toInstant(), ZoneId.systemDefault());
            resultMessage.put("sentDate", sentDate.format(dateTimeFormatter));
            HashMap<String, String> headers = new HashMap<>();
            Enumeration headersIterator = currentMessage.getAllHeaders();
            while (headersIterator.hasMoreElements()) {
                Header header = (Header) headersIterator.nextElement();
                headers.put(header.getName(), header.getValue());
            }
            resultMessage.put("headers", headers);
            resultMessages.add(resultMessage);
        }
        if (resultMessages.size() < minResults) {
            throw new RuntimeException(String.format("We were expecting to find at least %s email message(s) matching the " + "given criteria, but we found %s. You can set the minimum number " + "of messages expected by using the \"minResults\" argument.", minResults, resultMessages.size()));
        } else {
            Logger.info(String.format("We found %s email message(s) matching the given criteria", resultMessages.size()));
        }
        this.writeOutput("emails", ScriptUtils.wrapArray(resultMessages.toArray()));
        // Write the details of the most recent message again, to
        // dedicated output values. These three output values will be
        // deprecated and are only populated for backward compatibility.
        HashMap mostRecentMessage = resultMessages.get(0);
        this.writeOutput("subject", mostRecentMessage.get("subject"));
        this.writeOutput("body", mostRecentMessage.get("body"));
        this.writeOutput("from", mostRecentMessage.get("from"));
    } catch (Exception ex) {
        throw new RuntimeException(String.format("Failed to read emails from server %s", imapServer), ex);
    } finally {
        try {
            if (inbox != null) {
                inbox.close(true);
            }
            if (store != null) {
                store.close();
            }
        } catch (MessagingException ex) {
            Logger.error("Failed to close the email store and/or folder", ex);
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Message(javax.mail.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Store(javax.mail.Store) SentDateTerm(javax.mail.search.SentDateTerm) Folder(javax.mail.Folder) AndTerm(javax.mail.search.AndTerm) ZonedDateTime(java.time.ZonedDateTime) BodyTerm(javax.mail.search.BodyTerm) Pattern(java.util.regex.Pattern) Enumeration(java.util.Enumeration) MessagingException(javax.mail.MessagingException) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) MessagingException(javax.mail.MessagingException) FromStringTerm(javax.mail.search.FromStringTerm) Header(javax.mail.Header) DateTimeFormatter(java.time.format.DateTimeFormatter) Session(javax.mail.Session)

Aggregations

Store (javax.mail.Store)65 Folder (javax.mail.Folder)49 Message (javax.mail.Message)40 MimeMessage (javax.mail.internet.MimeMessage)30 Session (javax.mail.Session)23 Properties (java.util.Properties)18 MessagingException (javax.mail.MessagingException)16 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 IMAPFolder (com.sun.mail.imap.IMAPFolder)8 IOException (java.io.IOException)8 URLName (javax.mail.URLName)8 Flags (javax.mail.Flags)7 NoSuchProviderException (javax.mail.NoSuchProviderException)7 Date (java.util.Date)6 InternetAddress (javax.mail.internet.InternetAddress)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 Test (org.junit.Test)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)4 InputStream (java.io.InputStream)3