How do I set a property to the value of an incoming http request header? I tried a few things (see following), but my log values are all null, so I'm clearly not reading the header values correctly. The header value I really care about is X-EMPID. Using wso2esb 4.8.1.
Here are a couple of posts that led me to believe this would work, but I'm not having any luck yet.
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="getaccount2"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<property name="empid"
expression="get-property('transport', 'X-EMPID')"
scope="default"
type="STRING"/>
<log level="custom">
<property name="emp_id" expression="get-property('empid')"/>
</log>
<log level="custom">
<property name="content_length"
expression="get-property('transport', 'Content-Length')"/>
</log>
<log level="custom">
<property name="TRANSPORT_HEADERS" expression="get-property('TRANSPORT_HEADERS')"/>
</log>
You can conveniently access HTTP headers, which technically are transport headers in WSO2 ESB, by using XPath variables. The easiest way to read an HTTP header named X-EMPID is by using the following XPath: $trp:X-EMPID, where the $trp prefix indicates that the part following the colon is the name of a transport property. To log the header value you could use the following log mediator:
<log level="custom">
<property name="X-EMPID value" expression="$trp:X-EMPID" />
</log>
To set the property myProperty to the value of X-EMPID HTTP header (which is already stored in a transport property) you would use the property mediator:
<property name="myProperty" expression="$trp:X-EMPID" />
The functionality is documented on the WSO2 site.
Related
I'm creating a POST API service to upload files in WSO2EI. Request using multipart/form-data.
I got error
Access to XMLHttpRequest at 'x' from origin y has been blocked by CORS policy: Request header field x-requested-with is not allowed by Access-Controll-Allow-Headers in preflight response
I only got the error when hit the service from frontend using vue js.
I already added option method
<resource methods="OPTIONS" uri-template="/*">
<inSequence>
<property name="Access-Control-Request-Headers" scope="transport" type="STRING" value="authorization,content-type"/>
<sequence key="gov:commons/AccessControlAllow.xml"/>
<property name="RESPONSE" scope="default" type="STRING" value="true"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
sequence AccessControlAllow.xml
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="AccessControlAllow" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property name="Access-Control-Request-Headers" value="Authorization,Content-Type, Access-Control-Allow-Origin" scope="transport" type="STRING"/>
<property name="Access-Control-Allow-Methods" scope="transport" type="STRING" value="GET,POST,PUT,DELETE,OPTIONS"/>
<property name="Access-Control-Allow-Headers" scope="transport" type="STRING" value="Origin, X-Requested-With, Content-Type, Accept"/>
<property name="Access-Control-Allow-Origin" scope="transport" type="STRING" value="*"/>
</sequence>
I also added this sequence (AccessControlAllow.xml) before sending response. I got the error only when the request using mulltipart/form-data and hit from vue.js. Anyone can help me to solve this problem?
This might be because, You have defined a OPTION resource in your API. If you have defined OPTIONS as a resource method, then EI sends the request to the backend service to collect the information.
Therefore, Check the API definition and remove OPTION call if exist.
I need to have a schedule job to read list of file names from DB and them upload them to a ftp site. How do I do that in WSO2 ESB?
Thanks
Updates (7/19/16): Based on the suggestions given, I wrote a stored procedure to return all file names in a xml format. Now that I need to figure out how to parse this xml formatted result and they can be ftp using fileconnector. Please let me know if my approach is workable. Thank you so much for your help.
<proxy name="FileUpload2CDGPS" startOnLoad="true" trace="disable" transports="https http" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<sequence key="FileLookupSeq"/>
<sequence key="SendFile2VendorSeq"/>
<send/>
</inSequence>
<outSequence/>
<faultSequence>
<drop/>
</faultSequence>
</target>
</proxy>
<sequence name="FileLookupSeq" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<log description="LogMessage" level="custom">
<property name="message" value="Find CDR files to upload"/>
</log>
<dblookup description="Get CDR files to be upload">
<connection>
<pool>
<password>***</password>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432/esbcdrdb</url>
<user>***</user>
</pool>
</connection>
<statement>
<sql><![CDATA[SELECT * FROM find_cdr_file_to_upload(1)]]></sql>
<result name="xml_file" column="find_cdr_file_to_upload"/>
</statement>
</dblookup>
</sequence>
<sequence name="SendFile2VendorSeq" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<iterate attachPath="//files/files-set" description=""
expression="//files/files-set/file" preservePayload="true">
<target>
<sequence>
<property description="fileName"
expression="//files/files-set/file/name" name="fileName"
scope="default" type="STRING"/>
<property description="fileId"
expression="//files/files-set/file/id" name="fileId"
scope="default" type="STRING"/>
<property description="fileSource"
expression="//files/files-set/file/path" name="fileSource"
scope="default" type="STRING"/>
<property description="vendorId"
expression="//files/files-set/file/vendor-id" name="vendorId"
scope="default" type="STRING"/>
<property description="vendorDest"
expression="//files/files-set/file/vendor-dest"
name="vendorDest" scope="default" type="STRING"/>
<fileconnector.copy>
<source>{$ctx:fileSource}</source>
<destination>{$ctx:vendorDest}</destination>
<filePattern>{$ctx:fileName}</filePattern>
</fileconnector.copy>
</sequence>
</target>
</iterate>
</sequence>
There are many ways you can get this done.
You can directly write your requirement inside the schedule task, which handles the db connection and uploading the file to ftp site.
How to write a schedule task
Or You can invoke a proxy through a scheduled task sample can be found here. And inside the proxy you can connect to the db and get the required data and upload it to ftp using vfs, or you can write a custom mediator which get executed inside the proxy.
I have a simple RESTful service that I want to expose as SOAP based Web Service using WSO2 ESB.
My simple RESTful service can be invoked like http://<<my system>>:8080/myapp/person/read
As response, I get JSON data of the Person entity.
Problem: I am not able to pass parameters to the RESTful service. I am to strip the param value from the SOAP input, but don't know how to pass it to my RESTful; service using ESB.
I have configured the following in WSO2 ESB
<proxy xmlns="http://ws.apache.org/ns/synapse" name="PersonProxy" transports="https,http" statistics="enable" trace="enable" startOnLoad="true">
<target>
<inSequence>
<property xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" name="PERSON_ID" expression="//soapenv:Body/person/id"/>
<log level="full">
<property name="PERSON_ID" expression="get-property('PERSON_ID')"/>
</log>
<filter xpath="//person">
<then>
<property name="REST_URL_POSTFIX" value="read" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
<property name="id" expression="get-property('PERSON_ID')" scope="axis2" type="STRING"/>
<property name="ContentType" value="application/x-www-form-urlencoded" scope="axis2" type="STRING"/>
</then>
<else/>
</filter>
<send>
<endpoint>
<address uri="http://<<my system>>:8080/myapp/person" format="rest"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
<description></description>
</proxy>
My SOAP request looks like following
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<person>
<id>3</id>
</person>
</soapenv:Body>
</soapenv:Envelope>
I have another RESTful service with GET method and id as part of the URL itself, and that works fine. The ESB config looks like
<property name="REST_URL_POSTFIX" expression="get-property('PERSON_ID')" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
<endpoint>
<address uri="http://<<my system>>:8080/cfs/person" format="rest"/>
</endpoint>
Appreciate any pointers or help.
If your service can be invoked as http://<<my system>>:8080/myapp/person/id, you can read the id from the SOAP request and send it using "REST_URL_POSTFIX" property as below.
<property name="REST_URL_POSTFIX" expression="//person/id" scope="axis2" type="STRING"/>
Take a look at this example which implements a similar scenario.
You can also try using the HTTP Endpoint which is new in ESB 4.7.0. You can define a URI Template much like in the REST API. Populating the template variables is done via property mediators - so anything you can do with a property mediator can be used to define the endpoint URL during mediation run time.
my issue is i am getting data or json or string from front end or mobile app which contains details of error i need append the all details in single txt file in my local system
how can i do this we have any mediator for this or may i use vfs trnsport let me knoe i tried with this code giving error
My config is:
<proxy xmlns="http://ws.apache.org/ns/synapse" name="FileWrite" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<log level="custom">
<property name="sequence" value="fileWriteSequence"/>
</log>
<log>
<property name="transport.vfs.ReplyFileName" expression="fn:substring-after(get-property('MessageID'), 'urn:uuid:')"/>
<property name="OUT_ONLY" value="true"/>
</log>
<send>
<endpoint>
<address uri="///home/youtility2/Desktop/Errorlog"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
<parameter name="transport.vfs.ContentType">text/plain</parameter>
<description></description>
</proxy>
Error throing from esb side
2013-04-01 15:58:04,707] ERROR - ClientUtils The system cannot infer the transport information from the ///home/youtility2/Desktop/Errorlog URL.
[2013-04-01 15:58:04,708] ERROR - Axis2Sender Unexpected error during sending message out
org.apache.axis2.AxisFault: The system cannot infer the transport information from the ///home/youtility2/Desktop/Errorlog URL.
at org.apache.axis2.description.ClientUtils.inferOutTransport(ClientUtils.java:81
) have any refernace let me know.
You need to append the "transport.vfs.Append=true" to out-file URI to append the data in to the existing file... There is a thread regarding this in stackoverflow see the [1]. For more details regarding the VFS please refer the [2].
[1] How to append response message to a text file?
[2] http://docs.wso2.org/wiki/display/ESB403/VFS+Transport
Regards,
Mohan
Sequence Defined inside Log mediator.Define like below
<inSequence>
<log level="full"/>
<property name="sequence" value="fileWriteSequence"/>
<property name="transport.vfs.ReplyFileName" expression="fn:substring-after(get-property('MessageID'), 'urn:uuid:')"/>
<property name="OUT_ONLY" value="true"/>
<send>
<endpoint>
<address uri="///home/youtility2/Desktop/Errorlog"/>
</endpoint>
</send>
</inSequence>
I have a situation where in i have created a proxy service where in i have used an inline wsdl so that i can pass some data from try-it tool. After that i want to get the data passed from try-it to in seq that i am able to do.After that there is no use of inline-wsdl. In the "in-sequence" i have used a custom class mediator inside which i have set a property called "user" and i have saved that property value(user) in Property mediator. Now i want to send this property as response through out sequence.How to do this. Please help...
My Proxy code is:
<proxy xmlns="http://ws.apache.org/ns/synapse" name="ProviderPublication" transports="https,http" statistics="disable" trace="disable" startOnLoad="false">
<target>
<inSequence>
<property xmlns:xs="http://www.openandaway.org/xml/BBC/" name="URI" expression="//xs:SessionID" scope="default" type="STRING"/>
<payloadFactory>
<format>
<p:Session xmlns:p="http://www.openandaway.org/xml/BBC/">
<xs:ChannelURI xmlns:xs="http://www.openandaway.org/xml/BBC/">$1</xs:ChannelURI>
</p:Session>
</format>
<args>
<arg xmlns:ns="http://org.apache.synapse/xsd" expression="get-property('URI')"/>
</args>
</payloadFactory>
<log level="full">
<property name="Inside_In_Sequence" value="---------Hi i am inside in sequence--------------"/>
</log>
<log level="full">
<property name="PropValFromURI" expression="get-property('URI')"/>
</log>
</inSequence>
<outSequence>
<log level="full">
<property name="Inside_Out_Sequence" value="-------Hi inside Out Sequence--------------------"/>
</log>
<property name="GetDataFromINSeq" expression="get-property('URI')" scope="default" type="STRING"/>
<log level="full">
<property name="GetPropValueFromInSeq" expression="get-property('URI')"/>
</log>
<send/>
</outSequence>
</target>
<publishWSDL uri="http://10.224.188.87:2425/BBC1.0/services/BBCPublicationService?wsdl"/>
<description></description>
</proxy>
In the above code i am not able to get inside the out sequence. It is not hitting the out-sequence.Where am i doing wrong?
In your proxy's in-sequence, I dont see you sending the message to any backend. i.e. I dont see a send part in the in-sequence. Without the message going out, there is no way for a response to come to the out-sequence.
Regarding your original question on accessing properties set by your class mediator, if you set the property with the scope set to "synapse", you can access it within any place in your proxy.
You can access that property at the outsequence. Please refer following threads.
stWSO2ESB OutSequence Processing
Pass property from inSequence to outSequence