use of io.undertow.server.handlers.resource.PathResourceManager 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.PathResourceManager 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.PathResourceManager 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();
}
}
use of io.undertow.server.handlers.resource.PathResourceManager in project undertow by undertow-io.
the class DefaultServletCachingListenerTestCase method setup.
@BeforeClass
public static void setup() throws ServletException, IOException {
tmpDir = Files.createTempDirectory(DIR_NAME);
// assume tmp is in the default file system and watch-service is not the slow polling impl
Assume.assumeTrue("WatchService is going to work OK", FileSystems.getDefault().equals(tmpDir.getFileSystem()) && !FileSystems.getDefault().newWatchService().getClass().getName().equals("sun.nio.fs.PollingWatchService"));
final PathHandler root = new PathHandler();
final ServletContainer container = ServletContainer.Factory.newInstance();
DeploymentInfo builder = new DeploymentInfo().setClassIntrospecter(TestClassIntrospector.INSTANCE).setClassLoader(ServletPathMappingTestCase.class.getClassLoader()).setContextPath("/servletContext").addWelcomePage("index.html").setDeploymentName("servletContext.war").setResourceManager(new CachingResourceManager(100, MAX_FILE_SIZE, dataCache, new PathResourceManager(tmpDir, 10485760, false, false, true), -1));
builder.addServlet(new ServletInfo("DefaultTestServlet", PathTestServlet.class).addMapping("/path/default")).addFilter(Servlets.filter("message", MessageFilter.class).addInitParam(MessageFilter.MESSAGE, "FILTER_TEXT ")).addFilterUrlMapping("message", "*.txt", DispatcherType.REQUEST);
DeploymentManager manager = container.addDeployment(builder);
manager.deploy();
root.addPrefixPath(builder.getContextPath(), manager.start());
DefaultServer.setRootHandler(root);
}
use of io.undertow.server.handlers.resource.PathResourceManager in project undertow by undertow-io.
the class RangeRequestTestCase method setup.
@BeforeClass
public static void setup() throws URISyntaxException {
Path rootPath = Paths.get(RangeRequestTestCase.class.getResource("range.txt").toURI()).getParent();
PathHandler path = Handlers.path();
path.addPrefixPath("/path", new ByteRangeHandler(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.LAST_MODIFIED, DateUtils.toDateString(new Date(10000)));
exchange.getResponseHeaders().put(Headers.ETAG, "\"someetag\"");
exchange.getResponseSender().send("0123456789");
}
}, true));
path.addPrefixPath("/resource", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true));
path.addPrefixPath("/cachedresource", new ResourceHandler(new CachingResourceManager(1000, 1000000, new DirectBufferCache(1000, 10, 10000), new PathResourceManager(rootPath, 10485760), -1)).setDirectoryListingEnabled(true));
DefaultServer.setRootHandler(path);
}
Aggregations