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

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

Introduction

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

Prototype

FileName getName();

Source Link

Document

Returns the name of this file.

Usage

From source file:org.obiba.opal.shell.commands.ReportCommand.java

private void deleteFileSilently(FileObject file) {
    try {/*from   ww  w .j  a  v  a2 s.c o m*/
        if (file.exists()) {
            file.delete();
        }
    } catch (FileSystemException ex) {
        log.error("Could not delete file: {}", file.getName().getPath());
    }
}

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

private Response moveTo(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();/*  w w  w. j av  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();
        }
        if (!sourceFile.isWriteable()) {
            return Response.status(Status.FORBIDDEN).entity("Source file cannot be moved: " + sourcePath)
                    .build();
        }
    }

    // do action
    for (String sourcePath : sourcesPath) {
        FileObject sourceFile = resolveFileInFileSystem(sourcePath);
        FileObject destinationFile = resolveFileInFileSystem(
                destinationPath + "/" + sourceFile.getName().getBaseName());
        sourceFile.moveTo(destinationFile);
    }

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

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();//w ww  . ja v a 2 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 getFolder(FileObject folder, Collection<String> children) throws IOException {
    SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String folderName = folder.getName().getBaseName();
    File compressedFolder = new File(System.getProperty("java.io.tmpdir"),
            ("".equals(folderName) ? "filesystem" : folderName) + "_"
                    + dateTimeFormatter.format(System.currentTimeMillis()) + ".zip");
    compressedFolder.deleteOnExit();//w ww  .  j  a  v a 2 s . c o  m
    String mimeType = mimeTypes.getContentType(compressedFolder);

    compressFolder(compressedFolder, folder, children);

    return Response.ok(compressedFolder, mimeType)
            .header("Content-Disposition", getContentDispositionOfAttachment(compressedFolder.getName()))
            .build();
}

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

private Response getFileDetails(FileObject file) throws FileSystemException {
    Opal.FileDto.Builder fileBuilder;/*w  ww  .j a va2  s.  c om*/

    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 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;//from ww w .j  a v a 2  s  .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());

        // 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  . ja va 2 s  .com
                }
            }
        }
    }
}

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();/*from w ww . j a v a  2  s.  c o  m*/
    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

private void addFiles(Opal.FileDto.Builder parentFolderBuilder, FileObject parentFolder)
        throws FileSystemException {
    Opal.FileDto.Builder fileBuilder;/*from   w  w  w  . j  av a 2  s .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());

        // 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());
    }
}