You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Gavagai <ti...@gmail.com> on 2014/07/22 21:20:42 UTC

Adding HTTP Headers to All Jetty Routes

Hi all -

I am trying to add some headers to the exchange's "out" on all routes.
Specifically, I would like to add the following to support CORS:

    exch.out.headers['Access-Control-Allow-Origin'] = '*'
    exch.out.headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE,
GET, OPTIONS'
    exch.out.headers['Access-Control-Request-Method'] = '*'
    exch.out.headers['Access-Control-Allow-Headers'] = 'Origin,
X-Requested-With, Content-Type, Accept, Authorization, Cookie'

I do not understand how to do this for all routes. It works great when it is
done in a processor, but I obviously don't want to have to add these to each
processor. I tried intercepting the message using interceptFrom(), but the
headers so defined are not retained when the message is forwarded on to the
next destination. I also tried setting properties on the exchange, but this
presents problems when forming the response.

What is the best way to add HTTP headers to the messages returned from all
routes? Basically say "for every message, always add these headers as the
final step."

Tim



--
View this message in context: http://camel.465427.n5.nabble.com/Adding-HTTP-Headers-to-All-Jetty-Routes-tp5754265.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Adding HTTP Headers to All Jetty Routes

Posted by Gavagai <ti...@gmail.com>.
Thanks Claus - 

I think this has gotten me on the right track, but I'm not quite there yet
and am not sure why. Here are some snippets of code to help describe the
problem. We are using the Groovy DSL and executing using "Main."

Here is the new binding definition:

    class CORSHttpBinding extends DefaultHttpBinding {
        @Override
        public void writeResponse(exch, resp) {
            resp.Header("test", "test")
            super.writeResponse(exch, resp)
        }
    }

This is added to the Registry via "Main" as follows:

    Main main = new Main()
    main.bind("CORSHttpBinding", new CORSHttpBinding())
    .
    .
    .
    main.run()

Finally, the Jetty route is specified as follows (additional routing after
this has been omitted):

   
from("jetty:http://0.0.0.0:$port/validate/ping?httpBindingRef=CORSHttpBinding")

I've added some debugging code in various places, but "writeResponse()" is
never called.

Thanks for any help - I am fairly new to this, if that's not obvious, and am
still getting my Camel-legs.

Tim



--
View this message in context: http://camel.465427.n5.nabble.com/Adding-HTTP-Headers-to-All-Jetty-Routes-tp5754265p5754804.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Adding HTTP Headers to All Jetty Routes

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

You can use a custom http binding. See about this at
http://camel.apache.org/jetty

eg just extend the default, and add those headers in the writeResponse
method, and call its super to do the actual writing.

On Tue, Jul 22, 2014 at 9:20 PM, Gavagai <ti...@gmail.com> wrote:
> Hi all -
>
> I am trying to add some headers to the exchange's "out" on all routes.
> Specifically, I would like to add the following to support CORS:
>
>     exch.out.headers['Access-Control-Allow-Origin'] = '*'
>     exch.out.headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE,
> GET, OPTIONS'
>     exch.out.headers['Access-Control-Request-Method'] = '*'
>     exch.out.headers['Access-Control-Allow-Headers'] = 'Origin,
> X-Requested-With, Content-Type, Accept, Authorization, Cookie'
>
> I do not understand how to do this for all routes. It works great when it is
> done in a processor, but I obviously don't want to have to add these to each
> processor. I tried intercepting the message using interceptFrom(), but the
> headers so defined are not retained when the message is forwarded on to the
> next destination. I also tried setting properties on the exchange, but this
> presents problems when forming the response.
>
> What is the best way to add HTTP headers to the messages returned from all
> routes? Basically say "for every message, always add these headers as the
> final step."
>
> Tim
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Adding-HTTP-Headers-to-All-Jetty-Routes-tp5754265.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/