use of io.undertow.server.handlers.resource.Resource in project undertow by undertow-io.
the class PathResourceManagerTestCase method testListDir.
@Test
public void testListDir() throws Exception {
final Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent();
final PathResourceManager resourceManager = new PathResourceManager(rootPath, 1024 * 1024);
Resource subdir = resourceManager.getResource("subdir");
Resource found = subdir.list().get(0);
Assert.assertEquals("subdir" + File.separatorChar + "a.txt", found.getPath());
}
use of io.undertow.server.handlers.resource.Resource in project undertow by undertow-io.
the class ServletContextImpl method getResourcePaths.
@Override
public Set<String> getResourcePaths(final String path) {
final Resource resource;
try {
resource = getDeploymentInfo().getResourceManager().getResource(path);
} catch (IOException e) {
return null;
}
if (resource == null || !resource.isDirectory()) {
return null;
}
final Set<String> resources = new HashSet<>();
for (Resource res : resource.list()) {
Path file = res.getFilePath();
if (file != null) {
Path base = res.getResourceManagerRootPath();
if (base == null) {
// not much else we can do here
resources.add(file.toString());
} else {
String filePath = file.toAbsolutePath().toString().substring(base.toAbsolutePath().toString().length());
// for windows systems
filePath = filePath.replace('\\', '/');
if (Files.isDirectory(file)) {
filePath = filePath + "/";
}
resources.add(filePath);
}
}
}
return resources;
}
use of io.undertow.server.handlers.resource.Resource in project undertow by undertow-io.
the class DefaultServlet method doGet.
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
String path = getPath(req);
if (!isAllowed(path, req.getDispatcherType())) {
resp.sendError(StatusCodes.NOT_FOUND);
return;
}
if (File.separatorChar != '/') {
// if the separator char is not / we want to replace it with a / and canonicalise
path = CanonicalPathUtils.canonicalize(path.replace(File.separatorChar, '/'));
}
HttpServerExchange exchange = SecurityActions.requireCurrentServletRequestContext().getOriginalRequest().getExchange();
final Resource resource;
// we want to disallow windows characters in the path
if (File.separatorChar == '/' || !path.contains(File.separator)) {
resource = resourceSupplier.getResource(exchange, path);
} else {
resource = null;
}
if (resource == null) {
if (req.getDispatcherType() == DispatcherType.INCLUDE) {
// servlet 9.3
UndertowServletLogger.REQUEST_LOGGER.requestedResourceDoesNotExistForIncludeMethod(path);
throw new FileNotFoundException(path);
} else {
resp.sendError(StatusCodes.NOT_FOUND);
}
return;
} else if (resource.isDirectory()) {
if ("css".equals(req.getQueryString())) {
resp.setContentType("text/css");
resp.getWriter().write(DirectoryUtils.Blobs.FILE_CSS);
return;
} else if ("js".equals(req.getQueryString())) {
resp.setContentType("application/javascript");
resp.getWriter().write(DirectoryUtils.Blobs.FILE_JS);
return;
}
if (directoryListingEnabled) {
resp.setContentType("text/html");
StringBuilder output = DirectoryUtils.renderDirectoryListing(req.getRequestURI(), resource);
resp.getWriter().write(output.toString());
} else {
resp.sendError(StatusCodes.FORBIDDEN);
}
} else {
if (path.endsWith("/")) {
// UNDERTOW-432
resp.sendError(StatusCodes.NOT_FOUND);
return;
}
serveFileBlocking(req, resp, resource, exchange);
}
}
use of io.undertow.server.handlers.resource.Resource in project spring-boot by spring-projects.
the class JarResourceManagerTests method resourceIsFoundInJarFileWithoutLeadingSlash.
@ResourceManagersTest
void resourceIsFoundInJarFileWithoutLeadingSlash(String filename, ResourceManager resourceManager) throws IOException {
Resource resource = resourceManager.getResource("hello.txt");
assertThat(resource).isNotNull();
assertThat(resource.isDirectory()).isFalse();
assertThat(resource.getContentLength()).isEqualTo(5);
}
use of io.undertow.server.handlers.resource.Resource in project spring-boot by spring-projects.
the class JarResourceManagerTests method emptyPathIsHandledCorrectly.
@ResourceManagersTest
void emptyPathIsHandledCorrectly(String filename, ResourceManager resourceManager) throws IOException {
Resource resource = resourceManager.getResource("");
assertThat(resource).isNotNull();
assertThat(resource.isDirectory()).isTrue();
}
Aggregations