Search in sources :

Example 1 with Resource

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

the class JarResourceManagerTests method emptyPathIsHandledCorrectly.

@Test
public void emptyPathIsHandledCorrectly() 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 2 with Resource

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

the class KontraktorServlet method doGet.

/**
 * {
 *        facade = Actors.AsActor(ServletApp.class);
 *        ((ServletApp) facade).init();
 *    }*
 */
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String pathInfo = req.getPathInfo();
    if (pathInfo == null) {
        String dbg = req.getRequestURI();
        resp.sendRedirect(dbg + "/");
        return;
    }
    Resource resource = null;
    if ("".equals(pathInfo) || "/".equals(pathInfo)) {
        resource = dynamicResourceManager.getResource(pathInfo + "index.html");
    } else {
        resource = dynamicResourceManager.getResource(pathInfo);
    }
    if (resource != null) {
        Long contentLength = resource.getContentLength();
        if (contentLength != null) {
            resp.setContentLength((int) contentLength.longValue());
        }
        byte[] bytes = null;
        if (resource instanceof DynamicResourceManager.MyResource) {
            bytes = ((DynamicResourceManager.MyResource) resource).getBytes();
        } else if (resource.getFile() != null) {
            bytes = FileUtil.readFully(resource.getFile());
        }
        // FIXME: mimetype, lastModified / 304
        if (bytes != null) {
            ServletOutputStream out = resp.getOutputStream();
            out.write(bytes);
            out.flush();
            out.close();
            return;
        }
    }
    Log.Error(this, "Unhandled resource");
    unhandledGet(req, resp);
}
Also used : DynamicResourceManager(org.nustaq.kontraktor.webapp.javascript.DynamicResourceManager) Resource(io.undertow.server.handlers.resource.Resource)

Example 3 with Resource

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

the class ServletContextImpl method getRealPath.

@Override
public String getRealPath(final String path) {
    if (path == null) {
        return null;
    }
    final DeploymentInfo deploymentInfo = getDeploymentInfo();
    String canonicalPath = CanonicalPathUtils.canonicalize(path);
    Resource resource;
    try {
        resource = deploymentInfo.getResourceManager().getResource(canonicalPath);
        if (resource == null) {
            // UNDERTOW-373 even though the resource does not exist we still need to return a path
            Resource deploymentRoot = deploymentInfo.getResourceManager().getResource("/");
            if (deploymentRoot == null) {
                return null;
            }
            Path root = deploymentRoot.getFilePath();
            if (root == null) {
                return null;
            }
            if (!canonicalPath.startsWith("/")) {
                canonicalPath = "/" + canonicalPath;
            }
            if (File.separatorChar != '/') {
                canonicalPath = canonicalPath.replace('/', File.separatorChar);
            }
            return root.toAbsolutePath().toString() + canonicalPath;
        }
    } catch (IOException e) {
        return null;
    }
    Path file = resource.getFilePath();
    if (file == null) {
        return null;
    }
    return file.toAbsolutePath().toString();
}
Also used : Path(java.nio.file.Path) Resource(io.undertow.server.handlers.resource.Resource) IOException(java.io.IOException) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo)

Example 4 with Resource

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

the class ContentEncodedResourceManager method getResource.

/**
 * Gets a pre-encoded resource.
 * <p>
 * TODO: blocking / non-blocking semantics
 *
 * @param resource
 * @param exchange
 * @return
 * @throws IOException
 */
public ContentEncodedResource getResource(final Resource resource, final HttpServerExchange exchange) throws IOException {
    final String path = resource.getPath();
    Path file = resource.getFilePath();
    if (file == null) {
        return null;
    }
    if (minResourceSize > 0 && resource.getContentLength() < minResourceSize || maxResourceSize > 0 && resource.getContentLength() > maxResourceSize || !(encodingAllowed == null || encodingAllowed.resolve(exchange))) {
        return null;
    }
    AllowedContentEncodings encodings = contentEncodingRepository.getContentEncodings(exchange);
    if (encodings == null || encodings.isNoEncodingsAllowed()) {
        return null;
    }
    EncodingMapping encoding = encodings.getEncoding();
    if (encoding == null || encoding.getName().equals(ContentEncodingRepository.IDENTITY)) {
        return null;
    }
    String newPath = path + ".undertow.encoding." + encoding.getName();
    Resource preCompressed = encoded.getResource(newPath);
    if (preCompressed != null) {
        return new ContentEncodedResource(preCompressed, encoding.getName());
    }
    final LockKey key = new LockKey(path, encoding.getName());
    if (fileLocks.putIfAbsent(key, this) != null) {
        // we don't do anything fancy here, just return and serve non-compressed content
        return null;
    }
    FileChannel targetFileChannel = null;
    FileChannel sourceFileChannel = null;
    try {
        // double check, the compressing thread could have finished just before we acquired the lock
        preCompressed = encoded.getResource(newPath);
        if (preCompressed != null) {
            return new ContentEncodedResource(preCompressed, encoding.getName());
        }
        final Path finalTarget = encodedResourcesRoot.resolve(newPath);
        final Path tempTarget = encodedResourcesRoot.resolve(newPath);
        // horrible hack to work around XNIO issue
        OutputStream tmp = Files.newOutputStream(tempTarget);
        try {
            tmp.close();
        } finally {
            IoUtils.safeClose(tmp);
        }
        targetFileChannel = FileChannel.open(tempTarget, StandardOpenOption.READ, StandardOpenOption.WRITE);
        sourceFileChannel = FileChannel.open(file, StandardOpenOption.READ);
        StreamSinkConduit conduit = encoding.getEncoding().getResponseWrapper().wrap(new ImmediateConduitFactory<StreamSinkConduit>(new FileConduitTarget(targetFileChannel, exchange)), exchange);
        final ConduitStreamSinkChannel targetChannel = new ConduitStreamSinkChannel(null, conduit);
        long transferred = sourceFileChannel.transferTo(0, resource.getContentLength(), targetChannel);
        targetChannel.shutdownWrites();
        org.xnio.channels.Channels.flushBlocking(targetChannel);
        if (transferred != resource.getContentLength()) {
            UndertowLogger.REQUEST_LOGGER.failedToWritePreCachedFile();
        }
        Files.move(tempTarget, finalTarget);
        encoded.invalidate(newPath);
        final Resource encodedResource = encoded.getResource(newPath);
        return new ContentEncodedResource(encodedResource, encoding.getName());
    } finally {
        IoUtils.safeClose(targetFileChannel);
        IoUtils.safeClose(sourceFileChannel);
        fileLocks.remove(key);
    }
}
Also used : Path(java.nio.file.Path) FileChannel(java.nio.channels.FileChannel) OutputStream(java.io.OutputStream) Resource(io.undertow.server.handlers.resource.Resource) StreamSinkConduit(org.xnio.conduits.StreamSinkConduit) ConduitStreamSinkChannel(org.xnio.conduits.ConduitStreamSinkChannel)

Example 5 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)

Aggregations

Resource (io.undertow.server.handlers.resource.Resource)24 IOException (java.io.IOException)8 Test (org.junit.Test)7 Path (java.nio.file.Path)6 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)4 ResourceManager (io.undertow.server.handlers.resource.ResourceManager)3 HttpServerExchange (io.undertow.server.HttpServerExchange)2 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)2 UnitTest (io.undertow.testutils.category.UnitTest)2 File (java.io.File)2 OutputStream (java.io.OutputStream)2 VirtualFile (org.jboss.vfs.VirtualFile)2 Undertow (io.undertow.Undertow)1 Sender (io.undertow.io.Sender)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 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1