use of io.undertow.server.handlers.encoding.ContentEncodingRepository in project undertow by undertow-io.
the class ContentEncodedResourceTestCase method setup.
@BeforeClass
public static void setup() throws IOException {
tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), DIR_NAME);
final PathResourceManager resourceManager = new PathResourceManager(tmpDir, 10485760);
DefaultServer.setRootHandler(new ResourceHandler(resourceManager).setContentEncodedResourceManager(new ContentEncodedResourceManager(tmpDir, new CachingResourceManager(100, 10000, null, resourceManager, -1), new ContentEncodingRepository().addEncodingHandler("deflate", new DeflateEncodingProvider(), 50, null), 0, 100000, null)));
}
use of io.undertow.server.handlers.encoding.ContentEncodingRepository in project undertow by undertow-io.
the class ServerSentEventTestCase method testProgressiveSSEWithCompression.
@Test
public void testProgressiveSSEWithCompression() throws IOException {
final AtomicReference<ServerSentEventConnection> connectionReference = new AtomicReference<>();
DecompressingHttpClient client = new DecompressingHttpClient(new TestHttpClient());
try {
DefaultServer.setRootHandler(new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("deflate", new DeflateEncodingProvider(), 50)).setNext(new ServerSentEventHandler(new ServerSentEventConnectionCallback() {
@Override
public void connected(ServerSentEventConnection connection, String lastEventId) {
connectionReference.set(connection);
connection.send("msg 1", new ServerSentEventConnection.EventCallback() {
@Override
public void done(ServerSentEventConnection connection, String data, String event, String id) {
}
@Override
public void failed(ServerSentEventConnection connection, String data, String event, String id, IOException e) {
e.printStackTrace();
IoUtils.safeClose(connection);
}
});
}
})));
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
InputStream stream = result.getEntity().getContent();
assertData(stream, "data:msg 1\n\n");
connectionReference.get().send("msg 2");
assertData(stream, "data:msg 2\n\n");
connectionReference.get().close();
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.encoding.ContentEncodingRepository in project undertow by undertow-io.
the class PreCompressedResourceTestCase method testCorrectResourceSelected.
@Test
public void testCorrectResourceSelected() throws IOException, URISyntaxException {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html");
TestHttpClient client = new TestHttpClient();
Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
try (CloseableHttpClient compClient = HttpClientBuilder.create().build()) {
DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("gzip", new GzipEncodingProvider(), 50, Predicates.truePredicate())).setNext(new ResourceHandler(new PreCompressedResourceSupplier(new PathResourceManager(rootPath, 10485760)).addEncoding("gzip", ".gzip")).setDirectoryListingEnabled(true)))));
// assert response without compression
final String plainResponse = assertResponse(client.execute(get), false);
// assert compressed response generated by filter
assertResponse(compClient.execute(get), true, plainResponse);
// generate resources
generatePreCompressedResource("gzip");
generatePreCompressedResource("nonsense");
generatePreCompressedResource("gzip.nonsense");
// assert compressed response that was pre compressed
assertResponse(compClient.execute(get), true, plainResponse, "gzip", "text/html");
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.encoding.ContentEncodingRepository in project indy by Commonjava.
the class IndyDeployer method getGzipEncodeHandler.
private EncodingHandler getGzipEncodeHandler(final DeploymentManager dm) throws ServletException {
// FROM: https://stackoverflow.com/questions/28295752/compressing-undertow-server-responses#28329810
final Predicate sizePredicate = Predicates.parse("max-content-size[" + Long.toString(5 * 1024) + "]");
// For firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=610679
final Predicate fileTypePredicate = v -> {
final String path = v.getRequestPath();
return !NO_NEED_GZIPPED_CONTENT.contains(mimeTyper.getContentType(path).toLowerCase());
};
final Predicate mixePredicate = v -> sizePredicate.resolve(v) && fileTypePredicate.resolve(v);
EncodingHandler eh = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("gzip", new GzipEncodingProvider(), 50, mixePredicate)).setNext(dm.start());
// sizePredicate ) ).setNext( dm.start() );
return eh;
}
use of io.undertow.server.handlers.encoding.ContentEncodingRepository in project oap by oaplatform.
the class NioHttpServer method bind.
public void bind(String prefix, HttpHandler handler, boolean compressionSupport) {
log.debug("bind {}", prefix);
Preconditions.checkNotNull(prefix);
Preconditions.checkArgument(!prefix.isEmpty());
io.undertow.server.HttpHandler httpHandler = exchange -> handler.handleRequest(new HttpServerExchange(exchange));
if (!forceCompressionSupport && compressionSupport) {
httpHandler = new EncodingHandler(httpHandler, contentEncodingRepository);
httpHandler = new RequestEncodingHandler(httpHandler).addEncoding("gzip", GzipStreamSourceConduit.WRAPPER).addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER);
}
pathHandler.addPrefixPath(prefix, httpHandler);
}
Aggregations