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