WSO2 Custom Mediator Set value for the Envelope

353 Views Asked by At

Deal All,

I'd create one custom mediator in my WSO2 ESB project at the OutSequence.

I would like to change the result in SOAP Envelope being send from the back end to the consumer. But with a little bit of adjustment to the data according to the result.

this is the SOAP Envelope

<soapenv:Bodyxmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<jsonObject>
    <serviceRequestID>12345</serviceRequestID>
    <statusCode>1</statusCode>
    <errorCode></errorCode>
    <errorDescription></errorDescription>
    <addressID>1.23456794E9</addressID>
    <source>consumer name</source>
    <requestId>910514</requestId>
</jsonObject>

I want to create a custom mediator to set the Error Description base on the Error Code.

For example,

if error code 1 = error description = one

if error code 2 = error description = two

if error code 3 = error description = three

This is my custom mediator code.

private void ResponseLogging(MessageContext mc) throws OMException{
    try {

        String errorCode = mc.getEnvelope().getBody().getFirstElement().getFirstChildWithName(new QName("errorCode")).getText();
        String errorDescription = null;

        if(errorCode.equals("1")) {
            errorDescription = "One";
        }else if(errorCode.equals("2")) {
            errorDescription = "Two";
        }else if(errorCode.equals("3")) {
            errorDescription = "Three";
        }

        mc.getEnvelope().getBody().getFirstElement().getFirstChildWithName(new QName("errorDescription")).setText(errorDescription);

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}

Expected response,

<soapenv:Bodyxmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<jsonObject>
    <serviceRequestID>12345</serviceRequestID>
    <statusCode>1</statusCode>
    <errorCode>1</errorCode>
    <errorDescription>One</errorDescription>
    <addressID>1.23456794E9</addressID>
    <source>consumer name</source>
    <requestId>910514</requestId>
</jsonObject>

But now, I only get the error description as null. kindly help. any advice would be great !

Thanks.

UPDATED !

Apparently, after set the value using the setText() method. The error description value change to One as expected in the log.

</statusCode><errorCode>1</errorCode><errorDescription>One</errorDescription>

But when the response is sent back to the client, it still null value.

{"serviceRequestID": "12345","statusCode": 1,"errorCode": "1", "errorDescription": "","addressID": 1.23456794E9,"source": "consumer name",   "requestId": "910514"}
1

There are 1 best solutions below

0
On

Try this;

private void ResponseLogging(MessageContext mc) throws OMException{
    try {

        String errorCode = mc.getEnvelope().getBody().getFirstElement().getFirstChildWithName(new QName("jsonObject")).getFirstChildWithName(new QName("errorCode")).getText();
        String errorDescription = null;

        if(errorCode.equals("1")) {
            errorDescription = "One";
        }else if(errorCode.equals("2")) {
            errorDescription = "Two";
        }else if(errorCode.equals("3")) {
            errorDescription = "Three";
        }

        mc.getEnvelope().getBody().getFirstElement().getFirstChildWithName(new QName("jsonObject")).getFirstChildWithName(new QName("errorDescription")).setText(errorDescription);

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
}