Example usage for java.nio.file AccessDeniedException getFile

List of usage examples for java.nio.file AccessDeniedException getFile

Introduction

In this page you can find the example usage for java.nio.file AccessDeniedException getFile.

Prototype

public String getFile() 

Source Link

Document

Returns the file used to create this exception.

Usage

From source file:com.epam.catgenome.manager.FileManager.java

/**
 * Returns contents of a directory, specified by path, to browse NGS files
 *
 * @param path a path to directory to browse
 * @return {@link List} of {@link FsDirectory}s, and {@link FsFile}, representing subdirectories and files
 * @throws IOException/*  w  w w  . j a  v a 2s .c  om*/
 */
public List<AbstractFsItem> loadDirectoryContents(String path) throws IOException {
    if (!filesBrowsingAllowed) {
        throw new AccessDeniedException("Server file system browsing is not allowed");
    }

    List<File> parentDirs = new ArrayList<>();
    if (path == null) {
        if ("/".equals(ngsDataRootPath)) {
            parentDirs = Arrays.asList(File.listRoots());
        } else {
            parentDirs.add(new File(ngsDataRootPath));
        }
    } else {
        parentDirs.add(new File(path));
    }

    Assert.isTrue(parentDirs.stream().allMatch(File::exists), "Specified path does not exist: " + path);
    Assert.isTrue(parentDirs.stream().allMatch(File::isDirectory),
            "Specified path is not a directory: " + path);

    List<AbstractFsItem> items = new ArrayList<>();

    boolean accessDenied = false;
    String fileName = "";
    String otherFileName = "";

    for (File parentDir : parentDirs) {
        if (parentDir.listFiles() == null) {
            continue;
        }

        try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(parentDir.toPath())) {
            for (Path child : dirStream) {
                try {
                    File childFile = child.toFile();
                    if (childFile.isDirectory()) {
                        FsDirectory directory = new FsDirectory();
                        directory.setPath(childFile.getAbsolutePath());
                        if (childFile.canRead()) {
                            directory.setFileCount(countChildrenFiles(child));
                        }

                        items.add(directory);
                    } else {
                        addFsFile(items, childFile);
                    }
                } catch (AccessDeniedException e) {
                    LOGGER.error("Access denied:", e);
                    accessDenied = true;
                    fileName = e.getFile();
                    otherFileName = e.getOtherFile();
                }
            }

            if (items.isEmpty() && accessDenied) {
                throw new AccessDeniedException(fileName, otherFileName, "Access denied");
            }
        }
    }

    return items;
}