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

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

Introduction

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

Prototype

boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

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

@POST
@Path("/{path:.*}")
@Consumes("multipart/form-data")
@Produces("text/html")
@AuthenticatedByCookie//from w  w  w. j a v a 2 s.c  om
public Response uploadFile(@PathParam("path") String path, @Context UriInfo uriInfo,
        @Context HttpServletRequest request) throws FileSystemException, FileUploadException {

    String folderPath = getPathOfFileToWrite(path);
    FileObject folder = resolveFileInFileSystem(folderPath);

    if (folder == null || !folder.exists()) {
        return getPathNotExistResponse(path);
    }
    if (folder.getType() != FileType.FOLDER) {
        return Response.status(Status.FORBIDDEN).entity("Not a folder: " + path).build();
    }

    FileItem uploadedFile = getUploadedFile(request);
    if (uploadedFile == null) {
        return Response.status(Status.BAD_REQUEST).entity(
                "No file has been submitted. Please make sure that you are submitting a file with your request.")
                .build();
    }

    return doUploadFile(folderPath, folder, uploadedFile, uriInfo);
}

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

private Response doUploadFile(String folderPath, FileObject folder, FileItem uploadedFile, UriInfo uriInfo)
        throws FileSystemException {
    String fileName = uploadedFile.getName();
    FileObject file = folder.resolveFile(fileName);
    boolean overwrite = file.exists();

    writeUploadedFileToFileSystem(uploadedFile, file);

    log.info("The following file was uploaded to Opal file system : {}", file.getURL());

    if (overwrite) {
        return Response.ok().build();
    }//ww  w.j a va 2  s.  c  om
    URI fileUri = uriInfo.getBaseUriBuilder().path(FilesResource.class).path(folderPath).path(fileName).build();
    return Response.created(fileUri)//
            .header(AuthorizationInterceptor.ALT_PERMISSIONS, new OpalPermissions(fileUri, AclAction.FILES_ALL))//
            .build();
}

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

@Nullable
private Response validateFolder(FileObject folder, String path) throws FileSystemException {
    if (folder == null || !folder.exists()) {
        return getPathNotExistResponse(path);
    }//from   w w  w  .j  a v  a 2 s.  c  o m
    if (folder.getType() != FileType.FOLDER) {
        return Response.status(Status.FORBIDDEN).entity("Not a folder: " + path).build();
    }
    return null;
}

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

@Nullable
private Response validateFile(FileObject file) throws FileSystemException {
    // Folder or file already exist at specified path.
    if (file.exists()) {
        return Response.status(Status.FORBIDDEN).entity("cannotCreateFolderPathAlreadyExist").build();
    }/*from  www.j  a  v a2  s  . co  m*/

    // Parent folder is read-only.
    if (!file.getParent().isWriteable()) {
        return Response.status(Status.FORBIDDEN).entity("cannotCreateFolderParentIsReadOnly").build();
    }

    return null;
}

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

@DELETE
@Path("/{path:.*}")
public Response deleteFile(@PathParam("path") String path) throws FileSystemException {
    FileObject file = resolveFileInFileSystem(path);

    // File or folder does not exist.
    if (!file.exists()) {
        return getPathNotExistResponse(path);
    }/*from w  w  w. j a  va2s.c  o  m*/

    // Read-only file or folder.
    if (!file.isWriteable()) {
        return Response.status(Status.FORBIDDEN).entity("cannotDeleteReadOnlyFile").build();
    }

    try {
        if (file.getType() == FileType.FOLDER) {
            deleteFolder(file);
        } else {
            file.delete();
        }
        subjectAclService.deleteNodePermissions("/files/" + path);
        return Response.ok("The following file or folder has been deleted : " + path).build();
    } catch (FileSystemException couldNotDeleteFile) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("couldNotDeleteFileError").build();
    }
}

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

@After
public void tearDown() throws FileSystemException {
    // Delete any files created by the test.
    for (String filePath : filesCreatedByTest) {
        FileObject file = fileSystem.getRoot().resolveFile(filePath);
        if (file.exists()) {
            file.delete();/*ww  w  .  j  a  va2 s. c om*/
        }
    }
}

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

private int verifyThatChildrenExistInFileSystem(FileDto folder, int childrenCounter)
        throws FileSystemException {
    FileObject correspondingFileObj;
    int counter = childrenCounter;
    for (FileDto child : folder.getChildrenList()) {
        counter++;/*  w  w w.  j a v a2s  .  c  o  m*/
        correspondingFileObj = fileSystem.getRoot().resolveFile(child.getPath());
        assertThat(correspondingFileObj.exists()).isTrue();
        if (child.getType() == FileDto.FileType.FOLDER) {
            counter = verifyThatChildrenExistInFileSystem(child, childrenCounter);
        }
    }
    return counter;
}

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

@Test
public void testCreateFolder_CannotCreateFolderParentIsReadOnly()
        throws FileSystemException, URISyntaxException {
    expect(fileObjectMock.getType()).andReturn(FileType.FOLDER).atLeastOnce();
    expect(fileObjectMock.exists()).andReturn(true).atLeastOnce();
    FileObject childFolderMock = createMock(FileObject.class);
    expect(childFolderMock.exists()).andReturn(false).atLeastOnce();
    FileObject parentFolderMock = createMock(FileObject.class);
    expect(childFolderMock.getParent()).andReturn(parentFolderMock).atLeastOnce();
    expect(parentFolderMock.isWriteable()).andReturn(false).atLeastOnce();
    expect(fileObjectMock.resolveFile("folder")).andReturn(childFolderMock).atLeastOnce();

    replay(fileObjectMock, parentFolderMock, childFolderMock);

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

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 .  jav a2s . co 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.magma.view.FileViewDtoExtension.java

@Override
public View fromDto(ViewDto viewDto, Builder viewBuilder) {
    FileViewDto fileDto = viewDto.getExtension(FileViewDto.view);
    try {/*from  w  w w.java 2 s  .co m*/
        FileObject file = opalRuntime.getFileSystem().getRoot().resolveFile(fileDto.getFilename());
        if (file.exists()) {
            try (InputStream is = file.getContent().getInputStream()) {
                return makeViewFromFile(viewBuilder, fileDto, is);
            }
        }
        throw new RuntimeException("cannot find file specified '" + fileDto.getFilename() + "'");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}