Search in sources :

Example 1 with ReadmailConfig

use of org.opennms.netmgt.config.javamail.ReadmailConfig in project opennms by OpenNMS.

the class DefaultJavamailConfigurationDao method getReadMailConfig.

/* (non-Javadoc)
     * @see org.opennms.netmgt.dao.api.JavaMailConfigurationDao#getReadMailConfig(java.lang.String)
     */
@Override
public ReadmailConfig getReadMailConfig(String name) {
    ReadmailConfig config = null;
    List<ReadmailConfig> configs = getReadmailConfigs();
    for (ReadmailConfig readmailConfig : configs) {
        if (readmailConfig.getName() != null && readmailConfig.getName().equals(name)) {
            config = readmailConfig;
        }
    }
    return config;
}
Also used : ReadmailConfig(org.opennms.netmgt.config.javamail.ReadmailConfig)

Example 2 with ReadmailConfig

use of org.opennms.netmgt.config.javamail.ReadmailConfig in project opennms by OpenNMS.

the class MailAckProcessor method determineMailReaderConfig.

/**
 * <p>determineMailReaderConfig</p>
 *
 * @return a {@link org.opennms.netmgt.config.javamail.ReadmailConfig} object.
 */
public ReadmailConfig determineMailReaderConfig() {
    LOG.info("determineMailReaderConfig: determining mail reader configuration...");
    List<Parameter> parms = m_ackdDao.getParametersForReader("JavaMailReader");
    ReadmailConfig config = m_jmConfigDao.getDefaultReadmailConfig();
    for (Parameter parameter : parms) {
        if ("readmail-config".equalsIgnoreCase(parameter.getKey())) {
            config = m_jmConfigDao.getReadMailConfig(parameter.getValue());
        }
    }
    LOG.info("determinedMailReaderConfig: {}", config);
    return config;
}
Also used : Parameter(org.opennms.netmgt.config.ackd.Parameter) ReadmailConfig(org.opennms.netmgt.config.javamail.ReadmailConfig)

Example 3 with ReadmailConfig

use of org.opennms.netmgt.config.javamail.ReadmailConfig 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)

Example 4 with ReadmailConfig

use of org.opennms.netmgt.config.javamail.ReadmailConfig in project opennms by OpenNMS.

the class JavaReadMailerTest method createGoogleReadMailer.

private JavaReadMailer createGoogleReadMailer(String gmailAccount, String gmailPassword) throws JavaMailerException {
    ReadmailConfig config = new ReadmailConfig();
    config.setDebug(true);
    config.setDeleteAllMail(false);
    config.setMailFolder("INBOX");
    ReadmailHost readmailHost = new ReadmailHost();
    readmailHost.setHost("imap.gmail.com");
    readmailHost.setPort(993);
    ReadmailProtocol readmailProtocol = new ReadmailProtocol();
    readmailProtocol.setSslEnable(true);
    readmailProtocol.setStartTls(false);
    readmailProtocol.setTransport("imaps");
    readmailHost.setReadmailProtocol(readmailProtocol);
    config.setReadmailHost(readmailHost);
    UserAuth userAuth = new UserAuth();
    userAuth.setPassword(gmailPassword);
    userAuth.setUserName(gmailAccount);
    config.setUserAuth(userAuth);
    JavaReadMailer mailer = new JavaReadMailer(config, true);
    return mailer;
}
Also used : ReadmailProtocol(org.opennms.netmgt.config.javamail.ReadmailProtocol) ReadmailConfig(org.opennms.netmgt.config.javamail.ReadmailConfig) ReadmailHost(org.opennms.netmgt.config.javamail.ReadmailHost) UserAuth(org.opennms.netmgt.config.javamail.UserAuth)

Example 5 with ReadmailConfig

use of org.opennms.netmgt.config.javamail.ReadmailConfig in project opennms by OpenNMS.

the class JavaMailAckReaderIT method testIntegration.

/**
 * This test requires that 4 emails can be read from a Google account.  The mails should be
 * in this order:
 * Subject matching ackd-configuration expression of action type ack
 * Subject matching ackd-configuration expression of action type ack
 * Subject matching ackd-configuration expression of action type ack
 * Subject matching ackd-configuration expression of action type clear
 *
 * The test has been updated to now include sending an email message to a gmail account.  Just correct
 * the account details for your own local testing.
 *
 * @throws JavaMailerException
 */
@Ignore
@Test
public void testIntegration() throws JavaMailerException {
    String gmailAccount = getUser();
    String gmailPassword = getPassword();
    JavaSendMailer sendMailer = createSendMailer(gmailAccount, gmailPassword);
    SendmailMessage sendMsg = createAckMessage(gmailAccount, "1", "ack");
    sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
    sendMailer.send();
    sendMsg = createAckMessage(gmailAccount, "2", "ack");
    sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
    sendMailer.send();
    sendMsg = createAckMessage(gmailAccount, "3", "ack");
    sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
    sendMailer.send();
    sendMsg = createAckMessage(gmailAccount, "4", "clear");
    sendMailer.setMessage(sendMailer.buildMimeMessage(sendMsg));
    sendMailer.send();
    ReadmailConfig config = m_processor.determineMailReaderConfig();
    Assert.assertNotNull(config);
    updateConfigWithGoogleReadConfiguration(config, gmailAccount, gmailPassword);
    List<Message> msgs = m_processor.retrieveAckMessages();
    List<OnmsAcknowledgment> acks = m_processor.createAcks(msgs);
    Assert.assertNotNull(acks);
    Assert.assertEquals(4, acks.size());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(0).getAckType());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(0).getAckAction());
    Assert.assertEquals(Integer.valueOf(1), acks.get(0).getRefId());
    Assert.assertEquals(getUser() + "@gmail.com", acks.get(0).getAckUser());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(1).getAckType());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(1).getAckAction());
    Assert.assertEquals(Integer.valueOf(2), acks.get(1).getRefId());
    Assert.assertEquals(getUser() + "@gmail.com", acks.get(1).getAckUser());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(2).getAckType());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(2).getAckAction());
    Assert.assertEquals(Integer.valueOf(3), acks.get(2).getRefId());
    Assert.assertEquals(getUser() + "@gmail.com", acks.get(2).getAckUser());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(3).getAckType());
    Assert.assertEquals(AckAction.CLEAR, acks.get(3).getAckAction());
    Assert.assertEquals(Integer.valueOf(4), acks.get(3).getRefId());
    Assert.assertEquals(getUser() + "@gmail.com", acks.get(3).getAckUser());
}
Also used : OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) Message(javax.mail.Message) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MimeMessage(javax.mail.internet.MimeMessage) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) JavaSendMailer(org.opennms.javamail.JavaSendMailer) ReadmailConfig(org.opennms.netmgt.config.javamail.ReadmailConfig) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ReadmailConfig (org.opennms.netmgt.config.javamail.ReadmailConfig)5 Message (javax.mail.Message)2 ReadmailHost (org.opennms.netmgt.config.javamail.ReadmailHost)2 UserAuth (org.opennms.netmgt.config.javamail.UserAuth)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 MimeMessage (javax.mail.internet.MimeMessage)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 JavaReadMailer (org.opennms.javamail.JavaReadMailer)1 JavaSendMailer (org.opennms.javamail.JavaSendMailer)1 Parameter (org.opennms.netmgt.config.ackd.Parameter)1 ReadmailProtocol (org.opennms.netmgt.config.javamail.ReadmailProtocol)1 SendmailMessage (org.opennms.netmgt.config.javamail.SendmailMessage)1 OnmsAcknowledgment (org.opennms.netmgt.model.OnmsAcknowledgment)1