Example usage for org.springframework.boot.web.server Compression Compression

List of usage examples for org.springframework.boot.web.server Compression Compression

Introduction

In this page you can find the example usage for org.springframework.boot.web.server Compression Compression.

Prototype

Compression

Source Link

Usage

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

@Test
public void compressionWithoutContentSizeHeader() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    Compression compression = new Compression();
    compression.setEnabled(true);/*from w w w. j ava  2s .co  m*/
    factory.setCompression(compression);
    this.webServer = factory
            .getWebServer(new ServletRegistrationBean<>(new ExampleServlet(false, true), "/hello"));
    this.webServer.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();
}

From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java

private String setUpFactoryForCompression(int contentSize, String[] mimeTypes, String[] excludedUserAgents)
        throws Exception {
    char[] chars = new char[contentSize];
    Arrays.fill(chars, 'F');
    String testContent = new String(chars);
    AbstractServletWebServerFactory factory = getFactory();
    Compression compression = new Compression();
    compression.setEnabled(true);/*  w  ww. j  ava 2 s  . c  o  m*/
    if (mimeTypes != null) {
        compression.setMimeTypes(mimeTypes);
    }
    if (excludedUserAgents != null) {
        compression.setExcludedUserAgents(excludedUserAgents);
    }
    factory.setCompression(compression);
    factory.addInitializers(new ServletRegistrationBean<HttpServlet>(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            resp.setContentType("text/plain");
            resp.setContentLength(testContent.length());
            resp.getWriter().write(testContent);
            resp.getWriter().flush();
        }

    }, "/test.txt"));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    return testContent;
}