@Override public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { Header contentEncoding = entity.getContentEncoding(); if (contentEncoding != null) { HeaderElement[] codecs = contentEncoding.getElements(); for (HeaderElement codec : codecs) { if (codec.getName().equalsIgnoreCase(GZIP)) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } if (codec.getName().equalsIgnoreCase(DEFLATE)) { response.setEntity(new DeflateDecompressingEntity(response.getEntity())); return; } } } } }
private void handleContentEncoding(HttpResponse response) throws ServletException { HttpEntity entity = response.getEntity(); if (entity != null) { Header contentEncodingHeader = entity.getContentEncoding(); if (contentEncodingHeader != null) { HeaderElement[] codecs = contentEncodingHeader.getElements(); LOGGER.debug("Content-Encoding in response:"); for (HeaderElement codec : codecs) { String codecname = codec.getName().toLowerCase(); LOGGER.debug(" => codec: " + codecname); if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } else if ("deflate".equals(codecname)) { response.setEntity(new DeflateDecompressingEntity(response.getEntity())); return; } else if ("identity".equals(codecname)) { return; } else { throw new ServletException("Unsupported Content-Encoding: " + codecname); } } } } }
/** * Handles the following {@code Content-Encoding}s by * using the appropriate decompressor to wrap the response Entity: * <ul> * <li>gzip - see {@link GzipDecompressingEntity}</li> * <li>deflate - see {@link DeflateDecompressingEntity}</li> * <li>identity - no action needed</li> * </ul> * * @param response the response which contains the entity * @param context not currently used * * @throws HttpException if the {@code Content-Encoding} is none of the above */ public void process( final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); // It wasn't a 304 Not Modified response, 204 No Content or similar if (entity != null) { Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (HeaderElement codec : codecs) { String codecname = codec.getName().toLowerCase(Locale.US); if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); if (context != null) context.setAttribute(UNCOMPRESSED, true); return; } else if ("deflate".equals(codecname)) { response.setEntity(new DeflateDecompressingEntity(response.getEntity())); if (context != null) context.setAttribute(UNCOMPRESSED, true); return; } else if ("identity".equals(codecname)) { /* Don't need to transform the content - no-op */ return; } else { throw new HttpException("Unsupported Content-Coding: " + codec.getName()); } } } } }
public static HttpEntity handleCompressedEntity(HttpEntity entity) { org.apache.http.Header contentEncoding = entity.getContentEncoding(); if (contentEncoding != null) for (HeaderElement e : contentEncoding.getElements()) { if (CONTENT_ENCODING_GZIP.equalsIgnoreCase(e.getName())) { return new GzipDecompressingEntity(entity); } if (CONTENT_ENCODING_DEFLATE.equalsIgnoreCase(e.getName())) { return new DeflateDecompressingEntity(entity); } } return entity; }
/** * Handles the following {@code Content-Encoding}s by * using the appropriate decompressor to wrap the response Entity: * <ul> * <li>gzip - see {@link GzipDecompressingEntity}</li> * <li>deflate - see {@link DeflateDecompressingEntity}</li> * <li>identity - no action needed</li> * </ul> * * @param response the response which contains the entity * @param context not currently used * * @throws HttpException if the {@code Content-Encoding} is none of the above */ public void process( final HttpResponse response, final HttpContext context) throws HttpException, IOException { final HttpEntity entity = response.getEntity(); // entity can be null in case of 304 Not Modified, 204 No Content or similar // check for zero length entity. if (entity != null && entity.getContentLength() != 0) { final Header ceheader = entity.getContentEncoding(); if (ceheader != null) { final HeaderElement[] codecs = ceheader.getElements(); boolean uncompressed = false; for (final HeaderElement codec : codecs) { final String codecname = codec.getName().toLowerCase(Locale.ENGLISH); if ("gzip".equals(codecname) || "x-gzip".equals(codecname)) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); uncompressed = true; break; } else if ("deflate".equals(codecname)) { response.setEntity(new DeflateDecompressingEntity(response.getEntity())); uncompressed = true; break; } else if ("identity".equals(codecname)) { /* Don't need to transform the content - no-op */ return; } else { throw new HttpException("Unsupported Content-Coding: " + codec.getName()); } } if (uncompressed) { response.removeHeaders("Content-Length"); response.removeHeaders("Content-Encoding"); response.removeHeaders("Content-MD5"); } } } }