Hi Amith,

Explanation of how json-eval() works.

We have two kinds of path expressions in synapse, synapseJsonPath and
synapseXPath
Following method is where we evaluate the json-eval() expressions

public String stringValueOf(MessageContext synCtx) {
    org.apache.axis2.context.MessageContext amc =
((Axis2MessageContext) synCtx).getAxis2MessageContext();
    InputStream stream;
    if (!JsonUtil.hasAJsonPayload(amc) ||
"true".equals(enableStreamingJsonPath)) {
        try {
            if (null == amc.getEnvelope().getBody().getFirstElement()) {
                // Get message from PT Pipe.
                stream = getMessageInputStreamPT(amc);
                if (stream == null) {
                    stream = JsonUtil.getJsonPayload(amc);
                } else {
                    JsonUtil.getNewJsonPayload(amc, stream, true, true);
                }
            } else {
                // Message Already built.
                stream =
JsonUtil.toJsonStream(amc.getEnvelope().getBody().getFirstElement());
            }
            if(stream != null) {
                return stringValueOf(stream);
            }else{
                log.warn("Json Payload is empty.");
                return "";
            }
        } catch (IOException e) {
            handleException("Could not find JSON Stream in PassThrough
Pipe during JSON path evaluation.", e);
        }
    } else {
        stream = JsonUtil.getJsonPayload(amc);
        return stringValueOf(stream);
    }
    return "";
}


In all the branches we use *stringValueOf(stream)* method to evaluate the
jsonPath expressions against an input-stream.
Where we take the stream is dependent on certain conditions as in the above
implementation.

If message already build we convert message body to a JSON stream in

stream =
JsonUtil.toJsonStream(amc.getEnvelope().getBody().getFirstElement());

If message is not already built we take the stream from passthrough Pipe

Pipe pipe= (Pipe)
context.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);

In the final else condition we take the stream from a property in axis2
message context

Object o =
messageContext.getProperty(ORG_APACHE_SYNAPSE_COMMONS_JSON_JSON_INPUT_STREAM);


You can find all implementation details in [1]

[1] https://github.com/wso2/wso2-synapse

Thanks,
Lahiru

On Wed, Sep 26, 2018 at 7:31 PM Nuwan Dias <nuw...@wso2.com> wrote:

> Adding Isuru and Shafreen.
>
> The mediation engine (EI/API Gateway) supports transforming or changing
> messages from various protocols to various other protocols. Such as from
> XML to JSON, from Text to XML, from form-url to JSON, likewise. To support
> these various types of transformations, upon receiving a message the engine
> would first convert it into a common base, and then convert it to the
> requested base, likewise. Without doing which, the combinations of these
> transformations would be endless. In the case of EI/API Gateway, this
> common base is XML. So every message, if you need to process it, is first
> converted to XML and then all changes or transformation are done on the
> particular XML message and then converted to whatever the requested format
> is. So as far as I know you can't work with the message on plain JSON
> format only. However, I've added two of my colleagues who have a better
> idea about this than me to see if this is possible.
>
> Thanks,
> NuwanD.
>
> On Wed, Sep 26, 2018 at 2:11 PM Amitha Dissanayake <ami...@yaalalabs.com>
> wrote:
>
>> Hi Nuwan,
>>
>> Thanks a lot for the prompt response. That document helped. However, my
>> original question still remains unresolved to me.
>>
>> More specifically, when using the class mediator, I used the property
>> mediator and received the JSON string to the class. But I still have to
>> convert to XML and load it to the axis2 context message envelope (i.e.
>> SOAP).
>>
>> <property name="messageType" value="application/json" scope="axis2"/>
>> <property name="JSON-Payload" expression="json-eval($.)"/>
>>
>>
>>
>>
>> I couldn't find how this json-eval() function evaluates the JSON payload
>> (Whether it's accessing a JSON representation or an XML representation).
>>
>> How can I do this without converting the JSON to XML (without converting
>> it to XML and attaching to axis2 message)?
>>
>> How is JSON represented in the EI / API Manager by default (XML or JSON)?
>> I understand that conversions and manipulations are possible either way.
>>
>> Can the the EI / API Manager keep the JSON payload not converted
>> end-to-end (i.e. from the point of backend REST API sending the response
>> with a JSON payload till the requester receiving it?
>>
>> I'm evaluating API Manager, while hoping to apply it to a latency and
>> performance critical application. That is why I'm trying to understand how
>> it works in the EI.
>>
>> Thanks.
>>
>> Regards,
>> Amitha
>>
>> On Sep 26 2018, at 2:12 pm, Nuwan Dias <nuw...@wso2.com> wrote:
>>
>>
>> Hi Amitha,
>>
>> Maybe this document [1] would help you understand how to deal with JSON
>> payloads better.
>>
>> [1] -
>> https://docs.wso2.com/display/EI630/Working+with+JSON+Message+Payloads
>>
>> Thanks,
>> NuwanD.
>>
>> On Wed, Sep 26, 2018 at 10:01 AM Amitha Dissanayake <ami...@yaalalabs.com>
>> wrote:
>>
>> Hi,
>>
>> I'm a developer who's exploring API Manager. When I was attempting to
>> retrieve the JSON payload form a Context Message during mediation (out
>> sequence), I observed that the payload it attached in a SOAP Envelope
>> inside the Axis2 Context Message. However, I'm receiving the response from
>> a REST endpoint.
>>
>> In that case I had to convert the SOAP Evelope XML to JSON, then modify
>> the changes and again convert it to XML and attach to Axis2 Context
>> Message. In that operation, 4 conversions happen. That is,
>>
>>    1. Between the endpoint and mediation point. JSON to XML
>>    2. During mediation, I convert XML to JSON and do the operation,
>>    3. I again convert the JSON to XML and attach to the axis2 message.
>>    4. Postman receives a JSON; means XML has again been converted inside
>>    the API Manager.
>>
>>
>> When I ran the mediation on debug mode, I observed that the content is
>> received as SOAPAction to the mediation point.
>>
>> I feel that this adds an unnecessary latency overhead or I am doing
>> something wrong in the process. Is there a way to overcome this issue/
>> directly access the JSON payload or what is the correct way to access the
>> JSON payload without too many conversions?
>>
>> Thanks in Advance.
>>
>> Regards,
>>
>> Amitha Dissanayake
>> Senior Software Engineer [image: Logo] <https://www.yaalalabs.com/>
>>
>> Yaala Labs
>> 14 Sir Baron Jayathilake Mawatha
>> Colombo 1, Sri Lanka
>> m: + 94 77 548 1350
>> e: ami...@yaalalabs.com
>>
>> www.yaalalabs.com [image: LinkedIn icon]
>> <https://www.linkedin.com/company/yaalalabs/>
>> _______________________________________________
>> Dev mailing list
>> Dev@wso2.org
>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>
>>
>>
>> --
>> *Nuwan Dias* | Director | WSO2 Inc.
>> (m) +94 777 775 729 | (e) nuw...@wso2.com
>>
>>
>
> --
> *Nuwan Dias* | Director | WSO2 Inc.
> (m) +94 777 775 729 | (e) nuw...@wso2.com
> [image: Signature.jpg]
> _______________________________________________
> Dev mailing list
> Dev@wso2.org
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to