class ETagFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext request, ContainerResponseContext response) {
        
        // if our response object has a string ETag header value we're going
        // to replace it with its corresponding EntityTag object since the
        // the jersey GZIPResponse handler expects that object. 
        
        // We could be either setting the in the container response (this is when
        // we're returning not-modified response since we're creating the container
        // response directly) or in the servlet response (this is where we're just
        // returning our data set with the header included). 
        
        // So we'll check in the container response first and if we don't have 
        // anything there we'll check in the servlet response. If we find the 
        // header in the servlet response, we're going to remove it and set a new
        // entity tag object in the container response
        
        String etagStr = null;
        if (response.getHeaders().containsKey(HttpHeaders.ETAG)) {
            etagStr = (String) response.getHeaders().getFirst(HttpHeaders.ETAG);
        }
        if (etagStr == null && servletResponse != null) {
            etagStr = servletResponse.getHeader(HttpHeaders.ETAG);
            if (etagStr != null) {
                servletResponse.setHeader(HttpHeaders.ETAG, null);
            }
        }
        if (etagStr != null) {
            etagStr = removeLeadingAndTrailingQuotes(etagStr);
            response.getHeaders().putSingle("ETag", new EntityTag(etagStr));
        }
    }

}