Camel-Restlet producer message body is null - apache-camel

When invoking a rest endpoint using camel-restlet, the post body is not set.
Here is my blueprint.xml
<camelContext id="blueprint-bean-context-SF"
xmlns="http://camel.apache.org/schema/blueprint">
<route id="Camel-Restlet-Client">
<from uri="timer://example?repeatCount=1&period=1000" />
<setHeader headerName="Content-Type">
<constant>application/json</constant>
</setHeader>
<setHeader headerName="api-key">
<constant>{{drupal-api-key}}</constant>
</setHeader>
<setBody><constant>{name: "paul rudd", movies: ["I Love You Man", "Role Models"]}</constant></setBody>
<log message="Request: ${headers} \n ${body}" />
<to uri="restlet:https://reqres.in/api/users?restletMethod=POST" />
<log message="Response: ${headers} \n ${body}" />
</route>
</camelContext>
I am using camel 2.20.1. Any help is appreciated?

I am new to cmel myself, but i would guess the problem is the setHeader you are using. Because setHeader sets the header of the Camel-Message, not the header of the http-request which gets send over the wire. You would need to set a header on the message, that would be known by restlet. And restlet would transport the value of the setheader to the theader of the http-request.
Sorry but I dont how to do that. The following coding works, using the Header for authentification:
restConfiguration("restlet").bindingMode(RestBindingMode.json); // use json for all
from("timer://example?repeatCount=1&period=1000")
.setHeader("CamelRestletLogin", constant("xxx"))
.setHeader("CamelRestletPassword", constant("xxx"))
.to("restlet:https://xxx.hana.ondemand.com:443/ain/services/api/v1/models")
Sry I couldnt help more.

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}.

How to create a timer based camel polling route?

i created a route that is supposed to read from an OPC-UA endpoint. The read operation should be performed every second, based on a timer. Every example i found shows that the route can only have one from item. My route looks like this:
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="opctorest">
<from uri="timer://simpleTimer?period=1000"/>
<log message="Triggered Route: opctorest: Sensorreading body: ${body}"/>
<to uri="milo-client:tcp://0.0.0.0:4840/freeopcua/server?nodeId=2&namespaceUri=http://examples.freeopcua.github.io"/>
<convertBodyTo type="java.lang.String"/>
<to uri="stream:out"/>
</route>
</camelContext>
When i deploy the route, it gets called every second, but it writes to the endpoint, since the call is declared in a to element. How can i turn this into a read? I could not find a solution so far. Thanks!
Use the .enrich() to turn it into a read when you want to read in the middle of a route.
http://camel.apache.org/content-enricher.html
For your example something similar to (not tested):
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="opctorest">
<from uri="timer://simpleTimer?period=1000"/>
<log message="Triggered Route: opctorest: Sensorreading body: ${body}"/>
<enrich uri="milo-client:tcp://0.0.0.0:4840/freeopcua/server?nodeId=2&namespaceUri=http://examples.freeopcua.github.io"/>
<convertBodyTo type="java.lang.String"/>
<to uri="stream:out"/>
</route>
</camelContext>

Extracting the body from an ActiveMQ message via Camel

I have a route using the Spring DSL as such
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="activemq:queue:worker?mapJmsMessage=false" />
<convertBodyTo type="java.lang.String"/>
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<to uri="http://localhost/queue" />
</route>
</camelContext>
The message type is an ActiveMQTextMessage. I am able to POST the message to the HTTP URL, but what I get seems to be the toString() output:
ActiveMQTextMessage {commandId = 5, responseRequired = false, message....
I would like to call the getText() method on the ActiveMQTextMessage instance to populate the route, but I cannot figure out how to get that method called. I am quite sure I could get this to work in code, but I need to do everything via XML.
Figured out the problem. I had mapJmsMessage=false set to handle an exception a few days ago. I removed it and suddenly it worked fine.

How to call URL from Apache Camel in blueprint and return the data to a text file?

I am using Apache Camel blueprints, my route is triggered from a URL at port 8081 on localhost. This route generates a file but the problem is the file is a binary file without the HTML showing.
I then navigate my browser to http://localhost:8081/foo which triggers the URL.
What is it that I don't understand that is causing it to output a binary file rather than a text file? I guess that I must transform the body somehow?
<route id="url1">
<from uri="netty4-http:http://0.0.0.0:8081/foo" />
<to uri="http://www.google.com/?bridgeEndpoint=true" />
<to uri="file:/test"/>
</route>
Update: I think the problem is that the content coming back from www.google.com is gzip. When I look at the bridgeEndpoint parameter in the documentation it mentions something about gzip... now I am not sure, as I tried it on another website and it still doesn't work.
This line in my log might be relevant.
writing body: DefaultFullHttpResponse(decodeResult: success,
version: HTTP/1.1, content: UnpooledUnsafeDirectByteBuf(ridx: 0, widx: 0, cap: 0))
Update: I discover if I do:
<from uri="timer:secondfoo?period=20s" />
Replacing the from, then it works. Hmm... Something flowing from the netty4-http causes problem.
Update: I found something which works! Obsession pays off.
<route id="url1">
<from uri="netty-http:http://0.0.0.0:8081/foo" />
<removeHeaders pattern="*" />
<setBody>
<simple></simple>
</setBody>
<setHeader headerName="CamelHttpMethod">
<constant>GET</constant>
</setHeader>
<to uri="http://www.google.com/?bridgeEndpoint=true" />
<to uri="file:/test"/>
</route>
You can convert the message to a String type then its text based
<route id="url1">
<from uri="netty4-http:http://0.0.0.0:8081/foo" />
<to uri="http://www.google.com/?bridgeEndpoint=true" />
<convertBodyTo type="String"/>
<to uri="file:/test"/>
</route>

Resources