WSO2 get PDF from request - pdf

I need to get a pdf from a request to my WSO2 esb. This is my scenario:
Request:
CMS -- Request ----> wso2 ESB ----> backend
Response:
CMS <--- (PDF) --- wso2 ESB <--- PDF Byte Array --- backend
I think that I need to use something like 'aplication/pdf' but when I try to do it, my ESB throws a java error. The following is my code:
<api xmlns="http://ws.apache.org/ns/synapse" name="test" context="/test">
<resource methods="GET" uri-template="/myService">
<inSequence>
<call>
<endpoint>
<http method="get" uri-template="http://backendService/getPDF""></http>
</endpoint>
</call>
<loopback/>
</inSequence>
<outSequence>
<property name="messageType" value="application/pdf" scope="axis2"/>
<property name="ContentType" value="application/pdf" scope="axis2" type="STRING"></property>
<send/>
</outSequence>
<faultSequence></faultSequence>
</resource>
</api>
Any idea?
--- SOLUTION ---
It was really easy and stupid, but I post my solution to avoid that someone waste time in the future:
<api xmlns="http://ws.apache.org/ns/synapse" name="test" context="/test">
<resource methods="GET" uri-template="/myService">
<inSequence>
<send>
<endpoint>
<http method="get" uri-template="http://backendService/getPDF""></http>
</endpoint>
</send>
</inSequence>
<faultSequence></faultSequence>
</resource>
</api>
With this code I just returns the same request that I do to the server, so I get my PDFs files :D

Related

WSO2ESB: How to add <?xml into the message sent to POX endpoint

I'm using wso2ei 6.0.0 and I have a simple task - send specific xml message via POST to the endpoint.
I have API configured like this:
<api xmlns="http://ws.apache.org/ns/synapse" name="glisTest" context="/glisTest">
<resource methods="GET" uri-template="/{sampleid}">
<inSequence>
<property name="PRESERVE_WS_ADDRESSING" value="false"/>
<payloadFactory media-type="xml">
<format>
<register />
</format>
</payloadFactory>
<log level="full"/>
<property name="messageType" value="application/xml" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="post" scope="axis2" type="STRING"/>
<send>
<endpoint>
<address uri="https://localhost/manager" encoding="UTF-8" format="pox"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="full"/>
<send/>
</outSequence>
</resource>
</api>
But service is expected
<?xml version="1.0" encoding="UTF-8" ?>
to be inserted at the beginning and wso2 do not do it (I just added debug logging for org.apache.synapse.transport.http.wire and I see it simple send
<register/>
As result, it does not recognize message as an xml and returm me error.
Question - how to tell synapse to add this special xml header to the message?

WSO2 ESB - Api to Soap transformation

I would like to expose an API (REST) on ESB that traslate the request to a SOAP service.
My Api is definined in this way :
<api xmlns="http://ws.apache.org/ns/synapse" name="__test" context="/mytest">
<resource methods="GET" uri-template="/{symbol}">
<inSequence>
<property name="symbol" expression="get-property('uri.var.symbol')"/>
<payloadFactory media-type="xml">
<format>
<ser:getQuote xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
<ser:request>
<xsd:symbol>$1</xsd:symbol>
</ser:request>
</ser:getQuote>
</format>
<args>
<arg evaluator="xml" expression="get-property('symbol')"/>
</args>
</payloadFactory>
<log level="full"/>
<send>
<endpoint>
<wsdl service="SimpleStockQuoteService" port="SimpleStockQuoteServiceHttpEndpoint"
uri="http://localhost:9000/services/SimpleStockQuoteService?wsdl"/>
</endpoint>
</send>
</inSequence>
</resource>
</api>
But when i invoke the url with http://:8280/mytest/WSO2 I heve this error on console
<soapenv:Reason xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Text xml:lang="en-US">
The endpoint reference (EPR) for the Operation not found is /services/SimpleStockQuoteService.SimpleStockQuoteServiceHttpEndpoint/WSO2?request= and the WSA Action = null. If this EPR was previously reachable, please contact the server administrator.
</soapenv:Text>
</soapenv:Reason>
Because the esb append the WSO2 part of the request to URL.
How I can solve this ?
Thanks
You can follow this guide for achieving this. Note that the soap action header should be added as follows and endpoint is different.
<header name="Action" value="urn:getQuote"/>
Find the complete solution API below,
<api xmlns="http://ws.apache.org/ns/synapse" name="__test" context="/mytest"><resource methods="GET" uri-template="/{symbol}">
<inSequence>
<property name="symbol" expression="get-property('uri.var.symbol')"/>
<payloadFactory media-type="xml">
<format>
<ser:getQuote xmlns:ser="http://services.samples" xmlns:xsd="http://services.samples/xsd">
<ser:request>
<xsd:symbol>$1</xsd:symbol>
</ser:request>
</ser:getQuote>
</format>
<args>
<arg evaluator="xml" expression="get-property('symbol')"/>
</args>
</payloadFactory>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<log level="full"/>
<header name="Action" value="urn:getQuote"/>
<send>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService" format="soap11"/>
</endpoint>
</send>
</inSequence></resource></api>
If you want to use the WSDL url directly, you can achive this using WSO2 API Manager and it can be easily tried in WSO2 API Cloud. There is a similar question here.

WSO2 ESB: How to write a file from Base64 with VFS?

I have pretty much the same issue as this person:
WSO2 ESB - writing files out of base64
However, in the answer to that question there was no mention of how to decode the Base64 string in JavaScript and attach it to the payload. That's what I'm interested in.
Thanks in advance,
Strainy
Went with a slightly different approach. I created a utility proxy service to re-download the file and then push it into the file system.
utilty_createFile Proxy Service
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="utility_createFile"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target inSequence="utility_createFile_IN" outSequence="utility_createFile_OUT"/>
<description/>
</proxy>
utility_createFile_IN Sequence
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="utility_createFile_IN" onError="fault" xmlns="http://ws.apache.org/ns/synapse">
<property name="FORCE_SC_ACCEPTED" scope="axis2" value="true"/>
<property expression="//createFile/download_folder"
name="uri.var.download_folder" xmlns:ns="http://org.apache.synapse/xsd"/>
<property expression="//createFile/url" name="uri.var.download_url" xmlns:ns="http://org.apache.synapse/xsd"/>
<script language="js"><![CDATA[var download_url = mc.getProperty('uri.var.download_url');
download_url = decodeURIComponent(download_url);
mc.setProperty('uri.var.download_url', download_url);]]></script>
<send>
<endpoint>
<http method="GET" uri-template="{uri.var.download_url}"/>
</endpoint>
</send>
</sequence>
utility_createFile_OUT Sequence
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="utility_createFile_IN" onError="fault" xmlns="http://ws.apache.org/ns/synapse">
<property name="FORCE_SC_ACCEPTED" scope="axis2" value="true"/>
<property expression="//createFile/download_folder"
name="uri.var.download_folder" xmlns:ns="http://org.apache.synapse/xsd"/>
<property expression="//createFile/url" name="uri.var.download_url" xmlns:ns="http://org.apache.synapse/xsd"/>
<script language="js"><![CDATA[var download_url = mc.getProperty('uri.var.download_url');
download_url = decodeURIComponent(download_url);
mc.setProperty('uri.var.download_url', download_url);]]></script>
<send>
<endpoint>
<http method="GET" uri-template="{uri.var.download_url}"/>
</endpoint>
</send>
</sequence>

WSO2 ESB 4.7.0 API url-mapping

I am trying to make an API at WSO2 ESB 4.7.0 to process requests for a RESTful service. It seems that the URL-Mapping feature is not working or is working just like a filter, choosing what to send to the endpoint and what to drop. I have followed this tutorial: http://wso2.com/library/articles/2012/09/get-cup-coffee-wso2-way/. Here is my API configuration:
<api xmlns="http://ws.apache.org/ns/synapse" name="rest" context="/rest">
<resource methods="GET" url-mapping="/">
<inSequence>
<send>
<endpoint>
<http method="get" uri-template="http://myserver/REST"/>
</endpoint>
</send>
</inSequence>
</resource>
<resource methods="GET" url-mapping="/numbers">
<inSequence>
<send>
<endpoint>
<http method="get" uri-template="http://myserver/REST/allnumbers"/>
</endpoint>
</send>
</inSequence>
</resource>
</api>
There are three situations:
This URL works: http://esb/rest
This URL doesn't work: http://esb/rest/numbers
This URL works: http://myserver/REST/allnumbers
In situation 2, I got an Apache Tomcat error:
HTTP Status 404 - Not Found
type Status report
message Not Found
description The requested resource (Not Found) is not available.
Apache Tomcat/6.0.32
But if I try the endpoint address, it works. I thought that the URL-Mapping would route requests for "/numbers" to "/allnumbers". What am I doing wrong?
Solved! I had to remove the REST_URL_POSTFIX before sending to the endpoint:
<api xmlns="http://ws.apache.org/ns/synapse" name="rest" context="/rest">
<resource methods="GET" url-mapping="/">
<inSequence>
<send>
<endpoint>
<http method="get" uri-template="http://myserver/REST"/>
</endpoint>
</send>
</inSequence>
</resource>
<resource methods="GET" url-mapping="/numbers">
<inSequence>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<send>
<endpoint>
<http method="get" uri-template="http://myserver/REST/allnumbers"/>
</endpoint>
</send>
</inSequence>
</resource>
</api>
Now, http://esb/rest/numbers works too! :)

Removing SOAP Wrapper From a Message in wso2 ESB

I tried to invoke a web service through WSO2esb.My web service is only accepting a soap message like this.
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Envelope xmlns:ns1="http://www.webserviceX.NET/ConversionRate/Input" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Documents and Settings\RoGulk\Desktop\SPM\Envelope.xsd">
<ns1:Body>
<ns2:ConversionRate xmlns:ns2="http://www.webserviceX.NET/">
<ns2:FromCurrency>USD</ns2:FromCurrency>
<ns2:ToCurrency>USD</ns2:ToCurrency>
</ns2:ConversionRate>
</ns1:Body>
</ns1:Envelope>
But the problem is when I read the input message to the esb, The esb adds a additional wrapper to my meaasage like this
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:Envelope xmlns:ns1="http://www.webserviceX.NET/ConversionRate/Input">
<ns1:Body>
<ns2:ConversionRate xmlns:ns2="http://www.webserviceX.NET/">
<ns2:FromCurrency>INR</ns2:FromCurrency>
<ns2:ToCurrency>NAD</ns2:ToCurrency>
</ns2:ConversionRate>
</ns1:Body>
</ns1:Envelope>
</soapenv:Body>
</soapenv:Envelope>
I think it can be done by using the 'application/XML' message formatter before I call to my endpoint.But the problem is I don't know how to invoke the 'application/XML' message formatter and where to use it.**
Guys please help me, Thanks
EDITED
I thought that adding my proxy service configuration will be helpful.
In this I am not using default out sequance. Instead of that I am using sequance called "currencyFileWriter" which is responsible for writing a file to a output directory.
This is my proxy service
<proxy xmlns="http://ws.apache.org/ns/synapse" name="CurrencyProxy" transports="https,http,vfs" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<property name="messageType" value="application/xml" scope="default" type="STRING"/>
<property name="ContentType" value="application/xml" scope="default"/>
<log level="custom">
<property name="MessageType" expression="$axis2:ContentType"/>
</log>
<log level="full"/>
<log level="custom">
<property name="insequance" value="================================Forwerded to out File writer sequance====================="/>
</log>
<send receive="currencyFileWriter" buildmessage="true">
<endpoint key="CurrencyConverter"/>
</send>
<log level="custom">
<property name="After" value="============================AFTER SEND================"/>
</log>
<log level="full"/>
</inSequence>
</target>
<parameter name="transport.PollInterval">5</parameter>
<parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
<parameter name="transport.vfs.FileURI">file:///C:/test/Orginal1/</parameter>
<parameter name="transport.vfs.MoveAfterProcess">file:///C:/test/Pass1/</parameter>
<parameter name="transport.vfs.MoveAfterFailure">file:///C:/test/Failures1/</parameter>
<parameter name="transport.vfs.FileNamePattern">currencytest.xml</parameter>
<parameter name="transport.vfs.ContentType">application/xml</parameter>
<parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>
<description></description>
</proxy>
And tihs is my CurrencyFilewriter sequance
<sequence xmlns="http://ws.apache.org/ns/synapse" name="currencyFileWriter">
<log level="custom">
<property name="sequance" value="=====================RECIVED TO CURRENCYFILEWRITER=========================="/>
</log>
<log level="full"/>
<property name="transport.vfs.ReplyFileName" value="result.xml" scope="default" type="STRING"/>
<property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
<send>
<endpoint>
<address uri="vfs:file:///C:/test/Out1"/>
</endpoint>
</send>
</sequence>
I used the following configuration and I got the following results inside of the VFS folder.
Here's what I've done:
Endpoint Configuration:
<endpoint xmlns="http://ws.apache.org/ns/synapse" name="CurrencyConverter">
<address uri="http://www.webservicex.net/CurrencyConvertor.asmx"></address>
</endpoint>
Sequence Configuration:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="currencyFileWriter">
<log level="custom">
<property name="sequance" value="=====================RECIVED TO CURRENCYFILEWRITER=========================="/>
</log>
<log level="full"/>
<property name="transport.vfs.ReplyFileName" value="result.xml" scope="default" type="STRING"/>
<property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
<send>
<endpoint>
<address uri="vfs:file:///home/ravi/esb/wso2esb-4.5.1/vfs-temp/Out1"/>
</endpoint>
</send>
</sequence>
Proxy Configuration:
<proxy xmlns="http://ws.apache.org/ns/synapse" name="CurrencyProxy" transports="https,http,vfs" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<log level="custom">
<property name="insequance" value="================================Forwerded to out File writer sequance====================="/>
</log>
<send>
<endpoint key="CurrencyConverter"/>
</send>
<log level="custom">
<property name="After" value="============================AFTER SEND================"/>
</log>
<log level="full"/>
</inSequence>
<outSequence>
<sequence key="currencyFileWriter"/>
</outSequence>
</target>
<parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
<parameter name="transport.PollInterval">5</parameter>
<parameter name="transport.vfs.MoveAfterProcess">file:///home/ravi/esb/wso2esb-4.5.1/vfs-temp/Pass1/</parameter>
<parameter name="transport.vfs.FileURI">file:///home/ravi/esb/wso2esb-4.5.1/vfs-temp/Orginal1/</parameter>
<parameter name="transport.vfs.MoveAfterFailure">file:///home/ravi/esb/wso2esb-4.5.1/vfs-temp/Failures1/</parameter>
<parameter name="transport.vfs.FileNamePattern">currencytest.xml</parameter>
<parameter name="transport.vfs.ContentType">application/xml</parameter>
<parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>
<description></description>
</proxy>
This is what is passed from the client (SOAP UI in my case) to the ESB - I've used SOAP 1.2 here by pointing the namespace ns1 to http://www.w3.org/2003/05/soap-envelope:
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Envelope xmlns:ns1="http://www.w3.org/2003/05/soap-envelope" xmlns:ns2="http://www.webserviceX.NET/ConversionRate/Input">
<ns1:Body>
<ns2:ConversionRate xmlns:ns2="http://www.webserviceX.NET/">
<ns2:FromCurrency>USD</ns2:FromCurrency>
<ns2:ToCurrency>LKR</ns2:ToCurrency>
</ns2:ConversionRate>
</ns1:Body>
</ns1:Envelope>
This is what the ESB sends to the backend service:
POST /CurrencyConvertor.asmx?WSDL HTTP/1.1
Content-Type: application/soap+xml; charset=UTF-8; action="urn:mediate"
Accept-Encoding: gzip,deflate
Transfer-Encoding: chunked
Host: localhost:5000
Connection: Keep-Alive
User-Agent: Synapse-HttpComponents-NIO
<?xml version='1.0' encoding='UTF-8'?>
<ns1:Envelope xmlns:ns1="http://www.w3.org/2003/05/soap-envelope" xmlns:ns2="http://www.webserviceX.NET/ConversionRate/Input">
<ns1:Body>
<ns2:ConversionRate xmlns:ns2="http://www.webserviceX.NET/">
<ns2:FromCurrency>USD</ns2:FromCurrency>
<ns2:ToCurrency>LKR</ns2:ToCurrency>
</ns2:ConversionRate>
</ns1:Body>
</ns1:Envelope>
This is what the web service returns to the ESB and is also saved in the Out1 file:
Cache-Control: private, max-age=0
Content-Length: 380
Content-Type: application/soap+xml; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 23 Jan 2013 15:53:53 GMT
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ConversionRateResponse xmlns="http://www.webserviceX.NET/">
<ConversionRateResult>127.05</ConversionRateResult>
</ConversionRateResponse>
</soap:Body>
</soap:Envelope>
Hope this helps as with this setup I don't seem to be getting the issue you're having - with the ESB re-wrapping the request. :)
You need to add the following property just above the send mediator (before call your endpoint) to remove the additional soap envelope wrapper from the message.
<property name="messageType" value="application/xml" scope="axis2" type="STRING"/>
I tested it with following sample proxy and it worked for me.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="test"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<property name="messageType"
value="application/xml"
scope="axis2"
type="STRING"/>
<log level="full"/>
<send>
<endpoint>
<address uri="http://www.google.com"/>
</endpoint>
</send>
</inSequence>
</target>
<description/>
</proxy>
Thanks