Search in sources :

Example 1 with SearchTerm

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

the class MailComponent method createEndpoint.

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    URI url = new URI(uri);
    // must use copy as each endpoint can have different options
    MailConfiguration config = getConfiguration().copy();
    // only configure if we have a url with a known protocol
    config.configure(url);
    configureAdditionalJavaMailProperties(config, parameters);
    MailEndpoint endpoint = new MailEndpoint(uri, this, config);
    // special for search term bean reference
    Object searchTerm = getAndRemoveOrResolveReferenceParameter(parameters, "searchTerm", Object.class);
    if (searchTerm != null) {
        SearchTerm st;
        if (searchTerm instanceof SimpleSearchTerm) {
            // okay its a SimpleSearchTerm then lets convert that to SearchTerm
            st = MailConverters.toSearchTerm((SimpleSearchTerm) searchTerm, getCamelContext().getTypeConverter());
        } else {
            st = getCamelContext().getTypeConverter().mandatoryConvertTo(SearchTerm.class, searchTerm);
        }
        endpoint.setSearchTerm(st);
    }
    endpoint.setContentTypeResolver(contentTypeResolver);
    setProperties(endpoint.getConfiguration(), parameters);
    setProperties(endpoint, parameters);
    // special for searchTerm.xxx options
    Map<String, Object> sstParams = IntrospectionSupport.extractProperties(parameters, "searchTerm.");
    if (!sstParams.isEmpty()) {
        // use SimpleSearchTerm as POJO to store the configuration and then convert that to the actual SearchTerm
        SimpleSearchTerm sst = new SimpleSearchTerm();
        setProperties(sst, sstParams);
        SearchTerm st = MailConverters.toSearchTerm(sst, getCamelContext().getTypeConverter());
        endpoint.setSearchTerm(st);
    }
    // sanity check that we know the mail server
    ObjectHelper.notEmpty(config.getHost(), "host");
    ObjectHelper.notEmpty(config.getProtocol(), "protocol");
    return endpoint;
}
Also used : SearchTerm(javax.mail.search.SearchTerm) URI(java.net.URI)

Example 2 with SearchTerm

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

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

the class SearchTermBuilderTest method testSearchTermSentLast24Hours.

public void testSearchTermSentLast24Hours() throws Exception {
    SearchTermBuilder build = new SearchTermBuilder();
    long offset = -1 * (24 * 60 * 60 * 1000L);
    SearchTerm st = build.subject("Camel").sentNow(Comparison.GE, offset).build();
    assertNotNull(st);
    // create dummy message
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    MimeMessage msg = new MimeMessage(sender.getSession());
    msg.setSubject("Yeah Camel rocks");
    msg.setText("Apache Camel is a cool project. Have a fun ride.");
    msg.setFrom(new InternetAddress("someone@somewhere.com"));
    msg.setSentDate(new Date());
    assertTrue("Should match message", st.match(msg));
    MimeMessage msg2 = new MimeMessage(sender.getSession());
    msg2.setSubject("Camel in Action");
    msg2.setText("Hey great book");
    msg2.setFrom(new InternetAddress("dude@apache.org"));
    // mark it as sent 2 days ago
    long twoDays = 2 * 24 * 60 * 60 * 1000L;
    long time = new Date().getTime() - twoDays;
    msg2.setSentDate(new Date(time));
    assertFalse("Should not match message as its too old", st.match(msg2));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) SearchTerm(javax.mail.search.SearchTerm) Date(java.util.Date)

Example 4 with SearchTerm

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

the class EmailFilterTest method testAnd2.

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

Example 5 with SearchTerm

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

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