Search in sources :

Example 1 with ReadmailHost

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

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

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

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

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

the class JavaReadMailer method configureProperties.

/**
     * Configures the java mail api properties based on the settings ReadMailConfig
     * @return A set of javamail properties based on the mail configuration 
     * @throws JavaMailerException 
     */
private Properties configureProperties() throws JavaMailerException {
    Properties props = new Properties();
    props.setProperty("mail.debug", String.valueOf(m_config.isDebug()));
    //first set the actual properties defined in the sendmail configuration
    List<JavamailProperty> jmps = m_config.getJavamailProperties();
    for (JavamailProperty jmp : jmps) {
        props.setProperty(jmp.getName(), jmp.getValue());
    }
    final ReadmailHost readmailHost = getReadmailHost(m_config);
    final UserAuth userAuth = getUserAuth(m_config);
    String protocol = readmailHost.getReadmailProtocol().getTransport();
    props.put("mail." + protocol + ".host", readmailHost.getHost());
    props.put("mail." + protocol + ".user", userAuth.getUserName());
    props.put("mail." + protocol + ".port", readmailHost.getPort());
    props.put("mail." + protocol + ".starttls.enable", readmailHost.getReadmailProtocol().isStartTls());
    props.put("mail.smtp.auth", "true");
    if (readmailHost.getReadmailProtocol().isSslEnable()) {
        props.put("mail." + protocol + ".socketFactory.port", readmailHost.getPort());
        props.put("mail." + protocol + ".socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail." + protocol + ".socketFactory.fallback", "false");
    }
    //FIXME: need config for these
    props.put("mail." + protocol + ".connectiontimeout", 3000);
    props.put("mail." + protocol + ".timeout", 3000);
    props.put("mail.store.protocol", protocol);
    return props;
}
Also used : JavamailProperty(org.opennms.netmgt.config.javamail.JavamailProperty) ReadmailHost(org.opennms.netmgt.config.javamail.ReadmailHost) Properties(java.util.Properties) UserAuth(org.opennms.netmgt.config.javamail.UserAuth)

Aggregations

ReadmailHost (org.opennms.netmgt.config.javamail.ReadmailHost)6 UserAuth (org.opennms.netmgt.config.javamail.UserAuth)6 Message (javax.mail.Message)3 Folder (javax.mail.Folder)2 MessagingException (javax.mail.MessagingException)2 NoSuchProviderException (javax.mail.NoSuchProviderException)2 ReadmailConfig (org.opennms.netmgt.config.javamail.ReadmailConfig)2 ReadmailProtocol (org.opennms.netmgt.config.javamail.ReadmailProtocol)2 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Store (javax.mail.Store)1 JavaReadMailer (org.opennms.javamail.JavaReadMailer)1 JavamailProperty (org.opennms.netmgt.config.javamail.JavamailProperty)1