Wednesday, June 29, 2016

Adding LoadBalancer Endpoints in WSO2 Developer Studio 3.8.0

Wso2 Dev Studio can be used to create different carbon related projects, including ESB projects. https://docs.wso2.com/display/DVS380/Creating+ESB+Artifacts contains the basic information on how to add ESB artifacts. From the different endpoints we can add, Loadbalancer endpoint is one option that is available.

Most people are confused with the automatic folder that gets created when adding a LB endpoint in dev studio, called 'complex-endpoints'. You do not need to worry about this since this folder just contains some meta information for a scenario like below.

In a scenario where you need an LB endpoint inside another LB endpoint, suppose a user needs to add two address endpoints to inner LB endpoint . To open up the inner LB diagram in a graphical editor we needed a physical file in the workspace even though its an anonymous endpoint. Hence the creation of 'complex-endpoints' directory is to store these physical files that are automatically created , for anonymous endpoints like LB,Failover. These are metadata and will not be listed in any Composite application project's artifacts list.

Another issue most users complain is that though they have added LB endpoint, this does not appear in the tool pallet under 'Defined Endpoints'. This is probably because your ESB project's artifacts.xml does not have the entry for this endpoint. You could view this file through the file system.



Monday, June 20, 2016

Adding API name to JWT Token WSO2 API Manager 1.8.0

If we need to set additional claims to the JWT token, we need to implement a custom JWT token generator as explained in [1].  Assume you need to set the API Name to the JWT token. You could do it as below in your custom JWT Token generator.

public Map<String, String> populateCustomClaims(APIKeyValidationInfoDTO keyValidationInfoDTO, String apiContext, String version, String accessToken)
            throws APIManagementException {

        String apiName = keyValidationInfoDTO.getApiName();
        Map<String,String> map = new HashMap<String, String>();
        map.put("API_NAME", apiName);

        return map;
    }



This will set the API_NAME property with the value to the JWT token like below.

{"iss":"wso2.org/products/am","exp":1466431406496,"http://wso2.org/claims/subscriber":"admin","http://wso2.org/claims/applicationid":"2","http://wso2.org/claims/applicationname":"app1","http://wso2.org/claims/applicationtier":"Unlimited","http://wso2.org/claims/apicontext":"/custom","http://wso2.org/claims/version":"1.0","http://wso2.org/claims/tier":"Bronze","http://wso2.org/claims/keytype":"PRODUCTION","http://wso2.org/claims/usertype":"APPLICATION","http://wso2.org/claims/enduser":"null","http://wso2.org/claims/enduserTenantId":"null","API_NAME":"CustomAPI"};

[1] https://docs.wso2.com/display/AM180/Passing+Enduser+Attributes+to+the+Backend+Using+JWT

Wednesday, June 15, 2016

Creating a synapse handler with WSO2 ESB 4.9.0

There might be scenarios where you need to access data of a request/response at different points. For example you might want to perform some operation to the incoming request to ESB or you might want to access the payload that is sent from the Back End to ESB. So the perfect solution for this in ESB 4.9.0 is to write a synapse handler. [1] gives a very good explanation on how to implement and deploy this.


Following is a sample implementation of a synapse handler. Suppose your client is invoking an API artifact that is deployed in ESB, through API Manager. And suppose you need to get this client's IP address. Since we need to access the IP address of the client request that is coming into ESB, the method to access this property is 'handleRequestInFlow'. Now when you invoke an API artifact deployed in ESB, following logs will appear.

public class TestHandler extends AbstractSynapseHandler {


    static Logger log = Logger.getLogger(TestHandler.class.getName());


    protected void activate(ComponentContext ctxt) {
        try {
            log.debug("API test Handler");
        } catch (Throwable e) {
            log.error("Failed to activate API basic test Handler", e);
        }
    }

    @Override
    public boolean handleRequestInFlow(MessageContext synCtx) {
        log.info("Request In Flow");

        Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
        org.apache.axis2.context.MessageContext axis2MessageCtx =
                axis2smc.getAxis2MessageContext();
        log.info("Client IP is " + axis2MessageCtx.getProperty("REMOTE_ADDR"));

        return true;
    }

    @Override
    public boolean handleRequestOutFlow(MessageContext synCtx) {
 log.info("Request Out Flow");
return true;
}

    @Override
    public boolean handleResponseInFlow(MessageContext synCtx) {
 log.info("Response In Flow");
  return true;
}


    @Override
    public boolean handleResponseOutFlow(MessageContext synCtx) {
  log.info("Response Out Flow");
   return true;
}


}