robyp7
robyp7

Reputation: 491

apache camel RouteBuilder not work when passing header/param name from url

i'm watching a camel example about Servlet, it works with this xml camel definition (camel-context.xml):

 <camelContext xmlns="http://camel.apache.org/schema/spring">

    <route id="helloRoute">
      <!-- incoming requests from the servlet is routed -->
      <from uri="servlet:hello"/>
      <choice>
        <when>
          <!-- is there a header with the key name? -->
          <header>name</header>
          <!-- yes so return back a message to the user -->
          <transform>
            <simple>Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today? ****</simple>
          </transform>
        </when>
        <otherwise>
          <!-- if no name parameter then output a syntax to the user -->
          <transform>
            <constant>Add a name parameter to uri, eg ?name=foo</constant>
          </transform>
        </otherwise>
      </choice>
    </route>

It works with uri /hello and /hello?name=foo. I'm trying to replace xml dsl with Java dsl, like this (the camel context start one time when Servlet context start and stop when web application stop):

@WebListener
public class CamelRoutingInitializer implements ServletContextListener {

    DefaultCamelContext camctx;
    ServletContext ctx;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ctx = sce.getServletContext();
        RouteBuilder builder = new RouteBuilder() {
            /**
             *like camel-config.xml but with Java DSL syntax 
             * @see http://camel.apache.org/java-dsl.html
             */
            @Override
            public void configure() throws Exception {
                from("servlet:camel")
                .choice()
                .when(header("name").isEqualTo("name"))
                .transform(body().append("Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today? ****"))
                .otherwise()
                .transform(body().append("Add a name parameter to uri, eg ?name=yourname"));    
                ctx.log("** Route config ok");
            }
        };
        DefaultCamelContext camctx = new DefaultCamelContext();
        try {
            camctx.addRoutes(builder);
            camctx.start();
            System.out.println("** CAMEL STARTED...");
            ctx.log("** CAMEL STARTED...");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        if (camctx!=null && camctx.isStarted()){
            try {
                camctx.stop();
                ctx.log("** CAMEL STOPPED...");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

if uri is /camel i get "Add a name parameter to uri, eg ?name=yourname" but the same thing happens using "/camel?name=foo" (instead of "Hi I am xxx Hello foo how are you today? ****")

What's wrong with it? webapplication uses both (camel-config.xml and CamelRoutingInitializer class).

thanks

roby

Upvotes: 1

Views: 1096

Answers (1)

anon
anon

Reputation:

I think your issue is on the line header("name").isEqualTo("name"). It is expecting that the value of the header be equal to the literal "name". You should be simply stating header("name"), just like in the xml dsl.

Upvotes: 2

Related Questions