Search in sources :

Example 1 with JavaReadMailer

use of org.opennms.javamail.JavaReadMailer in project opennms by OpenNMS.

the class MailAckProcessor method retrieveAckMessages.

/**
     * <p>retrieveAckMessages</p>
     *
     * @return a {@link java.util.List} object.
     * @throws org.opennms.javamail.JavaMailerException if any.
     */
protected List<Message> retrieveAckMessages() throws JavaMailerException {
    LOG.debug("retrieveAckMessages: Retrieving messages...");
    ReadmailConfig readMailConfig = determineMailReaderConfig();
    if (readMailConfig.getReadmailHost() != null) {
        final ReadmailHost readmailHost = readMailConfig.getReadmailHost();
        final UserAuth userAuth = readMailConfig.getUserAuth();
        LOG.debug("retrieveAckMessages: creating JavaReadMailer with config: host: {} port: {} ssl: {} transport: {} user: {} password: {}", readmailHost.getHost(), readmailHost.getPort(), readmailHost.getReadmailProtocol().isSslEnable(), readmailHost.getReadmailProtocol().getTransport(), userAuth == null ? null : userAuth.getUserName(), userAuth == null ? null : userAuth.getPassword());
    }
    //TODO: make flag for folder open mode
    //TODO: Make sure configuration supports flag for deleting acknowledgments
    JavaReadMailer readMailer = new JavaReadMailer(readMailConfig, true);
    String notifRe = m_ackdDao.getConfig().getNotifyidMatchExpression();
    notifRe = notifRe.startsWith("~") ? notifRe.substring(1) : notifRe;
    String alarmRe = m_ackdDao.getConfig().getAlarmidMatchExpression();
    alarmRe = alarmRe.startsWith("~") ? alarmRe.substring(1) : alarmRe;
    Pattern notifPattern = Pattern.compile(notifRe);
    Pattern alarmPattern = Pattern.compile(alarmRe);
    List<Message> msgs = readMailer.retrieveMessages();
    LOG.info("retrieveAckMessages: Iterating {} messages with notif expression: {} and alarm expression: {}", msgs.size(), notifRe, alarmRe);
    for (Iterator<Message> iterator = msgs.iterator(); iterator.hasNext(); ) {
        Message msg = iterator.next();
        try {
            String subject = msg.getSubject();
            Matcher alarmMatcher = alarmPattern.matcher(subject);
            Matcher notifMatcher = notifPattern.matcher(subject);
            LOG.debug("retrieveAckMessages: comparing the subject: {}", subject);
            if (!(notifMatcher.matches() || alarmMatcher.matches())) {
                LOG.debug("retrieveAckMessages: Subject doesn't match either expression.");
                iterator.remove();
            } else {
                //TODO: this just looks wrong
                //delete this non-ack message because the acks will get deleted later and the config
                //indicates delete all mail from mailbox
                LOG.debug("retrieveAckMessages: Subject matched, setting deleted flag");
                if (readMailConfig.isDeleteAllMail()) {
                    msg.setFlag(Flag.DELETED, true);
                }
            }
        } catch (Throwable t) {
            LOG.error("retrieveAckMessages: Problem processing message: {}", t);
        }
    }
    return msgs;
}
Also used : Pattern(java.util.regex.Pattern) Message(javax.mail.Message) Matcher(java.util.regex.Matcher) ReadmailConfig(org.opennms.netmgt.config.javamail.ReadmailConfig) ReadmailHost(org.opennms.netmgt.config.javamail.ReadmailHost) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) JavaReadMailer(org.opennms.javamail.JavaReadMailer)

Aggregations

Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Message (javax.mail.Message)1 JavaReadMailer (org.opennms.javamail.JavaReadMailer)1 ReadmailConfig (org.opennms.netmgt.config.javamail.ReadmailConfig)1 ReadmailHost (org.opennms.netmgt.config.javamail.ReadmailHost)1 UserAuth (org.opennms.netmgt.config.javamail.UserAuth)1