Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

WSO2 Governance Registry generates notifications for events triggered by various operations performed on resources and collections stored in the repository. Notifications can be consumed in a variety of formats including E-mail. Read more about Notifications and also the API-Level Access provided for subscription operations. The contents of notification eE-mails can be customized by using an Axis2 Handler. WSO2 Governance Registry contains an Apache Axis2 based runtime as a part of the WSO2 Carbon Framework. We will be reusing the code of the Handler Sample in this example. This sample requires Apache Maven. See Installing Apache Maven for Governance Registry on Windows or Installing Apache Maven for Governance Registry on Linux.

This sample also includes two advanced use-cases which have been included at the bottom of this page.

Instructions

1. Navigate to GREG_HOME/samples/handler/src to find the source code of the Handler Sample.

...

This sample can be improved to restrict which users will receive an e-mail notification. For example, it might be required to get an approval from a particular include a clickable link in the E-mail body, making it possible for users to easily navigate to the resources in which changes have been made. The following modification can be made to the code segment to achieve this.

Code Block
languagejava
package org.wso2.carbon.registry.samples.notifications;
 
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.xpath.AXIOMXPath;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.axis2.transport.mail.MailConstants;
import org.jaxen.JaxenException;
 
import java.util.ArrayList;
import java.util.Map;
 
public class EmailTransformHandler extends AbstractHandler implements Handler {
    private String name;
 
    public String getName() {
        return name;
    }
 
    public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
        String address = msgContext.getTo().getAddress();
        if (msgContext.getTo() != null && address.startsWith("mailto:")) {
            try {
                SOAPEnvelope envelope = msgContext.getEnvelope();
                AXIOMXPath xPath = new AXIOMXPath("//ns:text");
                xPath.addNamespace("ns", "http://ws.apache.org/commons/ns/payload");
                OMElement element = (OMElement) ((ArrayList) xPath.evaluate(envelope)).get(0);
                fixPath(msgContext, element);
                element.setText(element.getText().replace("--", "This message was intercepted by " +
                        "EmailTransformHandler\n--"));
            } catch (JaxenException e) {
                e.printStackTrace();
            }
        }
        return InvocationResponse.CONTINUE;
    }
 
    private void fixPath(MessageContext msgContext, OMElement element) {
        String subject = ((Map<String, String>) msgContext.getOptions().getProperty(
                MessageContext.TRANSPORT_HEADERS)).get(MailConstants.MAIL_HEADER_SUBJECT);
        if (!subject.contains("Updated")) {
            return;
        }
        String path = element.getText();
        path = path.substring(path.indexOf("path ") + "path ".length());
        path = path.substring(0, path.indexOf(" was updated"));
        element.setText(element.getText().replace("path " + path, "path https://localhost:9443/" +
                "carbon/resources/resource.jsp?region=region3&item=resource_browser_menu&path=" +
                path + "&screenWidth=1600"));
    }
 
    public void revoke(MessageContext msgContext) {
    }
 
    public void setName(String name) {
        this.name = name;
    }
}

Advanced Use-case 2

This sample can be improved to restrict which users will receive an E-mail notification. For example, it might be required to get an approval from a particular group of users before moving from one state to another. However, this eE-mail might not make sense to be delivered to John Smith or Joe Blogg. The following modification can be made to the code segment to restrict the recipients such that John Doe will receive this eE-mail though John Smith or Joe Blogg would not.

...

Info
titleNote

In this example we have used the default ServiceLifeCycle. We have made the restriction in the code above for the event triggered by checking of the Smoke Test Passed checklist item of the Testing state. We have assumed that the Promote button which takes you to the Production state will be restricted to the role to which John Doe belongs. Note that the Transition Permissions must be properly set.

We have also assumed that the Smoke Test Passed checklist item will be the last to be checked in the Testing state and that the immediate next step would be the promotion to the Production state. Because only some users belonging to a particular role (such as John Doe in this case) will get the option to promote, it is required to keep the corresponding users informed, but not spam the others. This can be done by subscribing all users and blacklisting the ones who do not require receiving the eE-mails. See below to learn how to subscribe all users for the Check LC Item event.

For this to work, each user on Governance Registry should fill out their User Profile.

...