Example usage for org.apache.commons.vfs2 FileContent getInputStream

List of usage examples for org.apache.commons.vfs2 FileContent getInputStream

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileContent getInputStream.

Prototype

InputStream getInputStream() throws FileSystemException;

Source Link

Document

Returns an input stream for reading the file's content.

Usage

From source file:de.blizzy.backup.vfs.RemoteFileOrFolder.java

@Override
public void copy(final IOutputStreamProvider outputStreamProvider) throws IOException {
    final FileContent fileContent = getFileContent();
    IAction<Void> action = new IAction<Void>() {
        @Override//from ww  w. j a v a  2  s.c  o  m
        public Void run() throws IOException {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = new BufferedInputStream(fileContent.getInputStream(), BUFFER_SIZE);
                out = outputStreamProvider.getOutputStream();
                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
            return null;
        }

        @Override
        public boolean canRetry(IOException e) {
            return location.canRetryAction(e);
        }
    };
    new ActionRunner<>(action, RemoteLocation.MAX_TRIES, location).run();
}

From source file:de.unioninvestment.portal.explorer.view.vfs.TableView.java

private File getFileFromVFSObject(FileSystemManager fileSystemManager, FileSystemOptions opts, String vfsPath) {
    OutputStream outputStream = null;
    try {// www.j a  v a  2 s  .c o m
        FileObject downloadFile = fileSystemManager.resolveFile(vfsPath, opts);
        FileContent fc = downloadFile.getContent();
        InputStream inputStream = fc.getInputStream();
        String fn = getDisplayPath(vfsPath);
        String tDir = System.getProperty("java.io.tmpdir");
        File locFile = new File(tDir, fn);
        outputStream = new FileOutputStream(locFile);
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
        return locFile;

    } catch (FileSystemException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;

}

From source file:dslab.crawler.pack.CrawlerPack.java

public String getFromRemote(String uri) {

    // clear cache
    fileSystem.getFilesCache().close();//  w  ww  .ja  v a 2s .  c  o  m

    String remoteContent;
    String remoteEncoding = "utf-8";

    log.debug("Loading remote URI:" + uri);
    FileContent fileContent;

    try {
        // set cookie if cookies set
        if (0 < this.cookies.size()) {
            FileSystemOptions fsOptions = new FileSystemOptions();
            HttpFileSystemConfigBuilder.getInstance().setCookies(fsOptions, getCookies(uri));
            fileContent = fileSystem.resolveFile(uri, fsOptions).getContent();
        } else
            fileContent = fileSystem.resolveFile(uri).getContent();

        // 2016-03-22 only pure http/https auto detect encoding
        if ("http".equalsIgnoreCase(uri.substring(0, 4))) {
            fileContent.getSize(); // pass a bug {@link https://issues.apache.org/jira/browse/VFS-427}
            remoteEncoding = fileContent.getContentInfo().getContentEncoding();
        }

        if (null == remoteEncoding)
            remoteEncoding = "utf-8";

        if (!"utf".equalsIgnoreCase(remoteEncoding.substring(0, 3))) {
            log.debug("remote content encoding: " + remoteEncoding);

            // force charset encoding if setRemoteEncoding set
            if (!"utf".equalsIgnoreCase(encoding.substring(0, 3))) {
                remoteEncoding = encoding;
            } else {
                // auto detecting encoding
                remoteEncoding = detectCharset(IOUtils.toByteArray(fileContent.getInputStream()));
                log.info("real encoding: " + remoteEncoding);
            }
        }

        // 2016-02-29 fixed
        remoteContent = IOUtils.toString(fileContent.getInputStream(), remoteEncoding);

    } catch (FileSystemException fse) {
        log.warn(fse.getMessage());
        remoteContent = null;
    } catch (IOException ioe) {
        // return empty
        log.warn(ioe.getMessage());
        remoteContent = null;
    } catch (StringIndexOutOfBoundsException stre) {
        log.warn("uri: " + uri);
        log.warn(stre.getMessage());
        remoteContent = null;
    }

    clearCookies();

    // any exception will return "null"
    return remoteContent;
}

From source file:com.github.abola.crawler.CrawlerPack.java

/**
 * ?? Apache Common VFS  ???//from w  w w .  ja v  a  2 s  . co m
 *
 * ??
 * @see <a href="https://commons.apache.org/proper/commons-vfs/filesystems.html">commons-vfs filesystems</a>
 */
public String getFromRemote(String uri) {

    // clear cache
    fileSystem.getFilesCache().close();

    String remoteContent;
    String remoteEncoding = "utf-8";

    log.debug("getFromRemote: Loading remote URI=" + uri);
    FileContent fileContent;

    try {

        FileSystemOptions fsOptions = new FileSystemOptions();
        // set userAgent
        HttpFileSystemConfigBuilder.getInstance().setUserAgent(fsOptions, userAgent);

        // set cookie if cookies set
        if (0 < this.cookies.size()) {
            HttpFileSystemConfigBuilder.getInstance().setCookies(fsOptions, getCookies(uri));
        }

        log.debug("getFromRemote: userAgent=" + userAgent);
        log.debug("getFromRemote: cookieSize=" + cookies.size());
        log.debug("getFromRemote: cookies=" + cookies.toString());

        fileContent = fileSystem.resolveFile(uri, fsOptions).getContent();

        // 2016-03-22 only pure http/https auto detect encoding
        if ("http".equalsIgnoreCase(uri.substring(0, 4))) {
            fileContent.getSize(); // pass a bug {@link https://issues.apache.org/jira/browse/VFS-427}
            remoteEncoding = fileContent.getContentInfo().getContentEncoding();
        }

        log.debug("getFromRemote: remoteEncoding=" + remoteEncoding + "(auto detect) ");

        // 2016-03-21 zip file getContentEncoding null
        if (null == remoteEncoding)
            remoteEncoding = "utf-8";

        if (!"utf".equalsIgnoreCase(remoteEncoding.substring(0, 3))) {
            log.debug("getFromRemote: remote content encoding=" + remoteEncoding);

            // force charset encoding if setRemoteEncoding set
            if (!"utf".equalsIgnoreCase(encoding.substring(0, 3))) {
                remoteEncoding = encoding;
            } else {
                // auto detecting encoding
                remoteEncoding = detectCharset(IOUtils.toByteArray(fileContent.getInputStream()));
                log.debug("getFromRemote: real encoding=" + remoteEncoding);
            }
        }

        // ??  Apache VFS ??
        // 2016-02-29 fixed
        remoteContent = IOUtils.toString(fileContent.getInputStream(), remoteEncoding);

    } catch (FileSystemException fse) {
        log.warn("getFromRemote: FileSystemException=" + fse.getMessage());
        remoteContent = null;
    } catch (IOException ioe) {
        // return empty
        log.warn("getFromRemote: IOException=" + ioe.getMessage());
        remoteContent = null;
    } catch (StringIndexOutOfBoundsException stre) {
        log.warn("getFromRemote: StringIndexOutOfBoundsException=" + stre.getMessage());
        log.warn("getFromRemote: uri=" + uri);
        log.warn(stre.getMessage());
        remoteContent = null;
    }

    clearCookies();

    log.debug("getFromRemote: remoteContent=\n" + remoteContent);
    // any exception will return "null"
    return remoteContent;
}

From source file:com.yenlo.synapse.transport.vfs.VFSTransportListener.java

/**
 * Process a single file through Axis2/*  w  ww. j  a v a 2s.  c o  m*/
 * @param entry the PollTableEntry for the file (or its parent directory or archive)
 * @param file the file that contains the actual message pumped into Axis2
 * @throws AxisFault on error
 */
private void processFile(PollTableEntry entry, FileObject file) throws AxisFault {

    try {
        FileContent content = file.getContent();
        String fileName = file.getName().getBaseName();
        String filePath = file.getName().getPath();
        String fileURI = file.getName().getURI();

        metrics.incrementBytesReceived(content.getSize());

        Map<String, Object> transportHeaders = new HashMap<String, Object>();
        transportHeaders.put(VFSConstants.FILE_PATH, filePath);
        transportHeaders.put(VFSConstants.FILE_NAME, fileName);
        transportHeaders.put(VFSConstants.FILE_URI, fileURI);

        try {
            transportHeaders.put(VFSConstants.FILE_LENGTH, content.getSize());
            transportHeaders.put(VFSConstants.LAST_MODIFIED, content.getLastModifiedTime());
        } catch (FileSystemException ignore) {
        }

        MessageContext msgContext = entry.createMessageContext();

        String contentType = entry.getContentType();
        if (BaseUtils.isBlank(contentType)) {
            if (file.getName().getExtension().toLowerCase().endsWith(".xml")) {
                contentType = "text/xml";
            } else if (file.getName().getExtension().toLowerCase().endsWith(".txt")) {
                contentType = "text/plain";
            }
        } else {
            // Extract the charset encoding from the configured content type and
            // set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this.
            String charSetEnc = null;
            try {
                if (contentType != null) {
                    charSetEnc = new ContentType(contentType).getParameter("charset");
                }
            } catch (ParseException ex) {
                // ignore
            }
            msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
        }

        // if the content type was not found, but the service defined it.. use it
        if (contentType == null) {
            if (entry.getContentType() != null) {
                contentType = entry.getContentType();
            } else if (VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE) != null) {
                contentType = VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE);
            }
        }

        // does the service specify a default reply file URI ?
        String replyFileURI = entry.getReplyFileURI();
        if (replyFileURI != null) {
            msgContext.setProperty(Constants.OUT_TRANSPORT_INFO,
                    new VFSOutTransportInfo(replyFileURI, entry.isFileLockingEnabled()));
        }

        // Determine the message builder to use
        Builder builder;
        if (contentType == null) {
            log.debug("No content type specified. Using SOAP builder.");
            builder = new SOAPBuilder();
        } else {
            int index = contentType.indexOf(';');
            String type = index > 0 ? contentType.substring(0, index) : contentType;
            builder = BuilderUtil.getBuilderFromSelector(type, msgContext);
            if (builder == null) {
                if (log.isDebugEnabled()) {
                    log.debug("No message builder found for type '" + type + "'. Falling back to SOAP.");
                }
                builder = new SOAPBuilder();
            }
        }

        // set the message payload to the message context
        InputStream in;
        ManagedDataSource dataSource;
        if (builder instanceof DataSourceMessageBuilder && entry.isStreaming()) {
            in = null;
            dataSource = ManagedDataSourceFactory.create(new FileObjectDataSource(file, contentType));
        } else {
            in = new AutoCloseInputStream(content.getInputStream());
            dataSource = null;
        }

        try {
            OMElement documentElement;
            if (in != null) {
                documentElement = builder.processDocument(in, contentType, msgContext);
            } else {
                documentElement = ((DataSourceMessageBuilder) builder).processDocument(dataSource, contentType,
                        msgContext);
            }
            msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));

            handleIncomingMessage(msgContext, transportHeaders, null, //* SOAP Action - not applicable *//
                    contentType);
        } finally {
            if (dataSource != null) {
                dataSource.destroy();
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("Processed file : " + file + " of Content-type : " + contentType);
        }

    } catch (FileSystemException e) {
        handleException("Error reading file content or attributes : " + file, e);

    } finally {
        try {
            file.close();
        } catch (FileSystemException warn) {
            //  log.warn("Cannot close file after processing : " + file.getName().getPath(), warn);
            // ignore the warning, since we handed over the stream close job to AutocloseInputstream..
        }
    }
}

From source file:org.apache.hadoop.gateway.topology.file.FileTopologyProvider.java

private static Topology loadTopology(FileObject file) throws IOException, SAXException, URISyntaxException {
    log.loadingTopologyFile(file.getName().getFriendlyURI());
    Digester digester = digesterLoader.newDigester();
    FileContent content = file.getContent();
    TopologyBuilder topologyBuilder = digester.parse(content.getInputStream());
    Topology topology = topologyBuilder.build();
    topology.setUri(file.getURL().toURI());
    topology.setName(FilenameUtils.removeExtension(file.getName().getBaseName()));
    topology.setTimestamp(content.getLastModifiedTime());
    return topology;
}

From source file:org.apache.synapse.transport.vfs.VFSTransportListener.java

/**
 * Process a single file through Axis2/*from  w  w w .ja  v  a  2 s .  co m*/
 * @param entry the PollTableEntry for the file (or its parent directory or archive)
 * @param file the file that contains the actual message pumped into Axis2
 * @throws AxisFault on error
 */
private void processFile(PollTableEntry entry, FileObject file) throws AxisFault {

    try {
        FileContent content = file.getContent();
        String fileName = file.getName().getBaseName();
        String filePath = file.getName().getPath();
        String fileURI = file.getName().getURI();

        metrics.incrementBytesReceived(content.getSize());

        Map<String, Object> transportHeaders = new HashMap<String, Object>();
        transportHeaders.put(VFSConstants.FILE_PATH, filePath);
        transportHeaders.put(VFSConstants.FILE_NAME, fileName);
        transportHeaders.put(VFSConstants.FILE_URI, fileURI);

        try {
            transportHeaders.put(VFSConstants.FILE_LENGTH, content.getSize());
            transportHeaders.put(VFSConstants.LAST_MODIFIED, content.getLastModifiedTime());
        } catch (FileSystemException ignore) {
        }

        MessageContext msgContext = entry.createMessageContext();

        String contentType = entry.getContentType();
        if (BaseUtils.isBlank(contentType)) {
            if (file.getName().getExtension().toLowerCase().endsWith(".xml")) {
                contentType = "text/xml";
            } else if (file.getName().getExtension().toLowerCase().endsWith(".txt")) {
                contentType = "text/plain";
            }
        } else {
            // Extract the charset encoding from the configured content type and
            // set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this.
            String charSetEnc = null;
            try {
                if (contentType != null) {
                    charSetEnc = new ContentType(contentType).getParameter("charset");
                }
            } catch (ParseException ex) {
                // ignore
            }
            msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
        }

        // if the content type was not found, but the service defined it.. use it
        if (contentType == null) {
            if (entry.getContentType() != null) {
                contentType = entry.getContentType();
            } else if (VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE) != null) {
                contentType = VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE);
            }
        }

        // does the service specify a default reply file URI ?
        String replyFileURI = entry.getReplyFileURI();
        if (replyFileURI != null) {
            msgContext.setProperty(Constants.OUT_TRANSPORT_INFO,
                    new VFSOutTransportInfo(replyFileURI, entry.isFileLockingEnabled()));
        }

        // Determine the message builder to use
        Builder builder;
        if (contentType == null) {
            log.debug("No content type specified. Using SOAP builder.");
            builder = new SOAPBuilder();
        } else {
            int index = contentType.indexOf(';');
            String type = index > 0 ? contentType.substring(0, index) : contentType;
            builder = BuilderUtil.getBuilderFromSelector(type, msgContext);
            if (builder == null) {
                if (log.isDebugEnabled()) {
                    log.debug("No message builder found for type '" + type + "'. Falling back to SOAP.");
                }
                builder = new SOAPBuilder();
            }
        }

        // set the message payload to the message context
        InputStream in;
        ManagedDataSource dataSource;
        if (builder instanceof DataSourceMessageBuilder && entry.isStreaming()) {
            in = null;
            dataSource = ManagedDataSourceFactory.create(new FileObjectDataSource(file, contentType));
        } else {
            in = new AutoCloseInputStream(content.getInputStream());
            dataSource = null;
        }

        try {
            OMElement documentElement;
            if (in != null) {
                documentElement = builder.processDocument(in, contentType, msgContext);
            } else {
                documentElement = ((DataSourceMessageBuilder) builder).processDocument(dataSource, contentType,
                        msgContext);
            }
            msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));

            handleIncomingMessage(msgContext, transportHeaders, null, //* SOAP Action - not applicable *//
                    contentType);
        } finally {
            if (dataSource != null) {
                dataSource.destroy();
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("Processed file : " + VFSUtils.maskURLPassword(file.toString()) + " of Content-type : "
                    + contentType);
        }

    } catch (FileSystemException e) {
        handleException(
                "Error reading file content or attributes : " + VFSUtils.maskURLPassword(file.toString()), e);

    } finally {
        try {
            if (file != null) {
                file.close();
            }
        } catch (FileSystemException warn) {
            // ignore the warning,  since we handed over the stream close job to
            // AutocloseInputstream..
        }
    }
}

From source file:org.apache.zeppelin.notebook.repo.OldVFSNotebookRepo.java

private Note getNote(FileObject noteDir) throws IOException {
    if (!isDirectory(noteDir)) {
        throw new IOException(noteDir.getName().toString() + " is not a directory");
    }/*from   w  w w.  java2  s .com*/

    FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
    if (!noteJson.exists()) {
        throw new IOException(noteJson.getName().toString() + " not found");
    }

    FileContent content = noteJson.getContent();
    InputStream ins = content.getInputStream();
    String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
    ins.close();

    return Note.fromJson(json);
}

From source file:org.apache.zeppelin.notebook.repo.VFSNotebookRepo.java

private Note getNote(FileObject noteDir) throws IOException {
    if (!isDirectory(noteDir)) {
        throw new IOException(noteDir.getName().toString() + " is not a directory");
    }//from  w w  w.j  a  v a2s . c  om

    FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
    if (!noteJson.exists()) {
        throw new IOException(noteJson.getName().toString() + " not found");
    }

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();

    FileContent content = noteJson.getContent();
    InputStream ins = content.getInputStream();
    String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
    ins.close();

    Note note = gson.fromJson(json, Note.class);
    //    note.setReplLoader(replLoader);
    //    note.jobListenerFactory = jobListenerFactory;

    for (Paragraph p : note.getParagraphs()) {
        if (p.getStatus() == Status.PENDING || p.getStatus() == Status.RUNNING) {
            p.setStatus(Status.ABORT);
        }
    }

    return note;
}

From source file:org.celeria.minecraft.backup.Archive.java

private InputStream inputStreamFrom(final FileContent content) throws FileSystemException {
    return content.getInputStream();
}