How to set a message payload to a apache camel rest request - rest

I need to set a JSON content as the message payload of a apache camel rest request. I tried it as follows. But it doesn't work.
<rest path="/service" consumes="application/json" produces="application/json">
<put type="com.xxxx.yyyyy.esb.models.UserServiceMapping"
uri="/create">
<route>
<log message="${body.serviceUrl}" />
<log message="${body.mappedServiceObject}" />
<log message="${body.systemUserDetails.userName}" />
<log message="${body.systemUserDetails.password}" />
<setBody> <simple>{"${body.mappedServiceObject}":{"userName" :
"${body.userName}", "password" : "${body.password}"}} </simple> </setBody>
<setHeader headerName="CamelRestletLogin">
<simple>admin</simple>
</setHeader>
<setHeader headerName="CamelRestletPassword">
<simple>admin</simple>
</setHeader>
<to
uri="restlet:http://xxx.xxx.x.xx:8081/xxxxxx/services/yyyyyyy/V1.0/users/create?restletMethod=POST" />
</route>
</put>
</rest>

Related

NoTypeConversionAvailableException: camel rest dsl

I have a web service like this:
<camelContext id="camel-CallAPI" xmlns="http://camel.apache.org/schema/blueprint" >
<!--Web service starts working -->
<restConfiguration bindingMode="auto" component="restlet" host="localhost" port="8889"/>
<rest path="/check">
<post uri="/version" consumes="application/json" produces="application/json">
<to uri="direct:first"/>
</post>
</rest>
<route>
<from uri="direct:first"/>
<setHeader headerName="Content-Type" id="header_contentType">
<constant>application/json</constant>
</setHeader>
<log message="after set header :: ${body}"/>
<to uri="http4://..."/>
<convertBodyTo type="java.lang.String"/>
<log message="the result of the testCheckLastVersion :: ${body}"/>
I'm sending a json content to this web service with the help of postman, and I need my web service to send that json to another API that has been mentioned in direct:first.
but when I call my web service in postman an error is happening:
org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: {version=apk.1.10.0} of type: java.util.LinkedHashMap on: Message[ID-localhost-localdomain-1549348033140-14-4]. Caused by: No type converter available to convert from type: java.util.LinkedHashMap to the required type: java.io.InputStream with value {version=apk.1.10.0}.
can any one help me with this?
I found the solution, the exception caused because I was set the bindingMode="auto" in camelContext which was not suitable here, by removing that, the problem solved.
<camelContext id="camel-CallAPI" xmlns="http://camel.apache.org/schema/blueprint" >
<!--Web service starts working -->
<restConfiguration component="restlet" host="localhost" port="8889"/>
<rest path="/say">
<post uri="/hi" consumes="application/json" produces="application/json">
<to uri="direct:first"/>
</post>
</rest>
<route>
<from uri="direct:first"/>
<setHeader headerName="Content-Type" id="_setHeader2">
<constant>application/json</constant>
</setHeader>
<to uri="http4://..."/>
<convertBodyTo type="java.lang.String"/>
<log message="the result of the testCheckLastVersion :: ${body}"/>
</route>
</camelContext>

Routing acording to xml nodename

<Body>
<ProductMasterDataStatus>
<Status>INVALID</Status>
<Errors>
<Error source="HUB">
<Description>Data Validation Error (Product code length is invalid.)</Description>
<Code>#A16</Code>
</Error>
</Errors>
</ProductMasterDataStatus>
</Body>
I want to check the ProductMasterDataStatus is in this xml if it is there i want to change the route in camel route.
<camelContext id="cbr-example-context" xmlns="http://camel.apache.org/schema/spring">
<route id="_route1">
<from id="_from1" uri="file:///d:/in"/>
<camel:choice id="_choice1">
<camel:when id="_when1">
<camel:xpath>
</camel:xpath>
<camel:to id="_to1" uri="file:C:/outbox_Denmark"/>
</camel:when>
<process id="_process_transform" ref="myTransform"/>
<to id="_to1" uri="file:///d:/out?fileName=Product.xml"/>
</route>
The xpath expression is //ProductMasterDataStatus, so you should check if this is not null.
<camelContext id="cbr-example-context" xmlns="http://camel.apache.org/schema/spring">
<route id="_route1">
<from id="_from1" uri="file:///d:/in"/>
<camel:choice id="_choice1">
<camel:when id="_when1">
<camel:xpath>//ProductMasterDataStatus != null</camel:xpath>
<camel:to id="_to1" uri="file:C:/outbox_Denmark"/>
</camel:when>
<process id="_process_transform" ref="myTransform"/>
<to id="_to1" uri="file:///d:/out?fileName=Product.xml"/>
</route>

How To exit from a Loop In Apache camel/Jboss

I need to exit from a loop when the body contains 'SUCCEDDED' in Jboss/Apache camel. I have tried using Stop() but that doesnt work since it stops every connection(eg:DBConnection).
Here is the sample code:
60
10000
${body}
contains 'SUCCEEDED'
<CXF bean call />
<log loggingLevel="INFO"
message="response
: ${body}">
<description>Logs the 2nd SOAP Response.</description>
</log>
<setBody>
<xpath>{{status.xpath}}</xpath>
</setBody>
<convertBodyTo type="java.lang.String" />
</otherwise>
</choice>
</loop>
<to uri="direct:loopend">
</to>
</route>

WSO2 ESB unable to remove ws-security header in the response before DSS call

I have a proxy service which needs to call a exernal service with ws security. I have to
call the service, and based on the response, I need to extract some information
and then call a data service to update the database. As I get the response and create the
payload to call the data service it is also having the ws security header which ends up in an exception.
I have used
<header action="remove" name="wsse:Security" scope="default"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" />
while creating and calling the data service but end up with no luck.
Below is my proxy service.
<?xml version="1.0" encoding="UTF-8"?>
<proxy name="EDI_Test_Proxy_2" startOnLoad="true" trace="disable"
transports="http https" xmlns="http://ws.apache.org/ns/synapse">
<target>
<inSequence>
<header name="Action" scope="default" value="get_mcash_data" />
<property name="Content-Type" scope="transport" type="STRING"
value="application/soap+xml; charset=UTF-8; action=get_mcash_data" />
<call>
<endpoint>
<address trace="disable"
uri="http://localhost:9770/services/my_fetch_data_service" />
</endpoint>
</call>
<property expression="//mc:mcash/mc:tran_id/text()" name="tran_id"
scope="default" type="STRING" xmlns:mc="http://ws.wso2.org/dataservice"
xmlns:ns="http://org.apache.synapse/xsd" />
<log level="custom">
<property expression="$ctx:tran_id" name="tran_id" />
</log>
<filter xmlns:mc="http://ws.wso2.org/dataservice" xmlns:ns="http://org.apache.synapse/xsd"
xpath="boolean(//mc:mcash/mc:mobile_no)">
<then>
<property expression="//mc:mcash/mc:tran_id/text()" name="tran_id"
scope="default" type="STRING" />
<property expression="//mc:mcash/mc:mobile_no/text()"
name="mobile_no" scope="default" type="STRING" />
<property expression="//mc:mcash/mc:tran_amt/text()" name="tran_amt"
scope="default" type="STRING" />
<property expression="//mc:mcash/mc:tran_date/text()"
name="tran_date" scope="default" type="STRING" />
<property expression="//mc:mcash/mc:tran_time/text()"
name="tran_time" scope="default" type="STRING" />
<property expression="//mc:mcash/mc:part_tran_srl_num/text()"
name="part_tran_srl_num" scope="default" type="STRING" />
<log level="custom">
<property expression="$ctx:tran_id" name="tran_id" />
<property expression="$ctx:mobile_no" name="mobile_no" />
<property expression="$ctx:tran_amt" name="tran_amt" />
<property expression="$ctx:tran_date" name="tran_date" />
<property expression="$ctx:tran_time" name="tran_time" />
<property expression="$ctx:part_tran_srl_num" name="tran_time" />
</log>
<payloadFactory description="pf_mcash" media-type="xml">
<format>
<flw:purchaceFromMMR xmlns:flw="http://flw.mwt.mobitel.com/">
<!--Optional: -->
<bankPurchaseRequest xmlns="">
<amount>$1</amount>
<!--Optional: -->
<bankCode>XXX</bankCode>
<!--Optional: -->
<date>$2</date>
<!--Optional: -->
<mobile>$3</mobile>
<!--Optional: -->
<time>$4</time>
<!--Optional: -->
<transactionId>$5</transactionId>
</bankPurchaseRequest>
</flw:purchaceFromMMR>
</format>
<args>
<arg evaluator="xml" expression="$ctx:tran_amt" />
<arg evaluator="xml" expression="$ctx:tran_date" />
<arg evaluator="xml" expression="$ctx:mobile_no" />
<arg evaluator="xml" expression="$ctx:tran_time" />
<arg evaluator="xml" expression="$ctx:tran_id" />
</args>
</payloadFactory>
<call>
<endpoint>
<address trace="disable"
uri="https://my_external_web_service/to_be/called?wsdl">
<enableSec policy="gov:ws-policy/sample_policy.xml" />
</address>
</endpoint>
</call>
<loopback />
</then>
<else>
<log>
<property name="STATUS" value="*****No data available*****" />
</log>
</else>
</filter>
</inSequence>
<outSequence>
<header action="remove" name="wsse:Security" scope="default"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" />
<header action="remove" name="To" scope="default" />
<property expression="//return/mobile/text()" name="mobile"
scope="default" type="STRING" />
<property expression="//return/date/text()" name="date"
scope="default" type="STRING" />
<property expression="//return/recipetNo/text()" name="recipetNo"
scope="default" type="STRING" />
<property expression="//return/resultCode/text()" name="resultCode"
scope="default" type="STRING" />
<property expression="//return/resultDesc/text()" name="resultDesc"
scope="default" type="STRING" />
<payloadFactory description="pf_mcash_update"
media-type="xml">
<format>
<p:TestUpdate xmlns:p="http://ws.wso2.org/dataservice">
<!--Exactly 1 occurrence -->
<p:trf_status>$1</p:trf_status>
<!--Exactly 1 occurrence -->
<p:resp_code>$2</p:resp_code>
<!--Exactly 1 occurrence -->
<p:receipt>$3</p:receipt>
<!--Exactly 1 occurrence -->
<p:rsp_message>$4</p:rsp_message>
<!--Exactly 1 occurrence -->
<p:tran_id>$5</p:tran_id>
<!--Exactly 1 occurrence -->
<p:part_tran_srl_num>$6</p:part_tran_srl_num>
</p:TestUpdate>
</format>
<args>
<arg value="10" />
<arg evaluator="xml" expression="$ctx:resultCode" />
<arg evaluator="xml" expression="$ctx:recipetNo" />
<arg evaluator="xml" expression="$ctx:resultDesc" />
<arg value="SDC311521" />
<arg evaluator="xml" expression="$ctx:part_tran_srl_num" />
</args>
</payloadFactory>
<header name="Action" scope="default" value="TestUpdate" />
<property name="Content-Type" scope="transport" type="STRING"
value="application/soap+xml; charset=UTF-8; action=TestUpdate" />
<call>
<endpoint>
<address trace="disable"
uri="http://my_internal_data_service/which_ends_up_with/ws_sec_header" />
</endpoint>
</call>
</outSequence>
<faultSequence />
</target>
</proxy>
Any advice is very much appreciated.
Can you add the following log mediator before the call mediator in the outsequence and update this with the output logs.
it will help us to identify the issue.
<log level="full">
<property name="OutSequence" value="==== OUTSEQ ===="/>
</log>
The issue was I have engaged rampart module <module ref="rampart" /> in the axis2.xml found in <ESB_HOME>repository/conf/axis2/ . After commenting out this line the exception was gone and the dss call worked. The reason I believe, if this line of code is enabled, ws-securiy is engaged globally and all the service calls will look for ws-security header. Please correct me if I'm wrong in anyway.

Apache Camel Route Multicast does not copy the exchange message

i made a camel route in my application with spring dsl.I want to copy the exchange body with using multicast EIP.But when i use this, i can take the exchange bodyfrom one of my endpoint.In the second one (which is endpoint in multicast ) ,the exchange body is null.Why this is happening like this?
EDIT
This is my route:
<route errorHandlerRef="dlc1" id="mobitRoute1">
<from uri="cxf:bean:mobit-okuma?dataFormat=PAYLOAD" />
<wireTap uri="activemq:queue:anaMobitQueue" />
<to uri="velocity:response.vm" />
</route>
<route id="mobitRoute2" errorHandlerRef="dlc2">
<from uri="activemq:queue:anaMobitQueue" />
<unmarshal ref="myJaxb" />
<to uri="bean:timeChanging" />
<multicast>
<to uri="activemq:queue:mobitOkumaq" />
<to uri="activemq:queue:AysMobit" />
</multicast>
</route>
<route errorHandlerRef="dlc3" id="mobitRoute3">
<from uri="activemq:queue:AysMobit" />
<!-- <unmarshal ref="myJaxb" /> -->
<to uri="bean:fromPayload" />
<to uri="cxf:bean:ays-service?dataFormat=POJO" />
</route>
<route errorHandlerRef="dlc4" id="mobitRoute4">
<from uri="activemq:queue:mobitOkumaq" />
<to uri="cxf:bean:mobit-okumaReal?dataFormat=POJO" />
</route>
I found the solution by transforming xml when i put the exchange to the queue.This is why ? I don't know.
<route errorHandlerRef="dlc1" id="mobitRoute1">
<from uri="cxf:bean:mobit-okuma?dataFormat=PAYLOAD" />
<wireTap uri="activemq:queue:anaMobitQueue" />
<to uri="velocity:response.vm" />
</route>
<route errorHandlerRef="dlc1" id="mobitRoute2">
<from uri="activemq:queue:anaMobitQueue" />
<camel:pipeline>
<unmarshal ref="myJaxb" />
<to uri="bean:timeChanging" />
<!-- <to uri="direct:unmarshalled"/> -->
<marshal ref="myJaxb" />
<camel:multicast>
<to uri="activemq:queue:BegisMobit" />
<to uri="activemq:queue:AysMobit" />
</camel:multicast>
</camel:pipeline>
</route>
<route errorHandlerRef="dlc2" id="mobitRoute3">
<from uri="activemq:queue:AysMobit" />
<unmarshal ref="myJaxb" />
<to uri="bean:fromPayload" />
<to uri="cxf:bean:ays-service?dataFormat=POJO" />
</route>
<route errorHandlerRef="dlc3" id="mobitRoute4">
<from uri="activemq:queue:BegisMobit" />
<unmarshal ref="myJaxb" />
<to uri="cxf:bean:mobit-okumaReal?dataFormat=POJO" />
</route>