Java Code Examples for io.undertow.server.handlers.resource.Resource#getFilePath()

The following examples show how to use io.undertow.server.handlers.resource.Resource#getFilePath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServletContextImpl.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@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) {
                resources.add(file.toString()); //not much else we can do here
            } else {
                String filePath = file.toAbsolutePath().toString().substring(base.toAbsolutePath().toString().length());
                filePath = filePath.replace('\\', '/'); //for windows systems
                if (Files.isDirectory(file)) {
                    filePath = filePath + "/";
                }
                resources.add(filePath);
            }
        }
    }
    return resources;
}
 
Example 2
Source File: ServletContextImpl.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public String getRealPath(final String path) {
    if (path == null) {
        return null;
    }
    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();
}
 
Example 3
Source File: ServletContextImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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) {
                resources.add(file.toString()); //not much else we can do here
            } else {
                String filePath = file.toAbsolutePath().toString().substring(base.toAbsolutePath().toString().length());
                filePath = filePath.replace('\\', '/'); //for windows systems
                if (Files.isDirectory(file)) {
                    filePath = filePath + "/";
                }
                resources.add(filePath);
            }
        }
    }
    return resources;
}
 
Example 4
Source File: ServletContextImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getRealPath(final String path) {
    if (path == null) {
        return null;
    }
    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();
}
 
Example 5
Source File: ContentEncodedResourceManager.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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) {
        //another thread is already compressing
        //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);
    }
}