Example usage for org.springframework.boot.web.servlet.server AbstractServletWebServerFactory setCompression

List of usage examples for org.springframework.boot.web.servlet.server AbstractServletWebServerFactory setCompression

Introduction

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

Prototype

@Override
    public void setCompression(Compression 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 www. j  a va2s .  c  om*/
    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);//from w  ww. ja  va2s .  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;
}