Search in sources :

Example 26 with NotDirectoryException

use of java.nio.file.NotDirectoryException in project ballerina by ballerina-lang.

the class FileSystemService method createErrorResponse.

/**
 * Creates an error response for the given IO Exception.
 *
 * @param ex Thrown Exception
 * @return Error Message
 */
private Response createErrorResponse(Throwable ex) {
    JsonObject entity = new JsonObject();
    String errMsg = ex.getMessage();
    if (ex instanceof AccessDeniedException) {
        errMsg = "Access Denied to " + ex.getMessage();
    } else if (ex instanceof NoSuchFileException) {
        errMsg = "No such file: " + ex.getMessage();
    } else if (ex instanceof FileAlreadyExistsException) {
        errMsg = "File already exists: " + ex.getMessage();
    } else if (ex instanceof NotDirectoryException) {
        errMsg = "Not a directory: " + ex.getMessage();
    } else if (ex instanceof ReadOnlyFileSystemException) {
        errMsg = "Read only: " + ex.getMessage();
    } else if (ex instanceof DirectoryNotEmptyException) {
        errMsg = "Directory not empty: " + ex.getMessage();
    } else if (ex instanceof FileNotFoundException) {
        errMsg = "File not found: " + ex.getMessage();
    }
    entity.addProperty("Error", errMsg);
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(entity).header(ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, '*').type(MediaType.APPLICATION_JSON).build();
}
Also used : AccessDeniedException(java.nio.file.AccessDeniedException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NotDirectoryException(java.nio.file.NotDirectoryException) NoSuchFileException(java.nio.file.NoSuchFileException) FileNotFoundException(java.io.FileNotFoundException) JsonObject(com.google.gson.JsonObject) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) ReadOnlyFileSystemException(java.nio.file.ReadOnlyFileSystemException)

Example 27 with NotDirectoryException

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

the class MCRFileSystemProvider method resolvePath.

static MCRFilesystemNode resolvePath(MCRPath path) throws IOException {
    try {
        String ifsid = nodeCache.getUnchecked(path);
        MCRFilesystemNode node = MCRFilesystemNode.getNode(ifsid);
        if (node != null) {
            return node;
        }
        nodeCache.invalidate(path);
        return resolvePath(path);
    } catch (UncheckedExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof NoSuchFileException) {
            throw (NoSuchFileException) cause;
        }
        if (cause instanceof NotDirectoryException) {
            throw (NotDirectoryException) cause;
        }
        if (cause instanceof IOException) {
            throw (IOException) cause;
        }
        throw e;
    }
}
Also used : NotDirectoryException(java.nio.file.NotDirectoryException) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Example 28 with NotDirectoryException

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

the class MCRFileSystemProvider method newDirectoryStream.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#newDirectoryStream(java.nio.file.Path, java.nio.file.DirectoryStream.Filter)
     */
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException {
    MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(dir);
    MCRFilesystemNode node = resolvePath(mcrPath);
    if (node instanceof MCRDirectory) {
        return new MCRDirectoryStream((MCRDirectory) node, mcrPath);
    }
    throw new NotDirectoryException(dir.toString());
}
Also used : NotDirectoryException(java.nio.file.NotDirectoryException) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 29 with NotDirectoryException

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

the class MCRFileSystemProvider method createDirectory.

/* (non-Javadoc)
     * @see java.nio.file.spi.FileSystemProvider#createDirectory(java.nio.file.Path, java.nio.file.attribute.FileAttribute[])
     */
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
    if (attrs.length > 0) {
        throw new UnsupportedOperationException("Setting 'attrs' atomically is unsupported.");
    }
    MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(dir);
    MCRDirectory rootDirectory;
    if (mcrPath.isAbsolute() && mcrPath.getNameCount() == 0) {
        rootDirectory = MCRDirectory.getDirectory(mcrPath.getOwner());
        if (rootDirectory != null) {
            throw new FileAlreadyExistsException(mcrPath.toString());
        }
        rootDirectory = new MCRDirectory(mcrPath.getOwner());
        return;
    }
    rootDirectory = getRootDirectory(mcrPath);
    MCRPath parentPath = mcrPath.getParent();
    MCRPath absolutePath = getAbsolutePathFromRootComponent(parentPath);
    MCRFilesystemNode childByPath = rootDirectory.getChildByPath(absolutePath.toString());
    if (childByPath == null) {
        throw new NoSuchFileException(parentPath.toString(), dir.getFileName().toString(), "parent directory does not exist");
    }
    if (childByPath instanceof MCRFile) {
        throw new NotDirectoryException(parentPath.toString());
    }
    MCRDirectory parentDir = (MCRDirectory) childByPath;
    String dirName = mcrPath.getFileName().toString();
    if (parentDir.getChild(dirName) != null) {
        throw new FileAlreadyExistsException(mcrPath.toString());
    }
    new MCRDirectory(dirName, parentDir);
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) NotDirectoryException(java.nio.file.NotDirectoryException) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) NoSuchFileException(java.nio.file.NoSuchFileException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 30 with NotDirectoryException

use of java.nio.file.NotDirectoryException in project neo4j by neo4j.

the class FileUtils method moveFileToDirectory.

/**
 * Utility method that moves a file from its current location to the
 * provided target directory. If rename fails (for example if the target is
 * another disk) a copy/delete will be performed instead.
 *
 * @param toMove The File object to move.
 * @param targetDirectory the destination directory
 * @return the new file, null iff the move was unsuccessful
 * @throws IOException if an IO error occurs.
 */
public static Path moveFileToDirectory(Path toMove, Path targetDirectory) throws IOException {
    if (notExists(targetDirectory)) {
        Files.createDirectories(targetDirectory);
    }
    if (!isDirectory(targetDirectory)) {
        throw new NotDirectoryException(targetDirectory.toString());
    }
    Path target = targetDirectory.resolve(toMove.getFileName());
    moveFile(toMove, target);
    return target;
}
Also used : Path(java.nio.file.Path) NotDirectoryException(java.nio.file.NotDirectoryException)

Aggregations

NotDirectoryException (java.nio.file.NotDirectoryException)31 Path (java.nio.file.Path)17 NoSuchFileException (java.nio.file.NoSuchFileException)16 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)7 Test (org.junit.Test)7 IOException (java.io.IOException)6 AccessDeniedException (java.nio.file.AccessDeniedException)6 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)6 FileNotFoundException (java.io.FileNotFoundException)5 FileSystemException (java.nio.file.FileSystemException)5 MCRFilesystemNode (org.mycore.datamodel.ifs.MCRFilesystemNode)5 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)4 MCRPath (org.mycore.datamodel.niofs.MCRPath)4 EOFException (java.io.EOFException)3 HashSet (java.util.HashSet)3 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)3 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)3