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;
}
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;
}
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()));
}
}
}
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;
}
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();
}
Aggregations