How to append to an existing file with WSO2 ESB (VFS) - append

I'm writing to a single file with a WSO2 ESB using a VFS endpoint. But this does override the file each time. How can I configure the endpoint to append the content? Or does this feature even exist?
<inSequence>
<log level="full"/>type="STRING"/>
<property name="transport.vfs.ReplyFileName" expression="myOutputFile.txt" scope="transport" type="STRING"/>
<send>
<endpoint>
<address uri="vfs:file://C:/MyOutputDirectory"/>
</endpoint>
</send>
</inSequence>
(my content is text/plain)

In order to append to the same file, define the transport.vfs.Append=true parameter with the file address URI[1]
<address uri="vfs:file://C:/MyOutputDirectory?transport.vfs.Append=true"/>
[1]http://docs.wso2.org/wiki/display/ESB450/VFS+Transport

Related

How to convert text/xml response into application/json wso2 EI

I have a soap service and I need to expose this service as a rest api in Wso2 EI, content type is text/xml, I tried with
<property name="messageType" value="application/json"
scope="axis2"/>
in the outsequence, but it doesnt convert my response into a json. Can you please assist me on how to do it.
I tried this,
<resource methods="POST">
<inSequence>
<send>
<endpoint>
<address uri="http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<property name="messageType" value="application/json" scope="axis2"/>
<send/>
</outSequence>
</resource>
Reason for this is that WSO2 EI or ESB is developed in a manner where it responds to a SOAP request with a SOAP response by default. When you invoke the PROXY or API with SOAPAction and Content-Type: text/xml, EI understands this as a SOAP request and it'll respond with a SOAP response.
So if the client request is in SOAP-1.1 EI responds with a SOAP-1.1 response or if the client request is in SOAP-1.2 EI responds with a SOAP-1.2.
To bypass this behaviour they have provided an additional property as below.
<property name="IsClientDoingREST" scope="default" type="BOOLEAN" value="true"/>
So, before responding the client, properties should be set as below to get the expected behaviour of responding with a JSON.
<property name="IsClientDoingREST" scope="default" type="BOOLEAN" value="true"/>
<property name="messageType" scope="axis2" value="application/json"/>
This approach will help you to get the JSON response out of a SOAP request.
It should work perfectly. A sample REST API configuration given below.
<api xmlns="http://ws.apache.org/ns/synapse" name="CheckREST" context="/samplerest">
<resource methods="GET">
<inSequence>
<send>
<endpoint>
<http uri-template="http://localhost:8280/services/sampleSOAPproxy"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<send/>
</outSequence>
</resource>
</api>
If not working, please mention the EI version you are using.

WSO2 ESB - soap envelope that is automatically added is causing issues

I'm exploring WSO2 ESB and have hit an issue. I am using a simple pass through proxy to post XML data from SAP (or Postman, to test) which then gets forwarded to a REST API - should be easy!
When I POST direct to the REST API (not through ESB), it works fine.(200, OK)
But WSO2 ESB is adding a SOAP Envelope automatically, which the REST API will not accept. I've tried various approaches to remove the automatically added SOAP envelope, with no success. Tried XSLT transform, POX format, Enrich mediator etc., every suggestion I could find.
(I can remove envelope element using XSLT if it is sent as part of the body, but not the one WSO2 adds in)
I can access the body, without SOAP envelope, using :
<property name="body" expression="$body/*[1]" type="OM"/>
but am not sure how to forward this to the API.
Any ideas how to stop this envelope being added in the first place in WSO2 ESB, or on how to remove it?
I used the xslt code from this answer, which works fine when I include the SOAP tags in the body, but has no effect on the SOAP envelope that seems to get automatically added in WSO2 (except to give the error, below).
I tried different variations of the line:
<xsl:apply-templates select="soap:Envelope/soap:Body/*"/>
such as
<xsl:apply-templates select="soap:Envelope/*"/>
<xsl:apply-templates select="/*"/>
this is an error I see in the ESB log:
Unable to perform XSLT transformation using : Value {name ='null', keyValue ='discountPayment'} against source XPath : s11:Body/child::[position()=1] | s12:Body/child::[position()=1] reason : Unable to create an OMElement using XSLT result
I am pretty new to WSO2 ESB, and have not used XSLT before, so may be some very basic mistake in my approach....
here is my proxy xml, and the XSLT "removeSOAP":
<?xml version="1.0" encoding="UTF-8"?>
<proxy name="APIServer" startOnLoad="true" trace="disable"
transports="https http" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<log level="full">
<property name="FirstLog" value="APITest LOG...."/>
<property name="payload" expression="$body/*[1]" type="OM"/>
</log>
<xslt key="removeSOAP"/>
<log level="full">
<property name="SecondLog" value="after xslt...."/>
</log>
<property name="messageType" scope="axis2" type="STRING" value="application/xml"/>
<send>
<endpoint name="endpoint_urn_uuid_xxxxxx">
<address trace="disable" uri="http://myAPIendpoint " />
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="full"/>
<send/>
</outSequence>
<faultSequence/>
</target>
</proxy>
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="removeSOAP" xmlns="http://ws.apache.org/ns/synapse">
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:m="http://www.example.org/stock">
<xsl:template match="/">
<xsl:apply-templates select="soap:Envelope/soap:Body/*"/>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
</localEntry>
A colleague figured this out today, I post the answer here incase it helps someone in the future:
in the proxy service, before send, add the line
<property name="DISABLE_CHUNKING" value="true" scope="axis2"/>
then it works.
No need for the XSLT transform that is shown above, just this line fixes it.
if your backend it´s an API better you can use the http endpoint https://docs.wso2.com/display/ESB500/HTTP+Endpoint:
<send>
<endpoint>
<http method="POST"
uri-template="http://your.backend.endpoint.org/"/>
</endpoint>
</send>

WSO2 REST to SOAP passing operation parameters

Using WSO2 ESB 4.8.1, I have configured a WSDL proxy that I want to access over REST. the proxy points to the SOAP WSDL URI and has publish WSDL turned on. This seem to work fine and I can see the service and its various operations in the WSO2 admin UI. Likewise if I go to localhost:8280/services/
The questions is how do I pass operation specific parameters when accessing over HTTP REST?
Let's say my FooService OperationX expects a "p1" parameter, can I pass this directly when accessing localhost:8280/services/FooService/OperationX in a browser?
I tried for example localhost:8280/services/FooService/SomeOperation?p1=somevalue, but always get a validation error that the required parameter is missing:
cvc-complex-type.2.4.b: The content of element 'axis2ns15:OperationXRequest' is not complete. One of '{"somenamespace":p1}' is expected.
Can this be supported by a basic WSDL proxy? Or do I need to use the API?
I think the better option for your scenario is to use api to access over REST. Here I have created an api (I used http://jsonplaceholder.typicode.com/comments as my REST back end) which gets the query parameter(postId) which was sent in REST request (http://172.22.99.96:8290/test/comments?postId=1) and assign that value to a property called mypostId inside the api.
Then I am modifying the payload by adding the mypostId property using payload factory mediator which will match to the echo service request(I have used echo service as the SOAP backend).
Then I use enrich mediator to change my soap envelope to match the echo service request soap envelope by adding "xmlns:echo="http://echo.services.core.carbon.wso2.org"" name space. Finally I am sending my created request to echo service proxy.
<api xmlns="http://ws.apache.org/ns/synapse" name="test" context="/test">
<resource methods="GET" uri-template="/comments?postId={postId}">
<inSequence>
<log level="custom">
<property name="Message Flow" value="--- Order GET ---"></property>
</log>
<log level="custom">
<property name="postId" expression="$url:postId"></property>
</log>
<property name="mypostId" expression="$url:postId"></property>
<call>
<endpoint>
<http method="GET" uri-template="http://jsonplaceholder.typicode.com/comments?postId={uri.var.postId}"></http>
</endpoint>
</call>
<payloadFactory media-type="xml">
<format>
<echo:echoInt xmlns:echo="http://echo.services.core.carbon.wso2.org">
<in>$1</in>
</echo:echoInt>
</format>
<args>
<arg evaluator="xml" expression="get-property('mypostId')"></arg>
</args>
</payloadFactory>
<log level="full"></log>
<log level="custom">
<property name="Message Flow" value="--- After Palyload factory---"></property>
</log>
<property name="extarctedBody" expression="$body"></property>
<log level="custom">
<property name="MyextarctedBody" expression="get-property('extarctedBody')"></property>
</log>
<log level="full"></log>
<enrich>
<source type="inline" clone="true">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:echo="http://echo.services.core.carbon.wso2.org"></soapenv:Envelope>
</source>
<target type="envelope"></target>
</enrich>
<log level="custom">
<property name="Message Flow" value="--- Order GET2 ---"></property>
</log>
<log level="full"></log>
<enrich>
<source type="property" clone="true" property="extarctedBody"></source>
<target xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:echo="http://echo.services.core.carbon.wso2.org" action="child" xpath="//soapenv:Envelope"></target>
</enrich>
<log level="full"></log>
<send>
<endpoint>
<address uri="http://localhost:8290/services/echo"></address>
</endpoint>
</send>
</inSequence>
<outSequence>
<send></send>
</outSequence>
</resource>
</api>
Hope this may help you .

WSO2 esb: how to proxy REST service with numeric operation name

We are trying to implement basic SOAP-to-REST proxy service on WSO2 ESB. Our 3rd-party REST service accepts the requests in the following format:
http://<MYURL>/simpleQuery/16783484?oslc.select=value1
The problem is that operation name has numeric only format - "16783484" in our case. payloadFactory mediator does not allow having <16783484> as XML element, since XML spec restricts numeric-only element names.
<proxy xmlns="http://ws.apache.org/ns/synapse" name="CQProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<payloadFactory>
<format>
<16783484>
<oslc.select>$1</oslc.select>
</16783484>
</format>
<args>
<arg value="myvalue1"/>
</args>
</payloadFactory>
<send>
<endpoint>
<address uri="http://<MYURL>/simpleQuery" format="get"/>
</endpoint>
</send>
<drop/>
</inSequence>
<outSequence>
<log level="full"/>
<send/>
</outSequence>
</target>
</proxy>
How can this be overcome?
Appreciate your help!
WSO2 support team suggested the following solution. Thank you Sandapa!
In that case, you have to set endpoint format as 'rest'. And if it's a GET request you have to set 'HTTP_METHOD' as as GET. Please refer the example given below.
Example:
<proxy xmlns="http://ws.apache.org/ns/synapse" name="CQProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<property name="REST_URL_POSTFIX" value="/getSimpleQuote?symbol=IBM" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
<send>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService/" format="rest"/>
</endpoint>
</send>
<drop/>
</inSequence>
<outSequence>
<log level="full"/>
<send/>
</outSequence>
</target>
<description></description>
</proxy>
Although this comment is not going to suggest you a solution, I can say that this is a bad idea :-) You can try using XSLT instead of PayloadFactory to transform, but again that might choke up the XML parser. Problem is a lot of open source projects/libs that WSO2 products use and you might bump into elsewhere will honour the spec. In the long run, complying to the spec will give you less headaches when you integrate with other external tools/systems. Is it possible to change your rest service so that the service name at least have an underscore in front?

WSO2 ESB mediate SOAP services

I am struggling to make work my message flow in the wso2 esb so I would need some help to implement a basic communication:
Service1 wants to receive an integer number
Service2 Generates random numbers
Service1 has InSequence: log, send (to addresspoint specified). OutSequence: log, send
this looks like:
<proxy name="ClientAskNumber" transports="https http" startOnLoad="true"
trace="disable">
<target faultSequence="fault">
<inSequence>
<log level="full">
<property name="Insequence" value="***" />
</log>
<send>
<endpoint>
<address uri="http://localhost:8280/services/RandomNumbers" />
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="full">
<property name="Outsequence" value="***" />
</log>
<send />
</outSequence>
</target>
</proxy>
I have this response: <faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: urn:mediate. at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()....etc
What it means? Am I missing something else? Please help. Thank you
EDIT:
I am studying the Wso2 ESB and I just need to understand how to make work a message communication, after it I will try to add different kind of mediation. I am just breaking down the problem, because I am new to this technology and as you can see I am really struggling to make it work...
EDIT2:*
<proxy xmlns="http://ws.apache.org/ns/synapse" name="ClientAskNumber" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target endpoint="RandomNumbers">
<inSequence>
<log>
<property name="CLIENTASKS" value="******C_ASKS" />
</log>
<send>
<endpoint key="RandomNumbers" />
</send>
</inSequence>
<outSequence>
<log>
<property name="CLIENTRECEIVES" value="*******C_RECEIVES" />
</log>
</outSequence>
</target>
</proxy>
In the case this helps someone else: the problem with "Server did not recognize the value of HTTP Header SOAPAction: urn:mediate..." is that I needed to add an Header mediator in order to call my webservice method "getNumbers", into my InSequence as follows:
<inSequence>
<log>
<property name="CLIENTASKS" value="******C_ASKS" />
</log>
<header name="Action" value="http://tempuri.org/getNumbers" />
<send>
<endpoint key="RandomNumbers" />
</send>
</inSequence>
and send this request via soapUI:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Numbers xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>
I hope this can be useful to other persons who are using .Net Solutions with WSO2ESB (unfortunately there are not many examples out there...)
P.S. thanks to Ratha for his help
This is the flow i can tell you..
You have to point your 2nd service as the endpoint of your fist service..In the above configuration
http://localhost:8280/services/RandomNumbers --->2nd service url
ClientAskNumber -->1st service..
Now you can send the request which is needed to execute the 2nd service(ie:to retrive the random number)
So the proxy will forwrad to that endpoint and return the response to the outsequence..You should see that in the console/log since you used a log mediator.
In the error response you are getting i hope you are getting that from your second service. Check whether you are sending correct request to your endpoint