I am having a Camel route with several steps configured in Spring XML. After the last step I want to set the a header entry Content-Type using a value from the Exchange properties. How can I achieve that?
<route id="servlet.direct">
<from uri="direct:onlinePlatformExport"/>
<bean ref="exportService" method="doSomething"/>
<to uri="smooks://META-INF/spring/a.xml"/>
<bean ref="charsetConverterService" method="convertBody"/>
<setHeader headerName="Content-Type">
<constant>text/xml</constant>
</setHeader>
</route>
I found the answer. You can access the exchange object using Groovy.
<setHeader headerName="Content-Type">
<groovy>"text/xml; charset=${exchange.properties[<your-key>]}"</groovy>
</setHeader>
You can do it as following:
<setHeader headerName="Content-Type">
<simple>${property.myProperty}</simple>
</setHeader>
Related
I have a spring project using apache camel.
I want to try the services provided in camel-context.xml using a postman request.
How can I infer the path variables and endpoint?
The method I want to use is specified like below on camel-context.xml and I have parameters providing the contains conditions.
<route>
<from uri="netty:udp://{{camel.netty.server}}:{{camel.netty.udp.port}}?sync=false&allowDefaultCodec=true;" />
<convertBodyTo type="java.lang.String" charset="ISO-8859-9" />
<choice>
<when>
<simple trim="true">${bodyAs(String)} contains '"ISN":"90"'</simple>
<bean ref="Feaser" method="run" cache="false"></bean>
<to uri="mock:result" />
</when>
...
Postman doesn't support sending on UDP.
I get suggestions of Multicast,where a single request is routed to multiple endpoints as the snippet below.But the requirement is sending multiple requests in parallel to a single endpoint.
<route>
<from uri="direct:start"/>
<multicast>
<to uri="mock:first"/>
<to uri="mock:second"/>
<to uri="mock:third"/>
</multicast>
</route>
You can use parallelProcessing option.
<route>
<from uri="direct:start"/>
<multicast parallelProcessing="true">
<to uri="mock:first"/>
<to uri="mock:second"/>
<to uri="mock:third"/>
</multicast>
</route>
I'm trying to do a request response call inside a route which is actually called from the outside in an request response way. Now it seems this is not working, it looks like the outer replyTo/correlation id is lost.
Following situation:
Client
jmsRequest to myFirstQueue with replyTo=temp://xyz
Route
<route id="foo">
<from uri="jms:myFirstQueue"/>
<to uri="debugProcessor1"/>
<to uri="jms:myNestedIn?replyTo=myNestedOut" pattern="InOut"/>
<to uri="debugProcessor2"/>
</route>
<route id="myNestedFoo">
<from uri="jms:myNestedIn"/>
<to uri="someDummyProcessor"/>
<to uri="jms:myNestedOut" pattern="InOnly"/>
</route>
In debugProcessor1 I still see JMSReplyTo correctly set to temp://xyz, unfortunately in debugProcessor2 this is lost and therefore the client does not get back the response.
Figured it out, my debug processors actually destroyed it, without them it works!
I'm trying to create an application that gets information from a browser and drops it in a queue. That data is then picked up from the queue and sent through an application for security. The security app should drop it in a different queue when it is done to be picked up by a separate action application.
Could anyone help me along with the routing? Basically, the route I'm looking for is:
Browser/UI -> Qnonsecure -> security app -> QSecure -> action app
What I understand now is the following:
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="jms:queue:QnonSecure"/>
<to uri="jms:queue:QSecure"/>
</route>
</camelContext>
How Can I change this to route to and from applications.
How do I send the input from the browser into QnonSecure? Also, where in my code do I call the security app between the QnonSecure and QSecure?
There is more than one possible solution. Take the following route as a starting point:
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="restlet:http://localhost:8081/myApp?restletMethod=post"/>
<to uri="jms:queue:QnonSecure" pattern="InOut" />
<enrich uri="direct:securityApp"/>
<choice>
<when>
<simple>${header.myHeader} == "SECURE"</simple>
<to uri="jms:queue:QSecure" pattern="InOut" />
<to uri="direct:actionApp" />
</when>
<otherwise>
<!-- handle non valid requests -->
</otherwise>
</choice>
</route>
</camelContext>
Steps:
The browser sends a POST request to the Camel restlet component. This could be done via JavaScript, a link and/or just a ordinary submit button.
The body is sent to jms:queue:QnonSecure. As we use the InOut pattern, this is done in a synchronous manner and the response is fetched.
The response of jms:queue:QnonSecure is sent to direct:securityApp where the credentials are tested. If they are correct, the header myHeader is set to SECURE (or any other value).
In the choice statement, myHeader is tested. In the secure case, jms:queue:QSecure and finally direct:actionApp is invoked.
With the java JMS API, I got from a DB an array of bytes and then I'm sending it to an ActiveMQ as a javax.jms.BytesMessage. After that with camel I want to put the file on a location,
I have this route in camel:
<route>
<from uri="activemq:queue.fileOuput"/>
<convertBodyTo type="java.nio.ByteBuffer"/>
<to uri="file://C:/output/"/>
</route>
But my problem is that my file in c:\output\ directory, I got the file with the message id as the file name, like
queue-preVerificacion-fileoutput-ID-jmachine-57401-1347652410053-0-1-1-1-1
but I want to put the name I have in the database, like MyFile.xml.
I have tried to set a message property like fileName and file:name, and also I saw in the apache document that I need to put a header "org.apache.camel.file.name", but with jms I don't know how to do it.
So my question is how can I put a customized name in the camel route?
Thanks to all.
Just place the file name in the jms message (as a string property).
// Something like this if you send the message using plain java/jms:
msg.setStringProperty("filename","MyFile.xml");
..//Send msg
Then you can do something like this in camel
<to uri="file://C:/output/?fileName=${header.filename}"/>
you just need to set the "CamelFileName" header value (based on a message header, etc)
<route>
<from uri="activemq:queue.fileOuput"/>
<convertBodyTo type="java.nio.ByteBuffer"/>
<setHeader headerName="CamelFileName">
<constant>${header.fileName}</constant>
</setHeader>
<to uri="file://C:/output/"/>
</route>
I think "org.apache.camel.file.name" is for camel 1.x , in the 2.x version CamelFileName worked fine. But I wanted a more dynamic file name, name based on the content. This example using a processor worked well ( camel 2.18 )
<route>
<from uri="MQ:MY_Q_NAME" />
<process ref="MyMessageProcessor"/>
<to uri="file://E:\OUTPUT" />
</route>
Inside the Processor :
exchange.getIn().setHeader(Exchange.FILE_NAME, myFileName);