3

How can I apply CORS in Apache Wink? What I basically need is adding an Access-Control-Allow-Origin: * header to every response send from Wink (where we will replace the * for the allowed origins).

Jos de Jong
  • 5,819
  • 3
  • 32
  • 51

2 Answers2

1

A possible solution can be returning a javax.ws.rs.core.Response object. Using the javax.ws.rs.core.Response.ResponseBuilder you can add headers to the response.

Update:

Another solution is to add a Servlet Filter (javax.servlet.Filter) on top of Wink that will add the headers to all responses.

Btw, in JAX-RS 2 it's possible to add Filters and Interceptors.

Tarlog
  • 9,679
  • 2
  • 41
  • 66
  • Yes but that would mean I have to add this header for every request I have (including a copy of every request which handles the OPTIONS request method). That is quite a lot of work and error prone. I tried to add a custom ResponseHandler to the chain, which works, but I couldn't figure out how to add a header for every response coming by. – Jos de Jong Jul 31 '13 at 18:45
  • Another solution is to add a javax.servlet.Filter on top of Wink that will add the headers to all responses. – Tarlog Aug 01 '13 at 05:22
  • Hey that should work I guess, I will give that a try. Thanks! – Jos de Jong Aug 01 '13 at 11:43
1

Late answer, but can be useful for future readers. Use the following code when you send the response back:

Response
            .status(200)
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
            .header("Access-Control-Allow-Credentials", "true")
            .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
            .header("Access-Control-Max-Age", "1209600")
            .entity(yourJsonResponse)
            .build();

Response is of type : javax.ws.rs.core.Response;

Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92