2
votes

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?

1

1 Answers

2
votes

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.

  1. I used a respond mediator and drop mediator within the inSequence to return the payload generated via the payload factory mediator to the client and then drop the message. With this, message does not go beyond that point and then you dont need the outSequence.
  2. I am unable to explain how this works with the ms11:text tag. I found it from Transform response to plain-text using wso2 esb 4.0.6.
  3. I used a CDATA to enclose your javascript. It works without CDATA tag too. You can use it if there are xml non-friendly characters in your javascript.
  4. I have added the plain text formatter for text/javascript content type.

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.