Search in sources :

Example 31 with InvalidPathException

use of java.nio.file.InvalidPathException in project mycore by MyCoRe-Org.

the class MCRPaths method getPath.

static Path getPath(String scheme, String owner, String path) {
    URI uri;
    try {
        uri = getURI(scheme, owner, path);
        LogManager.getLogger(MCRPaths.class).debug("Generated path URI:{}", uri);
    } catch (URISyntaxException e) {
        throw new InvalidPathException(path, "URI syntax error (" + e.getMessage() + ") for path");
    }
    try {
        return Paths.get(uri);
    } catch (FileSystemNotFoundException e) {
        for (FileSystemProvider provider : webAppProvider) {
            if (provider.getScheme().equals(uri.getScheme())) {
                return provider.getPath(uri);
            }
        }
        throw e;
    }
}
Also used : FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) FileSystemProvider(java.nio.file.spi.FileSystemProvider) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) InvalidPathException(java.nio.file.InvalidPathException)

Example 32 with InvalidPathException

use of java.nio.file.InvalidPathException in project mybatis-3 by mybatis.

the class DefaultVFS method list.

@Override
public List<String> list(URL url, String path) throws IOException {
    InputStream is = null;
    try {
        List<String> resources = new ArrayList<>();
        // First, try to find the URL of a JAR file containing the requested resource. If a JAR
        // file is found, then we'll list child resources by reading the JAR.
        URL jarUrl = findJarForResource(url);
        if (jarUrl != null) {
            is = jarUrl.openStream();
            if (log.isDebugEnabled()) {
                log.debug("Listing " + url);
            }
            resources = listResources(new JarInputStream(is), path);
        } else {
            List<String> children = new ArrayList<>();
            try {
                if (isJar(url)) {
                    // Some versions of JBoss VFS might give a JAR stream even if the resource
                    // referenced by the URL isn't actually a JAR
                    is = url.openStream();
                    try (JarInputStream jarInput = new JarInputStream(is)) {
                        if (log.isDebugEnabled()) {
                            log.debug("Listing " + url);
                        }
                        for (JarEntry entry; (entry = jarInput.getNextJarEntry()) != null; ) {
                            if (log.isDebugEnabled()) {
                                log.debug("Jar entry: " + entry.getName());
                            }
                            children.add(entry.getName());
                        }
                    }
                } else {
                    /*
             * Some servlet containers allow reading from directory resources like a
             * text file, listing the child resources one per line. However, there is no
             * way to differentiate between directory and file resources just by reading
             * them. To work around that, as each line is read, try to look it up via
             * the class loader as a child of the current resource. If any line fails
             * then we assume the current resource is not a directory.
             */
                    is = url.openStream();
                    List<String> lines = new ArrayList<>();
                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
                        for (String line; (line = reader.readLine()) != null; ) {
                            if (log.isDebugEnabled()) {
                                log.debug("Reader entry: " + line);
                            }
                            lines.add(line);
                            if (getResources(path + "/" + line).isEmpty()) {
                                lines.clear();
                                break;
                            }
                        }
                    } catch (InvalidPathException e) {
                        // #1974
                        lines.clear();
                    }
                    if (!lines.isEmpty()) {
                        if (log.isDebugEnabled()) {
                            log.debug("Listing " + url);
                        }
                        children.addAll(lines);
                    }
                }
            } catch (FileNotFoundException e) {
                /*
           * For file URLs the openStream() call might fail, depending on the servlet
           * container, because directories can't be opened for reading. If that happens,
           * then list the directory directly instead.
           */
                if ("file".equals(url.getProtocol())) {
                    File file = new File(url.getFile());
                    if (log.isDebugEnabled()) {
                        log.debug("Listing directory " + file.getAbsolutePath());
                    }
                    if (file.isDirectory()) {
                        if (log.isDebugEnabled()) {
                            log.debug("Listing " + url);
                        }
                        children = Arrays.asList(file.list());
                    }
                } else {
                    // No idea where the exception came from so rethrow it
                    throw e;
                }
            }
            // The URL prefix to use when recursively listing child resources
            String prefix = url.toExternalForm();
            if (!prefix.endsWith("/")) {
                prefix = prefix + "/";
            }
            // Iterate over immediate children, adding files and recurring into directories
            for (String child : children) {
                String resourcePath = path + "/" + child;
                resources.add(resourcePath);
                URL childUrl = new URL(prefix + child);
                resources.addAll(list(childUrl, resourcePath));
            }
        }
        return resources;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            // Ignore
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) InvalidPathException(java.nio.file.InvalidPathException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvalidPathException(java.nio.file.InvalidPathException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 33 with InvalidPathException

use of java.nio.file.InvalidPathException in project spring-framework by spring-projects.

the class MockServletContext method getResourceAsStream.

@Override
@Nullable
public InputStream getResourceAsStream(String path) {
    String resourceLocation = getResourceLocation(path);
    Resource resource = null;
    try {
        resource = this.resourceLoader.getResource(resourceLocation);
        if (!resource.exists()) {
            return null;
        }
        return resource.getInputStream();
    } catch (InvalidPathException | IOException ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Could not open InputStream for resource " + (resource != null ? resource : resourceLocation), ex);
        }
        return null;
    }
}
Also used : Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) InvalidPathException(java.nio.file.InvalidPathException) Nullable(org.springframework.lang.Nullable)

Example 34 with InvalidPathException

use of java.nio.file.InvalidPathException in project kafka by apache.

the class DelegatingClassLoader method initPluginLoader.

private void initPluginLoader(String path) {
    try {
        if (CLASSPATH_NAME.equals(path)) {
            scanUrlsAndAddPlugins(getParent(), ClasspathHelper.forJavaClassPath().toArray(new URL[0]), null);
        } else {
            Path pluginPath = Paths.get(path).toAbsolutePath();
            // Update for exception handling
            path = pluginPath.toString();
            // containing plugins
            if (Files.isDirectory(pluginPath)) {
                for (Path pluginLocation : PluginUtils.pluginLocations(pluginPath)) {
                    registerPlugin(pluginLocation);
                }
            } else if (PluginUtils.isArchive(pluginPath)) {
                registerPlugin(pluginPath);
            }
        }
    } catch (InvalidPathException | MalformedURLException e) {
        log.error("Invalid path in plugin path: {}. Ignoring.", path, e);
    } catch (IOException e) {
        log.error("Could not get listing for plugin path: {}. Ignoring.", path, e);
    } catch (ReflectiveOperationException e) {
        log.error("Could not instantiate plugins in: {}. Ignoring: {}", path, e);
    }
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL) InvalidPathException(java.nio.file.InvalidPathException)

Example 35 with InvalidPathException

use of java.nio.file.InvalidPathException in project alluxio by Alluxio.

the class AlluxioJniFuseFileSystem method truncateInternal.

private int truncateInternal(String path, long size) {
    // Truncate scenarios:
    // 1. Truncate size = 0, file does not exist => no-op
    // 2. Truncate size = 0, file exists and completed
    // => noop if file size = 0 or delete file
    // TODO(lu) delete open file entry if any, blocked by libfuse 3
    // 3. Truncate size = 0, file exists and is being written by current Fuse
    // => noop if written size = 0, otherwise delete file and update create file entry
    // 4. Truncate size = 0, file exists and is being written by other applications
    // => error out
    // 5. Truncate size != 0, file does not exist => error out
    // 6. Truncate size != 0, file is completed
    // => no-op if file size = truncate size, error out otherwise
    // 7. Truncate size != 0, file is being written by other applications
    // => error out, don't know exact file written size
    // 8. Truncate size != 0, file is being written by current Fuse
    // => no-op if written size = truncate size, error out otherwise
    // Error out in all other cases
    final AlluxioURI uri = mPathResolverCache.getUnchecked(path);
    URIStatus status = null;
    try {
        status = mFileSystem.getStatus(uri);
    } catch (FileDoesNotExistException | InvalidPathException e) {
        status = null;
    } catch (Throwable t) {
        LOG.error("Failed to truncate path {} to {}. Failed to get file status", path, size, t);
        return -ErrorCodes.EIO();
    }
    CreateFileEntry<FileOutStream> ce = mCreateFileEntries.getFirstByField(PATH_INDEX, path);
    if (size == 0) {
        if (status == null) {
            // Case 1: Truncate size = 0, file does not exist => no-op
            return 0;
        }
        try {
            // Case 2: Truncate size = 0, file exists and completed
            if (status.isCompleted()) {
                if (status.getLength() == 0) {
                    return 0;
                }
                // support open(O_WRONLY or O_RDWR) -> truncate() -> write()
                mFileSystem.delete(uri);
                // because concurrent read and delete are not supported
                return 0;
            }
            // Case 3: Truncate size = 0, file exists and is being written by current Fuse
            if (ce != null) {
                FileOutStream os = ce.getOut();
                long bytesWritten = os.getBytesWritten();
                if (bytesWritten == 0) {
                    return 0;
                }
                mCreateFileEntries.remove(ce);
                mFileSystem.delete(uri);
                os = mFileSystem.createFile(uri);
                final long fd = ce.getId();
                ce = new CreateFileEntry(fd, path, os);
                mCreateFileEntries.add(ce);
                mAuthPolicy.setUserGroupIfNeeded(uri);
                return 0;
            }
            // Case 4: Truncate size = 0, file exists and is being written by other applications
            LOG.error("Failed to truncate path {} to size 0, " + "file is being written in other applications", path);
            return -ErrorCodes.EOPNOTSUPP();
        } catch (Throwable t) {
            LOG.error("Failed to truncate path {} to {}. Failed to delete path {} from Alluxio", path, size, path);
            return -ErrorCodes.EIO();
        }
    }
    // Case 5: Truncate size != 0, file does not exist => error out
    if (status == null) {
        LOG.error("Cannot truncate non-existing file {} to size {}", path, size);
        return -ErrorCodes.EEXIST();
    }
    // Case 6: Truncate size != 0, file is completed
    if (status.isCompleted()) {
        if (size == status.getLength()) {
            return 0;
        }
        LOG.error("Cannot truncate file {} to non-zero size {}", path, size);
        return -ErrorCodes.EOPNOTSUPP();
    }
    // Case 7: Truncate size != 0, file is being written by other applications
    if (ce == null) {
        LOG.error("Cannot truncate {} to {}. " + "File is being written in other Fuse applications or APIs", path, size);
        return -ErrorCodes.EOPNOTSUPP();
    }
    // Case 8: Truncate size != 0, file is being written by current Fuse
    FileOutStream os = ce.getOut();
    long bytesWritten = os.getBytesWritten();
    if (bytesWritten == size) {
        return 0;
    }
    // error out otherwise
    LOG.error("Truncate file {} of size {} to size {} is not supported. " + "Alluxio supports sequential writes only and the written contents cannot be modified", path, bytesWritten, size);
    return -ErrorCodes.EOPNOTSUPP();
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) FileOutStream(alluxio.client.file.FileOutStream) URIStatus(alluxio.client.file.URIStatus) InvalidPathException(java.nio.file.InvalidPathException) AlluxioURI(alluxio.AlluxioURI)

Aggregations

InvalidPathException (java.nio.file.InvalidPathException)97 Path (java.nio.file.Path)53 IOException (java.io.IOException)26 URL (java.net.URL)14 File (java.io.File)13 MalformedURLException (java.net.MalformedURLException)12 Test (org.junit.Test)11 AlluxioURI (alluxio.AlluxioURI)7 Resource (org.springframework.core.io.Resource)7 Nullable (org.springframework.lang.Nullable)7 URI (java.net.URI)6 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)5 URISyntaxException (java.net.URISyntaxException)5 ArrayList (java.util.ArrayList)5 URIStatus (alluxio.client.file.URIStatus)4 ViewResolve (com.nvlad.yii2support.views.entities.ViewResolve)4 InputStream (java.io.InputStream)4 FileOutStream (alluxio.client.file.FileOutStream)3 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)3 SetAttributePOptions (alluxio.grpc.SetAttributePOptions)2