Camel Body null check for a particular field is failing - apache-camel

This is my apache camel route where I am null checking for a particular
field in the body of the camel but I am getting a SimpleIllegalSyntaxException.
<choice>
<when>
<simple>${body} != null</simple>
<log message="body field is not null" />
</when>
<otherwise>
<log message="body field is null" />
</otherwise>
</choice>
For the following route I am getting the following error:
Caused by:
org.apache.camel.language.simple.types.SimpleIllegalSyntaxException:
expected symbol whiteSpace
How ever I am able to get value of <log message="body not null: ${body[CDR_JSON_FORM]}"/> in the log.
I am trying to get a row from the table <from uri="sql:{{sql.RecycleHBRSelectQuery}}"/> and the idea to check to if a particular field from the row extracted is null or not.

Related

How can I fail a camel-route when exec returns nonzero?

I'm processing files with a Camel route like this:
<route>
<from uri="file:inbox?delete=true"/>
<recipientList>
<simple>exec://process.sh?args=inbox/${file:name}</simple>
</recipientList>
<log message="processed ${file:name}: ${body.stdout} ${body.stderr}"/>
</route>
Now I'd like the route to fail when process.sh finishes with nonzero exit-code. I found ${headers.CamelExecExitValue} but don't really know what to do with it.
In the example above, the file should not get deleted when process.sh fails. In my actual use-case, the route consumes files from a JMS queue and I want the file to stay in the queue. I think this can be done with <transacted/> but need to know how to fail the route.
I found How to define exception to be thrown through ref in Apache Camel which in combination with CamelExecExitValue lets me abort this way:
<route>
<from uri="file:inbox?delete=true"/>
<to uri="exec://process.sh"/>
<choice>
<when>
<simple>
${headers.CamelExecExitValue} != 0
</simple>
<throwException exceptionType="java.lang.RuntimeException" message="failed importing ${file:name}: ${body.stdout} ${body.stderr}"/>
</when>
</choice>
<log message="processed ${file:name}"/>
</route>
A bit verbose for my taste but works fine.

Is HttpOperationFailedException available with apache's http4?

I'm using apache camel http4 for http server.
JBoss Fuse Karaf container 6.3.0.redhat-310
has bundled camel-core 2.17 - (2.17.0.redhat-630310)
I'm trying to use exception class
org.apache.camel.component.http.HttpOperationFailedException
to catch HTTP response exceptions.
However, the associated routes fail to start due to
Caused by: java.lang.ClassNotFoundException: org.apache.camel.component.http.HttpOperationFailedException
I've added as dependency camel-http, with no change, still fails.
It doesn't seem that this class is included anymore?
Q. Is the Caused by: java.lang.ClassNotFoundException: org.apache.camel.component.http.HttpOperationFailedException available to http4 servers, or does anyone know what I'm doing wrong.
<when id="w2">
<ognl>request.headers.TKNDB == true</ognl>
<process id="a3" ref="assetUploadProcessor"/>
<setHeader headerName="CamelHttpUri" id="h1">
<simple>${header.UPLOADURL}</simple>
</setHeader>
<setHeader headerName="CamelHttpMethod" id="h2">
<constant>GET</constant>
</setHeader>
<doTry id="_doTry1">
<to id="http4-1" uri="http4://d1e53858-2903-4c21-86c0-95edc7a5cef2.predix-uaa.run.aws-usw02-pr.ice.predix.io:443/oauth/token?throwExceptionOnFailure=false"/>
<doCatch id="_doCatch1">
<exception>org.apache.camel.component.http.HttpOperationFailedException</exception>
<onWhen>
<simple>${header.HTTP_RESPONSE_CODE} range "400..600"</simple>
</onWhen>
<log id="_log2" loggingLevel="ERROR" message="HTTP FAILURE - HTTP Response Code: ${header.HTTP_RESPONSE_CODE}"/>
</doCatch>
</doTry>
<log id="l1" loggingLevel="INFO" message="JSON Response: ${body}"/>
<process id="jsonmapperassets" ref="jsonMapperAssets"/>
<split id="as1"
YEILDS
... because of org.apache.camel.component.http.HttpOperationFailedException
Caused by: java.lang.ClassNotFoundException: org.apache.camel.component.http.HttpOperationFailedException
I've also tried using the Global exception handler using
<exception>org.apache.camel.component.http.HttpOperationFailedException</exception>
<continued>true</continued>
</onException>
but, here I get an ERROR exception saying Continued cannot have children????
thank you!
I was facing the same issue, the correct class is
org.apache.camel.http.common.HttpOperationFailedException
it should there inside the http4 component
I just used the simple language to achieve what I needed. I simply need to continue the route after an http error code not OK or 200. this approach works, but I would love to understand how to accomplish this with my original question.
can this be done with camel's http4 component, it appears these classes are not part of it anymore?
also, if someone can, what is wrong with my onException clause?
thanks!
this is what I did
<to id="http4-1" uri="http4://d1e53858-2903-4c21-86c0-95edc7a5cef2.predix-uaa.run.aws-usw02-pr.ice.predix.io:443/oauth/token?throwExceptionOnFailure=false"/>
<choice>
<when>
<simple>${header.CamelHttpResponseCode} == '200'</simple>
<log id="l1" loggingLevel="INFO" message="JSON Response Body: ${body}"/>
<process id="jsonmapperassets" ref="jsonMapperAssets"/>
<split id="as1"
strategyRef="tsAggregationStrategy" streaming="true">
<simple>${body}</simple>
<log id="al6" loggingLevel="INFO" message="Split line ${body}"/>
<process id="p1" ref="getAssets"/>
</split>
<process id="getassetlisting" ref="getAssetListing"/>
<split id="as2"
strategyRef="tsAggregationStrategy" streaming="true">
<simple>${body}</simple>
<log id="sl2" loggingLevel="INFO" message="Split assets ${body}"/>
<process id="gtags" ref="setTagURL"/>
<to id="surl" ref="setURL"/>
</split>
</when>
<otherwise>
<log id="logError1" loggingLevel="ERROR" message="HTTP FAILURE - Response Code: ${header.CamelHttpResponseCode}"/>
<log id="logError2" loggingLevel="ERROR" message="HTTP FAILURE - Response: ${header.CamelHttpResponseText}"/>
</otherwise>
</choice>

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: How do we parse URL path for Servlet input and then use it as parameters in SQL output

I'd wish to create a general restful service to perform crud operations using Camel, but without using Restlet, just plain Servlet and SQL components.
I am familiar with Camel for 2 days only, and I am not able to get URL parts in order to use them in SQL query.
The idea is to use only XML configuring. I tried many doferent ways, came to using javascript. Here's my code so far (below).
I can't get how to check the url path, parse out it's parts, put into "message body" which should be a source for parameters for SQL query.
In this particular scenario it says, "ReferenceError: "response" is not defined".
<route>
<from uri="servlet:///crud?matchOnUriPrefix=true"/>
<setBody>
<javaScript><![CDATA[
request.headers.get("CamelHttpPath").match(/\/(\w+)\/(\w*)/)
]]></javaScript>
</setBody>
<choice>
<when>
<javaScript><![CDATA[
request.headers.get("CamelHttpMethod") == "GET" &&
response.body != null
]]></javaScript>
<to uri="sql:select * from person where id=2?dataSource=dataSource"></to>
</when>
<when>
<xpath>$CamelHttpMethod = 'POST'</xpath>
<transform>
<simple>Update!!! path - ${header.CamelHttpPath}, url - ${header.CamelHttpUrl}, uri = ${header.CamelHttpUri}, base uri = ${header.CamelHttpBaeUri}. And request is ${header.CamelHttpServletRequest}</simple>
</transform>
</when>
<when>
<xpath>$CamelHttpMethod = 'PUT'</xpath>
<transform>
<simple>Insert</simple>
</transform>
</when>
<when>
<xpath>$CamelHttpMethod = 'DELETE'</xpath>
<transform>
<simple>Delete...</simple>
</transform>
</when>
<otherwise>
<transform>
<simple>Unsupported method ${header.CamelHttpMethod} (${header.CamelHttpPath})</simple>
</transform>
</otherwise>
</choice>
</route>

stop route in otherwise

hi have a route like this
<route id="route1">
<from uri="activemq:queuer1"/>
<choice>
<when>
<simple>${header.urn} regex '^user*'</simple>
<to uri="xslt:classpath:/xslt/rdf/user.xsl"/>
</when>
<when>
<simple>${header.urn} regex '^userdata:.*'</simple>
<to uri="xslt:classpath:/xslt/rdf/userdata.xsl"/>
</when>
....
<otherwise>
<setHeader headerName="errorMsg ">
<constant>no xsl file for this type</constant>
</setHeader>
<to uri="activemq:error"/>
</otherwise>
</choice>
<process ref="importer"/>
</route>
Now if the route goes into the otherwise part, the message should not be processed.
Can I somehow stop the route if message goes into the otherwise ?
On possibility would be I add the process part in all the when parts and delete it at the end.
But we have already several when parts and more to come.
Other solution would be prefered.
You can add a <stop/> to stop continue routing the message.
In Java code:
exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);
In Java DSL:
.when(simple("${in.header.CamelHttpResponseCode} == 404"))
.stop()
.otherwise()
...

Resources