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

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

Introduction

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

Prototype

FileContent getContent() throws FileSystemException;

Source Link

Document

Returns this file's content.

Usage

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();//w  w w. ja  v  a2 s.  c  om
    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  ww. jav a  2  s .c o  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);
    }
}

From source file:org.obiba.opal.web.magma.view.FileViewDtoExtension.java

@Override
public TableDto asTableDto(ViewDto viewDto, Magma.TableDto.Builder tableDtoBuilder) {
    FileViewDto fileDto = viewDto.getExtension(FileViewDto.view);
    try {//  w  ww.ja v  a 2  s .co  m
        FileObject file = opalRuntime.getFileSystem().getRoot().resolveFile(fileDto.getFilename());
        if (file.exists()) {
            try (InputStream is = file.getContent().getInputStream()) {
                return makeTableDtoFromFile(tableDtoBuilder, fileDto, is);
            }
        }
        throw new RuntimeException("cannot find file specified '" + fileDto.getFilename() + "'");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

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

private Opal.ReportDto getReportDto(FileObject reportFile) throws FileSystemException {
    String publicLink = "/report/public/" + opalRuntime.getFileSystem().getObfuscatedPath(reportFile)
            + "?project=" + getReportTemplate().getProject();

    return Opal.ReportDto.newBuilder()//
            .setName(reportFile.getName().getBaseName())//
            .setLink("/files" + reportFile.getName().getPath())//
            .setSize(reportFile.getContent().getSize())//
            .setLastModifiedTime(reportFile.getContent().getLastModifiedTime())//
            .setPublicLink(publicLink).build();
}

From source file:org.onehippo.forge.content.exim.core.impl.AbstractContentMigrationTask.java

/**
 * {@inheritDoc}/*from w ww. ja va 2s.c o  m*/
 */
@Override
public ContentNode readContentNodeFromJsonFile(final FileObject sourceFile) throws ContentMigrationException {
    ContentNode contentNode = null;

    InputStream is = null;
    BufferedInputStream bis = null;

    try {
        is = sourceFile.getContent().getInputStream();
        bis = new BufferedInputStream(is);
        contentNode = getObjectMapper().readValue(bis, ContentNode.class);
    } catch (IOException e) {
        throw new ContentMigrationException(e.toString(), e);
    } finally {
        IOUtils.closeQuietly(bis);
        IOUtils.closeQuietly(is);
    }

    return contentNode;
}

From source file:org.onehippo.forge.content.exim.core.impl.AbstractContentMigrationTask.java

/**
 * {@inheritDoc}/*from   w w  w  .j av  a2 s .com*/
 */
@Override
public ContentNode readContentNodeFromXmlFile(final FileObject sourceFile) throws ContentMigrationException {
    ContentNode contentNode = null;

    InputStream is = null;
    BufferedInputStream bis = null;

    try {
        is = sourceFile.getContent().getInputStream();
        bis = new BufferedInputStream(is);
        JAXBContext jaxbContext = JAXBContext.newInstance(ContentNode.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        contentNode = (ContentNode) jaxbUnmarshaller.unmarshal(bis);
    } catch (IOException | JAXBException e) {
        throw new ContentMigrationException(e.toString(), e);
    } finally {
        IOUtils.closeQuietly(bis);
        IOUtils.closeQuietly(is);
    }

    return contentNode;
}

From source file:org.onehippo.forge.content.exim.core.impl.AbstractContentMigrationTask.java

/**
 * {@inheritDoc}//  www .ja v  a  2  s. c  o  m
 */
@Override
public void writeContentNodeToJsonFile(final ContentNode contentNode, final FileObject targetFile)
        throws ContentMigrationException {
    OutputStream os = null;
    BufferedOutputStream bos = null;

    try {
        os = targetFile.getContent().getOutputStream();
        bos = new BufferedOutputStream(os);
        getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(bos, contentNode);
    } catch (IOException e) {
        throw new ContentMigrationException(e.toString(), e);
    } finally {
        IOUtils.closeQuietly(bos);
        IOUtils.closeQuietly(os);
    }
}

From source file:org.onehippo.forge.content.exim.core.impl.AbstractContentMigrationTask.java

/**
 * {@inheritDoc}/*from   w  ww .  j  a  va2  s .c  om*/
 */
@Override
public void writeContentNodeToXmlFile(final ContentNode contentNode, final FileObject targetFile)
        throws ContentMigrationException {
    OutputStream os = null;
    BufferedOutputStream bos = null;

    try {
        os = targetFile.getContent().getOutputStream();
        bos = new BufferedOutputStream(os);
        JAXBContext jaxbContext = JAXBContext.newInstance(ContentNode.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(contentNode, bos);
    } catch (IOException | JAXBException e) {
        throw new ContentMigrationException(e.toString(), e);
    } finally {
        IOUtils.closeQuietly(bos);
        IOUtils.closeQuietly(os);
    }
}

From source file:org.onehippo.forge.content.pojo.common.jcr.DefaultJcrContentValueConverter.java

@Override
public BinaryValue toBinaryValue(Value value, String mimeType) throws ContentNodeException {
    try {//from w  w  w .jav a 2s.  com
        Binary binary = value.getBinary();
        long size = binary.getSize();

        if (size < getDataUrlSizeThreashold()) {
            InputStream input = null;
            ByteArrayOutputStream output = null;

            try {
                input = binary.getStream();
                output = new ByteArrayOutputStream((int) size);
                IOUtils.copy(input, output);

                input.close();
                input = null;
                binary.dispose();
                binary = null;

                return new BinaryValue(output.toByteArray());
            } finally {
                IOUtils.closeQuietly(output);
                IOUtils.closeQuietly(input);

                if (binary != null) {
                    binary.dispose();
                }
            }
        } else {
            FileObject binaryFile = createRandomBinaryValueFileObject(mimeType);
            InputStream input = null;
            OutputStream output = null;

            try {
                input = binary.getStream();
                output = binaryFile.getContent().getOutputStream();
                IOUtils.copy(input, output);

                output.close();
                output = null;
                input.close();
                input = null;
                binary.dispose();
                binary = null;

                return new BinaryValue(binaryFile);
            } finally {
                IOUtils.closeQuietly(output);
                IOUtils.closeQuietly(input);

                if (binary != null) {
                    binary.dispose();
                }
            }
        }
    } catch (Exception e) {
        throw new ContentNodeException(e.toString(), e);
    }
}

From source file:org.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java

private static void fillDirProps(FileObject fo, Map<String, Object> properties) throws FileSystemException {
    properties.put("x-proactive-ds-type", "DIRECTORY");
    properties.put("Last-Modified", new Date(fo.getContent().getLastModifiedTime()));
}