Can this handling of a null body in Apache Camel be more elegant?

apache-camel, code-cleanup, java

Solution

You can use an interceptor, such as interceptFrom with a when, to check for the empty bod, as there is an example of here: http://camel.apache.org/intercept

And then use stop to indicate no further processing:

interceptFrom("servlet*").when(body().isNull()).to("direct:syntaxError").stop();

Problem

I'm new to Camel and trying to learn idioms and best practices. I am writing web services which need to handle several different error cases. Here is my error handling and routing: ``` onException(JsonParseException.class).inOut("direct:syntaxError").handled(true); onException(UnrecognizedPropertyException.class).inOut("direct:syntaxError").handled(true); // Route service through direct to allow testing. from("servlet:///service?matchOnUriPrefix=true").inOut("direct:service"); from("direct:service") .choice() .when(body().isEqualTo(null)) .inOut("direct:syntaxError") .otherwise() .unmarshal().json(lJsonLib, AuthorizationParameters.class).inOut("bean:mybean?method=serviceMethod").marshal().json(lJsonLib); ``` As you can see, I have special handling (content based routing) to deal with a request with a null body. Is there a way to handle this more elegantly? I'm writing several services of this type and it seems like they could be much cleaner.

Original source