Search in sources :

Example 1 with SubjectTerm

use of javax.mail.search.SubjectTerm in project camel by apache.

the class SearchTermBuilder method subject.

public SearchTermBuilder subject(Op op, String pattern) {
    SearchTerm st = new SubjectTerm(pattern);
    addTerm(op, st);
    return this;
}
Also used : SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm)

Example 2 with SubjectTerm

use of javax.mail.search.SubjectTerm in project jodd by oblac.

the class EmailFilterTest method testAndOrNotAlt.

@Test
public void testAndOrNotAlt() {
    EmailFilter emailFilter = filter().or(filter().and(filter().from("from"), filter().to("to")), filter().not(filter().subject("subject")), filter().from("from2"));
    SearchTerm expected = new OrTerm(new SearchTerm[] { new AndTerm(new FromStringTerm("from"), new RecipientStringTerm(Message.RecipientType.TO, "to")), new NotTerm(new SubjectTerm("subject")), new FromStringTerm("from2") });
    assertEquals(expected, emailFilter.searchTerm);
}
Also used : AndTerm(javax.mail.search.AndTerm) OrTerm(javax.mail.search.OrTerm) FromStringTerm(javax.mail.search.FromStringTerm) RecipientStringTerm(javax.mail.search.RecipientStringTerm) NotTerm(javax.mail.search.NotTerm) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) Test(org.junit.Test)

Example 3 with SubjectTerm

use of javax.mail.search.SubjectTerm in project opennms by OpenNMS.

the class MailTransportMonitor method processMailSubject.

/**
 * After a mailbox has been opened, search through the retrieved messages
 * for a matching subject.
 *
 * @param mailParms
 * @param mailFolder
 * @return a PollStatus indicative of the success of matching a subject or just retieving
 *         mail folder contents... dependent on configuration.
 */
private PollStatus processMailSubject(final MailTransportParameters mailParms, final Folder mailFolder) {
    PollStatus status = PollStatus.unknown();
    try {
        final String subject = computeMatchingSubject(mailParms);
        if (mailFolder.isOpen() && subject != null) {
            final Message[] mailMessages = mailFolder.getMessages();
            final SearchTerm searchTerm = new SubjectTerm(subject);
            final SearchTerm deleteTerm = new HeaderTerm(MTM_HEADER_KEY, m_headerValue);
            LOG.debug("searchMailSubject: searching {} message(s) for subject '{}'", mailMessages.length, subject);
            boolean delete = false;
            boolean found = false;
            for (int i = 1; i <= mailMessages.length; i++) {
                final Message mailMessage = mailFolder.getMessage(i);
                LOG.debug("searchMailSubject: retrieved message subject '{}'", mailMessage.getSubject());
                if (mailMessage.match(searchTerm)) {
                    found = true;
                    LOG.debug("searchMailSubject: message with subject '{}' found.", subject);
                    if (mailParms.isEnd2EndTestInProgress()) {
                        if (!delete)
                            LOG.debug("searchMailSubject: flagging message with subject '{}' for deletion for end2end test.", subject);
                        delete = true;
                    }
                }
                final boolean deleteAllMail = mailParms.getReadTest().getDeleteAllMail();
                final boolean foundMTMHeader = mailMessage.match(deleteTerm);
                LOG.debug("searchMailSubject: deleteAllMail = {}, MTM header found = {}", Boolean.toString(deleteAllMail), Boolean.toString(foundMTMHeader));
                if (deleteAllMail) {
                    if (!delete)
                        LOG.debug("searchMailSubject: flagging message with subject '{}' for deletion because deleteAllMail is set.", subject);
                    delete = true;
                } else if (foundMTMHeader) {
                    if (!delete)
                        LOG.debug("searchMailSubject: flagging message with subject '{}' for deletion because we sent it (found header {}={})", subject, MTM_HEADER_KEY, m_headerValue);
                    delete = true;
                }
                if (delete) {
                    mailMessage.setFlag(Flag.DELETED, true);
                }
            // since we want to delete old messages matchin MTM_HEADER_KEY, we can't break early
            // if (found) break;
            }
            if (!found) {
                LOG.debug("searchMailSubject: message with subject: '{}' NOT found.", subject);
                status = PollStatus.down("searchMailSubject: matching test message: '" + subject + "', not found.");
            } else {
                status = PollStatus.available();
            }
        }
    } catch (final MessagingException e) {
        return PollStatus.down(e.getLocalizedMessage());
    }
    return status;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) HeaderTerm(javax.mail.search.HeaderTerm) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm)

Example 4 with SubjectTerm

use of javax.mail.search.SubjectTerm in project opennms by OpenNMS.

the class JavaReadMailerTest method testReadMessagesWithSearchTerm.

/**
 * Un-ignore this test with a proper gmail account
 * @throws JavaMailerException
 * @throws MessagingException
 * @throws InterruptedException
 */
@Test
@Ignore
public void testReadMessagesWithSearchTerm() throws JavaMailerException, MessagingException, InterruptedException {
    String gmailAccount = getUser();
    String gmailPassword = getPassword();
    JavaSendMailer sendMailer = createSendMailer(gmailAccount, gmailPassword);
    String term1 = String.valueOf(Calendar.getInstance().getTimeInMillis());
    Thread.sleep(2);
    String term2 = String.valueOf(Calendar.getInstance().getTimeInMillis());
    Thread.sleep(2);
    String term3 = String.valueOf(Calendar.getInstance().getTimeInMillis());
    Thread.sleep(2);
    SendmailMessage sendMsg = createAckMessage(gmailAccount, "1", term1, "ack");
    sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
    sendMailer.send();
    sendMsg = createAckMessage(gmailAccount, "2", term2, "ack");
    sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
    sendMailer.send();
    sendMsg = createAckMessage(gmailAccount, "3", term3, "ack");
    sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
    sendMailer.send();
    JavaReadMailer readMailer = createGoogleReadMailer(gmailAccount, gmailPassword);
    // See if search finds all 3 messages
    SearchTerm st = new OrTerm(new SubjectTerm(".*" + term1 + " #.*"), new SubjectTerm(".*" + term2 + " #.*"));
    st = new OrTerm(st, new SubjectTerm("*." + term3 + " #.*"));
    List<Message> msgs = null;
    try {
        msgs = readMailer.retrieveMessages(st);
    } catch (JavaMailerException e) {
        e.printStackTrace();
    }
    Assert.assertEquals(3, msgs.size());
    st = new OrTerm(new SubjectTerm(".*" + term1 + " #.*"), new SubjectTerm(".*" + term2 + " #.*"));
    try {
        msgs = readMailer.retrieveMessages(st);
    } catch (JavaMailerException e) {
        e.printStackTrace();
    }
    // Should find only term1 and term2 messages
    Assert.assertNotNull(msgs);
    Assert.assertEquals(2, msgs.size());
    // Delete the term1 and term2 messages
    for (Message msg : msgs) {
        msg.setFlag(Flag.DELETED, true);
    }
    // Find and delete the term3 messages
    st = new SubjectTerm("*." + term3 + " #.*");
    try {
        msgs = readMailer.retrieveMessages(st);
    } catch (JavaMailerException e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(msgs);
    Assert.assertTrue(msgs.size() >= 1);
    for (Message eventMsg : msgs) {
        eventMsg.setFlag(Flag.DELETED, true);
    }
    // Make sure they're all gone
    st = new OrTerm(new SubjectTerm(".*" + term1 + " #.*"), new SubjectTerm(".*" + term2 + " #.*"));
    st = new OrTerm(st, new SubjectTerm("*." + term3 + " #.*"));
    try {
        msgs = readMailer.retrieveMessages(st);
    } catch (JavaMailerException e) {
        e.printStackTrace();
    }
    Assert.assertTrue(msgs.isEmpty());
}
Also used : OrTerm(javax.mail.search.OrTerm) Message(javax.mail.Message) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with SubjectTerm

use of javax.mail.search.SubjectTerm in project jodd by oblac.

the class EmailFilterTest method testAndOrNot.

@Test
public void testAndOrNot() {
    EmailFilter emailFilter = filter().from("from").to("to").or().not().subject("subject").from("from2");
    SearchTerm expected = new OrTerm(new OrTerm(new AndTerm(new FromStringTerm("from"), new RecipientStringTerm(Message.RecipientType.TO, "to")), new NotTerm(new SubjectTerm("subject"))), new FromStringTerm("from2"));
    assertEquals(expected, emailFilter.searchTerm);
}
Also used : AndTerm(javax.mail.search.AndTerm) OrTerm(javax.mail.search.OrTerm) FromStringTerm(javax.mail.search.FromStringTerm) RecipientStringTerm(javax.mail.search.RecipientStringTerm) NotTerm(javax.mail.search.NotTerm) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) Test(org.junit.Test)

Aggregations

SearchTerm (javax.mail.search.SearchTerm)12 SubjectTerm (javax.mail.search.SubjectTerm)12 AndTerm (javax.mail.search.AndTerm)8 FromStringTerm (javax.mail.search.FromStringTerm)6 InternetAddress (javax.mail.internet.InternetAddress)5 Message (javax.mail.Message)4 URISyntaxException (java.net.URISyntaxException)3 DateFormat (java.text.DateFormat)3 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 MessagingException (javax.mail.MessagingException)3 MimeMessage (javax.mail.internet.MimeMessage)3 ArrayList (java.util.ArrayList)2 Folder (javax.mail.Folder)2 Session (javax.mail.Session)2 Store (javax.mail.Store)2 FromTerm (javax.mail.search.FromTerm)2 NotTerm (javax.mail.search.NotTerm)2 OrTerm (javax.mail.search.OrTerm)2 SentDateTerm (javax.mail.search.SentDateTerm)2