log.debug("Number of user notifications: " + l.size());
}
Iterator<UserNotification> i = l.iterator();
String rcptemail = useremail; // Default value
while (i.hasNext()) {
UserNotification not = i.next();
Collection<String> events = not.getNotificationEventsCollection();
if (events.contains(String.valueOf(newstatus))) {
if (log.isDebugEnabled()) {
log.debug("Status is " + newstatus + ", notification sent for notificationevents: " + not.getNotificationEvents());
}
try {
if (StringUtils.equals(not.getNotificationRecipient(), UserNotification.RCPT_USER)) {
rcptemail = useremail;
} else if (StringUtils.contains(not.getNotificationRecipient(), UserNotification.RCPT_CUSTOM)) {
rcptemail = "custom"; // Just if this fail it will
// say that sending to user
// with email "custom" failed.
// Plug-in mechanism for retrieving custom
// notification email recipient addresses
if (not.getNotificationRecipient().length() < 6) {
String msg = intres.getLocalizedMessage("ra.errorcustomrcptshort", not.getNotificationRecipient());
log.error(msg);
} else {
String cp = not.getNotificationRecipient().substring(7);
if (StringUtils.isNotEmpty(cp)) {
ICustomNotificationRecipient plugin = (ICustomNotificationRecipient) Thread.currentThread().getContextClassLoader()
.loadClass(cp).newInstance();
rcptemail = plugin.getRecipientEmails(data);
if (StringUtils.isEmpty(rcptemail)) {
String msg = intres.getLocalizedMessage("ra.errorcustomnoemail", not.getNotificationRecipient());
log.error(msg);
} else {
if (log.isDebugEnabled()) {
log.debug("Custom notification recipient plugin returned email: " + rcptemail);
}
}
} else {
String msg = intres.getLocalizedMessage("ra.errorcustomnoclasspath", not.getNotificationRecipient());
log.error(msg);
}
}
} else {
// Just a plain email address specified in the
// recipient field
rcptemail = not.getNotificationRecipient();
}
if (StringUtils.isEmpty(rcptemail)) {
String msg = intres.getLocalizedMessage("ra.errornotificationnoemail", data.getUsername());
throw new Exception(msg);
}
// Get the administrators DN from the admin certificate,
// if one exists
// When approvals is used, this will be the DN of the
// admin that approves the request
Certificate adminCert = admin.getAdminInformation().getX509Certificate();
String approvalAdminDN = CertTools.getSubjectDN(adminCert);
if (log.isDebugEnabled()) {
log.debug("approvalAdminDN: " + approvalAdminDN);
}
UserNotificationParamGen paramGen = new UserNotificationParamGen(data, approvalAdminDN, findUser(admin, admin.getUsername()));
/*
* substitute any $ fields in the receipient and from
* fields
*/
rcptemail = paramGen.interpolate(rcptemail);
String fromemail = paramGen.interpolate(not.getNotificationSender());
String subject = paramGen.interpolate(not.getNotificationSubject());
String message = paramGen.interpolate(not.getNotificationMessage());
MailSender.sendMailOrThrow(fromemail, Arrays.asList(rcptemail), MailSender.NO_CC, subject, message, MailSender.NO_ATTACHMENTS);
String logmsg = intres.getLocalizedMessage("ra.sentnotification", data.getUsername(), rcptemail);
logSession.log(admin, data.getCAId(), LogConstants.MODULE_RA, new Date(), data.getUsername(), null,
LogConstants.EVENT_INFO_NOTIFICATION, logmsg);
} catch (Exception e) {
String msg = intres.getLocalizedMessage("ra.errorsendnotification", data.getUsername(), rcptemail);
log.error(msg, e);
try {
logSession.log(admin, data.getCAId(), LogConstants.MODULE_RA, new Date(), data.getUsername(), null,
LogConstants.EVENT_ERROR_NOTIFICATION, msg);
} catch (Exception f) {
throw new EJBException(f);
}
}
} else { // if (events.contains(String.valueOf(newstatus)))
if (log.isDebugEnabled()) {
log.debug("Status is " + newstatus + ", no notification sent for notificationevents: " + not.getNotificationEvents());
}
}
}
} else { // if ( ((data.getType() & SecConst.USER_SENDNOTIFICATION) !=
// 0) )