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;
}
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);
}
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;
}
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());
}
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);
}
Aggregations