Search in sources :

Example 1 with UserAuth

use of org.opennms.netmgt.config.javamail.UserAuth 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 2 with UserAuth

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

the class JavaMailAckReaderIT method updateConfigWithGoogleReadConfiguration.

private void updateConfigWithGoogleReadConfiguration(ReadmailConfig config, String gmailAccount, String gmailPassword) {
    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);
}
Also used : ReadmailProtocol(org.opennms.netmgt.config.javamail.ReadmailProtocol) ReadmailHost(org.opennms.netmgt.config.javamail.ReadmailHost) UserAuth(org.opennms.netmgt.config.javamail.UserAuth)

Example 3 with UserAuth

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

the class JavaSendMailer method send.

/**
     * Send.
     *
     * @param message the message
     * @throws JavaMailerException the java mailer exception
     */
public void send(MimeMessage message) throws JavaMailerException {
    Transport t = null;
    if (m_config.getSendmailProtocol() == null || m_config.getSendmailHost() == null) {
        throw new JavaMailerException("sendmail-protocol or sendmail-host are not configured!");
    }
    try {
        SendmailProtocol protoConfig = m_config.getSendmailProtocol();
        t = m_session.getTransport(protoConfig.getTransport());
        LOG.debug("for transport name '{}' got: {}@{}", protoConfig.getTransport(), t.getClass().getName(), Integer.toHexString(t.hashCode()));
        LoggingTransportListener listener = new LoggingTransportListener();
        t.addTransportListener(listener);
        if ("mta".equals(t.getURLName().getProtocol())) {
            // JMTA throws an AuthenticationFailedException if we call connect()
            LOG.debug("transport is 'mta', not trying to connect()");
        } else {
            final SendmailHost sendmailHost = m_config.getSendmailHost();
            if (m_config.isUseAuthentication() && m_config.getUserAuth() != null) {
                LOG.debug("authenticating to {}", sendmailHost.getHost());
                final UserAuth userAuth = m_config.getUserAuth();
                t.connect(sendmailHost.getHost(), sendmailHost.getPort(), userAuth.getUserName(), userAuth.getPassword());
            } else {
                LOG.debug("not authenticating to {}", sendmailHost.getHost());
                t.connect(sendmailHost.getHost(), sendmailHost.getPort(), null, null);
            }
        }
        t.sendMessage(message, message.getAllRecipients());
        listener.assertAllMessagesDelivered();
    } catch (NoSuchProviderException e) {
        LOG.error("Couldn't get a transport: {}", e, e);
        throw new JavaMailerException("Couldn't get a transport: " + e, e);
    } catch (MessagingException e) {
        LOG.error("Java Mailer messaging exception: {}", e, e);
        throw new JavaMailerException("Java Mailer messaging exception: " + e, e);
    } finally {
        try {
            if (t != null && t.isConnected()) {
                t.close();
            }
        } catch (MessagingException e) {
            throw new JavaMailerException("Java Mailer messaging exception on transport close: " + e, e);
        }
    }
}
Also used : SendmailProtocol(org.opennms.netmgt.config.javamail.SendmailProtocol) MessagingException(javax.mail.MessagingException) Transport(javax.mail.Transport) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) NoSuchProviderException(javax.mail.NoSuchProviderException) SendmailHost(org.opennms.netmgt.config.javamail.SendmailHost)

Example 4 with UserAuth

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

the class JavaReadMailer method retrieveMessages.

/*
     * TODO: Need readers that:
     *   - use FetchProfiles
     *   - make use of pre-fetch nature of getMessages()
     *   - A reader for the entire system could be implemented using events/event listeners
     * TODO: Need to make this more efficient... probably needs state so that message contents can be retrieved from the
     *       store after they are read via this message.
     */
/**
     * <p>retrieveMessages</p>
     *
     * @param term a {@link javax.mail.search.SearchTerm} object.
     * @return a {@link java.util.List} object.
     * @throws org.opennms.javamail.JavaMailerException if any.
     */
public List<Message> retrieveMessages(SearchTerm term) throws JavaMailerException {
    Message[] msgs;
    Folder mailFolder = null;
    final ReadmailHost readmailHost = getReadmailHost(m_config);
    final UserAuth userAuth = getUserAuth(m_config);
    try {
        Store store = m_session.getStore(readmailHost.getReadmailProtocol().getTransport());
        store.connect(readmailHost.getHost(), (int) readmailHost.getPort(), userAuth.getUserName(), userAuth.getPassword());
        mailFolder = store.getFolder(m_config.getMailFolder());
        mailFolder.open(Folder.READ_WRITE);
        msgs = mailFolder.search(term);
    } catch (NoSuchProviderException e) {
        throw new JavaMailerException("No provider matching:" + readmailHost.getReadmailProtocol().getTransport() + " from config:" + m_config.getName(), e);
    } catch (MessagingException e) {
        throw new JavaMailerException("Problem reading messages from configured mail store", e);
    }
    List<Message> msgList = Arrays.asList(msgs);
    return msgList;
}
Also used : Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) Store(javax.mail.Store) ReadmailHost(org.opennms.netmgt.config.javamail.ReadmailHost) Folder(javax.mail.Folder) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) NoSuchProviderException(javax.mail.NoSuchProviderException)

Example 5 with UserAuth

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

the class JavaMailAckReaderIT method createSendMailer.

private JavaSendMailer createSendMailer(String gmailAccount, String gmailPassword) throws JavaMailerException {
    SendmailConfig config = new SendmailConfig();
    config.setAttemptInterval(1000l);
    config.setDebug(true);
    config.setName("test");
    SendmailMessage sendmailMessage = new SendmailMessage();
    sendmailMessage.setBody("service is down");
    sendmailMessage.setFrom("bamboo.opennms@gmail.com");
    sendmailMessage.setSubject("Notice #1234: service down");
    sendmailMessage.setTo("bamboo.opennms@gmail.com");
    config.setSendmailMessage(sendmailMessage);
    SendmailHost host = new SendmailHost();
    host.setHost("smtp.gmail.com");
    host.setPort(465);
    config.setSendmailHost(host);
    SendmailProtocol protocol = new SendmailProtocol();
    protocol.setSslEnable(true);
    protocol.setTransport("smtps");
    config.setSendmailProtocol(protocol);
    config.setUseAuthentication(true);
    config.setUseJmta(false);
    UserAuth auth = new UserAuth();
    auth.setUserName(gmailAccount);
    auth.setPassword(gmailPassword);
    config.setUserAuth(auth);
    return new JavaSendMailer(config);
}
Also used : SendmailProtocol(org.opennms.netmgt.config.javamail.SendmailProtocol) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) JavaSendMailer(org.opennms.javamail.JavaSendMailer) SendmailConfig(org.opennms.netmgt.config.javamail.SendmailConfig) UserAuth(org.opennms.netmgt.config.javamail.UserAuth) SendmailHost(org.opennms.netmgt.config.javamail.SendmailHost)

Aggregations

UserAuth (org.opennms.netmgt.config.javamail.UserAuth)10 ReadmailHost (org.opennms.netmgt.config.javamail.ReadmailHost)6 SendmailHost (org.opennms.netmgt.config.javamail.SendmailHost)4 SendmailProtocol (org.opennms.netmgt.config.javamail.SendmailProtocol)4 Message (javax.mail.Message)3 MessagingException (javax.mail.MessagingException)3 NoSuchProviderException (javax.mail.NoSuchProviderException)3 SendmailMessage (org.opennms.netmgt.config.javamail.SendmailMessage)3 Properties (java.util.Properties)2 Folder (javax.mail.Folder)2 ReadmailConfig (org.opennms.netmgt.config.javamail.ReadmailConfig)2 ReadmailProtocol (org.opennms.netmgt.config.javamail.ReadmailProtocol)2 SendmailConfig (org.opennms.netmgt.config.javamail.SendmailConfig)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Store (javax.mail.Store)1 Transport (javax.mail.Transport)1 JavaReadMailer (org.opennms.javamail.JavaReadMailer)1