Search in sources :

Example 6 with SearchTerm

use of jakarta.mail.search.SearchTerm in project exist by eXist-db.

the class MessageListFunctions method parseChildSearchTerms.

private SearchTerm[] parseChildSearchTerms(Node terms) throws XPathException {
    // Parent allows multiple child search terms
    ArrayList<SearchTerm> st = new ArrayList<>();
    NodeList children = terms.getChildNodes();
    if (children.getLength() > 0) {
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            st.add(parseSearchTerms(child));
        }
    } else {
        throw (new XPathException(this, "At least one child term is required for term with type: " + ((Element) terms).getAttribute("type")));
    }
    return st.toArray(new SearchTerm[0]);
}
Also used : XPathException(org.exist.xquery.XPathException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) SearchTerm(jakarta.mail.search.SearchTerm)

Example 7 with SearchTerm

use of jakarta.mail.search.SearchTerm in project spring-integration by spring-projects.

the class ImapMailSearchTermsTests method validateSearchTermsWhenShouldMarkAsReadWithExistingFlags.

@Test
public void validateSearchTermsWhenShouldMarkAsReadWithExistingFlags() throws Exception {
    ImapMailReceiver receiver = new ImapMailReceiver();
    receiver.setShouldMarkMessagesAsRead(true);
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();
    Field folderField = AbstractMailReceiver.class.getDeclaredField("folder");
    folderField.setAccessible(true);
    Folder folder = mock(Folder.class);
    when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER));
    folderField.set(receiver, folder);
    Method compileSearchTerms = ReflectionUtils.findMethod(receiver.getClass(), "compileSearchTerms", Flags.class);
    compileSearchTerms.setAccessible(true);
    Flags flags = new Flags();
    flags.add(Flag.ANSWERED);
    SearchTerm searchTerms = (SearchTerm) compileSearchTerms.invoke(receiver, flags);
    assertThat(searchTerms instanceof AndTerm).isTrue();
    AndTerm andTerm = (AndTerm) searchTerms;
    SearchTerm[] terms = andTerm.getTerms();
    assertThat(terms.length).isEqualTo(2);
    NotTerm notTerm = (NotTerm) terms[0];
    assertThat(((FlagTerm) notTerm.getTerm()).getFlags().contains(Flag.ANSWERED)).isTrue();
    notTerm = (NotTerm) terms[1];
    Flags siFlags = new Flags();
    siFlags.add(AbstractMailReceiver.DEFAULT_SI_USER_FLAG);
    assertThat(((FlagTerm) notTerm.getTerm()).getFlags().contains(siFlags)).isTrue();
}
Also used : AndTerm(jakarta.mail.search.AndTerm) Field(java.lang.reflect.Field) NotTerm(jakarta.mail.search.NotTerm) BeanFactory(org.springframework.beans.factory.BeanFactory) Flags(jakarta.mail.Flags) Method(java.lang.reflect.Method) Folder(jakarta.mail.Folder) SearchTerm(jakarta.mail.search.SearchTerm) Test(org.junit.jupiter.api.Test)

Example 8 with SearchTerm

use of jakarta.mail.search.SearchTerm in project spring-integration by spring-projects.

the class ImapMailSearchTermsTests method validateSearchTermsWhenShouldMarkAsReadNoExistingFlagsGuts.

public void validateSearchTermsWhenShouldMarkAsReadNoExistingFlagsGuts(String userFlag, ImapMailReceiver receiver) throws NoSuchFieldException, IllegalAccessException, InvocationTargetException {
    receiver.setShouldMarkMessagesAsRead(true);
    receiver.setBeanFactory(mock(BeanFactory.class));
    Field folderField = AbstractMailReceiver.class.getDeclaredField("folder");
    folderField.setAccessible(true);
    Folder folder = mock(Folder.class);
    when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER));
    folderField.set(receiver, folder);
    Method compileSearchTerms = ReflectionUtils.findMethod(receiver.getClass(), "compileSearchTerms", Flags.class);
    compileSearchTerms.setAccessible(true);
    Flags flags = new Flags();
    SearchTerm searchTerms = (SearchTerm) compileSearchTerms.invoke(receiver, flags);
    assertThat(searchTerms instanceof NotTerm).isTrue();
    NotTerm notTerm = (NotTerm) searchTerms;
    Flags siFlags = new Flags();
    siFlags.add(userFlag);
    assertThat(((FlagTerm) notTerm.getTerm()).getFlags()).isEqualTo(siFlags);
}
Also used : Field(java.lang.reflect.Field) FlagTerm(jakarta.mail.search.FlagTerm) NotTerm(jakarta.mail.search.NotTerm) BeanFactory(org.springframework.beans.factory.BeanFactory) Flags(jakarta.mail.Flags) Method(java.lang.reflect.Method) Folder(jakarta.mail.Folder) SearchTerm(jakarta.mail.search.SearchTerm)

Example 9 with SearchTerm

use of jakarta.mail.search.SearchTerm in project spring-integration by spring-projects.

the class ImapMailReceiver method searchForNewMessages.

/**
 * Retrieves new messages from this receiver's folder. This implementation
 * creates a {@link SearchTerm} that searches for all messages in the
 * folder that are {@link jakarta.mail.Flags.Flag#RECENT RECENT}, not
 * {@link jakarta.mail.Flags.Flag#ANSWERED ANSWERED}, and not
 * {@link jakarta.mail.Flags.Flag#DELETED DELETED}. The search term is used
 * to {@link Folder#search(SearchTerm) search} for new messages.
 * @return the new messages
 * @throws MessagingException in case of JavaMail errors
 */
@Override
protected Message[] searchForNewMessages() throws MessagingException {
    Folder folderToUse = getFolder();
    Flags supportedFlags = folderToUse.getPermanentFlags();
    SearchTerm searchTerm = compileSearchTerms(supportedFlags);
    if (folderToUse.isOpen()) {
        return nullSafeMessages(searchTerm != null ? folderToUse.search(searchTerm) : folderToUse.getMessages());
    }
    throw new MessagingException("Folder is closed");
}
Also used : MessagingException(jakarta.mail.MessagingException) Flags(jakarta.mail.Flags) Folder(jakarta.mail.Folder) IMAPFolder(com.sun.mail.imap.IMAPFolder) SearchTerm(jakarta.mail.search.SearchTerm)

Example 10 with SearchTerm

use of jakarta.mail.search.SearchTerm in project exist by eXist-db.

the class MessageListFunctions method parseChildSearchTerm.

private SearchTerm parseChildSearchTerm(Node terms) throws XPathException {
    // Parent only allows a single child search term
    SearchTerm st = null;
    NodeList children = terms.getChildNodes();
    if (children.getLength() == 1) {
        Node child = children.item(0);
        st = parseSearchTerms(child);
    } else {
        throw (new XPathException(this, "Only one child term is allowed for term with type: " + ((Element) terms).getAttribute("type")));
    }
    return (st);
}
Also used : XPathException(org.exist.xquery.XPathException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) SearchTerm(jakarta.mail.search.SearchTerm)

Aggregations

SearchTerm (jakarta.mail.search.SearchTerm)14 XPathException (org.exist.xquery.XPathException)10 Element (org.w3c.dom.Element)8 Flags (jakarta.mail.Flags)5 Folder (jakarta.mail.Folder)4 NotTerm (jakarta.mail.search.NotTerm)3 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 BeanFactory (org.springframework.beans.factory.BeanFactory)3 FlagTerm (jakarta.mail.search.FlagTerm)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Test (org.junit.jupiter.api.Test)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 IMAPFolder (com.sun.mail.imap.IMAPFolder)1 Message (jakarta.mail.Message)1 MessagingException (jakarta.mail.MessagingException)1 AndTerm (jakarta.mail.search.AndTerm)1