How to replace response body in WSO2 ESB 4.8.1 Custom Handler
Asked Answered
D

1

5

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.

Davie answered 16/6, 2014 at 8:51 Comment(0)
L
7

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...)

Luxemburg answered 17/6, 2014 at 2:34 Comment(1)
For me worked the script mediator alternative in WSO2 ESB 4.8.1.Becalm

© 2022 - 2024 — McMap. All rights reserved.