Search in sources :

Example 1 with HeaderTerm

use of javax.mail.search.HeaderTerm 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;
}
Also used : PollStatus(org.opennms.netmgt.poller.PollStatus) Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) HeaderTerm(javax.mail.search.HeaderTerm) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm)

Example 2 with HeaderTerm

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

the class EmailFilter method header.

/**
	 * Defines filter for header.
	 */
public EmailFilter header(String headerName, String pattern) {
    SearchTerm term = new HeaderTerm(headerName, pattern);
    concat(term);
    return this;
}
Also used : HeaderTerm(javax.mail.search.HeaderTerm) SearchTerm(javax.mail.search.SearchTerm)

Example 3 with HeaderTerm

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

the class SearchTermBuilder method header.

public SearchTermBuilder header(Op op, String headerName, String pattern) {
    SearchTerm st = new HeaderTerm(headerName, pattern);
    addTerm(op, st);
    return this;
}
Also used : HeaderTerm(javax.mail.search.HeaderTerm) SearchTerm(javax.mail.search.SearchTerm)

Aggregations

HeaderTerm (javax.mail.search.HeaderTerm)3 SearchTerm (javax.mail.search.SearchTerm)3 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 SubjectTerm (javax.mail.search.SubjectTerm)1 PollStatus (org.opennms.netmgt.poller.PollStatus)1