use of io.undertow.server.handlers.resource.PreCompressedResourceSupplier in project undertow by undertow-io.
the class PreCompressedResourceTestCase method testContentEncodedJsonResource.
@Test
public void testContentEncodedJsonResource() throws IOException, URISyntaxException {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/data1.json");
TestHttpClient client = new TestHttpClient();
Path rootPath = Paths.get(getClass().getResource("data1.json").toURI()).getParent();
try (CloseableHttpClient compClient = HttpClientBuilder.create().build()) {
DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PreCompressedResourceSupplier(new PathResourceManager(rootPath, 10485760)).addEncoding("gzip", ".gz")).setDirectoryListingEnabled(true))));
// assert response without compression
final String plainResponse = assertResponse(client.execute(get), false, null, "web", "application/json");
// assert compressed response, that doesn't exists, so returns plain
assertResponse(compClient.execute(get), false, plainResponse, "web", "application/json");
// generate compressed resource with extension .gz
Path json = rootPath.resolve("data1.json");
generateGZipFile(json, rootPath.resolve("data1.json.gz"));
// assert compressed response that was pre compressed
assertResponse(compClient.execute(get), true, plainResponse, "gz", "application/json");
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.resource.PreCompressedResourceSupplier in project undertow by undertow-io.
the class DefaultServlet method init.
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
ServletContextImpl sc = (ServletContextImpl) config.getServletContext();
this.deployment = sc.getDeployment();
DefaultServletConfig defaultServletConfig = deployment.getDeploymentInfo().getDefaultServletConfig();
if (defaultServletConfig != null) {
defaultAllowed = defaultServletConfig.isDefaultAllowed();
allowed = new HashSet<>();
if (defaultServletConfig.getAllowed() != null) {
allowed.addAll(defaultServletConfig.getAllowed());
}
disallowed = new HashSet<>();
if (defaultServletConfig.getDisallowed() != null) {
disallowed.addAll(defaultServletConfig.getDisallowed());
}
}
if (config.getInitParameter(DEFAULT_ALLOWED) != null) {
defaultAllowed = Boolean.parseBoolean(config.getInitParameter(DEFAULT_ALLOWED));
}
if (config.getInitParameter(ALLOWED_EXTENSIONS) != null) {
String extensions = config.getInitParameter(ALLOWED_EXTENSIONS);
allowed = new HashSet<>(Arrays.asList(extensions.split(",")));
}
if (config.getInitParameter(DISALLOWED_EXTENSIONS) != null) {
String extensions = config.getInitParameter(DISALLOWED_EXTENSIONS);
disallowed = new HashSet<>(Arrays.asList(extensions.split(",")));
}
if (config.getInitParameter(RESOLVE_AGAINST_CONTEXT_ROOT) != null) {
resolveAgainstContextRoot = Boolean.parseBoolean(config.getInitParameter(RESOLVE_AGAINST_CONTEXT_ROOT));
}
if (config.getInitParameter(ALLOW_POST) != null) {
allowPost = Boolean.parseBoolean(config.getInitParameter(ALLOW_POST));
}
if (deployment.getDeploymentInfo().getPreCompressedResources().isEmpty()) {
this.resourceSupplier = new DefaultResourceSupplier(deployment.getDeploymentInfo().getResourceManager());
} else {
PreCompressedResourceSupplier preCompressedResourceSupplier = new PreCompressedResourceSupplier(deployment.getDeploymentInfo().getResourceManager());
for (Map.Entry<String, String> entry : deployment.getDeploymentInfo().getPreCompressedResources().entrySet()) {
preCompressedResourceSupplier.addEncoding(entry.getKey(), entry.getValue());
}
this.resourceSupplier = preCompressedResourceSupplier;
}
String listings = config.getInitParameter(DIRECTORY_LISTING);
if (Boolean.valueOf(listings)) {
this.directoryListingEnabled = true;
}
}
use of io.undertow.server.handlers.resource.PreCompressedResourceSupplier 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.resource.PreCompressedResourceSupplier in project undertow by undertow-io.
the class PreCompressedResourceTestCase method testContentEncodedJsonResourceWithoutUncompressed.
@Test
public void testContentEncodedJsonResourceWithoutUncompressed() throws IOException, URISyntaxException {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/data3.json");
TestHttpClient client = new TestHttpClient();
Path rootPath = Paths.get(getClass().getResource("data2.json").toURI()).getParent();
try (CloseableHttpClient compClient = HttpClientBuilder.create().build()) {
DefaultServer.setRootHandler(new CanonicalPathHandler().setNext(new PathHandler().addPrefixPath("/path", new ResourceHandler(new PreCompressedResourceSupplier(new PathResourceManager(rootPath, 10485760)).addEncoding("gzip", ".gz")).setDirectoryListingEnabled(true))));
// generate compressed resource with extension .gz and delete the uncompressed
Path json = rootPath.resolve("data2.json");
Path jsonFileToBeZippedAndDeleted = rootPath.resolve("data3.json");
Files.copy(json, jsonFileToBeZippedAndDeleted);
// data3.json.gz has no corresponding data3.json in the filesystem (UNDERTOW-1950)
generateGZipFile(jsonFileToBeZippedAndDeleted, rootPath.resolve("data3.json.gz"));
Files.delete(jsonFileToBeZippedAndDeleted);
// assert compressed response even with missing uncompressed
assertResponse(compClient.execute(get), true, null, "gz", "application/json");
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.server.handlers.resource.PreCompressedResourceSupplier in project undertow by undertow-io.
the class PreCompressedResourceTestCase method testContentEncodedResource.
@Test
public void testContentEncodedResource() 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 ResourceHandler(new PreCompressedResourceSupplier(new PathResourceManager(rootPath, 10485760)).addEncoding("gzip", ".gz")).setDirectoryListingEnabled(true))));
// assert response without compression
final String plainResponse = assertResponse(client.execute(get), false);
// assert compressed response, that doesn't exists, so returns plain
assertResponse(compClient.execute(get), false, plainResponse);
// generate compressed resource with extension .gz
generatePreCompressedResource("gz");
// assert compressed response that was pre compressed
assertResponse(compClient.execute(get), true, plainResponse, "gz", "text/html");
} finally {
client.getConnectionManager().shutdown();
}
}
Aggregations