Skip to content

Commit

Permalink
Merge pull request #9 from tsegismont/jira/HWKALERTS-3
Browse files Browse the repository at this point in the history
HWKALERTS-3 Implement an Email notification sender
  • Loading branch information
jshaughn committed Feb 11, 2015
2 parents 2e41d0c + 335b738 commit fbd1831
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@
*/
package org.hawkular.notifiers.email.notifications;

import org.hawkular.bus.common.consumer.BasicMessageListener;
import org.hawkular.notifiers.api.log.MsgLogger;
import org.hawkular.notifiers.api.model.NotificationMessage;
import org.jboss.logging.Logger;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.MessageListener;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.hawkular.bus.common.consumer.BasicMessageListener;
import org.hawkular.notifiers.api.log.MsgLogger;
import org.hawkular.notifiers.api.model.NotificationMessage;

/**
* An example of listener for emails processing.
Expand All @@ -37,9 +45,26 @@
@ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "NotifierType like 'email'")})
public class EmailListener extends BasicMessageListener<NotificationMessage> {
private final MsgLogger msgLog = MsgLogger.LOGGER;
private final Logger log = Logger.getLogger(EmailListener.class);

@Resource(mappedName = "java:jboss/mail/Default")
Session mailSession;

protected void onBasicMessage(NotificationMessage msg) {
msgLog.infoNotificationReceived("Email", msg.toString());
try {
Message message = createMimeMessage(msg);
Transport.send(message);
} catch (MessagingException e) {
msgLog.errorCannotSendMessage("email", e.getLocalizedMessage());
}
}

Message createMimeMessage(NotificationMessage msg) throws MessagingException {
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("noreply@hawkular"));
Address toAddress = new InternetAddress(msg.getNotifierId());
message.addRecipient(RecipientType.TO, toAddress);
message.setSubject("Hawkular alert");
message.setContent(msg.getMessage(), "text/plain");
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.notifiers.email.notifications;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Message.RecipientType;

import org.hawkular.notifiers.api.model.NotificationMessage;
import org.junit.Test;

/**
* @author Thomas Segismont
*/
public class EmailListenerTest {

private EmailListener emailListener = new EmailListener();

@Test
public void testMessageContent() throws Exception {
String recipient = "root@localhost";
String alertContent = "marseille";

NotificationMessage notificationMessage = new NotificationMessage();
notificationMessage.setNotifierId(recipient);
notificationMessage.setMessage(alertContent);

Message mimeMessage = emailListener.createMimeMessage(notificationMessage);

Address[] from = mimeMessage.getFrom();
assertNotNull(from);
assertEquals(1, from.length);
assertEquals("noreply@hawkular", from[0].toString());

Address[] recipients = mimeMessage.getRecipients(RecipientType.TO);
assertNotNull(recipients);
assertEquals(1, recipients.length);
assertEquals(recipient, recipients[0].toString());

assertEquals("Hawkular alert", mimeMessage.getSubject());

assertEquals("text/plain", mimeMessage.getContentType());
assertEquals(alertContent, mimeMessage.getContent());
}
}

0 comments on commit fbd1831

Please sign in to comment.