Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.tectonica.jersey1;
 import  com.sun.jersey.spi.container.ContainerRequest;
 import  com.sun.jersey.spi.container.ContainerResponse;
 import  com.sun.jersey.spi.container.ContainerResponseFilter;

Jersey filter for adding CORS headers to the response of the REST APIs where needed

Zach Melamed
public class CorsFilter implements ContainerResponseFilter
	public ContainerResponse filter(ContainerRequest request, ContainerResponse response)
		if (!isCorsNeeded(request))
			return response;
		MultivaluedMap<StringObjectheaders = response.getHttpHeaders();
		headers.add("Access-Control-Allow-Methods""GET, POST, OPTIONS");
		String acrh = request.getHeaderValue("Access-Control-Request-Headers");
		if (acrh != null && !acrh.isEmpty())
		return response;

returns whether or not a given request needs CORS header in its response. For example:
 return request.getPath().startsWith("debug");
	protected boolean isCorsNeeded(ContainerRequest request)
		return true;
New to GrepCode? Check out our FAQ X