Search in sources :

Example 21 with SearchTerm

use of javax.mail.search.SearchTerm 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 22 with SearchTerm

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

the class MailConsumer method retrieveMessages.

/**
     * @return Messages from input folder according to the search and sort criteria stored in the endpoint
     * @throws MessagingException If message retrieval fails
     */
private List<KeyValueHolder<String, Message>> retrieveMessages() throws MessagingException {
    List<KeyValueHolder<String, Message>> answer = new ArrayList<>();
    Message[] messages;
    final SortTerm[] sortTerm = getEndpoint().getSortTerm();
    final SearchTerm searchTerm = computeSearchTerm();
    if (sortTerm != null && serverCanSort) {
        final IMAPFolder imapFolder = (IMAPFolder) folder;
        if (searchTerm != null) {
            // Sort and search using server capability
            messages = imapFolder.getSortedMessages(sortTerm, searchTerm);
        } else {
            // Only sort using server capability
            messages = imapFolder.getSortedMessages(sortTerm);
        }
    } else {
        if (searchTerm != null) {
            messages = folder.search(searchTerm, retrieveAllMessages());
        } else {
            messages = retrieveAllMessages();
        }
        // Now we can sort (emulate email sort but restrict sort terms)
        if (sortTerm != null) {
            MailSorter.sortMessages(messages, sortTerm);
        }
    }
    for (Message message : messages) {
        String key = getEndpoint().getMailUidGenerator().generateUuid(getEndpoint(), message);
        if (isValidMessage(key, message)) {
            answer.add(new KeyValueHolder<>(key, message));
        }
    }
    return answer;
}
Also used : SortTerm(com.sun.mail.imap.SortTerm) Message(javax.mail.Message) IMAPFolder(com.sun.mail.imap.IMAPFolder) ArrayList(java.util.ArrayList) KeyValueHolder(org.apache.camel.util.KeyValueHolder) SearchTerm(javax.mail.search.SearchTerm)

Example 23 with SearchTerm

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

the class SearchTermBuilder method from.

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

Example 24 with SearchTerm

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

the class SearchTermBuilder method body.

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

Example 25 with SearchTerm

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

the class SearchTermBuilder method unseen.

public SearchTermBuilder unseen(Op op) {
    SearchTerm st = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
    addTerm(op, st);
    return this;
}
Also used : FlagTerm(javax.mail.search.FlagTerm) Flags(javax.mail.Flags) SearchTerm(javax.mail.search.SearchTerm)

Aggregations

SearchTerm (javax.mail.search.SearchTerm)38 RecipientStringTerm (javax.mail.search.RecipientStringTerm)9 FromStringTerm (javax.mail.search.FromStringTerm)8 Test (org.junit.Test)8 OrTerm (javax.mail.search.OrTerm)6 SubjectTerm (javax.mail.search.SubjectTerm)6 AndTerm (javax.mail.search.AndTerm)5 Message (javax.mail.Message)3 InternetAddress (javax.mail.internet.InternetAddress)3 MimeMessage (javax.mail.internet.MimeMessage)3 FlagTerm (javax.mail.search.FlagTerm)3 HeaderTerm (javax.mail.search.HeaderTerm)3 NotTerm (javax.mail.search.NotTerm)3 IMAPFolder (com.sun.mail.imap.IMAPFolder)2 Date (java.util.Date)2 Flags (javax.mail.Flags)2 MessagingException (javax.mail.MessagingException)2 BodyTerm (javax.mail.search.BodyTerm)2 MessageIDTerm (javax.mail.search.MessageIDTerm)2 ReceivedDateTerm (javax.mail.search.ReceivedDateTerm)2