0
votes

I am not so into WSO2 ESB and I have the following situation that have to be handle. I try to explain it in details.

I have an ESB project containing some REST APIs. These REST APIs implements the HATEOAS concept. In practice I am simply adding the link to the returned resoruces. I am doing it using a script mediator that manipulated the JESON before return it, in my APIs I have something like this:

<script language="js">
<![CDATA[
    var log = mc.getServiceLog();
    function checkForNull(value) {
        if (value instanceof Object && "@nil" in value) {
            return null;
        }

        return value;
    }

    log.info("----------- COMMODITY DETAILS JS SCRIPT START --------------------");
    // Strange workaround for getting JSON Payload. getPayloadJSON returned null. 
    var pl_string = mc.getProperty("JSONPayload"); 
    var payload = JSON.parse(pl_string);
    log.info("PAYLOAD STRING: " + pl_string);

    // Create new response:          
    var response = payload.Commodity.Commodity;
    log.info("RESPONSE: " + JSON.stringify(response));

    // Convert array of markets into required HATEOS format
    var markets = new Array();

    // Convert array of markets into required HATEOS format:
    for (i = 0; i < response.markets.el.length; ++i) {
        log.info("MARKET " + i);
        var el = response.markets.el[i];

        var newEl = new Object();

        newEl.market_id = el.market_id;
        newEl.market_name = el.market_name;

        // Convert null values:
        newEl.region = checkForNull(el.region);
        newEl.province = checkForNull(el.province);
        newEl.city = checkForNull(el.city);
        newEl.district = checkForNull(el.district);
        newEl.town = checkForNull(el.town);
        newEl.village = checkForNull(el.village);


        newEl.rel = "market_details";
        newEl.href = "http://XXX.YYY.ZZZ.KKK:8280/market_details/" + el.market_id;
        newEl.type = "GET";

        markets.push(newEl);
    }

    response.markets = markets;

    log.info("RESPONSE AFTER checkForNull(): " + JSON.stringify(response));

    // Put payload back:
    mc.setPayloadJSON(response);
    log.info("----------- COMMODITY DETAILS JS SCRIPT END --------------------");
]]>
</script>

As you can see in this script mediator I am adding the HATEOAS link:

    newEl.rel = "market_details";
    newEl.href = "http://XXX.YYY.ZZZ.KKK:8280/market_details/" + el.market_id;
    newEl.type = "GET";

I don't know if it is the neater solution but it is working fine and at the moment I can't chage it.

The problem is that I am hardcoding the href URL into the previous JavaScript that manipulate my JSON.

Now I have 2 environments: one for DEV and another one for PROD. And I have a lot of APIs. I know that I can simply do a string search and replace (I search for the XXX.YYY.ZZZ.KKK DEV URL replacing it with the PROD URL before deploy in PROD).

But I think that this is an absolutly dirty and horrible solution.

So: what could be neater solution?

My idea is:

  • I create a new sequence that contains a current_env property in which I set the URL of the environment.

  • I include this sequence at the beginning of every ESB file containing the definition of my API.

  • I retrieve the value of this property into my script mediator and I use it to generate my HATEOAS link.

Could be this a pretty decent solution? Or exist some better way to do it? (maybe a value like this can be stored somewhere else different from a sequence? I really don't know)

1

1 Answers

1
votes

Mentioning URL's in script mediator or in the code is a bad practice,Rather than following this approach you can add the URL in Local Entries, fetch the value in your sequence and then append in script mediator, so in this case whenever a CAR file is deployed that will have generic code and all dynamic artifacts can be mentioned in local entires.