Search in sources :

Example 6 with Resource

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

the class ServletPathMatches method getServletHandlerByPath.

public ServletPathMatch getServletHandlerByPath(final String path) {
    ServletPathMatch existing = pathMatchCache.get(path);
    if (existing != null) {
        return existing;
    }
    ServletPathMatch match = getData().getServletHandlerByPath(path);
    if (!match.isRequiredWelcomeFileMatch()) {
        pathMatchCache.add(path, match);
        return match;
    }
    try {
        String remaining = match.getRemaining() == null ? match.getMatched() : match.getRemaining();
        Resource resource = resourceManager.getResource(remaining);
        if (resource == null || !resource.isDirectory()) {
            pathMatchCache.add(path, match);
            return match;
        }
        boolean pathEndsWithSlash = remaining.endsWith("/");
        final String pathWithTrailingSlash = pathEndsWithSlash ? remaining : remaining + "/";
        ServletPathMatch welcomePage = findWelcomeFile(pathWithTrailingSlash, !pathEndsWithSlash);
        if (welcomePage != null) {
            pathMatchCache.add(path, welcomePage);
            return welcomePage;
        } else {
            welcomePage = findWelcomeServlet(pathWithTrailingSlash, !pathEndsWithSlash);
            if (welcomePage != null) {
                pathMatchCache.add(path, welcomePage);
                return welcomePage;
            } else if (pathEndsWithSlash) {
                pathMatchCache.add(path, match);
                return match;
            } else {
                ServletPathMatch redirect = new ServletPathMatch(match.getServletChain(), match.getMatched(), match.getRemaining(), REDIRECT, "/");
                pathMatchCache.add(path, redirect);
                return redirect;
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Resource(io.undertow.server.handlers.resource.Resource) IOException(java.io.IOException)

Example 7 with Resource

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

the class ServletPathMatches method findWelcomeFile.

private ServletPathMatch findWelcomeFile(final String path, boolean requiresRedirect) {
    if (File.separatorChar != '/' && path.contains(File.separator)) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    for (String i : welcomePages) {
        try {
            sb.append(path);
            sb.append(i);
            final String mergedPath = sb.toString();
            sb.setLength(0);
            Resource resource = resourceManager.getResource(mergedPath);
            if (resource != null) {
                final ServletPathMatch handler = data.getServletHandlerByPath(mergedPath);
                return new ServletPathMatch(handler.getServletChain(), mergedPath, null, requiresRedirect ? REDIRECT : REWRITE, mergedPath);
            }
        } catch (IOException e) {
        }
    }
    return null;
}
Also used : Resource(io.undertow.server.handlers.resource.Resource) IOException(java.io.IOException)

Example 8 with Resource

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

the class FilePredicate method resolve.

@Override
public boolean resolve(final HttpServerExchange value) {
    String location = this.location.readAttribute(value);
    ServletRequestContext src = value.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if (src == null) {
        return false;
    }
    ResourceManager manager = src.getDeployment().getDeploymentInfo().getResourceManager();
    if (manager == null) {
        return false;
    }
    try {
        Resource resource = manager.getResource(location);
        if (resource == null) {
            return false;
        }
        return !resource.isDirectory();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Resource(io.undertow.server.handlers.resource.Resource) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) ResourceManager(io.undertow.server.handlers.resource.ResourceManager) IOException(java.io.IOException)

Example 9 with Resource

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

the class DirectoryPredicate method resolve.

@Override
public boolean resolve(final HttpServerExchange value) {
    String location = this.location.readAttribute(value);
    ServletRequestContext src = value.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if (src == null) {
        return false;
    }
    ResourceManager manager = src.getDeployment().getDeploymentInfo().getResourceManager();
    if (manager == null) {
        return false;
    }
    try {
        Resource resource = manager.getResource(location);
        if (resource == null) {
            return false;
        }
        return resource.isDirectory();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Resource(io.undertow.server.handlers.resource.Resource) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) ResourceManager(io.undertow.server.handlers.resource.ResourceManager) IOException(java.io.IOException)

Example 10 with Resource

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

the class JarResourceManagerTests method resourceIsFoundInJarFileWithoutLeadingSlash.

@Test
public void resourceIsFoundInJarFileWithoutLeadingSlash() 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)

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