How to configure a Servlet to HazelCast route using Camel - apache-camel

I want to build a route something like
<route>
<from uri="servlet:///user?matchOnUriPrefix=true"/>
<to uri="direct:put"/>
</route>
<route>
<from uri="direct:put"/>
<setHeader headerName="CamelHazelcastOperationType">
<constant>put</constant>
</setHeader>
<to uri="hazelcast:map:foo"/>
</route>
ie everything that matches POST:/user/{cachename}/{key1} should take the key1 as key and place the payload under key:key1 to map:{cachename}.
Same thing for
GET:/user/{cachename}/{key1} should take the {key1} as key and retrieve the payload under key1 from map:{cachename}.
Any Help will be highly appreciated.
Thanks,

You should have a number of Camel headers available from the incoming servlet, such as:
CamelHttpMethod = GET
CamelHttpPath = /user/{cachename}/{key1}
You could use code or an expression language to extract the information from there, a very basic example would be:
<setHeader headerName="cachename">
<simple>${header.CamelHttpPath.split("/")[2]}</simple>
</setHeader>
<setHeader headerName="key1">
<simple>${header.CamelHttpPath.split("/")[3]}</simple>
</setHeader>

Related

Camel blueprint <setHeader headerName="CamelHttpPath" id="_setHeader1"><el>${header.uriPattern}</el></setHeader> waiting for dependencies

I have the following Camel Context.
<camelContext id="_camuatomicservicecontext" xmlns="http://camel.apache.org/schema/blueprint">
<route id="_camuatomicserviceroute1">
<from id="_from1" uri="direct-vm:camuatomicservice">
<description>accepts vm messages directly </description>
</from>
<log id="_log1" message="Camu Atomic Service body = ${body}, header= ${header.uriPattern}"/>
<!-- <to id="_to1" uri="restlet:protocol:localhost:8189/"/> -->
<setHeader headerName="api.key" id="_setHeader1">
<constant>replace later with properties api.key Does not matter for this poc</constant>
</setHeader>
<setHeader headerName="CamelHttpPath" id="_setOutHeader1">
<el>${header.uriPattern}</el>
</setHeader>
<to id="_to1" pattern="InOut" uri="netty4-http:http:localhost:8189/path"/>
<log id="_log2" message="CamuAtomicService Response body ${body}"/>
</route>
</camelContext>
From the documentation I expect the CamelHttpPath header to override the endpoint configuration "/path" such that calling Facade Services can pass the header.uriPattern in and dynamically change the resource they want to access. The bundle worked fine until I added the setHeader for CamelHttpPath and now getting "Waiting for dependencies." I assume I need to install a feature, but Simple EL in other bundles on that server work already so not sure what feature I need to install.
Instead of I used and it worked fine. The choice was farther down in the options.

Unable to send body,subject in mail from camel spring SMTP component

I'm writing one basic camel application using camel-spring.I'm able to send email but Im not able to send body and subject in it.I have tried several ways but only thing im getting in mail body is file contents.Please provide your inputs if you have any idea.
<route id="notification">
<from uri="file:///home?noop=true" />
<camel:choice>
<camel:when>
<camel:simple>
${file:name} contains '{{data}}'
</camel:simple>
<camel:setHeader headerName="from">
<simple>nikhil#from.com</simple>
</camel:setHeader>
<setHeader headerName="subject">
<constant>Hello subject</constant>
</setHeader>
<setHeader headerName="contentType">
<constant>text/plain;charset=UTF-8</constant>
</setHeader>
<camel:setHeader headerName="body">
<constant>Test body</constant>
</camel:setHeader>
<camel:setBody>
<constant>Test camel set body</constant>
</camel:setBody>
<setBody>
<constant>Test set body</constant>
</setBody>
<camel:setOutHeader headerName="subject">
<simple>subject from outheader</simple>
</camel:setOutHeader>
<setHeader headerName="subject">
<constant>Status of check report extractor</constant>
</setHeader>
<camel:to uri="smtps://smtp.server.com:465?username=user#gmail.com&password=pass&to=receiver#yahoo.com&subject=${subject}"></camel:to>
</camel:when>
</camel:choice>
</route>
I have added all combinations that I have tried,but they are not workingThanks in advance.
Please, give a try to this route.
<route id="notification">
<from uri="file:///home?noop=true" />
<setHeader headerName="from">
<constant>nikhil#from.com</constant>
</setHeader>
<setHeader headerName="subject">
<constant>Hello subject</constant>
</setHeader>
<setHeader headerName="contentType">
<constant>text/plain;charset=UTF-8</constant>
</setHeader>
<setBody>
<constant>Test camel set body</constant>
</setBody>
<to uri="smtps://smtp.server.com:465?username=user#gmail.com&password=pass&to=receiver#yahoo.com&from=${header.from}&subject=${header.subject}&contentType=${header.contentType}"/>
</route>
Then we can check the "when/simple" condition.
Note that all the stored headers in the route may be retrieved with ${header.X}.

Terminate the current camel exchange

I am processing file in cluster environment. The cluster works fine. It is being processed on Only one server.
But on the second server It identifies as duplicates but still execute the form route delete=true
ERROR:
org.apache.camel.component.file.GenericFileOperationFailedException: Cannot delete file:
I am setting header CamelRouteStop to true but the exchange still try's to delete a file, instead of stop executing the route.
All I need is to end the route if it is duplicate.
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="smb:url?delete=true"/>
<idempotentConsumer messageIdRepositoryRef="myRepo">
<header>messageId</header>
<setHeader headerName="fileExist">
<simple>true</simple>
</setHeader>
</idempotentConsumer>
<when>
<simple>${header.fileExist} == null</simple>
<log message="File ${header.CamelFileName} processing/processed by other Nodes - DUPLICATE" loggingLevel="INFO" />
<setHeader headerName="CamelRouteStop">
<simple <simple resultType="java.lang.Boolean">true</simple>>true</simple>
</setHeader>
</when>
</route>
</camelContext>
For CamelRouteStop you need to use setProperty, not setHeader.

apache camel hazelcast getting custom header

I am trying to get the header or property which i set before calling hazelcast but i couldn't able to get the custom header which i set.
<route>
<from uri=""/>
<setHeader headerName="CamelHazelcastObjectId">
<constant>123</constant>
</setHeader>
<setHeader headerName="CamelHazelcastOperationType">
<constant>put</constant>
</setHeader>
<setHeader headerName="test">
<constant>test</constant>
</setHeader>
<setProperty propertyName="test">
<constant>test</constant>
</setProperty>
<to uri="hazelcast:map:testMap"/>
</route>
When i trying to retrieve my custom header or property couldn't able to get that
<route>
<from uri="hazelcast:map:testMap"/>
<log message = "printing:::: ${in.header.test} and ${property[test]}"/>
<to uri=""/>
Please help on this.
The camel-hazelcast component is only for storing the message body, not message headers.

Apache Camel: Send empty body ws

How can i call a SOAP web service with an empty message body using Apache Camel?
For example, the final endpoint on a route would be the invocation of a method on my proxy that takes 0 arguments.
EDIT:
example xml configuration:
<route id="someRoute">
<from uri="ref:activemq-queue"/>
<setHeader headerName="operationName">
<constant>invoke</constant>
</setHeader>
<to uri="cxf:bean:someWS"/>
</route>
...
<cxf:cxfEndpoint id="someWS" address="${ws.address}"
serviceClass="com.example.ws.SomeWS"
The problem is that the method 'invoke' on the WS takes 0 arguments, and an exception is thrown stating that 1 argument is being received. Is there a way for me to specify to ignore this received input?
You can set the message body to be null, if the invocation just take 0 argument. null simple expression is added since camel 2.12.3.
<route id="someRoute">
<from uri="ref:activemq-queue"/>
<setBody>
<simple>null</simple>
</setBody>
<setHeader headerName="operationName">
<constant>invoke</constant>
</setHeader>
<to uri="cxf:bean:someWS"/>
</route>
I also needed to set an empty body to Apache Camel xml and the solution below worked for me.
<setBody id="set-empty-body">
<constant/>
</setBody>
Hope this helps to whoever need it.
The xml configuration will fail if the content of the body is not with the correct type of the class required by the method of your bean. By default, it could be an Object and not a String. When the method recieves the body in an incorrect format, it would process a null value, instead of the body of the route. To solve this problem, you will need to convert the body into the expected class of the method of your bean. For example, if the method's argument of the bean is a String, then you could do something like:
<route id="someRoute">
<from uri="ref:activemq-queue"/>
<setHeader headerName="operationName">
<constant>invoke</constant>
</setHeader>
<convertBodyTo type="String"/>
<to uri="cxf:bean:someWS"/>
</route>
For more information about body conversion, you can check the docs: https://camel.apache.org/components/3.14.x/eips/convertBodyTo-eip.html

Resources