MD5 of javascript content in WSO2 ESB API - wso2

I have to deal with javascript content in API resource in WSO2 ESB. Specifically, I want to get the md5 hash from message which it is a javascript.
When I use messageType = application/javascript I get a binary, no JS message.
Can I do this? If I use binary to get md5, Will it work?

Finally, I solved this question as given below:
At first, I changed in axis2.xml the messageBuldir and messageFormater property to "text/javascript" pointing to "org.apache.axis2.format.PlainTextBuilder" and "org.apache.axis2.format.PlainTextFormatter".
At second I got the message in API with next expression: "s11:Body/child::[fn:position()=1] | s12:Body/child::[fn:position()=1]". With that, I can save the js message in a property.
Finally, I wrote a class mediator to calculate MD5 of property with JS.

Related

How to get payload size from custom handler in WSO2 APIM

I am trying to get payload size from both request and response for every kind of content type with custom handler in WSO2 APIM1.10.0
By reference this code, I can get the payload size with 2 ways:
Get header content length (at line 127)
Build message and get the body length (at line 132)
But I think both of these two way might have some problem.
User might give incorrect content length when invoking the API.
The API content type might be unpredictable.
So, how can I get payload size from both request and response?
You can do this by using a script mediator.
The length of the payload of the message can be received by calling mc.getPayloadXML() inside script mediator in both in-sequence and out-sequence.
Please refer this blog for more information and the documentation on usage of mediation extentions in API Manager.

WSO2 API Manager "error": "no response from server"

I have published API in WSO2 using swagger JSON. After publish I am trying to call rest api using swagger in APP Console. It says
Response Body no content
Response Code 0
Response Headers {
"error": "no response from server"
}
There is no any error on server which will help me to understand problem.
Here is the request URL which I am using in local server : https://192.168.1.118:8243/api/2.0/questions/1/answers?start=1&end=1&fields=answerId%2CanswerDescription%2CcreateDate
In my API there are some custom header parameter. Because of this custom header parameter it was not working. I have added custom header parameter in api-manager.xml file.
<Access-Control-Allow-Headers>authorization,Access-Control-Allow-Origin,Content-Type,loggedInUserId,accessToken,clientToken</Access-Control-Allow-Headers>
I have faced a similar issue. I have edited the URL(from apicreator login) after it was published for first time and published it again. But the changes were never reflected. WSO2 was pointing to the old URL. Check the logs for error at "WSO2 HOME\repository\logs\wso2-apigw-errors" . Create another version and publish again and it should work.
The error is real, but misleading. In most likelihood, you've set the spec so that it returns a specific content type (say, application/json) but you actually return plain text (like a string or a number). swagger-ui expects it to be a JSON, tries to parse it and fails, giving you the wrong error message. However, it does mean your spec does not match what your API actually does.

How to send Queryparam with JSON value using Jersey client in unit test

I am trying to write a test case for a jersey resource using InMemory container provided by Jersey.
As my service method contains many multivalued parameters as filters, I opted to send all of those values as single JSON parameter, so that it will be easy to send a list of values for each filter.
When i send the JSON string using target("path").queryParam("filters", jsonString).request().get(); the call fails die to Jersey clients internal query builder, which is parsing the url and checking for path param templates in the url. Since the url contains my JSON with "{" in it, they are interpreted as path param.
If I try to encode the JSON using URLEncode.encode(jsonStr, "UTF-8"), the path param template issue is solved but white spaces in JSON are received by server as "+" as jersey client encoding URL one more time, but server decoding it only once.
If I make the Queryparam as post param test is working, but i don't want to use POST for just to retrieve data.
I can't post original code due to company policies.
My question is, is there any way to disable path template check in jersey clieny by setting custom builder.
A simpler solution would be to replace the '+' by '%20' as suggested here and here:
URLEncode.encode(jsonStr, "UTF-8").replaceAll("\\+", "%20");

wso2 esb endpoint can not be changed

I have some external URL (restful api) to be integrated.
Those URL have different prefixed URL with different parameter at url, for example:
www.abc.com/books
www.abc.com/book/11
www.abc.com/book/11/authors
When get response from those invocation, esb needs to convert response from one json format to our standard json format.
I plan to use esb javascript mediator to perform convert operation, but I didn't find any way to attach url parameters.
Any one have any idea?
I have used mediator by java code to implement it, but it is too heavy.
I am also looking into connector for another option.
I have got a solution by use url template. By this solution, I can change url according to template defined.
With this solution, I didn't need to write mediator or connector.

Problem with Posting JSON object with WSRequest

I want Play to call a webservice. The webservice accepts application/json and returns this as well. With the following code I'm trying to achieve this. (Note, the headers.put(xxx) are added later in an effort to solve the problem).
WSRequest request = WS.url(targetURL);
request.body = new Gson().toJson(user);
request.headers.put("Content-type","application/json");
request.headers.put("Accept","application/json");
request.post();
The strange thing is that my JBOSS server replies: "Cannot consume content type". If I use my 'Simple REST client' plugin in my Chrome browser, and provide the entire JSON Body GSon created and add the content-type header, I get a valid response. Is this not the way to send JSON to the server? Or am I missing some fundamental piece here?
While checking the API documentation on the WSRequest class i noticed the field mime-type
By setting it as follows JBOSS (resteasy) accepted my request succesfully.
request.mimeType = "application/json";