Search in sources :

Example 11 with Resource

use of io.undertow.server.handlers.resource.Resource in project spring-boot by spring-projects.

the class JarResourceManagerTests method rootPathIsHandledCorrectly.

@Test
public void rootPathIsHandledCorrectly() throws IOException {
    Resource resource = this.resourceManager.getResource("/");
    assertThat(resource).isNotNull();
    assertThat(resource.isDirectory()).isTrue();
}
Also used : Resource(io.undertow.server.handlers.resource.Resource) Test(org.junit.Test)

Example 12 with Resource

use of io.undertow.server.handlers.resource.Resource in project spring-boot by spring-projects.

the class JarResourceManagerTests method resourceIsFoundInJarFile.

@Test
public void resourceIsFoundInJarFile() throws IOException {
    Resource resource = this.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) Test(org.junit.Test)

Example 13 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, '/'));
    }
    final Resource resource;
    //we want to disallow windows characters in the path
    if (File.separatorChar == '/' || !path.contains(File.separator)) {
        resource = resourceManager.getResource(path);
    } else {
        resource = null;
    }
    if (resource == null) {
        if (req.getDispatcherType() == DispatcherType.INCLUDE) {
            //servlet 9.3
            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) {
            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);
    }
}
Also used : RangeAwareResource(io.undertow.server.handlers.resource.RangeAwareResource) Resource(io.undertow.server.handlers.resource.Resource) FileNotFoundException(java.io.FileNotFoundException)

Example 14 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 = deploymentInfo.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 15 with Resource

use of io.undertow.server.handlers.resource.Resource in project undertow by undertow-io.

the class GetResourceTestCase method testSpecialCharacterInFileURL.

@Test
public void testSpecialCharacterInFileURL() throws IOException {
    String tmp = System.getProperty("java.io.tmpdir");
    PathResourceManager pathResourceManager = new PathResourceManager(Paths.get(tmp), 1);
    Path file = Paths.get(tmp, "1#2.txt");
    Files.write(file, "Hi".getBytes());
    Resource res = pathResourceManager.getResource("1#2.txt");
    try (InputStream in = res.getUrl().openStream()) {
        Assert.assertEquals("Hi", FileUtils.readFile(in));
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) Resource(io.undertow.server.handlers.resource.Resource) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) Test(org.junit.Test)

Aggregations

Resource (io.undertow.server.handlers.resource.Resource)15 IOException (java.io.IOException)7 Test (org.junit.Test)5 Path (java.nio.file.Path)4 ResourceManager (io.undertow.server.handlers.resource.ResourceManager)3 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)2 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)2 VirtualFile (org.jboss.vfs.VirtualFile)2 RangeAwareResource (io.undertow.server.handlers.resource.RangeAwareResource)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 FileChannel (java.nio.channels.FileChannel)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ConduitStreamSinkChannel (org.xnio.conduits.ConduitStreamSinkChannel)1 StreamSinkConduit (org.xnio.conduits.StreamSinkConduit)1