WSO2 Couldn't save JSON payload - esb

I use WSO2 EI 6.6.0 and Micro Integrator 4.0.0. I have an api to get data, but for this I need to authenticate and get a token to use in a further request. for authentication I use separately created api and call it from first flow. For authentication I need to create a payload and send it in the request body to the server, but I get an error. The problem is repeated on different servers and I do not understand what is the reason.
My Auth API looks like this:
<api context="/auth" name="AuthGateway" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" uri-template="/gateway">
<inSequence>
<payloadFactory media-type="json">
<format>{"username": "adm", "password": "admin"}</format>
<args/>
</payloadFactory>
<call>
<endpoint>
<http method="post" uri-template="https://1537-212-90-188-166.ngrok.io/auth/login">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>-1</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</http>
</endpoint>
</call>
<log separator="
">
<property expression="json-eval($)" name="log_auth"/>
</log>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
My Data API looks like this:
<resource methods="GET" uri-template="/fieldCard?*">
<inSequence>
<propertyGroup>
<property name="uri.var.version" expression="$url:version" scope="default" type="STRING"/>
<property name="uri.var.size" expression="$url:size" scope="default" type="STRING"/>
<property name="uri.var.page" expression="$url:page" scope="default" type="STRING"/>
</propertyGroup>
<log level="full" separator="
">
<property name="version" expression="$ctx:uri.var.version"/>
<property name="size" expression="$ctx:uri.var.size"/>
<property name="page" expression="$ctx:uri.var.page"/>
</log>
<call>
<endpoint>
<http method="GET" uri-template="http://localhost:8290/auth/gateway"/>
</endpoint>
</call> .......
In the console I get messages:
[2022-05-02 19:16:12,962] ERROR {JsonStreamBuilder} - Error occurred while processing document for application/json java.lang.reflect.InvocationTargetException
Caused by: org.apache.axis2.AxisFault: #Can not parse stream. MessageID: urn:uuid:7ac6a4c9-0ed1-4973-87d4-69dad6d4a950. Error>>> #getNewJsonPayload. Could not save JSON payload. Invalid input stream found. Payload is not a JSON string.
org.apache.synapse.commons.json.JsonStreamBuilder.processDocument(JsonStreamBuilder.java:43)
... 21 more
Caused by: org.apache.axis2.AxisFault: #getNewJsonPayload. Could not save JSON payload. Invalid input stream found. Payload is not a JSON string.
[2022-05-02 19:16:12,963] ERROR {DeferredMessageBuilder} - Error building message org.apache.axis2.AxisFault
[2022-05-02 19:16:12,964] ERROR {RelayUtils} - Error while building Passthrough stream org.apache.axis2.AxisFault
[2022-05-02 19:16:12,964] ERROR {SequenceMediator} - {api:AuthGateway} Error while building message. Error while building Passthrough stream org.apache.axis2.AxisFault: Error while building Passthrough stream
I removed the detailed stack trace. If it is needed to understand the problem - I can add it.
P.S. When calling the auth API directly I get the token and the response is correct.
[2022-05-02 19:20:17,560] INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: http://www.w3.org/2005/08/addressing/anonymous
WSAction:
SOAPAction:
MessageID: urn:uuid:817185a7-39a4-4418-a696-72473672997d
Direction: request
log_auth = {"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMyIsInVzZXJfbmFtZSI6Im...
Payload: {"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMyIsInVzZXJfbmFtZSI6Im...
Please help me understand the problem.
UPD: I reinstalled the server because I thought the error might be there or I broke the config files. created a new project and wrote a new same thread, but the error still repeats.

Good. The problem was that Postman has a Header Accept-Encoding option. When this parameter was disabled, the request became correct and the response began to come without errors!

Related

WSO2 ESB array with single element json to json conversion brackets missing

I'm using wso2 esb 4.9 and created a proxy with payloadFactory element and call it with REST to start my BPMN process, but the problem is esb cannot convert single element json array , and esb drops json array's brackets. here is my proxy :
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="InformationIncome"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<log level="full"/>
<property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/>
<property name="OUT_ONLY" value="true"/>
<payloadFactory media-type="json">
<format>
{
"processDefinitionId":"myProcess:6:25188",
"variables": [
{
"name":"text",
"value":"$1"
}
]
}
</format>
<args>
<arg evaluator="json" expression="$.text"/>
</args>
</payloadFactory>
<header name="Authorization"
scope="transport"
expression="fn:concat('Basic ', base64Encode('admin:admin'))"/>
<log level="full"/>
<store messageStore="InformationIncomeMS"/>
</inSequence>
</target>
<description/>
</proxy>
And Here is the content of my request from soapUI :
{
"text" : "Hello"
}
And I found out my json becomes like this after payloadFactory:
{
"processDefinitionId":"myProcess:6:25188",
"variables": {
{
"name":"text",
"value":"Hello"
}
}
}
it seems esb json converter drops brackets of arrays with single element. Any ideas?
I tested above scenario with Postman, and it works properly. Please find steps as follows;
Add proxy and remove message store. (Because adding null message to message store giving following error)
[2016-07-11 13:46:53,291] ERROR - NativeWorkerPool Uncaught exception
java.lang.Error: Error: could not match input
at org.apache.synapse.commons.staxon.core.json.stream.impl.JsonScanner.zzScanError(JsonScanner.java:530)
at org.apache.synapse.commons.staxon.core.json.stream.impl.JsonScanner.yylex(JsonScanner.java:941)
at org.apache.synapse.commons.staxon.core.json.stream.impl.JsonScanner.nextSymbol(JsonScanner.java:310)
at org.apache.synapse.commons.staxon.core.json.stream.impl.JsonStreamSourceImpl.next(JsonStreamSourceImpl.java:149)
at org.apache.synapse.commons.staxon.core.json.stream.impl.JsonStreamSourceImpl.peek(JsonStreamSourceImpl.java:272)
at org.apache.synapse.commons.staxon.core.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:129)
at org.apache.synapse.commons.staxon.core.json.JsonXMLStreamReader.consume(JsonXMLStreamReader.java:132)
at org.apache.synapse.commons.staxon.core.base.AbstractXMLStreamReader.hasNext(AbstractXMLStreamReader.java:446)
at org.apache.synapse.commons.staxon.core.base.AbstractXMLStreamReader.next(AbstractXMLStreamReader.java:456)
at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.parserNext(StAXOMBuilder.java:681)
at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:214)
at org.apache.axiom.om.impl.llom.OMElementImpl.getNextOMSibling(OMElementImpl.java:336)
at org.apache.axiom.om.impl.OMNavigator._getFirstChild(OMNavigator.java:199)
at org.apache.axiom.om.impl.OMNavigator.updateNextNode(OMNavigator.java:140)
at org.apache.axiom.om.impl.OMNavigator.getNext(OMNavigator.java:112)
at org.apache.axiom.om.impl.SwitchingWrapper.updateNextNode(SwitchingWrapper.java:1113)
at org.apache.axiom.om.impl.SwitchingWrapper.(SwitchingWrapper.java:235)
at org.apache.axiom.om.impl.OMStAXWrapper.(OMStAXWrapper.java:74)
at org.apache.axiom.om.impl.llom.OMStAXWrapper.(OMStAXWrapper.java:52)
at org.apache.axiom.om.impl.llom.OMContainerHelper.getXMLStreamReader(OMContainerHelper.java:51)
at org.apache.axiom.om.impl.llom.OMElementImpl.getXMLStreamReader(OMElementImpl.java
Use Postman and invoke proxy service with "POST" command
Add json content in to body
Send it. ESB will print the message correctly with brackets.
As DilshaniS said :
Looks like this is a bug in WSO2 ESB 4.9.0 and reported an jira this link

How to config WSO2 API to access rails REST json web service

I have a rails app exposed an rest json interface like: http://foo.com/agency/:name.json
I have written a wso2 esb api config to access the api
<api name="agency" context="/agency">
<resource methods="GET" uri-template="/view/{name}">
<inSequence>
<property name="REST_URL_POSTFIX"
expression="fn:concat('/',get-property('uri.var.name'), '.json')"
scope="axis2"/>
<send>
<endpoint>
<address uri="http://foo.com/agency/" format="pox"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
</api>
when I tested the wso2 esb api using curl, I get
\dev\curl-w32>curl http://192.168.181.132:8280/agency/view/blah-blah
curl: (52) Empty reply from server
And the ESB log write
[2013-11-14 16:17:49,047] INFO - LogMediator To: /agency/view/blah-blah, MessageID: urn:uuid:d0a4c4ec-7600-430d-bb52-e580b
6ec2516, Direction: request, MESSAGE = Executing default 'fault' sequence, ERROR
_CODE = 101500, ERROR_MESSAGE = Error in Sender, Envelope: <?xml version='1.0' e
ncoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap
-envelope"><soapenv:Body/></soapenv:Envelope>
[2013-11-14 16:18:49,063] WARN - SourceHandler Connection time out after reques
t is read: http-incoming-48
seems endpoint url translate is not work.
How to correct my API config to make it work? Thx
Try changing the scope="axis2" to scope="transport" and removing format="pox" from the address endpoint.

Compression for Spring remoting with JSON

I am trying to enable compression for json responses from server. Currently i see the response but not compressed, below i have mentioned the config.
Server: Jetty
Servlet.xml
<bean name="/TaskSearchApp.json" class="com.googlecode.jsonrpc4j.spring.JsonServiceExporter">
<property name="service" ref="taskSearchAppBean"/>
<property name="serviceInterface" value="com.webmethods.caf.TaskSearchAppInterface"/>
</bean>
Web.xml
<filter>
<filter-name>GzipFilter</filter-name>
<filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>*json</url-pattern>
</filter-mapping>
Client-bean.xml
</bean>
<bean id="taskSearchAppBeanJSON" class="com.googlecode.jsonrpc4j.spring.JsonProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8888/TaskSearchApp/TaskSearchApp.json"></property>
<property name="serviceInterface" value="com.webmethods.caf.TaskSearchAppInterface"></property>
</bean>
Ok I could sort this out by adding
extraHttpHeaders.put("Accept-Encoding","gzip, deflate");
But now i get the error while parsing the gzipped response
Caused by: com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
at [Source: com.googlecode.jsonrpc4j.NoCloseInputStream#700a4488; line: 1, column: 2]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1369)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:532)
at com.fasterxml.jackson.core.base.ParserMinimalBase._throwInvalidSpace(ParserMinimalBase.java:478)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._skipWSOrEnd(UTF8StreamJsonParser.java:2508)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:617)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:2926)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2873)

HttpMediaTypeNotAcceptableException with Spring 3.1.2 and Jackson 1.9.11

~R
I'm trying to do Spring MVC REST JSON channel with automatic conversion to JSON using Jackson. Although I'm doing everything as recommended in other StackOverflow questions, I'm getting HttpMediaTypeNotAcceptableException and my beans/lists are not converted to JSON. I'm running the code on Jetty.
I've followed the instruction from UTF-8 encoding problem in Spring MVC, Spring 3.0 making JSON response using jackson message converter and Jackson annotations being ignored in Spring, but none of them enables me to send neither bean nor a List<String>. Both return HTTP status 406. Only the method returning String succeeds with HTTP 200.
My method is:
#RequestMapping(value = "/list", produces = "text/plain; charset=utf-8")
public #ResponseBody List<String> getList() {
return createList();
}
Spring file:
<context:component-scan base-package="de.myapp.rest" />
<context:annotation-config />
<mvc:annotation-driven />
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
Adding this section also doesn't help:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="1">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
</list>
</property>
</bean>
The request HTTP header is generated by jQuery and looks like that:
Accept application/json, text/javascript, /; q=0.01
Accept-Encoding gzip, deflate
Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101
Firefox/18.0
However, I get HTTP 406 with HTML error message, and the following lines in log file:
Resolving exception from handler [public de.myapp.rest.TestBean
de.myapp.rest.Test.getList()]:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not
find acceptable representation
So the question is, what I'm missing here? Why the jackson converter is not registered/how can I debug it? I'm running the code via jetty:run. But it should not matter, since Spring is application-server-independent.
In your #RequestMapping you have produces = "text/plain", but your request header says Accept application/json, text/javascript. There is no match.
Use either produces="application/json" or don't use produces at all. It's main use case is to provide more than one method for the same URL, each producing output in a different format (JSP, JSON, XML ...).

Jackson Jaxb Json with Apache CXf

I am using Jackson Jaxb JSON in my REST project with Apache CXF
JacksonJaxb version . 1.7.0
Apache CXF 2.3.1
I am using following code to return from my method.
#GET
#Consumes({ "application/json", "application/xml", "text/xml" })
#Path("/job/autosuggest")
#Override
public Response getSuggestions(String searchField, Integer resPerPage, String typeCont)
{
List<String> respo = new ArrayList<String>();
respo.add("Atish");
respo.add("Narlawar");
respo.add("India");
return Response.ok(respo).build();
}
Now issue is coming when I compile and run the code on jetty, I get stuck with
DEBUG o.s.b.f.s.DefaultListableBeanFactory [] Finished creating instance of bean 'org.apache.cxf.phase.PhaseManager'
org.codehaus.jackson.map.JsonMappingException: Incompatible types: declared root type ([simple type, class javax.ws.rs.core.Response]) vs java.util.ArrayList
This is not particular to array or wrapper, but any object If I pass rather than String in Response.ok(object) fails to parse.
My configuration is
<util:map id="jsonNamespaceMap" map-class="java.util.Hashtable">
<entry key="http://services.institute.com" value=""/>
<entry key="http://cxf.apache.org/bindings/xformat" value="cxf"/>
</util:map>
<bean id="jsonInputFactory" class="org.codehaus.jettison.mapped.MappedXMLInputFactory">
<constructor-arg ref="jsonNamespaceMap"/>
</bean>
<bean id="jsonOutputFactory" class="org.codehaus.jettison.mapped.MappedXMLOutputFactory">
<constructor-arg ref="jsonNamespaceMap"/>
</bean>
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider"/>
<jaxrs:server id="jobsearch" address="/">
<jaxrs:serviceBeans>
<ref bean="jobSearchService" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="text" value="text/xml"/>
<entry key="xml" value="application/xml"/>
<entry key="json" value="application/json"/>
</jaxrs:extensionMappings>
<jaxrs:languageMappings/>
<jaxrs:properties>
<entry key="javax.xml.stream.XMLInputFactory">
<ref bean="jsonInputFactory"/>
</entry>
<entry key="javax.xml.stream.XMLOutputFactory">
<ref bean="jsonOutputFactory"/>
</entry>
</jaxrs:properties>
<jaxrs:providers>
<ref bean="jsonProvider"/>
</jaxrs:providers>
</jaxrs:server>
</beans>
I am not sure how to proceed on this issue. I already lost 1/2 day to get some workaround. Any help will be appreciated.
Thanks in advance !
At last I got the answer.
The issue is with the version itself.
JacksonJaxb has reported bug in 1.7.0.
I updated the version to higher...in my case it is 1.8.5 and it got fixed.
Hope this helps.
Thanks
Atish