@Test public void compressionWithoutContentSizeHeader() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); Compression compression = new Compression(); compression.setEnabled(true); factory.setCompression(compression); this.container = factory.getEmbeddedServletContainer( new ServletRegistrationBean(new ExampleServlet(false, true), "/hello")); this.container.start(); TestGzipInputStreamFactory inputStreamFactory = new TestGzipInputStreamFactory(); Map<String, InputStreamFactory> contentDecoderMap = Collections .singletonMap("gzip", (InputStreamFactory) inputStreamFactory); getResponse(getLocalUrl("/hello"), new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create() .setContentDecoderRegistry(contentDecoderMap).build())); assertThat(inputStreamFactory.wasCompressionUsed()).isTrue(); }
private boolean doTestCompression(int contentSize, String[] mimeTypes, String[] excludedUserAgents) throws Exception { String testContent = setUpFactoryForCompression(contentSize, mimeTypes, excludedUserAgents); TestGzipInputStreamFactory inputStreamFactory = new TestGzipInputStreamFactory(); Map<String, InputStreamFactory> contentDecoderMap = Collections .singletonMap("gzip", (InputStreamFactory) inputStreamFactory); String response = getResponse(getLocalUrl("/test.txt"), new HttpComponentsClientHttpRequestFactory( HttpClientBuilder.create().setUserAgent("testUserAgent") .setContentDecoderRegistry(contentDecoderMap).build())); assertThat(response).isEqualTo(testContent); return inputStreamFactory.wasCompressionUsed(); }
/** * @since 4.5 */ public ResponseContentEncoding(final Lookup<InputStreamFactory> decoderRegistry, final boolean ignoreUnknown) { this.decoderRegistry = decoderRegistry != null ? decoderRegistry : RegistryBuilder.<InputStreamFactory>create() .register("gzip", GZIP) .register("x-gzip", GZIP) .register("deflate", DEFLATE) .build(); this.ignoreUnknown = ignoreUnknown; }
@Override public void process( final HttpResponse response, final HttpContext context) throws HttpException, IOException { final HttpEntity entity = response.getEntity(); final HttpClientContext clientContext = HttpClientContext.adapt(context); final RequestConfig requestConfig = clientContext.getRequestConfig(); // entity can be null in case of 304 Not Modified, 204 No Content or similar // check for zero length entity. if (requestConfig.isContentCompressionEnabled() && entity != null && entity.getContentLength() != 0) { final Header ceheader = entity.getContentEncoding(); if (ceheader != null) { final HeaderElement[] codecs = ceheader.getElements(); for (final HeaderElement codec : codecs) { final String codecname = codec.getName().toLowerCase(Locale.ROOT); final InputStreamFactory decoderFactory = decoderRegistry.lookup(codecname); if (decoderFactory != null) { response.setEntity(new DecompressingEntity(response.getEntity(), decoderFactory)); response.removeHeaders("Content-Length"); response.removeHeaders("Content-Encoding"); response.removeHeaders("Content-MD5"); } else { if (!"identity".equals(codecname) && !ignoreUnknown) { throw new HttpException("Unsupported Content-Encoding: " + codec.getName()); } } } } } }
private boolean doTestCompression(int contentSize, String[] mimeTypes, String[] excludedUserAgents) throws Exception { String testContent = setUpFactoryForCompression(contentSize, mimeTypes, excludedUserAgents); TestGzipInputStreamFactory inputStreamFactory = new TestGzipInputStreamFactory(); Map<String, InputStreamFactory> contentDecoderMap = Collections .singletonMap("gzip", (InputStreamFactory) inputStreamFactory); String response = getResponse(getLocalUrl("/test.txt"), new HttpComponentsClientHttpRequestFactory( HttpClientBuilder.create().setUserAgent("testUserAgent") .setContentDecoderRegistry(contentDecoderMap).build())); assertThat(response, equalTo(testContent)); return inputStreamFactory.wasCompressionUsed(); }
BrotliDecompressingEntity(HttpEntity entity) { super(entity, new InputStreamFactory() { public InputStream create(InputStream instream) throws IOException { return new BrotliInputStream(instream); } }); }
/** * @since 4.4 */ public ResponseContentEncoding(final Lookup<InputStreamFactory> decoderRegistry) { this(decoderRegistry, true); }
/** * Assigns a map of {@link org.apache.http.client.entity.InputStreamFactory}s * to be used for automatic content decompression. */ public final HttpClientBuilder setContentDecoderRegistry( final Map<String, InputStreamFactory> contentDecoderMap) { this.contentDecoderMap = contentDecoderMap; return this; }