@Test public void testNoEntity() throws IOException { EncodingFilter filter = initializeAndGetFilter(); ContainerRequest request = RequestContextBuilder.from("/resource", "GET").header(HttpHeaders.ACCEPT_ENCODING, "gzip").build(); ContainerResponse response = new ContainerResponse(request, Response.ok().build()); filter.filter(request, response); assertNull(response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING)); assertNull(response.getHeaders().getFirst(HttpHeaders.VARY)); }
@Override public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException { if (!response.hasEntity()) { return; } List<String> varyHeader = response.getStringHeaders().get(HttpHeaders.VARY); if (varyHeader == null || !varyHeader.contains(HttpHeaders.ACCEPT_ENCODING)) { response.getHeaders().add(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); } if (response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING) != null) { return; } List<String> acceptEncoding = request.getHeaders().get(HttpHeaders.ACCEPT_ENCODING); if (acceptEncoding == null || acceptEncoding.isEmpty()) { return; } List<ContentEncoding> encodings = new ArrayList<>(); for (String input : acceptEncoding) { String[] tokens = input.split(","); for (String token : tokens) { try { ContentEncoding encoding = ContentEncoding.fromString(token); encodings.add(encoding); } catch (ParseException e) { Logger.getLogger(EncodingFilter.class.getName()).log(Level.WARNING, e.getLocalizedMessage(), e); } } } Collections.sort(encodings); encodings.add(new ContentEncoding(IDENTITY_ENCODING, -1)); SortedSet<String> acceptedEncodings = new TreeSet<>(getSupportedEncodings()); boolean anyRemaining = false; String contentEncoding = null; for (ContentEncoding encoding : encodings) { if (encoding.q == 0) { if ("*".equals(encoding.name)) { break; } acceptedEncodings.remove(encoding.name); } else { if ("*".equals(encoding.name)) { anyRemaining = true; } else { if (acceptedEncodings.contains(encoding.name)) { contentEncoding = encoding.name; break; } } } } if (contentEncoding == null) { if (anyRemaining && !acceptedEncodings.isEmpty()) { contentEncoding = acceptedEncodings.first(); } else { throw new NotAcceptableException(); } } if (!IDENTITY_ENCODING.equals(contentEncoding)) { response.getHeaders().putSingle(HttpHeaders.CONTENT_ENCODING, contentEncoding); } }
EncodingFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException { if (!response.hasEntity()) { return; } List<String> varyHeader = response.getStringHeaders().get(HttpHeaders.VARY); if (varyHeader == null || !varyHeader.contains(HttpHeaders.ACCEPT_ENCODING)) { response.getHeaders().add(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); } if (response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING) != null) { return; } List<String> acceptEncoding = request.getHeaders().get(HttpHeaders.ACCEPT_ENCODING); if (acceptEncoding == null || acceptEncoding.isEmpty()) { return; } List<ContentEncoding> encodings = new ArrayList<>(); for (String input : acceptEncoding) { String[] tokens = input.split(","); for (String token : tokens) { try { ContentEncoding encoding = ContentEncoding.fromString(token); encodings.add(encoding); } catch (ParseException e) { Logger.getLogger(EncodingFilter.class.getName()).log(Level.WARNING, e.getLocalizedMessage(), e); } } } Collections.sort(encodings); encodings.add(new ContentEncoding(IDENTITY_ENCODING, -1)); SortedSet<String> acceptedEncodings = new TreeSet<>(getSupportedEncodings()); boolean anyRemaining = false; String contentEncoding = null; for (ContentEncoding encoding : encodings) { if (encoding.q == 0) { if ("*".equals(encoding.name)) { break; } acceptedEncodings.remove(encoding.name); } else { if ("*".equals(encoding.name)) { anyRemaining = true; } else { if (acceptedEncodings.contains(encoding.name)) { contentEncoding = encoding.name; break; } } } } if (contentEncoding == null) { if (anyRemaining && !acceptedEncodings.isEmpty()) { contentEncoding = acceptedEncodings.first(); } else { throw new NotAcceptableException(); } } if (!IDENTITY_ENCODING.equals(contentEncoding)) { response.getHeaders().putSingle(HttpHeaders.CONTENT_ENCODING, contentEncoding); } } }
EncodingFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException { if (!response.hasEntity()) { return; } List<String> varyHeader = response.getStringHeaders().get(HttpHeaders.VARY); if (varyHeader == null || !varyHeader.contains(HttpHeaders.ACCEPT_ENCODING)) { response.getHeaders().add(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); } if (response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING) != null) { return; } List<String> acceptEncoding = request.getHeaders().get(HttpHeaders.ACCEPT_ENCODING); if (acceptEncoding == null || acceptEncoding.isEmpty()) { return; } List<ContentEncoding> encodings = new ArrayList<>(); for (String input : acceptEncoding) { String[] tokens = input.split(","); for (String token : tokens) { try { ContentEncoding encoding = ContentEncoding.fromString(token); encodings.add(encoding); } catch (ParseException e) { Logger.getLogger(EncodingFilter.class.getName()).log(Level.WARNING, e.getLocalizedMessage(), e); } } } Collections.sort(encodings); encodings.add(new ContentEncoding(IDENTITY_ENCODING, -1)); SortedSet<String> acceptedEncodings = new TreeSet<>(getSupportedEncodings()); boolean anyRemaining = false; String contentEncoding = null; for (ContentEncoding encoding : encodings) { if (encoding.q == 0) { if ("*".equals(encoding.name)) { break; } acceptedEncodings.remove(encoding.name); } else { if ("*".equals(encoding.name)) { anyRemaining = true; } else { if (acceptedEncodings.contains(encoding.name)) { contentEncoding = encoding.name; break; } } } } if (contentEncoding == null) { if (anyRemaining && !acceptedEncodings.isEmpty()) { contentEncoding = acceptedEncodings.first(); } else { throw new NotAcceptableException(); } } if (!IDENTITY_ENCODING.equals(contentEncoding)) { response.getHeaders().putSingle(HttpHeaders.CONTENT_ENCODING, contentEncoding); } }  }
EncodingFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException { if (!response.hasEntity()) { return; } List<String> varyHeader = response.getStringHeaders().get(HttpHeaders.VARY); if (varyHeader == null || !varyHeader.contains(HttpHeaders.ACCEPT_ENCODING)) { response.getHeaders().add(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); } if (response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING) != null) { return; } List<String> acceptEncoding = request.getHeaders().get(HttpHeaders.ACCEPT_ENCODING); if (acceptEncoding == null || acceptEncoding.isEmpty()) { return; } List<ContentEncoding> encodings = new ArrayList<>(); for (String input : acceptEncoding) { String[] tokens = input.split(","); for (String token : tokens) { try { ContentEncoding encoding = ContentEncoding.fromString(token); encodings.add(encoding); } catch (ParseException e) { Logger.getLogger(EncodingFilter.class.getName()).log(Level.WARNING, e.getLocalizedMessage(), e); } } } Collections.sort(encodings); encodings.add(new ContentEncoding(IDENTITY_ENCODING, -1)); SortedSet<String> acceptedEncodings = new TreeSet<>(getSupportedEncodings()); boolean anyRemaining = false; String contentEncoding = null; for (ContentEncoding encoding : encodings) { if (encoding.q == 0) { if ("*".equals(encoding.name)) { break; } acceptedEncodings.remove(encoding.name); } else { if ("*".equals(encoding.name)) { anyRemaining = true; } else { if (acceptedEncodings.contains(encoding.name)) { contentEncoding = encoding.name; break; } } } } if (contentEncoding == null) { if (anyRemaining && !acceptedEncodings.isEmpty()) { contentEncoding = acceptedEncodings.first(); } else { throw new NotAcceptableException(); } } if (!IDENTITY_ENCODING.equals(contentEncoding)) { response.getHeaders().putSingle(HttpHeaders.CONTENT_ENCODING, contentEncoding); } }  @SafeVarargs static void enableFor(ResourceConfig rc, Class<? extends ContentEncoder>... encoders); @Override void filter(ContainerRequestContext request, ContainerResponseContext response); }
EncodingFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException { if (!response.hasEntity()) { return; } List<String> varyHeader = response.getStringHeaders().get(HttpHeaders.VARY); if (varyHeader == null || !varyHeader.contains(HttpHeaders.ACCEPT_ENCODING)) { response.getHeaders().add(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); } if (response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING) != null) { return; } List<String> acceptEncoding = request.getHeaders().get(HttpHeaders.ACCEPT_ENCODING); if (acceptEncoding == null || acceptEncoding.isEmpty()) { return; } List<ContentEncoding> encodings = new ArrayList<>(); for (String input : acceptEncoding) { String[] tokens = input.split(","); for (String token : tokens) { try { ContentEncoding encoding = ContentEncoding.fromString(token); encodings.add(encoding); } catch (ParseException e) { Logger.getLogger(EncodingFilter.class.getName()).log(Level.WARNING, e.getLocalizedMessage(), e); } } } Collections.sort(encodings); encodings.add(new ContentEncoding(IDENTITY_ENCODING, -1)); SortedSet<String> acceptedEncodings = new TreeSet<>(getSupportedEncodings()); boolean anyRemaining = false; String contentEncoding = null; for (ContentEncoding encoding : encodings) { if (encoding.q == 0) { if ("*".equals(encoding.name)) { break; } acceptedEncodings.remove(encoding.name); } else { if ("*".equals(encoding.name)) { anyRemaining = true; } else { if (acceptedEncodings.contains(encoding.name)) { contentEncoding = encoding.name; break; } } } } if (contentEncoding == null) { if (anyRemaining && !acceptedEncodings.isEmpty()) { contentEncoding = acceptedEncodings.first(); } else { throw new NotAcceptableException(); } } if (!IDENTITY_ENCODING.equals(contentEncoding)) { response.getHeaders().putSingle(HttpHeaders.CONTENT_ENCODING, contentEncoding); } }  @SafeVarargs static void enableFor(ResourceConfig rc, Class<? extends ContentEncoder>... encoders); @Override void filter(ContainerRequestContext request, ContainerResponseContext response);  }