Search in sources :

Example 1 with SortTerm

use of com.sun.mail.imap.SortTerm in project camel by apache.

the class MailSorterTest method testSortMessages.

@Test
public void testSortMessages() throws Exception {
    Message[] expected = new Message[] { MESSAGES[0], MESSAGES[1], MESSAGES[2] };
    // Sort using all the terms. Message order should be the same no matter what term is used
    for (SortTerm term : POSSIBLE_TERMS) {
        Message[] actual = MESSAGES.clone();
        MailSorter.sortMessages(actual, new SortTerm[] { term });
        try {
            assertArrayEquals(actual, expected);
        } catch (Exception ex) {
            throw new Exception("Term: " + term.toString(), ex);
        }
    }
}
Also used : SortTerm(com.sun.mail.imap.SortTerm) Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) Test(org.junit.Test)

Example 2 with SortTerm

use of com.sun.mail.imap.SortTerm in project camel by apache.

the class MailSorterTest method testSortMessagesReverse.

@Test
public void testSortMessagesReverse() throws Exception {
    Message[] expected = new Message[] { MESSAGES[2], MESSAGES[1], MESSAGES[0] };
    // Sort using all the terms. Message order should be the same no matter what term is used
    for (SortTerm term : POSSIBLE_TERMS) {
        Message[] actual = MESSAGES.clone();
        MailSorter.sortMessages(actual, new SortTerm[] { SortTerm.REVERSE, term });
        try {
            assertArrayEquals(actual, expected);
        } catch (AssertionError ex) {
            throw new AssertionError("Term: " + term.toString(), ex);
        }
    }
}
Also used : SortTerm(com.sun.mail.imap.SortTerm) Message(javax.mail.Message) Test(org.junit.Test)

Example 3 with SortTerm

use of com.sun.mail.imap.SortTerm in project camel by apache.

the class MailSorterTest method testSortMessagesMulti.

@Test
public void testSortMessagesMulti() throws Exception {
    Message[] expected = new Message[] { MESSAGES[0], MESSAGES[1], MESSAGES[2] };
    // should be ignored since it is already the decider.
    for (SortTerm term1 : POSSIBLE_TERMS) {
        for (SortTerm term2 : POSSIBLE_TERMS) {
            Message[] actual = MESSAGES.clone();
            MailSorter.sortMessages(actual, new SortTerm[] { term1, SortTerm.REVERSE, term2 });
            try {
                assertArrayEquals(actual, expected);
            } catch (AssertionError ex) {
                throw new AssertionError(String.format("Terms: %s, %s", term1.toString(), term2.toString()), ex);
            }
        }
    }
}
Also used : SortTerm(com.sun.mail.imap.SortTerm) Message(javax.mail.Message) Test(org.junit.Test)

Example 4 with SortTerm

use of com.sun.mail.imap.SortTerm 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 5 with SortTerm

use of com.sun.mail.imap.SortTerm in project camel by apache.

the class MailSorter method getSortTermsWithDescending.

/**
     * Compute the potentially descending sort terms from the input list
     *
     * @param sortTerm Input list
     * @return Sort terms list including if the respective sort should be sorted in descending order
     */
private static List<SortTermWithDescending> getSortTermsWithDescending(SortTerm[] sortTerm) {
    // List of reversable sort terms. If the boolean is true the respective sort term is descending
    final List<SortTermWithDescending> sortTermsWithDescending = new ArrayList<SortTermWithDescending>(sortTerm.length);
    // Descending next item in input because the last item was a "descending"
    boolean descendingNext = false;
    for (SortTerm term : sortTerm) {
        if (term.equals(SortTerm.REVERSE)) {
            if (descendingNext) {
                throw new IllegalArgumentException("Double reverse in sort term is not allowed");
            }
            descendingNext = true;
        } else {
            sortTermsWithDescending.add(new SortTermWithDescending(term, descendingNext));
            descendingNext = false;
        }
    }
    return sortTermsWithDescending;
}
Also used : SortTerm(com.sun.mail.imap.SortTerm) ArrayList(java.util.ArrayList)

Aggregations

SortTerm (com.sun.mail.imap.SortTerm)5 Message (javax.mail.Message)4 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 IMAPFolder (com.sun.mail.imap.IMAPFolder)1 MessagingException (javax.mail.MessagingException)1 SearchTerm (javax.mail.search.SearchTerm)1 KeyValueHolder (org.apache.camel.util.KeyValueHolder)1