Enabling CORS and Enabling Options Method for a Camel Servlet Rest Endpoint

77 Views Asked by At

I have a Camel Route which generates a Rest Consumer that uses the Servlet Component as below:

from("rest://post:order/checkout")
  .process()
  .end()

This starts a Consumer as below:

cl=org.apache.camel.spring.SpringCamelContext : Route: my-route-consumer started and consuming from: servlet:/order/checkout?httpMethodRestrict=POST

The POST endpoint works as expected and if I do an OPTIONS call to this endpoint then I do get a 200 response but without any CORS header like "Access-Control-Allow-Origin". The Header "Allow" with value "POST,OPTIONS" is seen in the response of OPTIONS call.

To enable CORS for this and for the OPTIONS call to this endpoint to return CORS headers, I tried to use restConfiguration as below.

restConfiguration()
      .enableCORS(true)
      .component("servlet")
      .corsHeaderProperty("Access-Control-Allow-Origin", "*");

from("rest://post:order/checkout")
      .process()
      .end()

This starts a Consumer as below:

cl=org.apache.camel.spring.SpringCamelContext : Route: my-route-consumer started and consuming from: servlet:/order/checkout?httpMethodRestrict=POST%2COPTIONS&optionsEnabled=true

The problem is that I do not see the "Access-Control-Allow-Origin" Headers and also the original POST endpoint route gets invoked when I do the OPTIONS call which I do not expect to happen.

How should I configure the restConfiguration or servlet component in such a way that CORS is enabled for the exposed endpoint and it returns the CORS headers but does not invoke the original route?

0

There are 0 best solutions below