WSO2 Custom Mediator Set value for the Envelope












0















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"}









share|improve this question

























  • Can you share the source of your sequence where you use this mediator?

    – ophychius
    Nov 19 '18 at 7:33











  • @ophychius Hi there. do you mean my api.xml ? This is my api.xml <api> <inSequence> <send><endpoint key="backendApi"/></send> </inSequence> <outSequence> <sequence key="ResponseLoggingMediator"/> <send/> </outSequence> </api>

    – ameruddin jamil
    Nov 19 '18 at 7:51













  • @ophychius or you mean my sequence source code. here it is <sequence name="ResponseLoggingMediator" trace="disable" xmlns="ws.apache.org/ns/synapse"> <class name="myipcs.csam.cdm.java.ResponseLoggingMediator"/> </sequence>

    – ameruddin jamil
    Nov 19 '18 at 7:55











  • @ameruddinjamil Why are you using a custom class mediator to do the transformation? Isn't it something you can do with the available mediators (enrich mediator or payload factory mediator)?

    – Asanka Abeyweera
    Nov 20 '18 at 4:03











  • @AsankaAbeyweera Hi there. please teach me on how to do this ? so far that i know, the payload factory is to change the value of the content. I dont see any example where the value is change base on some other value (condition)

    – ameruddin jamil
    Nov 21 '18 at 3:20
















0















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"}









share|improve this question

























  • Can you share the source of your sequence where you use this mediator?

    – ophychius
    Nov 19 '18 at 7:33











  • @ophychius Hi there. do you mean my api.xml ? This is my api.xml <api> <inSequence> <send><endpoint key="backendApi"/></send> </inSequence> <outSequence> <sequence key="ResponseLoggingMediator"/> <send/> </outSequence> </api>

    – ameruddin jamil
    Nov 19 '18 at 7:51













  • @ophychius or you mean my sequence source code. here it is <sequence name="ResponseLoggingMediator" trace="disable" xmlns="ws.apache.org/ns/synapse"> <class name="myipcs.csam.cdm.java.ResponseLoggingMediator"/> </sequence>

    – ameruddin jamil
    Nov 19 '18 at 7:55











  • @ameruddinjamil Why are you using a custom class mediator to do the transformation? Isn't it something you can do with the available mediators (enrich mediator or payload factory mediator)?

    – Asanka Abeyweera
    Nov 20 '18 at 4:03











  • @AsankaAbeyweera Hi there. please teach me on how to do this ? so far that i know, the payload factory is to change the value of the content. I dont see any example where the value is change base on some other value (condition)

    – ameruddin jamil
    Nov 21 '18 at 3:20














0












0








0








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"}









share|improve this question
















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"}






wso2 wso2esb synapse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 7:26







ameruddin jamil

















asked Nov 19 '18 at 6:38









ameruddin jamilameruddin jamil

2319




2319













  • Can you share the source of your sequence where you use this mediator?

    – ophychius
    Nov 19 '18 at 7:33











  • @ophychius Hi there. do you mean my api.xml ? This is my api.xml <api> <inSequence> <send><endpoint key="backendApi"/></send> </inSequence> <outSequence> <sequence key="ResponseLoggingMediator"/> <send/> </outSequence> </api>

    – ameruddin jamil
    Nov 19 '18 at 7:51













  • @ophychius or you mean my sequence source code. here it is <sequence name="ResponseLoggingMediator" trace="disable" xmlns="ws.apache.org/ns/synapse"> <class name="myipcs.csam.cdm.java.ResponseLoggingMediator"/> </sequence>

    – ameruddin jamil
    Nov 19 '18 at 7:55











  • @ameruddinjamil Why are you using a custom class mediator to do the transformation? Isn't it something you can do with the available mediators (enrich mediator or payload factory mediator)?

    – Asanka Abeyweera
    Nov 20 '18 at 4:03











  • @AsankaAbeyweera Hi there. please teach me on how to do this ? so far that i know, the payload factory is to change the value of the content. I dont see any example where the value is change base on some other value (condition)

    – ameruddin jamil
    Nov 21 '18 at 3:20



















  • Can you share the source of your sequence where you use this mediator?

    – ophychius
    Nov 19 '18 at 7:33











  • @ophychius Hi there. do you mean my api.xml ? This is my api.xml <api> <inSequence> <send><endpoint key="backendApi"/></send> </inSequence> <outSequence> <sequence key="ResponseLoggingMediator"/> <send/> </outSequence> </api>

    – ameruddin jamil
    Nov 19 '18 at 7:51













  • @ophychius or you mean my sequence source code. here it is <sequence name="ResponseLoggingMediator" trace="disable" xmlns="ws.apache.org/ns/synapse"> <class name="myipcs.csam.cdm.java.ResponseLoggingMediator"/> </sequence>

    – ameruddin jamil
    Nov 19 '18 at 7:55











  • @ameruddinjamil Why are you using a custom class mediator to do the transformation? Isn't it something you can do with the available mediators (enrich mediator or payload factory mediator)?

    – Asanka Abeyweera
    Nov 20 '18 at 4:03











  • @AsankaAbeyweera Hi there. please teach me on how to do this ? so far that i know, the payload factory is to change the value of the content. I dont see any example where the value is change base on some other value (condition)

    – ameruddin jamil
    Nov 21 '18 at 3:20

















Can you share the source of your sequence where you use this mediator?

– ophychius
Nov 19 '18 at 7:33





Can you share the source of your sequence where you use this mediator?

– ophychius
Nov 19 '18 at 7:33













@ophychius Hi there. do you mean my api.xml ? This is my api.xml <api> <inSequence> <send><endpoint key="backendApi"/></send> </inSequence> <outSequence> <sequence key="ResponseLoggingMediator"/> <send/> </outSequence> </api>

– ameruddin jamil
Nov 19 '18 at 7:51







@ophychius Hi there. do you mean my api.xml ? This is my api.xml <api> <inSequence> <send><endpoint key="backendApi"/></send> </inSequence> <outSequence> <sequence key="ResponseLoggingMediator"/> <send/> </outSequence> </api>

– ameruddin jamil
Nov 19 '18 at 7:51















@ophychius or you mean my sequence source code. here it is <sequence name="ResponseLoggingMediator" trace="disable" xmlns="ws.apache.org/ns/synapse"> <class name="myipcs.csam.cdm.java.ResponseLoggingMediator"/> </sequence>

– ameruddin jamil
Nov 19 '18 at 7:55





@ophychius or you mean my sequence source code. here it is <sequence name="ResponseLoggingMediator" trace="disable" xmlns="ws.apache.org/ns/synapse"> <class name="myipcs.csam.cdm.java.ResponseLoggingMediator"/> </sequence>

– ameruddin jamil
Nov 19 '18 at 7:55













@ameruddinjamil Why are you using a custom class mediator to do the transformation? Isn't it something you can do with the available mediators (enrich mediator or payload factory mediator)?

– Asanka Abeyweera
Nov 20 '18 at 4:03





@ameruddinjamil Why are you using a custom class mediator to do the transformation? Isn't it something you can do with the available mediators (enrich mediator or payload factory mediator)?

– Asanka Abeyweera
Nov 20 '18 at 4:03













@AsankaAbeyweera Hi there. please teach me on how to do this ? so far that i know, the payload factory is to change the value of the content. I dont see any example where the value is change base on some other value (condition)

– ameruddin jamil
Nov 21 '18 at 3:20





@AsankaAbeyweera Hi there. please teach me on how to do this ? so far that i know, the payload factory is to change the value of the content. I dont see any example where the value is change base on some other value (condition)

– ameruddin jamil
Nov 21 '18 at 3:20












1 Answer
1






active

oldest

votes


















0














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);
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369473%2fwso2-custom-mediator-set-value-for-the-envelope%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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);
    }
    }





    share|improve this answer




























      0














      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);
      }
      }





      share|improve this answer


























        0












        0








        0







        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);
        }
        }





        share|improve this answer













        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);
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 13:06









        Erandika HarshaniErandika Harshani

        1245




        1245






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369473%2fwso2-custom-mediator-set-value-for-the-envelope%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)