List<NotificationMessageTemplateElement>> templates = templateMap;
// First, see if the notification type is one that we handle. If not, then
// return without doing anything.
AccountStatusNotificationType notificationType =
notification.getNotificationType();
List<NotificationMessageTemplateElement> templateElements =
templates.get(notificationType);
if (templateElements == null)
{
if (debugEnabled())
{
TRACER.debugInfo("No message template for notification type " +
notificationType.getName());
}
return;
}
// It is a notification that should be handled, so we can start generating
// the e-mail message. First, check to see if there are any mail attributes
// that would cause us to send a message to the end user.
LinkedList<String> recipients = new LinkedList<String>();
Set<AttributeType> addressAttrs = config.getEmailAddressAttributeType();
Set<String> recipientAddrs = config.getRecipientAddress();
if ((addressAttrs != null) && (! addressAttrs.isEmpty()))
{
Entry userEntry = notification.getUserEntry();
for (AttributeType t : addressAttrs)
{
List<Attribute> attrList = userEntry.getAttribute(t);
if (attrList != null)
{
for (Attribute a : attrList)
{
for (AttributeValue v : a)
{
if (debugEnabled())
{
TRACER.debugInfo("Adding end user recipient " +
v.getValue().toString() + " from attr " +
a.getNameWithOptions());
}
recipients.add(v.getValue().toString());
}
}
}
}
if (recipients.isEmpty())
{
if ((recipientAddrs == null) || recipientAddrs.isEmpty())
{
// There are no recipients at all, so there's no point in generating
// the message. Return without doing anything.
if (debugEnabled())
{
TRACER.debugInfo("No end user recipients, and no explicit " +
"recipients");
}
return;
}
else
{
if (! config.isSendMessageWithoutEndUserAddress())
{
// We can't send the message to the end user, and the handler is
// configured to not send only to administrators, so we shouln't
// do anything.
if (debugEnabled())
{
TRACER.debugInfo("No end user recipients, and shouldn't send " +
"without end user recipients");
}
return;
}
}
}
}
// Next, add any explicitly-defined recipients.
if (recipientAddrs != null)
{
if (debugEnabled())
{
for (String s : recipientAddrs)
{
TRACER.debugInfo("Adding explicit recipient " + s);
}
}
recipients.addAll(recipientAddrs);
}
// Get the message subject to use. If none is defined, then use a generic
// subject.
String subject = subjects.get(notificationType);
if (subject == null)
{
subject = INFO_SMTP_ASNH_DEFAULT_SUBJECT.get().toString();
if (debugEnabled())
{
TRACER.debugInfo("Using default subject of " + subject);
}
}
else if (debugEnabled())
{
TRACER.debugInfo("Using per-type subject of " + subject);
}
// Generate the message body.
MessageBuilder messageBody = new MessageBuilder();
for (NotificationMessageTemplateElement e : templateElements)
{
e.generateValue(messageBody, notification);
}
// Create and send the e-mail message.
EMailMessage message = new EMailMessage(config.getSenderAddress(),
recipients, subject);
message.setBody(messageBody);
if (debugEnabled())
{
TRACER.debugInfo("Set message body of " + messageBody.toString());
}
try
{
message.send();
if (debugEnabled())
{
TRACER.debugInfo("Successfully sent the message");
}
}
catch (Exception e)
{
if (debugEnabled())
{
TRACER.debugCaught(DebugLogLevel.ERROR, e);
}
logError(ERR_SMTP_ASNH_CANNOT_SEND_MESSAGE.get(notificationType.getName(),
notification.getUserDN().toString(),
getExceptionMessage(e)));
}
}