Search in sources :

Example 21 with Resource

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());
}
Also used : Path(java.nio.file.Path) Resource(io.undertow.server.handlers.resource.Resource) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 22 with Resource

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;
}
Also used : Path(java.nio.file.Path) Resource(io.undertow.server.handlers.resource.Resource) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 23 with Resource

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);
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) RangeAwareResource(io.undertow.server.handlers.resource.RangeAwareResource) Resource(io.undertow.server.handlers.resource.Resource) FileNotFoundException(java.io.FileNotFoundException)

Example 24 with Resource

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);
}
Also used : Resource(io.undertow.server.handlers.resource.Resource)

Example 25 with Resource

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();
}
Also used : Resource(io.undertow.server.handlers.resource.Resource)

Aggregations

Resource (io.undertow.server.handlers.resource.Resource)25 IOException (java.io.IOException)9 Path (java.nio.file.Path)7 Test (org.junit.Test)7 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)4 ResourceManager (io.undertow.server.handlers.resource.ResourceManager)4 HttpServerExchange (io.undertow.server.HttpServerExchange)3 File (java.io.File)3 Sender (io.undertow.io.Sender)2 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)2 UnitTest (io.undertow.testutils.category.UnitTest)2 OutputStream (java.io.OutputStream)2 Date (java.util.Date)2 VirtualFile (org.jboss.vfs.VirtualFile)2 Undertow (io.undertow.Undertow)1 IoCallback (io.undertow.io.IoCallback)1 HttpHandler (io.undertow.server.HttpHandler)1 PathHandler (io.undertow.server.handlers.PathHandler)1 FileResource (io.undertow.server.handlers.resource.FileResource)1 RangeAwareResource (io.undertow.server.handlers.resource.RangeAwareResource)1