Having trouble using Bean method in Apache Camel - java

I have a Java class and a Camel-route configured with XML and am trying to call the getData method from the Camel-route, but the method is not being invoked.
Java class:
class Test {
public String getData(String name) {
return name;
}
}
Camel-route:
<route id="validate" autoStartup="true" streamCache="true">
<setHeader name="data">
<simple>
<method ref="test" method="getData('Test')"/>
</simple>
</setHeader>
</route>
In my camel-context.xml I've created the bean-reference as such:
<bean id="test" class="com.Test" />

This is resolved now.. instead of using method tag use bean tag
<bean ref="test" method="getData" />

Related

Apache Camel. How to manage with a Custom Processor the error raised at runtime

I'm trying to convert my route made with Java DSL in a route made with XML.
The following is my original route that works. And what it does is simple.
Get as input an array of integer and at runtime throws some errors.
At the end of the route I need to read all the error raised by myself and not with a long stacktrace or other long messages in console.
other lines of code...
camelContext.addRoutes(new RouteBuilder() {
#Override
public void configure() throws Exception {
onException(Exception.class)
.handled(true)
.process(new ErrorProcessor());
from("direct:start_route")
.split(body())
.processRef("payloadProcessor")
.to("direct:a")
.end()
.to("direct:d");
from("direct:a")
.beanRef("stupidBean", "stupidMethod");
from("direct:d")
.beanRef("errorProcessor", "check");
}
});
other lines of code...
The following is my xml route that doesn't work.
...
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:call.playWithPayload" />
<onException>
<exception>java.lang.Exception</exception>
<process ref="errorProcessor" />
</onException>
<split onPrepareRef="payloadProcessor">
<simple>${body}</simple>
<to uri="direct:call.a" />
</split>
<to uri="direct:call.d" />
</route>
<!--SUB ROUTE-->
<route>
<from uri="direct:call.a" />
<bean ref="stupidBean" method="stupidMethod" />
</route>
<route>
<from uri="direct:call.d" />
<bean ref="errorProcessor" method="check" />
</route>
</camelContext>
...
What I need is that the direct:call.d is called after the split.
Thanks to this I can read all the errors added into a List<Exception> that is stored into the header.
I think that the problem is in the onException management.
When I try to add the handled to reproduce the my Java DSL
<onException>
...
<handled>
<constant>
true
</constant>
</handled>
...
I got this error:
Invalid content was found starting with element 'handled'.
One of '{"http://camel.apache.org/schema/spring":description, "http://camel.apache.org/schema/spring":exception}' is expected.
Found solution.
My problem was an incorrect format of my xml route.
<onException>
<exception>java.lang.Exception</exception>
<handled>
<constant>true</constant>
</handled>
<process ref="errorProcessor" />
</onException>
now it works.

Camel CXF - Security (Authentication)

I created a webservice with apache camel using CXF component as bellow :
blueprint.xml:
<bean class="ngtrend.ws.Testws"/>
<!-- Defined the server endpoint to create the cxf-rs consumer -->
<cxf:rsServer id="rsServer" address="http://localhost:9050/route"
serviceClass="ngtrend.ws.Testws" />
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="timerToLog">
<from uri="cxfrs://bean://rsServer"/>
<to uri="bean:ngtrend.ws.HelloBean?method=test(Exchange)"/>
<log message="${body}"/>
</route>
</camelContext>
Testws.java:
public class Testws {
#GET
#Path("/test/{id}")
#Produces("application/xml")
//#Consumes("text/xml")
public Integer getAssets(#PathParam("id") int id){
return null;
}
}
and I would like to secure it forcing the customer to send ( or enter on a dialog box if using a browser) login and password (BASIC Http authentication). How can i make this configuration ?
In CXF framework, restful services authentication can be done by using the following approach:
<cxf:rsServer id="rsServer"
address="http://localhost:9050/route">
<jaxrs:serviceBeans>
<ref bean="serviceBean"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="authenticationHandler"/>
</jaxrs:providers>
</cxf:server>
<bean id="serviceBean" class="ngtrend.ws.Testws"/>
<bean id="authenticationHandler" class="yourpackage.Class" />
Create your own handler for authenticationHandler that will implement import org.apache.cxf.jaxrs.ext.RequestHandler.
Use the authentication strategy needed in this class , for example authenticate against database etc.. This should allow for basic authentication.
You can write a class which implements ContainerRequestFilter. And then set it in the cxf:providers as below:
<bean id="authenticationHandler" class="a class which implements ContainerRequestFilter" />
<cxf:rsServer id="xxxRsServer"
address="/xxxservice" serviceClass="xxx.XXXService"
loggingFeatureEnabled="true" loggingSizeLimit="20">
<cxf:providers>
<ref component-id="authenticationHandler"/>
</cxf:providers>
</cxf:rsServer>
In this way, you could override below method
public void filter(ContainerRequestContext requestContext)
For example, you could make a simple authentication based on requestContext.getHeaderString("UserPassInfo"). If succeed, do nothing, otherwise call requestContext.abortWith(Response.status(401).header("WWW-Authenticate", "Basic").build());

Mule - Set properties on spring object call in Mule flow

I'm using Mule 3.3.CE
I have a Class called SpringObject which implements Callable interface
package com.threads.test;
import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;
public class SpringObject implements Callable {
private String someData;
public String getSomeData() {
return someData;
}
public void setSomeData(String someData) {
this.someData = someData;
}
#Override
public Object onCall(MuleEventContext eventContext) throws Exception {
System.out.println("CALL SPRING --->>"+someData);
return eventContext.getMessage();
}
}
And my flow is
<http:connector name="httpConnectorEntryPoint" doc:name="HTTP\HTTPS"/>
<spring:beans xmlns="http://www.springframework.org/schema/beans">
<spring:bean id="component" name="component" class="com.threads.test.SpringObject" lazy-init="false">
</spring:bean>
</spring:beans>
<flow name="TestThreadsFlow1" doc:name="TestThreadsFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8099" path="m" connector-ref="httpConnectorEntryPoint" doc:name="HTTP"/>
<set-payload value="#["ExitA"]" doc:name="Set Payload"/>
<component doc:name="Java">
<spring-object bean="component">
<property key="someData" value="Information"/>
</spring-object>
</component>
</flow>
The problem is that when I run my flow and use the http connector, the console shows
CALL SPRING --->>null
instead of
CALL SPRING --->>Information
What could be?
you can try to configure your spring bean outside the flow as follow:
<spring:bean id="component" name="component"
class="com.threads.test.SpringObject" lazy-init="false">
<spring:property name="someData" value="Information" />
</spring:bean>
and inside the flow do:
<component>
<spring-object bean="component" />
</component>
From the property element description in the XSD:
Sets a Mule property. This is a name/value pair that can be set on
components, services, etc., and which provide a generic way of
configuring the system. Typically, you shouldn't need to use a generic
property like this, since almost all functionality is exposed via
dedicated elements. However, it can be useful in configuring obscure
or overlooked options and in configuring transports from the generic
endpoint elements.
This means it's not intended for what you are trying to use it. The appropriate way to set a property in your bean is as follows:
<spring:bean id="component" name="component" class="com.threads.test.SpringObject" lazy-init="false">
<spring:property name="someData" value="Information"/>
</spring:bean>

camel serialize json in dead letter queue

I have to stock my object (CcRequest) in a DeadLetterQueue with Json format.
How it is possible ?
Here my simple context :
<camelContext id="el1DMRCamelContext" autoStartup="true" xmlns="http://camel.apache.org/schema/blueprint" >
<template id="producerTemplate" />
<!-- Routes -->
<route id="createCcProcessorRoute" errorHandlerRef="createCcErrorHandler" >
<from uri="activemq:queue:createCc" />
<process ref="createCcProcessor" />
</route>
</camelContext>
<bean id="createCcErrorHandler" class="org.apache.camel.builder.DeadLetterChannelBuilder" >
<property name="deadLetterUri" value="activemq:queue:createCcDLQ" />
<property name="redeliveryPolicy" ref="redeliveryPolicy" />
</bean>
I would like my object (CcRequest stocked in the body of the exchange) in my activemq:queue:createCcDLQ to be in Json format instead of binaries.
Is it possible ?
I would not classify what you want to do as a dead letter handler. Dead letter is usually just moving/redelivery of the original message.
It is pretty simple you use the excetion clause instead.
<dataFormats>
<json id="jsonFormat" library="Jackson"/>
</dataFormats>
<onException>
<exception>java.lang.RuntimeException</exception>
<marshal ref="jsonFormat"/>
<to uri="activemq:queue:createCcDLQ"/>
</onException>
Camel Exception Clause docs
Camel JSON docs

Apache Camel conditional routing

I have a service which has two operations.
RegisterUser
UpdateUser
I have a camel rout:
<camel:route id="myRoute">
<camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&synchronous=true" />
<camel:bean ref="processor" method="processMessage"/>
<camel:to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/>
<camel:to uri="cxf:bean:myTargetEndpoint"/>
</camel:route>
In my processor bean, when I specify:
RegisterUser registerUser = exchange.getIn().getBody(RegisterUser.class);
I get the register user object. Everything works fine.
The problem is that I want camel to route my request conditionally, for e.g:
If the service operation is RegisterUser I want to route the message to my specific bean and if the service operation is UpdateUser I want to route the message to the other bean.
I have tried to use camel xPath, but it not seems to be working.
<camel:route id="myRoute">
<camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&synchronous=true" />
<camel:choice>
<camel:when>
<camel:xpath>
//RegisterUser
</camel:xpath>
<camel:bean ref="processor" method="processMessage"/>
<camel:to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/>
</camel:when>
</camel:choice>
<camel:to uri="cxf:bean:myTargetEndpoint"/>
</camel:route>
I was searching how to set up camel to route to the different targets but did not find anything. Maybe somebody knows where might be the problem?
The information of the operation required will be in the header of the message.
The header you are looking for is called 'operationName'
So here is an example :
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="example">
<from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&synchronous=true" />
<log message="The expected operation is :: ${headers.operationName}" />
<choice>
<when>
<simple>${headers.operationName} == 'RegisterUser'</simple>
<bean ref="processor" method="processMessage"/>
<to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/>
</when>
<when>
<simple>${headers.operationName} == 'UpdateUser'</simple>
<!-- Do the update user logic here -->
<bean ref="processor" method="updateUser" />
</when>
</choice>
<to uri="cxf:bean:myTargetEndpoint"/>
</route>
</camelContext>
(Note the example is using apache aries blueprint - but it will be identical for spring, other than the namespace)
try using camel-simple expressions instead of xpath for this...
<when><simple>${body} is 'com.RegisterUser'</simple><to uri="..."/></when>
Spring XML route
In my case I use inbound Jetty EP.
I check parametr in request.
Invole URL http://localhost:8080/srv?alg=1
<choice id="_choice1">
<when id="_when1">
<simple>${in.header.alg} == '1'</simple>
<log id="_log10" message="LOG ALG 1"/>
</when>
...
<otherwise id="_otherwise1">
<setFaultBody id="_setFaultBody1">
<constant>Return message about ERROR</constant>
</setFaultBody>
</otherwise>
</choice>
final CamelContext context = exchange.getContext();
if (isAlive) {
context.startRoute("table-reader-route");
log.info("starting dailycase route= " + response);
} else {
context.stopRoute("table-reader-route");
log.info("stoping dailycase route= " + response);
}

Categories

Resources