Example usage for org.apache.commons.vfs2 FileObject isReadable

List of usage examples for org.apache.commons.vfs2 FileObject isReadable

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject isReadable.

Prototype

boolean isReadable() throws FileSystemException;

Source Link

Document

Determines if this file can be read.

Usage

From source file:org.obiba.opal.web.FilesResource.java

private Response copyFrom(FileObject destinationFolder, Iterable<String> sourcesPath) throws IOException {
    // destination check
    String destinationPath = destinationFolder.getName().getPath();
    if (destinationFolder.getType() != FileType.FOLDER)
        return Response.status(Status.BAD_REQUEST).entity("Destination must be a folder: " + destinationPath)
                .build();//from w ww . ja  v a2  s. c o m

    // sources check
    for (String sourcePath : sourcesPath) {
        FileObject sourceFile = resolveFileInFileSystem(sourcePath);
        if (!sourceFile.exists())
            getPathNotExistResponse(sourcePath);
        if (!sourceFile.isReadable()) {
            return Response.status(Status.FORBIDDEN).entity("Source file is not readable: " + sourcePath)
                    .build();
        }
    }

    // do action
    for (String sourcePath : sourcesPath) {
        FileObject sourceFile = resolveFileInFileSystem(sourcePath);
        FileObject destinationFile = resolveFileInFileSystem(
                destinationPath + "/" + sourceFile.getName().getBaseName());
        FileSelector selector = sourceFile.getType() == FileType.FOLDER ? Selectors.SELECT_ALL
                : Selectors.SELECT_SELF;
        destinationFile.copyFrom(sourceFile, selector);
    }

    return Response.ok().build();
}

From source file:org.obiba.opal.web.FilesResource.java

private Response getFileDetails(FileObject file) throws FileSystemException {
    Opal.FileDto.Builder fileBuilder;/*from  w ww.j  a  v  a 2s.co  m*/

    fileBuilder = Opal.FileDto.newBuilder();
    fileBuilder.setName(file.getName().getBaseName()).setPath(file.getName().getPath());
    fileBuilder.setType(
            file.getType() == FileType.FILE ? Opal.FileDto.FileType.FILE : Opal.FileDto.FileType.FOLDER);
    fileBuilder.setReadable(file.isReadable()).setWritable(file.isWriteable());

    // Set size on files only, not folders.
    if (file.getType() == FileType.FILE) {
        fileBuilder.setSize(file.getContent().getSize());
    }

    fileBuilder.setLastModifiedTime(file.getContent().getLastModifiedTime());

    return Response.ok(fileBuilder.build()).build();
}

From source file:org.obiba.opal.web.FilesResource.java

private Response getFolderDetails(FileObject folder) throws FileSystemException {
    // Create a FileDto representing the folder identified by the path.
    Opal.FileDto.Builder folderBuilder = getBaseFolderBuilder(folder);

    // Create FileDtos for each file & folder in the folder corresponding to the path.
    if (folder.isReadable()) {
        addChildren(folderBuilder, folder, 2);
    }/*  w w w. j av  a  2 s  .c  om*/

    return Response.ok(folderBuilder.build()).build();

}

From source file:org.obiba.opal.web.FilesResource.java

private Opal.FileDto.Builder getBaseFolderBuilder(FileObject folder) throws FileSystemException {
    Opal.FileDto.Builder fileBuilder = Opal.FileDto.newBuilder();
    String folderName = folder.getName().getBaseName();
    fileBuilder.setName("".equals(folderName) ? "root" : folderName).setType(Opal.FileDto.FileType.FOLDER)
            .setPath(folder.getName().getPath());
    fileBuilder.setLastModifiedTime(folder.getContent().getLastModifiedTime());
    fileBuilder.setReadable(folder.isReadable()).setWritable(folder.isWriteable());
    return fileBuilder;
}

From source file:org.obiba.opal.web.FilesResource.java

private void addChildren(Opal.FileDto.Builder folderBuilder, FileObject parentFolder, int level)
        throws FileSystemException {
    Opal.FileDto.Builder fileBuilder;// w w  w .j av a 2s  . c  o  m

    // Get the children for the current folder (list of files & folders).
    List<FileObject> children = Arrays.asList(parentFolder.getChildren());

    Collections.sort(children, new Comparator<FileObject>() {

        @Override
        public int compare(FileObject arg0, FileObject arg1) {
            return arg0.getName().compareTo(arg1.getName());
        }
    });

    // Loop through all children.
    for (FileObject child : children) {
        // Build a FileDto representing the child.
        fileBuilder = Opal.FileDto.newBuilder();
        fileBuilder.setName(child.getName().getBaseName()).setPath(child.getName().getPath());
        fileBuilder.setType(
                child.getType() == FileType.FILE ? Opal.FileDto.FileType.FILE : Opal.FileDto.FileType.FOLDER);
        fileBuilder.setReadable(child.isReadable()).setWritable(child.isWriteable());

        // Set size on files only, not folders.
        if (child.getType() == FileType.FILE) {
            fileBuilder.setSize(child.getContent().getSize());
        }

        fileBuilder.setLastModifiedTime(child.getContent().getLastModifiedTime());

        if (child.getType().hasChildren() && child.getChildren().length > 0 && level - 1 > 0
                && child.isReadable()) {
            addChildren(fileBuilder, child, level - 1);
        }

        // Add the current child to the parent FileDto (folder).
        folderBuilder.addChildren(fileBuilder.build());
    }
}

From source file:org.obiba.opal.web.FilesResource.java

private void addFolder(String basePath, FileObject folder, ZipOutputStream outputStream,
        Collection<String> children) throws IOException {
    int baseLength = "/".equals(basePath) ? 1 : basePath.length() + 1;

    // Add the folder.
    outputStream.putNextEntry(new ZipEntry(folder.getName().getPath().substring(baseLength) + "/"));

    // Add its children files and subfolders.
    FileObject[] files = folder.getChildren();
    for (FileObject file : files) {
        if (children == null || children.isEmpty() || children.contains(file.getName().getBaseName())) {
            // only add files for which download is authorized
            if (file.isReadable()) {
                if (file.getType() == FileType.FOLDER) {
                    addFolder(basePath, file, outputStream, null);
                } else {
                    outputStream.putNextEntry(new ZipEntry(file.getName().getPath().substring(baseLength)));
                    try (FileInputStream inputStream = new FileInputStream(
                            opalRuntime.getFileSystem().getLocalFile(file))) {
                        StreamUtil.copy(inputStream, outputStream);
                        outputStream.closeEntry();
                    }/*from   w w  w .jav a2s  . co m*/
                }
            }
        }
    }
}

From source file:org.obiba.opal.web.FilesResourceTest.java

@Test
public void testCreateFolder_FolderCreatedSuccessfully() throws FileSystemException, URISyntaxException {
    expect(fileObjectMock.getType()).andReturn(FileType.FOLDER).atLeastOnce();
    expect(fileObjectMock.exists()).andReturn(true).atLeastOnce();

    FileObject childFolderMock = createMock(FileObject.class);

    FileName fileNameMock = createMock(FileName.class);
    expect(fileNameMock.getBaseName()).andReturn("folder").atLeastOnce();
    expect(fileNameMock.getPath()).andReturn("folder1/folder").atLeastOnce();

    expect(childFolderMock.getName()).andReturn(fileNameMock).atLeastOnce();
    expect(childFolderMock.exists()).andReturn(false).atLeastOnce();

    FileContent mockContent = createMock(FileContent.class);
    expect(childFolderMock.getContent()).andReturn(mockContent).atLeastOnce();
    expect(mockContent.getLastModifiedTime()).andReturn((long) 1).atLeastOnce();

    childFolderMock.createFolder();/*  ww  w .j  av a2 s.com*/
    FileObject parentFolderMock = createMock(FileObject.class);
    expect(childFolderMock.getParent()).andReturn(parentFolderMock).atLeastOnce();
    expect(childFolderMock.isReadable()).andReturn(true).atLeastOnce();
    expect(childFolderMock.isWriteable()).andReturn(true).atLeastOnce();
    expect(parentFolderMock.isWriteable()).andReturn(true).atLeastOnce();
    expect(fileObjectMock.resolveFile("folder")).andReturn(childFolderMock).atLeastOnce();
    expect(uriInfoMock.getBaseUriBuilder()).andReturn(UriBuilder.fromPath("/"));

    replay(fileObjectMock, uriInfoMock, parentFolderMock, childFolderMock, fileNameMock, mockContent);

    Response response = getFileResource().createFolder("folder1", "folder", uriInfoMock);
    assertThat(response.getStatus()).isEqualTo(Status.CREATED.getStatusCode());
    verify(fileObjectMock, uriInfoMock, parentFolderMock, childFolderMock, fileNameMock, mockContent);
}

From source file:org.obiba.opal.web.FileSystemResource.java

@GET
public Opal.FileDto getFileSystem() throws FileSystemException {
    FileObject root = opalRuntime.getFileSystem().getRoot();

    // Create a root FileDto representing the root of the FileSystem.
    Opal.FileDto.Builder fileBuilder = Opal.FileDto.newBuilder();
    fileBuilder.setName("root").setType(Opal.FileDto.FileType.FOLDER).setPath("/");
    fileBuilder.setReadable(root.isReadable()).setWritable(root.isWriteable());

    // Create FileDtos for each file & folder in the FileSystem and add them to the root FileDto recursively.
    addFiles(fileBuilder, root);//w w w  . j a v  a2  s. c  o m

    return fileBuilder.build();
}

From source file:org.obiba.opal.web.FileSystemResource.java

private void addFiles(Opal.FileDto.Builder parentFolderBuilder, FileObject parentFolder)
        throws FileSystemException {
    Opal.FileDto.Builder fileBuilder;/*ww w.j ava 2s . c  om*/

    // Get the children for the current folder (list of files & folders).
    List<FileObject> children = Arrays.asList(parentFolder.getChildren());

    Collections.sort(children, new Comparator<FileObject>() {

        @Override
        public int compare(FileObject arg0, FileObject arg1) {
            return arg0.getName().compareTo(arg1.getName());
        }
    });

    // Loop through all children.
    for (FileObject child : children) {

        // Build a FileDto representing the child.
        fileBuilder = Opal.FileDto.newBuilder();
        fileBuilder.setName(child.getName().getBaseName()).setPath(child.getName().getPath());
        fileBuilder.setType(
                child.getType() == FileType.FILE ? Opal.FileDto.FileType.FILE : Opal.FileDto.FileType.FOLDER);
        fileBuilder.setReadable(child.isReadable()).setWritable(child.isWriteable());

        // If the current child is a folder, add its children recursively.
        if (child.getType() == FileType.FOLDER && child.isReadable()) {
            addFiles(fileBuilder, child);
        }

        // Add the current child to the parent FileDto (folder).
        parentFolderBuilder.addChildren(fileBuilder.build());
    }
}

From source file:org.obiba.opal.web.shell.reporting.ProjectReportTemplateResource.java

@GET
@Path("/reports")
public List<Opal.ReportDto> getReports() throws FileSystemException {
    getReportTemplate();/*from  w w w.j a va  2  s.c  o  m*/
    FileObject reportFolder = getReportFolder();
    List<Opal.ReportDto> reports = Lists.newArrayList();
    if (reportFolder.exists()) {
        for (FileObject reportFile : reportFolder.getChildren()) {
            if (reportFile.getType() == FileType.FILE
                    && reportFile.getName().getBaseName().startsWith(name + "-") && reportFile.isReadable()) {
                reports.add(getReportDto(reportFile));
            }
        }
    }
    return reports;
}