'Apache Camel: using compound conditions in when()
I am using Apache Camel DSL route and want to check if the body is not null
and body does not contains substring like authenticate failed
. In java something like:
if(body != null && !body.contains("authenticate failed")) {
//Do something.
}
Apache Camel DSL:
.choice()
.when(body().contains("authenticate failed"))
.log("after choice body().contains('authenticate failed') body: ${body}")
.when(body().isNotNull()) //Here I want to add the addiontional condition to this when `body not contains authenticate failed`.
How can I write a condition like this? predicates objects in the process method and write tin my case?
Solution 1:[1]
You can use PredicateBuilder to create pretty composite predicates. Here's example how to do simple Body is not null or empty check with PredicateBuilder.
package com.example;
import org.apache.camel.Predicate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.PredicateBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class ExampleTests extends CamelTestSupport {
@Test
public void testValidBody() throws Exception {
MockEndpoint resultMockEndpoint = getMockEndpoint("mock:notNull");
resultMockEndpoint.expectedMessageCount(1);
template.sendBody("direct:predicateExample", "Hello world!");
resultMockEndpoint.assertIsSatisfied();
}
@Test
public void testEmptyBody() throws Exception {
MockEndpoint resultMockEndpoint = getMockEndpoint("mock:nullOrEmpty");
resultMockEndpoint.expectedMessageCount(1);
template.sendBody("direct:predicateExample", null);
resultMockEndpoint.assertIsSatisfied();
}
@Test
public void testNullBody() throws Exception {
MockEndpoint resultMockEndpoint = getMockEndpoint("mock:nullOrEmpty");
resultMockEndpoint.expectedMessageCount(1);
template.sendBody("direct:predicateExample", null);
resultMockEndpoint.assertIsSatisfied();
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
Predicate bodyNotNullOrEmpty = PredicateBuilder.and(
body().isNotNull(),
body().isNotEqualTo("")
);
from("direct:predicateExample")
.routeId("predicateExample")
.choice().when(bodyNotNullOrEmpty)
.log("Received body: ${body}")
.to("mock:notNull")
.otherwise()
.log("Body was null or empty!")
.to("mock:nullOrEmpty")
.end()
.log("done");
}
};
}
}
Not and Or can take lists of predicates as arguments or even other composite predicates which allows one to make some fairly complex composite predicates. However when using PredicateBuilder starts to get overly verbose it's good to just use processor or bean instead to abstract away some of the complexity.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Pasi Österman |