I have a route with multiple entry points (servlet and direct). It needs to do certain work when activated through the servlet. This work must be done for servlet requests (even in the presence of bad actors). In the case of exchanges that come through direct, this work must not be done. Here is an example in code:
// In a Route Builder somewhere.
from("servlet:///myService").inOut("direct:myService");
from("direct:myService").process(new ConditionalProcessor());
// Implementation of processor above.
public class ConditionalProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
if(comesFromServlet(exchange)){
// Logic for Servlet.
} else {
// Logic for direct and other.
}
}
/**
* Must return true if the exchange started as a request to the servlet.
* Otherwise must return false.
*
* @param exchange
* @return
*/
public boolean comesFromServlet(Exchange exchange){
// What goes here?
}
}
I was inspired by this comment from another post. Here is my solution: