Reputation: 1193
I want to create an API resource in WSO2 ESB 4.9.0 where it send back a payload with javascript content. This resource must return response with Content-type:text/javascript.
For this purpose, I use a payloadFactory mediator where I set a simple comment line. Because, there is no a payloadFactory with media-type text-plain or text-javascript, I use media-type="json".
And, I get response well. But, when I set messageType like "text/javascript" I get 202 http code in the response and the payload is empty.
I can see a nullPointerException in logs when it call PlainTextFormatter because I set org.apache.axis2.format.PlainTextFormatter and org.apache.axis2.format.PlainTextBuilder in axis2.xml.
My resource is as given below:
<resource methods="GET" uri-template="/js">
<inSequence>
<log>
<property name="*** IN" value="[API] /test/v1/jsEcho/js"/>
</log>
<payloadFactory media-type="json">
<format>//tealium universal tag - utag.sync ut4.0.201604181647, Copyright 2016 Tealium.com Inc. All Rights Reserved.</format>
<args></args>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<loopback/>
</inSequence>
<outSequence>
<log>
<property name="*** OUT" value="[API] /test/v1/jsEcho/js"/>
</log>
<property name="messageType" value="text/javascript" scope="axis2" type="STRING"/>
<send/>
</outSequence>
<faultSequence></faultSequence>
</resource>
And, the error in log is as given here.
Is there anyway to do this?
Upvotes: 2
Views: 895
Reputation: 2149
Since this was an interesting question, I tried this. Good news is, I was able to get it done.
Following is my api configuration.
<api xmlns="http://ws.apache.org/ns/synapse" name="Stack" context="/stack">
<resource methods="GET" url-mapping="/js">
<inSequence>
<payloadFactory media-type="xml">
<format>
<ms11:text xmlns:ms11="http://ws.apache.org/commons/ns/payload"><![CDATA[//tealium universal tag - utag.sync ut4.0.201604181647, Copyright 2016 Tealium.com Inc. All Rights Reserved.]]></ms11:text>
</format>
<args/>
</payloadFactory>
<property name="messageType" value="text/javascript" scope="axis2"/>
<respond/>
<drop/>
</inSequence>
</resource>
</api>
Let me explain some of the things I have done.
See the response I get to a curl request.
curl -v -X GET http://localhost:8280/stack/js
< HTTP/1.1 200 OK
< Host: localhost:8280
< Content-Type: text/javascript; charset=UTF-8
< Accept: */*
< Date: Mon, 13 Jun 2016 16:52:36 GMT
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
//tealium universal tag - utag.sync ut4.0.201604181647, Copyright 2016 Tealium.com Inc. All Rights Reserved.
Upvotes: 2