JoeTheSchmoe
JoeTheSchmoe

Reputation: 25

Apache Camel Restlet - CORS Issue

This route works, and works nicely using SoapUI:

from("restlet:http://localhost:8484/restletTestService/submit?restletMethod=POST")
    .routeId("myRestletSubmitRoute")
    .unmarshal().json(JsonLibrary.Jackson, MyRequest.class)
    .to("bean:myServiceSubmitProcessor")
    .marshal().json(JsonLibrary.Jackson, MyResponse.class);

Using SoapUI, I can post jsons that conform to the structure of the MyRequest class and I get a json back with the info I think I should.

However, when I created a quick angularjs page to allow users to build a request on the fly and then 'POST' to my rest endpoint, well:

XMLHttpRequest cannot load http://localhost:8484/restletAddressService/addressPersonator/submit. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 405.

I'm pretty sure that I have to somehow set the header Access-Control-Allow-Origin to some value on the restlet URI, but I haven't a clue how to do that. I researched the documentation (http://camel.apache.org/restlet.html) and googled it, but I'm not finding anything that has helped.

Does anyone know the answer? Thanks!

UPDATE

So the answer from fiw gave me the hint I needed along with some google research on HOW a call to a rest resource determines what's allowed (plus quite a bit of good-ole-fashioned trial and error :) ). This is what wound up working:

from("restlet:http://localhost:8484/restletTestService/submit?restletMethod=POST")
    .routeId("myRestletSubmitRoutePOST")
    .unmarshal().json(JsonLibrary.Jackson, MyRequest.class)
    .to("bean:myServiceSubmitProcessor")
    .marshal().json(JsonLibrary.Jackson, MyResponse.class)
    .setHeader("Access-Control-Allow-Headers", constant("Content-Type"))
    .setHeader("Access-Control-Allow-Origin", constant("*"));

from("restlet:http://localhost:8484/restletTestService/submit?restletMethod=OPTIONS")
    .routeId("myRestletSubmitRouteOPTIONS")
    .setHeader("Access-Control-Allow-Headers", constant("Content-Type"))
    .setHeader("Access-Control-Allow-Origin", constant("*"));

Thanks!

Upvotes: 1

Views: 2203

Answers (1)

fiw
fiw

Reputation: 756

I think you need to set Access-Control-Allow-Origin as a header on the response with a value of * as you've suggested. So to do this in camel:

from("restlet:http://localhost:8484/restletTestService/submit?restletMethod=POST")
    .routeId("myRestletSubmitRoute")
    .unmarshal().json(JsonLibrary.Jackson, MyRequest.class)
    .to("bean:myServiceSubmitProcessor")
    .marshal().json(JsonLibrary.Jackson, MyResponse.class)
    .setHeader("Access-Control-Allow-Origin", constant("*"));

This came from reading: enable cors on your server.

Upvotes: 2

Related Questions