Search in sources :

Example 1 with Compression

use of org.springframework.boot.web.server.Compression in project spring-boot by spring-projects.

the class JettyServletWebServerFactoryTests method setUpFactoryForCompression.

@Override
@SuppressWarnings("serial")
protected // Workaround for Jetty issue - https://bugs.eclipse.org/bugs/show_bug.cgi?id=470646
String setUpFactoryForCompression(final int contentSize, String[] mimeTypes, String[] excludedUserAgents) throws Exception {
    char[] chars = new char[contentSize];
    Arrays.fill(chars, 'F');
    final String testContent = new String(chars);
    AbstractServletWebServerFactory factory = getFactory();
    Compression compression = new Compression();
    compression.setEnabled(true);
    if (mimeTypes != null) {
        compression.setMimeTypes(mimeTypes);
    }
    if (excludedUserAgents != null) {
        compression.setExcludedUserAgents(excludedUserAgents);
    }
    factory.setCompression(compression);
    this.webServer = factory.getWebServer(new ServletRegistrationBean<HttpServlet>(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentLength(contentSize);
            resp.setHeader(HttpHeaders.CONTENT_TYPE, "text/plain");
            resp.getWriter().print(testContent);
        }
    }, "/test.txt"));
    this.webServer.start();
    return testContent;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Compression(org.springframework.boot.web.server.Compression) AbstractServletWebServerFactory(org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 2 with Compression

use of org.springframework.boot.web.server.Compression in project spring-boot by spring-projects.

the class JettyServletWebServerFactory method createGzipHandler.

private HandlerWrapper createGzipHandler() {
    GzipHandler handler = new GzipHandler();
    Compression compression = getCompression();
    handler.setMinGzipSize(compression.getMinResponseSize());
    handler.setIncludedMimeTypes(compression.getMimeTypes());
    if (compression.getExcludedUserAgents() != null) {
        handler.setExcludedAgentPatterns(compression.getExcludedUserAgents());
    }
    return handler;
}
Also used : Compression(org.springframework.boot.web.server.Compression) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler)

Example 3 with Compression

use of org.springframework.boot.web.server.Compression in project spring-boot by spring-projects.

the class TomcatServletWebServerFactory method customizeCompression.

private void customizeCompression(Connector connector) {
    ProtocolHandler handler = connector.getProtocolHandler();
    if (handler instanceof AbstractHttp11Protocol) {
        AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
        Compression compression = getCompression();
        protocol.setCompression("on");
        protocol.setCompressionMinSize(compression.getMinResponseSize());
        protocol.setCompressableMimeType(StringUtils.arrayToCommaDelimitedString(compression.getMimeTypes()));
        if (getCompression().getExcludedUserAgents() != null) {
            protocol.setNoCompressionUserAgents(StringUtils.arrayToCommaDelimitedString(getCompression().getExcludedUserAgents()));
        }
    }
}
Also used : ProtocolHandler(org.apache.coyote.ProtocolHandler) Compression(org.springframework.boot.web.server.Compression) AbstractHttp11Protocol(org.apache.coyote.http11.AbstractHttp11Protocol)

Example 4 with Compression

use of org.springframework.boot.web.server.Compression in project spring-boot by spring-projects.

the class AbstractServletWebServerFactoryTests method setUpFactoryForCompression.

protected 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();
    FileCopyUtils.copy(testContent, new FileWriter(this.temporaryFolder.newFile("test.txt")));
    factory.setDocumentRoot(this.temporaryFolder.getRoot());
    Compression compression = new Compression();
    compression.setEnabled(true);
    if (mimeTypes != null) {
        compression.setMimeTypes(mimeTypes);
    }
    if (excludedUserAgents != null) {
        compression.setExcludedUserAgents(excludedUserAgents);
    }
    factory.setCompression(compression);
    this.webServer = factory.getWebServer();
    this.webServer.start();
    return testContent;
}
Also used : Compression(org.springframework.boot.web.server.Compression) FileWriter(java.io.FileWriter)

Example 5 with Compression

use of org.springframework.boot.web.server.Compression in project spring-boot by spring-projects.

the class AbstractServletWebServerFactoryTests method compressionWithoutContentSizeHeader.

@Test
public void compressionWithoutContentSizeHeader() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    Compression compression = new Compression();
    compression.setEnabled(true);
    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();
}
Also used : Compression(org.springframework.boot.web.server.Compression) ServletRegistrationBean(org.springframework.boot.web.servlet.ServletRegistrationBean) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) InputStreamFactory(org.apache.http.client.entity.InputStreamFactory) Test(org.junit.Test)

Aggregations

Compression (org.springframework.boot.web.server.Compression)5 ServletRegistrationBean (org.springframework.boot.web.servlet.ServletRegistrationBean)2 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServlet (javax.servlet.http.HttpServlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ProtocolHandler (org.apache.coyote.ProtocolHandler)1 AbstractHttp11Protocol (org.apache.coyote.http11.AbstractHttp11Protocol)1 InputStreamFactory (org.apache.http.client.entity.InputStreamFactory)1 GzipHandler (org.eclipse.jetty.server.handler.gzip.GzipHandler)1 Test (org.junit.Test)1 AbstractServletWebServerFactory (org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory)1 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)1