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:fr.cls.atoll.motu.library.misc.intfce.Organizer.java

/**
 * Gets the uri as input stream./*  w  w w.  j av a 2 s . c  o m*/
 * 
 * @param uri the uri
 * 
 * @return the uri as input stream
 * 
 * @throws MotuException the motu exception
 */
public static InputStream getUriAsInputStream(String uri) throws MotuException {
    InputStream in = null;
    try {

        in = ConfigLoader.getInstance().getAsStream(uri);
        if (in == null) {
            FileObject fileObject = Organizer.resolveFile(uri);
            if (fileObject != null) {
                // URL url = fileObject.getURL();
                // URLConnection urlConnection = url.openConnection();
                // in = urlConnection.getInputStream();
                // fileObject.close();
                in = fileObject.getContent().getInputStream();

                // With sftp, session seems not to be disconnected, so force
                // close ?
                // ((AbstractFileSystem)fileObject.getFileSystem()).closeCommunicationLink();

            }
        }
    } catch (IOException e) {
        throw new MotuException(String.format("'%s' uri file has not be found", uri), e);
    }
    return in;
}

From source file:com.sonicle.webtop.mail.Service.java

public void processAttachFromCloud(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
    try {/*w  ww  .jav a 2 s.  c  o m*/
        int storeId = ServletUtils.getIntParameter(request, "storeId", true);
        String path = ServletUtils.getStringParameter(request, "path", true);
        String tag = ServletUtils.getStringParameter(request, "tag", true);

        WebTopSession.UploadedFile uploadedFile = null;
        FileObject fo = vfsmanager.getStoreFile(storeId, path);
        if (fo == null)
            throw new WTException("Unable to get file [{}, {}]", storeId, path);
        InputStream is = null;
        try {
            is = fo.getContent().getInputStream();
            String name = fo.getName().getBaseName();
            String mediaType = ServletHelper.guessMediaType(name, true);
            uploadedFile = addAsUploadedFile(tag, name, mediaType, is);
        } finally {
            IOUtils.closeQuietly(is);
        }
        if (uploadedFile == null)
            throw new WTException("Unable to prepare uploaded file");

        MapItem data = new MapItem();
        data.add("uploadId", uploadedFile.getUploadId());
        data.add("name", uploadedFile.getFilename());
        data.add("size", uploadedFile.getSize());
        new JsonResult(data).printTo(out);

    } catch (FileSystemException | WTException ex) {
        Service.logger.error("Exception", ex);
        new JsonResult(false, ex.getMessage()).printTo(out);
    } catch (Exception ex) {
        Service.logger.error("Exception", ex);
        new JsonResult(false, ex.getMessage()).printTo(out);
    }
}

From source file:org.aludratest.service.file.impl.AbstractFileAction.java

/** Creates an {@link InputStream} for accessing the content of a file.
 * @param filePath the path of the file to read
 * @return an InputStream for accessing the file content */
public InputStream getInputStreamForFile(String filePath) {
    File.verifyFilePath(filePath);
    FileObject file = getFileObject(filePath);
    try {//  w w w .  ja  v  a2 s.c  o m
        logger.debug("Providing InputStream for binary file: {}", filePath);
        return file.getContent().getInputStream();
    } catch (IOException e) {
        throw new TechnicalException("Error opening InputStream", e);
    }
}

From source file:org.aludratest.service.file.impl.FileActionImpl.java

/** Creates a text file and writes to it all content provided by the source Reader.
 *  @param filePath the path of the file to save
 *  @param source a {@link Reader} which provides the file content
 *  @param overwrite flag which indicates if an existing file may be overwritten by the operation
 *  @return true if a formerly existing file was overwritten.
 *  @throws FilePresentException if a file was already present and overwriting was disabled. */
@Override/*from www . j a  v  a  2 s .c  om*/
public boolean writeTextFile(String filePath, Reader source, boolean overwrite) {
    assertWritingPermitted("writeTextFile()");
    FileUtil.verifyFilePath(filePath);
    String encoding = configuration.getEncoding();
    Writer writer = null;
    BufferedReader reader = null;
    try {
        String linefeed = configuration.getLinefeed();
        FileObject target = getFileObject(filePath);
        boolean existedBefore = checkWritable(target, overwrite);
        writer = new OutputStreamWriter(target.getContent().getOutputStream(), encoding);
        reader = new BufferedReader(source);
        boolean firstLine = true;
        String line;
        while ((line = reader.readLine()) != null) {
            if (!firstLine) {
                writer.write(linefeed);
            }
            writer.write(line);
            firstLine = false;
        }
        LOGGER.info("Wrote text file {}", filePath);
        return existedBefore;
    } catch (UnsupportedEncodingException e) {
        throw new TechnicalException("Unsupported Encoding:" + encoding, e);
    } catch (Exception e) {
        throw new TechnicalException("Error writing text file", e);
    } finally {
        IOUtil.close(writer);
        IOUtil.close(reader);
    }
}

From source file:org.aludratest.service.file.impl.FileActionImpl.java

/** Creates a binary file and writes to it all content provided by the source {@link InputStream}.
 *  @param filePath the path of the file to save
 *  @param source an {@link InputStream} which provides the content to write to the file
 *  @param overwrite flag which indicates if an existing file may be overwritten by the operation
 *  @return true if a formerly existing file was overwritten.
 *  @throws FilePresentException if a file was already present and overwriting was disabled. */
@Override/*from   w w  w.  ja v a  2  s  .c o  m*/
public boolean writeBinaryFile(String filePath, InputStream source, boolean overwrite) {
    assertWritingPermitted("writeBinaryFile()");
    FileUtil.verifyFilePath(filePath);
    OutputStream out = null;
    try {
        FileObject target = getFileObject(filePath);
        boolean existedBefore = checkWritable(target, overwrite);
        out = target.getContent().getOutputStream();
        IOUtil.transfer(source, out);
        LOGGER.info("Wrote binary file {}", filePath);
        return existedBefore;
    } catch (Exception e) {
        throw new TechnicalException("Error writing text file", e);
    } finally {
        IOUtil.close(out);
    }
}

From source file:org.aludratest.service.file.impl.FileActionImpl.java

/** Creates an {@link InputStream} for accessing the content of a file. */
@Override// www .  j ava  2  s. c o m
public InputStream getInputStreamForFile(String filePath) {
    FileUtil.verifyFilePath(filePath);
    FileObject file = getFileObject(filePath);
    try {
        LOGGER.info("Providing InputStream for binary file: {}", filePath);
        return file.getContent().getInputStream();
    } catch (FileSystemException e) {
        throw new TechnicalException("Error opening InputStream", e);
    }
}

From source file:org.aludratest.service.file.impl.FileInteractionImpl.java

/** Creates a text file and writes to it all content provided by the source Reader.
 * @param filePath the path of the file to save
 * @param source a {@link Reader} which provides the file content
 * @param overwrite flag which indicates if an existing file may be overwritten by the operation
 * @return true if a formerly existing file was overwritten.
 * @throws FunctionalFailure if a file was already present and overwriting was disabled. */
@Override/*from  w  w  w.ja  v a2s.  c o m*/
public boolean writeTextFile(String filePath, Reader source, boolean overwrite) {
    assertWritingPermitted("writeTextFile()");
    File.verifyFilePath(filePath);
    String encoding = configuration.getEncoding();
    Writer writer = null;
    BufferedReader reader = null;
    try {
        String linefeed = configuration.getLinefeed();
        FileObject target = getFileObject(filePath);
        boolean existedBefore = checkWritable(target, overwrite); // NOSONAR
        writer = new OutputStreamWriter(target.getContent().getOutputStream(), encoding);
        reader = new BufferedReader(source);
        boolean firstLine = true;
        String line;
        while ((line = reader.readLine()) != null) {
            if (!firstLine) {
                writer.write(linefeed);
            }
            writer.write(line);
            firstLine = false;
        }
        logger.debug("Wrote text file {}", filePath);
        return existedBefore;
    } catch (UnsupportedEncodingException e) {
        throw new TechnicalException("Unsupported Encoding:" + encoding, e);
    } catch (IOException e) {
        throw new TechnicalException("Error writing text file", e);
    } finally {
        IOUtil.close(writer);
        IOUtil.close(reader);
    }
}

From source file:org.aludratest.service.file.impl.FileInteractionImpl.java

/** Creates a binary file and writes to it all content provided by the source {@link InputStream}.
 * @param filePath the path of the file to save
 * @param source an {@link InputStream} which provides the content to write to the file
 * @param overwrite flag which indicates if an existing file may be overwritten by the operation
 * @return true if a formerly existing file was overwritten.
 * @throws FunctionalFailure if a file was already present and overwriting was disabled. */
@Override/*from  w  w w .  j a v  a 2s  .  c  o m*/
public boolean writeBinaryFile(String filePath, InputStream source, boolean overwrite) {
    assertWritingPermitted("writeBinaryFile()");
    File.verifyFilePath(filePath);
    OutputStream out = null;
    try {
        FileObject target = getFileObject(filePath);
        boolean existedBefore = checkWritable(target, overwrite); // NOSONAR
        out = target.getContent().getOutputStream();
        IOUtil.transfer(source, out);
        logger.debug("Wrote binary file {}", filePath);
        return existedBefore;
    } catch (IOException e) {
        throw new TechnicalException("Error writing text file", e);
    } finally {
        IOUtil.close(out);
    }
}

From source file:org.anarres.filechooser.impl.vfs2.VFSJFileChooserTest.java

private static void print(@Nonnull FileObject file) {
    try {// ww w  .  j a  v  a  2s  . c  om
        LOG.info("Selected file is " + file);
        if (file == null)
            return;
        LOG.info("Selected size is " + file.getContent().getSize());
        LOG.info("Selected operations is " + file.getFileOperations());
    } catch (FileSystemException e) {
        LOG.info("Exception is " + e);
    }
}

From source file:org.apache.accumulo.start.classloader.vfs.providers.ReadOnlyHdfsFileProviderTest.java

@Test
public void testGetContentSize() throws Exception {
    FileObject fo = manager.resolveFile(TEST_DIR1);
    Assert.assertNotNull(fo);/* w w w .j a va  2s .c  o  m*/
    Assert.assertFalse(fo.exists());

    // Create the test file
    FileObject file = createTestFile(hdfs);
    Assert.assertEquals(0, file.getContent().getSize());
}