3

lets say for an example i have a code:

       from(servlet://abc?id={id}&name={name}).process(new Processor(){
     @Override
        public void process(Exchange arg0) throws Exception {
            id = arg0.getIn().getHeader("id", String.class);
            id_type = arg0.getIn().getHeader("name",String.class);

            System.out.println(id);
            System.out.println(name);
            String url = "//example.com/"+id+"?name="+name;
            System.out.println(url);

            /*Thread.sleep(10000);*/

        }.setHeader(Exchange.HTTP).to("http:"+url+"&bridgeEndpoint=true&throwExceptionOnFailure=false)"

I dont see my url there. its showing null value. how to solve this problem? I used to set this string in Exchange header but its giving me java.lang.IllegalArgumentException:

2 Answers 2

10

Try following route:

from("servlet://abc")  
    .process(new Processor(){
        @Override
        public void process(Exchange exchange) throws Exception {
            // Camel will populate all request.parameter and request.headers, 
            // no need for placeholders in the "from" endpoint
            String id = exchange.getIn().getHeader("id", String.class);
            String name = exchange.getIn().getHeader("name", String.class);           

            // This URI will override http://dummyhost
            exchange.getIn().setHeader(Exchange.HTTP_URI, "http://example.com");

            // Add input path. This will override the original input path.
            // If you need to keep the original input path, then add the id to the 
            // URI above instead
            exchange.getIn().setHeader(Exchange.HTTP_PATH, id);

            // Add query parameter such as "?name=xxx"
            exchange.getIn().setHeader(Exchange.HTTP_QUERY, "name="+name);     
    }
    .to("http://dummyhost")

If you request is http://localhost:8080/hello/world?id=111&name=moon, then it should be forwarded to http://example.com/111?name=moon.

Sign up to request clarification or add additional context in comments.

3 Comments

@bks4line From the Camel's doc: "If the option is true , HttpProducer will ignore the Exchange.HTTP_URI header, and use the endpoint's URI for request.". As we use Exchange.HTTP_URI, the option bridgeEndpoint is not needed. To use a custom header parameter in the URI instead, didn't work in my tests.
Thanks Peter! Thanks for helping me every single time! :)
If the solution worked for you, then don't hesitate to accept the answer... Thanks.
0

The url cannot be know when camel setup the route.

You can use Exchange.HTTP_URI message header to override the setting of http endpoint.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.