3

Using the WSO2 Enterprise Integrator 6.4.0 for creating web services. Web service structure briefly;

  1. Request comes to the API.xml

  2. In Sequence.xml taking JSON payload with;

<property expression="json-eval($.)" name="payload" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
  1. Giving the JSON payload to the Transformation.xslt file as payload;
    <xslt key="conf:xsl/RequestTransform.xsl" source="$body[1]/*" xmlns:ns="http://org.apache.synapse/xsd"/>
  1. Sending the payload to the endpoint as JSON. Result of the transformation is XML payload, changing the type to JSON with PayloadFactory.
<payloadFactory media-type="json">
        <format>$1</format>
        <args>
            <arg evaluator="xml" expression="$body[1]/*" literal="false" xmlns:ns="http://org.apache.synapse/xsd"/>
        </args>
    </payloadFactory>
  1. Taking the response from endpoint in JSON format. Receiving the payload with '@' chars. This chars means that it is an attribute. Ex:
{
    "address": "1590 Lar",
    "@country": "SMCountry123",
    "language": "SMLanguage123"
}

Here is the problem I have that I need to remove '@' char from my response JSON payload which is came from external system. And need to transform response with .xslt file after removing '@'.

My first try was Script Mediator;

<script language="js">
            <![CDATA[var payload = mc.getProperty("payload");
                payload=String(payload).replace('@'/gm,' ');
                payload = JSON.parse(payload);
            mc.setPayloadJSON(payload);]]>
        </script>

but recieving the errors

javax.xml.stream.XMLStreamException: Cannot read attribute: element has children or text and Existing json payload is malformed.

Then I try to use Class Mediator prepare a .jar for WSO2 lib for;

import org.apache.synapse.MessageContext; 
import org.apache.synapse.mediators.AbstractMediator;

public class ChangeAtType extends AbstractMediator { 
    private String payload = "payload";

    public boolean mediate(MessageContext context) { 
        String payload = (String)context.getProperty(this.payload);
        payload = payload.replace("@","");
        context.setProperty(this.payload, payload);
        return true;
    }
}

and called it from sequence;

<property expression="json-eval($.)" name="payload" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
<class name="org.com.ChangeAtType.ChangeAtType"/>
<property expression="get-property('payload')" name="payload" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>

but still same errors I have.

Could you please advise? Thanks

1 Answers1

0

According to the exception thrown, this issue could be a bug in EI 6.4.0. However, it seems this has been fixed in the updated EI 6.4.0 version.