WSO2 EI, how to stream or pipe file response to user? - wso2

I have REST API url method, that will return file as its response and I need to wrap it around with my WSO2 EI REST API.
Is it possible to stream or pipe this file based response back to requested user, without storing this file?

You should be able to use the wso2 file connector[1] to read the file as a stream inside the API sequence. You can download and enable the file connector as in the doc[2]. Follow the "Reading content from a file" section in [1] and you should be able to set the streaming optional parameter to true. Also, follow the Note section to enable streaming for large files.
A sample API sequence is shown below
<?xml version="1.0" encoding="UTF-8"?>
<api xmlns="http://ws.apache.org/ns/synapse"
name="fileStream"
context="/file-stream"
version="1.0"
version-type="context">
<resource methods="GET" url-mapping="/test">
<inSequence>
<fileconnector.read>
<source>{source_location}</source>
<filePattern>{file_pattern}</filePattern>
<contentType>{content_type}</contentType>
<streaming>true</streaming>
</fileconnector.read>
<respond/>
</inSequence>
</resource>
</api>
[1] - https://docs.wso2.com/display/ESBCONNECTORS/Working+with+the+File+Connector
[2] - https://docs.wso2.com/display/ESBCONNECTORS/File+Connector

Related

ERROR {InvokeMediator} - {api:EmailMicroService} Sequence named Value {name ='null', keyValue ='techlogixSmtp'} cannot be found

Scenario
I'm using Wso2 Email Connector for sending emails to our company's smtp server. Following is the code for connection and Api
Connection
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="techlogixSmtp" xmlns="http://ws.apache.org/ns/synapse">
<email.init>
<connectionType>SMTPS</connectionType>
<password>xxxx</password>
<host>smtp.techlogix.com</host>
<requireTLS>true</requireTLS>
<port>25</port>
<name>techlogixSmtp</name>
<username>xxxx</username>
</email.init>
</localEntry>
Api
<?xml version="1.0" encoding="UTF-8"?>
<api context="/email" name="EmailMicroService" port="8290" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/send">
<inSequence>
<log level="full"/>
<email.send configKey="techlogixSmtp">
<from>{json-eval($.from)}</from>
<to>{json-eval($.to)}</to>
<subject>{json-eval($.subject)}</subject>
<content>{json-eval($.content)}</content>
<contentType>{json-eval($.contentType)}</contentType>
<attachments>{json-eval($.attachments)}</attachments>
</email.send>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Question
I'm facing the ERROR {InvokeMediator} - {api:EmailMicroService} Sequence named Value {name ='null', keyValue ='techlogixSmtp'} cannot be found. What possible configuration is missing from my end which is causing this issue?
I made sure that updated Api is deployed on MicroIntegrator
As Sanoj, mentioned your Local Entry techlogixSmtp seems not deployed to the Runtime. So in your CompositeExporter Project make sure your techlogixSmtp is checked and the correct Server Role is selected. Please refer to the following image.

Basic Authentication handler is working http protocol but not with https in wso2 ei

I am following the example 'Using a Basic Auth handler' at https://docs.wso2.com/m/mobile.action#page/33136403/header/SecuringAPIs-BasicAuthUsingaBasicAuthhandler.
With the same steps followed I am able to test with http protocol i.e http://localhost:8290/api/ and basic auth username:password admin:admin
Now I want to do below 2 things
enable https for the same
if I try as same above and url 'https://localhost:8243/api' nothing happens
I want to change the username:password to something else like newuser:password1
For http and https my api looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/api" name="newAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" uri-template="/">
<inSequence></inSequence>
<outSequence/>
<faultSequence></faultSequence>
</resource>
<handlers>
<!-- <handler class="test1.BasicAuthHandler"/> -->
<handler class="org.wso2.rest.BasicAuthHandler"/>
</handlers>
</api>
<?xml version="1.0" encoding="UTF-8"?>
<api context="/api" name="newAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" uri-template="/" protocol="https">
<inSequence></inSequence>
<outSequence/>
<faultSequence></faultSequence>
</resource>
<handlers>
<handler class="org.wso2.rest.BasicAuthHandler"/>
</handlers>
</api>
can you add your WSO2 EI version and your java handler code?
My basic auth with https works. If you want change username and password you must change your default java code or you can connect to local UserStore or AD from handler.
Are you sure you are using the correct port? The HTTP port of 8290 suggests an offset of 10. In that case the HTTPS port would be 8253 instead of 8243. That might cause that 'nothing happens'.

WSO2 ESB not returning payloads

We're downgrading our ESB from 5.0.0 to 4.8.1 to match a different development environment and we've hit an unexpected snag: our APIs aren't returning anything.
We were originally using the mediator, which I'm gathering was changed between 4.8.1 and 5.0.0 and cannot in the older version be used as a response so we switched to This is a resource I have set up in an effort to get a smoke test up
<resource methods="GET" uri-template="/StaticTest">
<inSequence>
<log level="full"></log>
<loopback></loopback>
</inSequence>
<outSequence>
<log level="full"></log>
<payloadFactory media-type="json">
<format>{"asdf":"fdsa","blah":3}</format>
<args></args>
</payloadFactory>
<respond></respond>
</outSequence>
</resource>
I know the resource is being hit, because the logging statements make it into the logs. In fact, placing another mediator over the respond tag will display the following
[2017-05-09 15:48:19,968] INFO - LogMediator To: ,
MessageID: urn:uuid:6376072e-d005-497e-b7a1-9f89cff49099,
Direction: response,
Envelope: <?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<jsonObject>
<asdf>fdsa</asdf>
<blah>3</blah>
</jsonObject>
</soapenv:Body>
</soapenv:Envelope>
Which I have taken the liberty of formatting a bit. As you can see a soap envelope has been made matching my static JSON object, but SOAP UI is showing nothing. I get an HTTP 200, but no content. Any idea what could cause this?

WSO2 ESB: Can we use Call Mediator for Rest Service to post (POST HTTP Method) the xml request?

When I create proxy with send mediator with Rest service Post HTTP Method in HTTP endpoint url. Selected the endpoint as HTTP endpoint on proxy and post the request xml without soap envelop, this perfectly works and get the response in the response window.
But when I use the call mediator with the same HTTP end point url configuration, this does not works. I would like to know can we use call mediator for Post HTTP method? When I use Call mediator for the GET HTTP method which require only query parameters and does not require any request xml this works absolutely fine.
Here is the further information:
However issue is resloved by using the address endpoint in callmediator. When I Invoke the proxy from external Restt client ot Soap UI, it does works. If I use the Try this Service option in wso2 ESB will fail with the results 1. When Soap12 endpoint is selected and 2 when HTTP end point is selected as shown below.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="postIDMPCall"
transports="https http"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<property name="ContentType" value="text/xml" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
<payloadFactory media-type="xml">
<format>
<organizationList xmlns="">
<xml content>
</organizationList>
</format>
<args/>
</payloadFactory>
<header name="_user" scope="transport" value="username"/>
<header name="_password" scope="transport" value="Password"/>
<call blocking="true">
<endpoint>
<address uri="http://<ip-address>:<port>/<resource-path>/UpdateOrganization"
format="rest"/>
</endpoint>
</call>
</inSequence>
</target>
</proxy>
Output: When soap12 endpoint is selected
Though posted the correct xml service does not recorgonize the correct xml format for soap12 endpoint.
FAILURE
Record is not processed successfully. Please Provide valid Request XML
When Http end point is selected
[2016-04-21 12:07:50,179] INFO - HTTPSender Unable to sendViaPost to url[http://://UpdateOrganization/mediate]
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
I think we can't use call mediator for this purpose because call mediator is context unaware mediator.
Your call should already perform a post out the box.
Did you try to set format="pox" if you expect simple xml as a response

WSO2 ESB Proxy CRLF normalized to LF

A simple proxy that only adds authentication.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="QueryTestProxy"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<property xmlns:ns="http://org.apache.synapse/xsd"
name="Authorization"
expression="fn:concat('Basic ', base64Encode(fn:concat('admin:', wso2:vault-lookup('QueryTest'))))"
scope="transport"
type="STRING"/>
<send>
<endpoint key="conf:/QueryTest"/>
</send>
</inSequence>
<faultSequence>
<send/>
</faultSequence>
</target>
<publishWSDL key="conf:/WSDL/QueryTest.wsdl"/>
<description/>
</proxy>
The endpoint service does a split on CRLF on one of the fields, also the endpoint can not be modified and can't use CDATA.
The problem is that WSO2 ESB always replaces CRLF with LF and the split won't work, does anyone know a way to stop WSO2 ESB to normalize the message?
Axiom uses stax and stax remove those CR in CR-LF
I've had a similar issue when writing text files from a soap message generated like this :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:guid="http://com/cylande/unitedretail/guidedsale/service/common/GuidedSaleManagerService/">
<soapenv:Header/>
<soapenv:Body>
<text xmlns="http://ws.apache.org/commons/ns/payload">my flat datas whith carriage return and line feeds</text>
</soapenv:Body>
</soapenv:Envelope>
We should be able to configure this behavior creating a XMLOutputFactory.properties file inside ESB_HOME, with this content : com.ctc.wstx.outputEscapeCr=false
But in my case, I were not able to start the ESB any more...
(See http://ws.apache.org/axiom/apidocs/org/apache/axiom/om/util/StAXUtils.html for more details)
Here is the javascrip that I've added in my mediation before calling send mediator :
<script language="js"><![CDATA[
try {
var payloadXML = mc.getPayloadXML();
var envelopeXML = mc.getEnvelope();
if (payloadXML != null) {
var text = payloadXML.toString();
if ((envelopeXML != null) && (envelopeXML.getBody() != null) && (envelopeXML.getBody().getFirstElement() != null))
// Do not use mc.setPayloadXML(), it depends on Stax that delete the carriage return we are trying to add...
envelopeXML.getBody().getFirstElement().setText(text.replace(new RegExp('\n','g'),'\r\n'));
}
} catch (e) {
}
]]></script>
Hope you will be able to adapt this script to your need.
ESB's XML parser (AXIOM) is just working according to XML specification
http://www.w3.org/TR/REC-xml/#sec-line-ends
So XML parser must replace CR-LF with LF. This is mentioned in above XML spec .
This is the reason of replacing CRLF with LF in wso2 ESB