How to access payload content from custom handler in WSO2 APIM

While using WSO2 APIM you might want to browse the content of the payload that coming APIM side. Therefore I will explain the code that can be used to this scenario line by line and include the whole code at the end of the document.

First to access the payload content you need to create a new handler or modify exiting handler. For demonstration and for ease of use I will use APIManagerExtensionHandler class.

Now Identify the method which get MessageContext as a argument. In this example it's mediate().

From that method call a private method call getMessageBody().

public boolean mediate(MessageContext messageContext, String direction) {

    getMessageBody(messageContext);

    // In order to avoid a remote registry call occurring on each invocation, we    // directly get the extension sequences from the local registry.    Map localRegistry = messageContext.getConfiguration().getLocalRegistry();

    Object sequence = localRegistry.get(EXT_SEQUENCE_PREFIX + direction);
    if (sequence != null && sequence instanceof Mediator) {
        if (!((Mediator) sequence).mediate(messageContext)) {
            return false;
        }
    }

    String apiName = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API);
    sequence = localRegistry.get(apiName + "--" + direction);
    if (sequence != null && sequence instanceof Mediator) {
        return ((Mediator) sequence).mediate(messageContext);
    }
    return true;
}

Then create a private method call getMessageBody() which takes MessageContext as a argument.

private void getMessageBody(MessageContext synCtx) {

In the new method first you need convert MessageContext to Axis2MessageContext.

org.apache.axis2.context.MessageContext axisCtx = ((Axis2MessageContext)synCtx).getAxis2MessageContext();

Now you need to build the message to retrieve the content of the payload. For that we are going to use RelayUtils. Sometimes you might need to import this component therefore please find the dependacy tag to be add in the pom.xml and other related imports.

Note: I have added below dependancy to relevant pom (org.wso2.carbon.apimgt.gateway/1.2.1/pom.xml) 

<dependency>
<groupId>org.apache.synapse</groupId>
<artifactId>synapse-nhttp-transport</artifactId>
<version>2.1.2-wso2v4</version>
</dependency>

import org.apache.synapse.transport.passthru.util.RelayUtils;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;

Lets build the message.

try{
    RelayUtils.buildMessage(axisCtx);
}
catch (IOException ex){
    log.error("Error occurred while building the message", ex);
} catch (XMLStreamException ex) {
    log.error("Error occurred while building the message", ex);
}

That's it now you can browse the content by using axisCtx.getEnvelope().

log.info(axisCtx.getEnvelope());

Kindly find the full code from below.


private void getMessageBody(MessageContext synCtx) {
    org.apache.axis2.context.MessageContext axisCtx = ((Axis2MessageContext)synCtx).getAxis2MessageContext();
    try{
        RelayUtils.buildMessage(axisCtx);
    }
    catch (IOException ex){
        log.error("Error occurred while building the message", ex);
    } catch (XMLStreamException ex) {
        log.error("Error occurred while building the message", ex);
    }
    log.info(axisCtx.getEnvelope());
}

public boolean mediate(MessageContext messageContext, String direction) {

    getMessageBody(messageContext);
    // In order to avoid a remote registry call occurring on each invocation, we    // directly get the extension sequences from the local registry.    Map localRegistry = messageContext.getConfiguration().getLocalRegistry();

    Object sequence = localRegistry.get(EXT_SEQUENCE_PREFIX + direction);
    if (sequence != null && sequence instanceof Mediator) {
        if (!((Mediator) sequence).mediate(messageContext)) {
            return false;
        }
    }

    String apiName = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API);
    sequence = localRegistry.get(apiName + "--" + direction);
    if (sequence != null && sequence instanceof Mediator) {
        return ((Mediator) sequence).mediate(messageContext);
    }
    return true;
}


Reference
http://stackoverflow.com/questions/24239813/how-to-replace-response-body-in-wso2-esb-4-8-1-custom-handler


Comments

  1. Incredible! This blog looks exactly like my old one! It's on a entirely different topic but it has pretty much the same page layout and design. Superb choice of colors! How To Insulate A Shipping Container


    ReplyDelete

Post a Comment