Sunday, December 18, 2011

Customizing the email subject for RegistryEvents in WSO2 Carbon

Recently I used org.wso2.carbon.registry.common.eventing.RegistryEvent in WSO2 G-Reg 4.1.0 (based on Carbon 3.2.2) to generate custom email notifications based on different events. Though it was possible to customize the email body, it was not possible to customize the email subject due to a missing feature. In the upcoming Carbon releases this feature will be available and you can set the subject as a parameter with the key "MailConstants.MAIL_HEADER_SUBJECT" for the event as follows.

import org.wso2.carbon.registry.common.eventing.NotificationService;
import org.wso2.carbon.registry.core.jdbc.handlers.RequestContext;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.registry.eventing.events.ResourceUpdatedEvent;
import org.wso2.carbon.registry.common.eventing.RegistryEvent;
import org.apache.axis2.transport.mail.MailConstants;
public static void sendEmail(RequestContext requestContext){
String emailMsg = "You have subscribed to this event";
String emailSubject = "Custom Subject";
String subscriptionPath = requestContext.getResourcePath().getPath();
UserRegistry registry = null;
try {
registry = (UserRegistry) requestContext.getSystemRegistry();
RegistryEvent<String> resourceUpdatedEvent = new ResourceUpdatedEvent<String>(emailMsg);
resourceUpdatedEvent.setParameter(MailConstants.MAIL_HEADER_SUBJECT, emailSubject);
resourceUpdatedEvent.setResourcePath(subscriptionPath);
if (registry != null) {
resourceUpdatedEvent.setTenantId(registry.getTenantId());
}
NotificationService.notify(resourceUpdatedEvent, requestContext.getSystemRegistry(),
requestContext.getResourcePath().getPath());
} catch (Exception e) {
e.printStackTrace();
}
}

No comments: