@Test public void testNoRecipients() { Notification notification = new Notification(); DispatchCallback callback = EasyMock.createMock(DispatchCallback.class); notification.Callback = callback; List<String> callbackIds = new ArrayList<String>(); callbackIds.add(UUID.randomUUID().toString()); notification.CallbackIds = callbackIds; callback.onFailure(callbackIds); EasyMock.expectLastCall(); EasyMock.replay(callback); NotificationDispatcher dispatcher = m_dispatchFactory.getDispatcher(TargetType.EMAIL.name()); dispatcher.dispatch(notification); EasyMock.verify(callback); }
@Override public void dispatch(Notification notification) { LOG.info("Sending email: {}", notification); if (null == notification.DispatchProperties) { LOG.error("Unable to dispatch an email notification that does not contain SMTP properties"); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } return; } String fromAddress = null; Properties properties = new Properties(); for (Entry<String, String> entry : notification.DispatchProperties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); properties.put(key, value); if (key.equals(JAVAMAIL_FROM_PROPERTY)) { fromAddress = value; } } if (null == notification.Recipients) { LOG.error("Unable to dispatch an email notification that does not have recipients"); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } return; } final Session session; EmailAuthenticator authenticator = null; if (null != notification.Credentials) { authenticator = new EmailAuthenticator(notification.Credentials); } session = Session.getInstance(properties, authenticator); try { MimeMessage message = new MimeMessage(session); for (Recipient recipient : notification.Recipients) { InternetAddress address = new InternetAddress(recipient.Identifier); message.addRecipient(RecipientType.TO, address); } message.setSentDate(new Date()); message.setSubject(notification.Subject); message.setText(notification.Body, "UTF-8", "html"); if (null != fromAddress) { message.setFrom(fromAddress); } Transport.send(message); if (LOG.isDebugEnabled()) { LOG.debug("Successfully dispatched email to {}", notification.Recipients); } if (null != notification.Callback) { notification.Callback.onSuccess(notification.CallbackIds); } } catch (Exception exception) { LOG.error("Unable to dispatch notification via Email", exception); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } } finally { try { session.getTransport().close(); } catch (MessagingException me) { LOG.warn("Dispatcher unable to close SMTP transport", me); } } }
EmailDispatcher implements NotificationDispatcher { @Override public void dispatch(Notification notification) { LOG.info("Sending email: {}", notification); if (null == notification.DispatchProperties) { LOG.error("Unable to dispatch an email notification that does not contain SMTP properties"); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } return; } String fromAddress = null; Properties properties = new Properties(); for (Entry<String, String> entry : notification.DispatchProperties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); properties.put(key, value); if (key.equals(JAVAMAIL_FROM_PROPERTY)) { fromAddress = value; } } if (null == notification.Recipients) { LOG.error("Unable to dispatch an email notification that does not have recipients"); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } return; } final Session session; EmailAuthenticator authenticator = null; if (null != notification.Credentials) { authenticator = new EmailAuthenticator(notification.Credentials); } session = Session.getInstance(properties, authenticator); try { MimeMessage message = new MimeMessage(session); for (Recipient recipient : notification.Recipients) { InternetAddress address = new InternetAddress(recipient.Identifier); message.addRecipient(RecipientType.TO, address); } message.setSentDate(new Date()); message.setSubject(notification.Subject); message.setText(notification.Body, "UTF-8", "html"); if (null != fromAddress) { message.setFrom(fromAddress); } Transport.send(message); if (LOG.isDebugEnabled()) { LOG.debug("Successfully dispatched email to {}", notification.Recipients); } if (null != notification.Callback) { notification.Callback.onSuccess(notification.CallbackIds); } } catch (Exception exception) { LOG.error("Unable to dispatch notification via Email", exception); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } } finally { try { session.getTransport().close(); } catch (MessagingException me) { LOG.warn("Dispatcher unable to close SMTP transport", me); } } } }
EmailDispatcher implements NotificationDispatcher { @Override public void dispatch(Notification notification) { LOG.info("Sending email: {}", notification); if (null == notification.DispatchProperties) { LOG.error("Unable to dispatch an email notification that does not contain SMTP properties"); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } return; } String fromAddress = null; Properties properties = new Properties(); for (Entry<String, String> entry : notification.DispatchProperties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); properties.put(key, value); if (key.equals(JAVAMAIL_FROM_PROPERTY)) { fromAddress = value; } } if (null == notification.Recipients) { LOG.error("Unable to dispatch an email notification that does not have recipients"); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } return; } final Session session; EmailAuthenticator authenticator = null; if (null != notification.Credentials) { authenticator = new EmailAuthenticator(notification.Credentials); } session = Session.getInstance(properties, authenticator); try { MimeMessage message = new MimeMessage(session); for (Recipient recipient : notification.Recipients) { InternetAddress address = new InternetAddress(recipient.Identifier); message.addRecipient(RecipientType.TO, address); } message.setSentDate(new Date()); message.setSubject(notification.Subject); message.setText(notification.Body, "UTF-8", "html"); if (null != fromAddress) { message.setFrom(fromAddress); } Transport.send(message); if (LOG.isDebugEnabled()) { LOG.debug("Successfully dispatched email to {}", notification.Recipients); } if (null != notification.Callback) { notification.Callback.onSuccess(notification.CallbackIds); } } catch (Exception exception) { LOG.error("Unable to dispatch notification via Email", exception); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } } finally { try { session.getTransport().close(); } catch (MessagingException me) { LOG.warn("Dispatcher unable to close SMTP transport", me); } } }  }
EmailDispatcher implements NotificationDispatcher { @Override public void dispatch(Notification notification) { LOG.info("Sending email: {}", notification); if (null == notification.DispatchProperties) { LOG.error("Unable to dispatch an email notification that does not contain SMTP properties"); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } return; } String fromAddress = null; Properties properties = new Properties(); for (Entry<String, String> entry : notification.DispatchProperties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); properties.put(key, value); if (key.equals(JAVAMAIL_FROM_PROPERTY)) { fromAddress = value; } } if (null == notification.Recipients) { LOG.error("Unable to dispatch an email notification that does not have recipients"); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } return; } final Session session; EmailAuthenticator authenticator = null; if (null != notification.Credentials) { authenticator = new EmailAuthenticator(notification.Credentials); } session = Session.getInstance(properties, authenticator); try { MimeMessage message = new MimeMessage(session); for (Recipient recipient : notification.Recipients) { InternetAddress address = new InternetAddress(recipient.Identifier); message.addRecipient(RecipientType.TO, address); } message.setSentDate(new Date()); message.setSubject(notification.Subject); message.setText(notification.Body, "UTF-8", "html"); if (null != fromAddress) { message.setFrom(fromAddress); } Transport.send(message); if (LOG.isDebugEnabled()) { LOG.debug("Successfully dispatched email to {}", notification.Recipients); } if (null != notification.Callback) { notification.Callback.onSuccess(notification.CallbackIds); } } catch (Exception exception) { LOG.error("Unable to dispatch notification via Email", exception); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } } finally { try { session.getTransport().close(); } catch (MessagingException me) { LOG.warn("Dispatcher unable to close SMTP transport", me); } } }  @Override String getType(); @Override boolean isNotificationContentGenerationRequired(); @Override void dispatch(Notification notification); @Override boolean isDigestSupported(); @Override TargetConfigurationResult validateTargetConfig(Map<String, Object> properties); }
EmailDispatcher implements NotificationDispatcher { @Override public void dispatch(Notification notification) { LOG.info("Sending email: {}", notification); if (null == notification.DispatchProperties) { LOG.error("Unable to dispatch an email notification that does not contain SMTP properties"); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } return; } String fromAddress = null; Properties properties = new Properties(); for (Entry<String, String> entry : notification.DispatchProperties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); properties.put(key, value); if (key.equals(JAVAMAIL_FROM_PROPERTY)) { fromAddress = value; } } if (null == notification.Recipients) { LOG.error("Unable to dispatch an email notification that does not have recipients"); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } return; } final Session session; EmailAuthenticator authenticator = null; if (null != notification.Credentials) { authenticator = new EmailAuthenticator(notification.Credentials); } session = Session.getInstance(properties, authenticator); try { MimeMessage message = new MimeMessage(session); for (Recipient recipient : notification.Recipients) { InternetAddress address = new InternetAddress(recipient.Identifier); message.addRecipient(RecipientType.TO, address); } message.setSentDate(new Date()); message.setSubject(notification.Subject); message.setText(notification.Body, "UTF-8", "html"); if (null != fromAddress) { message.setFrom(fromAddress); } Transport.send(message); if (LOG.isDebugEnabled()) { LOG.debug("Successfully dispatched email to {}", notification.Recipients); } if (null != notification.Callback) { notification.Callback.onSuccess(notification.CallbackIds); } } catch (Exception exception) { LOG.error("Unable to dispatch notification via Email", exception); if (null != notification.Callback) { notification.Callback.onFailure(notification.CallbackIds); } } finally { try { session.getTransport().close(); } catch (MessagingException me) { LOG.warn("Dispatcher unable to close SMTP transport", me); } } }  @Override String getType(); @Override boolean isNotificationContentGenerationRequired(); @Override void dispatch(Notification notification); @Override boolean isDigestSupported(); @Override TargetConfigurationResult validateTargetConfig(Map<String, Object> properties); static final String JAVAMAIL_FROM_PROPERTY; }