Fabrizio Stellato
Fabrizio Stellato

Reputation: 1891

How to enable CORS with JAX RS API 1.1

I'm using JBoss 6.4 EAP with Resteasy 2.3.10 Final and jaxrs api spec 1.1 (all of these libraries are already provided within the container).

I miss ContainerResponseFilter because it's contained into jaxrs 2.0 spec, therefore this SO question doesn't work in my case. Which method could I use to enable CORS ?

Upvotes: 0

Views: 953

Answers (1)

Fabrizio Stellato
Fabrizio Stellato

Reputation: 1891

I had to implement standard javax.servlet.Filter in this way:

package javax.servlet;

import java.io.IOException;

import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;

@WebFilter(urlPatterns="/*")
public class ApiOriginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse) response;
        res.addHeader("Access-Control-Allow-Origin", "*");
        res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
        res.addHeader("Access-Control-Allow-Headers", "Content-Type, api_key, Authorization");

        chain.doFilter(request, response);

    }

    @Override
    public void destroy() {

    }

}

Upvotes: 3

Related Questions