Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace response body in WSO2 ESB 4.8.1 Custom Handler

Tags:

esb

wso2

wso2-esb

I'm doing some validation in custom handler and in case of error sending response back to client from custom handler. However original payload of the client request is also sent back to client. How body can be emptied from response message ? If I debug SoapBody it seems to be empty but still somehow original payload is sent back to client.

Here's my function for sending message back to user.

private void myAuthErrorHandler(MessageContext msgCtx)
{
    Axis2MessageContext axis2smc = (Axis2MessageContext) msgCtx;
    org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();

    SOAPBody body = msgCtx.getEnvelope().getBody();

    // remove the existing payload
    for (Iterator itr = body.getChildElements(); itr.hasNext();) {
        OMElement child = (OMElement) itr.next();
        child.detach();
    }

    log.error(""+msgCtx.getEnvelope());
    axis2MessageCtx.setProperty("HTTP_SC", "403");
    axis2MessageCtx.setProperty("NO_ENTITY_BODY", new Boolean("true"));
    axis2MessageCtx.setProperty("RESPONSE", "true");
    axis2MessageCtx.setTo(null);
    Axis2Sender.sendBack(msgCtx); 
}

log.error(""+msgCtx.getEnvelope()); looks to be empty but still original body (payload) is sent back to client.

<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body></soapenv:Body></soapenv:Envelope> 

Is this possibly a bug or why original body is sent back to client ? Is there's any other methods to clear response body ?

Thanks for any help.

like image 987
kleht8 Avatar asked Jun 16 '14 08:06

kleht8


1 Answers

To drop the message body, you can use a property mediator in the sequence as follows:

<property name="NO_ENTITY_BODY" value="true" scope="axis2" type="BOOLEAN"/> 

Alternatively you can use a script mediator as well.

<script language="js"><![CDATA[mc.getEnvelope().getBody().getFirstElement().detach();]]></script> 

If both doesn't work, check whether your configuration has a log mediator. (<log level="full"/>). If it's there, try removing it. (In some older ESB versions, there was a bug where when you add a log mediator with level=full, response body doesn't get dropped coz it causes the body to be rebuilt for logging. But AFAIK, it was fixed in 4.8.x versions...so this may not be the case for your issue...)

like image 132
Rajeev Sampath Avatar answered Oct 31 '22 22:10

Rajeev Sampath