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.mycore.common.content.MCRContent.java

/**
 * Sends the content to the given Apache VFS file object
 * // w ww  .  ja va2  s .  c o m
 * @param target
 *            the file to write the content to
 */
public void sendTo(FileObject target) throws IOException {
    sendTo(target.getContent().getOutputStream(), true);
}

From source file:org.obiba.opal.core.security.OpalKeyStore.java

public X509Certificate importCertificate(String alias, FileObject certFile) {
    try {/*w ww. j a  v  a  2  s . c o m*/
        return importCertificate(alias, certFile.getContent().getInputStream());
    } catch (FileSystemException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.obiba.opal.core.security.OpalKeyStore.java

/**
 * Import a private key and it's associated certificate into the keystore at the given alias.
 *
 * @param alias name of the key/* w ww. ja va2  s  .c o  m*/
 * @param privateKey private key in the PEM format
 * @param certificate certificate in the PEM format
 */
public void importKey(String alias, FileObject privateKey, FileObject certificate) {
    try {
        InputStream privateIn = privateKey.getContent().getInputStream();
        InputStream certificateIn = certificate.getContent().getInputStream();
        importKey(alias, privateIn, certificateIn);
    } catch (FileSystemException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.obiba.opal.core.security.OpalKeyStore.java

/**
 * Import a private key into the keystore and generate an associated certificate at the given alias.
 *
 * @param alias name of the key/*  ww w .  ja v a 2s .  com*/
 * @param privateKey private key in the PEM format
 * @param certificateInfo Certificate attributes as a String (e.g. CN=Administrator, OU=Bioinformatics, O=GQ,
 * L=Montreal, ST=Quebec, C=CA)
 */
public void importKey(String alias, FileObject privateKey, String certificateInfo) {
    try {
        importKey(alias, privateKey.getContent().getInputStream(), certificateInfo);
    } catch (FileSystemException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.obiba.opal.core.service.TaxonomyServiceImpl.java

@Override
public Taxonomy importFileTaxonomy(@NotNull String file) throws FileSystemException {
    FileObject fileObj = resolveFileInFileSystem(file);

    try {/*from  www.j  a  v a 2s.com*/
        InputStream input = fileObj.getContent().getInputStream();
        TaxonomyYaml yaml = new TaxonomyYaml();
        Taxonomy taxonomy = yaml.load(input);
        saveTaxonomy(taxonomy);
        return taxonomy;
    } catch (Exception e) {
        log.error("Failed loading taxonomy from: " + file, e);
    }

    return null;
}

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

private Writer getCertificateWriter() throws FileSystemException {
    Writer certificateWriter;//from w  w  w . j  av a2s  . c om
    if (options.isCertificate()) {
        FileObject outputFile = getFileSystemRoot().resolveFile(options.getCertificate());
        certificateWriter = new OutputStreamWriter(outputFile.getContent().getOutputStream());
    } else {
        certificateWriter = new StringWriter();
    }
    return certificateWriter;
}

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

private Response getFileDetails(FileObject file) throws FileSystemException {
    Opal.FileDto.Builder fileBuilder;//from w ww.ja va 2 s  .  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 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   w w w . j a v a2s.  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 writeUploadedFileToFileSystem(FileItem uploadedFile, FileObject fileToWriteTo) {

    // OPAL-919: We need to wrap the OutputStream returned by commons-vfs into another OutputStream
    // to force a call to flush() on every call to write() in order to prevent the system from running out of memory
    // when copying large files.
    try (OutputStream localFileStream = new BufferedOutputStream(fileToWriteTo.getContent().getOutputStream()) {
        @Override/*  w w  w  .  ja va2  s. c  o  m*/
        public synchronized void write(byte[] b, int off, int len) throws IOException {
            flush();
            super.write(b, off, len);
        }

    }; InputStream uploadedFileStream = uploadedFile.getInputStream()) {

        StreamUtil.copy(uploadedFileStream, localFileStream);
    } catch (IOException couldNotWriteUploadedFile) {
        throw new RuntimeException("Could not write uploaded file to Opal file system",
                couldNotWriteUploadedFile);
    }
}