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:edu.scripps.fl.pubchem.app.RelationDownloader.java

public void call() throws Exception {
    Pattern pattern = Pattern.compile("^AID(\\d+)\\s+AID(\\d+)$");

    FileObject folder = VFS.getManager().resolveFile(assayNeighborURL);
    for (FileObject rFile : folder.getChildren()) {
        String name = rFile.getName().getBaseName();
        log.info("Processing file: " + name);
        BufferedReader reader = new BufferedReader(new InputStreamReader(rFile.getContent().getInputStream()));
        String line = null;/*from   ww  w .  j a  va  2 s.  c o m*/
        long lastFrom = 0;
        List<Relation> relations = new ArrayList(100);
        while (null != (line = reader.readLine())) {
            Matcher matcher = pattern.matcher(line);
            if (!matcher.matches())
                throw new java.lang.UnsupportedOperationException("Cannot determine AIDs from line: " + line);
            long from = Long.parseLong(matcher.group(1));
            long to = Long.parseLong(matcher.group(2));
            if (lastFrom == 0) // very first time only.
                lastFrom = from;
            if (from != lastFrom) { // when we change to the next aid in the file
                update(from, name, relations);
                PubChemDB.getSession().clear();
                lastFrom = from;
            }
            Relation relation = new Relation();
            relation.setFromDb("pcassay");
            relation.setToDb("pcassay");
            relation.setFromId(from);
            relation.setToId(to);
            relation.setRelationName(name);
            relations.add(relation);
        }
        update(lastFrom, name, relations);
    }
}

From source file:com.stratuscom.harvester.liaison.VirtualFileSystemConfiguration.java

public VirtualFileSystemConfiguration(String[] options, ClassLoader cl) throws ConfigurationException {

    /* no options; just delegate. */
    if (options == null || options.length == 0) {
        delegate = new MyConfigurationFile(options, cl);
        return;/*  www.j  a va2  s.c o  m*/
    }

    /* No file called for; just delegate. */
    if (Strings.DASH.equals(options[0])) {
        delegate = new MyConfigurationFile(options, cl);
        return;
    }

    /* Else, find the configuration file inside the working directory and
    open it.
    TODO: Should probably check to make sure that the supplied file
    name does not include absolute path or '..' path, i.e. make sure
    that the resolved file is actually a descendant of the working
    directory.
     */
    Reader reader = null;
    try {
        FileObject configFile = rootDirectory.resolveFile(options[0]);
        reader = new InputStreamReader(configFile.getContent().getInputStream());
        delegate = new MyConfigurationFile(reader, options, cl);
    } catch (FileSystemException ex) {
        throw new ConfigurationNotFoundException(options[0], ex);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
                throw new ConfigurationException(Strings.ERROR_CLOSING_FILE, ex);
            }
        }
    }

}

From source file:de.innovationgate.wgpublisher.design.sync.DesignDeployment.java

public boolean isUpdated()
        throws InstantiationException, IllegalAccessException, IOException, WGDesignSyncException {

    FileObject metadataFile = getMetadataFile();
    FileObject codeFile = getCodeFile();

    if (_codeFileSize == -1) {
        _codeFileSize = codeFile.getContent().getSize();
    }//from  w w w .  j  a v  a  2 s .co m

    return ((metadataFile.exists()
            && metadataFile.getContent().getLastModifiedTime() != _timestampOfMetadataFile)
            || codeFile.getContent().getLastModifiedTime() != _timestampOfCodeFile
            || codeFile.getContent().getSize() != _codeFileSize);

}

From source file:de.innovationgate.wgpublisher.design.sync.DesignDeployment.java

public void resetUpdateInformation()
        throws InstantiationException, IllegalAccessException, IOException, WGDesignSyncException {
    _timestampOfCodeFile = getCodeFile().getContent().getLastModifiedTime();
    _codeFileSize = getCodeFile().getContent().getSize();
    FileObject metadataFile = getMetadataFile();
    if (metadataFile.exists()) {
        _timestampOfMetadataFile = metadataFile.getContent().getLastModifiedTime();
    }//from   www.  j ava  2s  .  c  om
}

From source file:gridool.sqlet.catalog.MapReduceConf.java

public void loadReducers(String uri) throws SqletException {
    if (uri.endsWith(".csv") || uri.endsWith(".CSV")) {
        final InputStream is;
        try {/* w w w . ja  v a 2 s.co  m*/
            FileSystemManager fsManager = VFS.getManager();
            FileObject fileObj = fsManager.resolveFile(uri);
            FileContent fileContent = fileObj.getContent();
            is = fileContent.getInputStream();
        } catch (FileSystemException e) {
            throw new SqletException(SqletErrorType.configFailed, "failed to load a file: " + uri, e);
        }
        InputStreamReader reader = new InputStreamReader(new FastBufferedInputStream(is));
        HeaderAwareCsvReader csvReader = new HeaderAwareCsvReader(reader, ',', '"');

        final Map<String, Integer> headerMap;
        try {
            headerMap = csvReader.parseHeader();
        } catch (IOException e) {
            throw new SqletException(SqletErrorType.configFailed, "failed to parse a header: " + uri, e);
        }

        final int[] fieldIndexes = toFieldIndexes(headerMap);
        while (csvReader.next()) {
            String id = csvReader.get(fieldIndexes[0]);
            String nodeStr = csvReader.get(fieldIndexes[1]);
            String dbUrl = csvReader.get(fieldIndexes[2]);
            String user = csvReader.get(fieldIndexes[3]);
            String password = csvReader.get(fieldIndexes[4]);
            String xferPortStr = csvReader.get(fieldIndexes[5]);
            String shuffleDataSinkStr = csvReader.get(fieldIndexes[6]);
            String[] shuffleDataSink = FileUtils.parsePathExpressions(shuffleDataSinkStr, ",");

            Preconditions.checkNotNull(id, nodeStr);

            GridNode hostNode = GridUtils.getNode(nodeStr);
            int xferPort = (xferPortStr == null || xferPortStr.isEmpty()) ? GridXferService.DEFAULT_RECV_PORT
                    : Integer.parseInt(xferPortStr);
            Reducer r = new Reducer(id, hostNode, dbUrl, user, password, xferPort, shuffleDataSink);
            reducers.add(r);
        }
    } else {
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
}

From source file:mondrian.spi.impl.ApacheVfs2VirtualFileHandler.java

public InputStream readVirtualFile(String url) throws FileSystemException {
    // Treat catalogUrl as an Apache VFS (Virtual File System) URL.
    // VFS handles all of the usual protocols (http:, file:)
    // and then some.
    FileSystemManager fsManager = VFS.getManager();
    if (fsManager == null) {
        throw Util.newError("Cannot get virtual file system manager");
    }//from w  ww .  j  a  v a  2  s.  co m

    File userDir = new File("").getAbsoluteFile();
    FileObject file = fsManager.resolveFile(userDir, url);
    FileContent fileContent = null;
    try {
        if (!file.isReadable()) {
            throw Util.newError("Virtual file is not readable: " + url);
        }

        fileContent = file.getContent();
    } finally {
        file.close();
    }

    if (fileContent == null) {
        throw Util.newError("Cannot get virtual file content: " + url);
    }

    return fileContent.getInputStream();
}

From source file:com.googlecode.japi.checker.cli.Main.java

private File writeToTempFile(FileObject fo) throws IOException {
    File temp = File.createTempFile("lib-", fo.getName().getExtension());
    temp.deleteOnExit();/*w w  w .  j  av  a2s.c  o  m*/
    InputStream is = fo.getContent().getInputStream();
    OutputStream os = new FileOutputStream(temp);
    int c = 0;
    try {
        while ((c = is.read()) != -1) {
            os.write(c);
        }
    } finally {
        os.close();
        is.close();
    }
    return temp;
}

From source file:com.streamsets.pipeline.stage.origin.remote.FTPRemoteDownloadSourceDelegate.java

long populateMetadata(String remotePath, Map<String, Object> metadata) throws IOException {
    FileObject fileObject = getChild(remotePath);
    long size = fileObject.getContent().getSize();
    metadata.put(HeaderAttributeConstants.SIZE, size);
    metadata.put(HeaderAttributeConstants.LAST_MODIFIED_TIME, getModTime(fileObject));
    metadata.put(RemoteDownloadSource.CONTENT_TYPE, fileObject.getContent().getContentInfo().getContentType());
    metadata.put(RemoteDownloadSource.CONTENT_ENCODING,
            fileObject.getContent().getContentInfo().getContentEncoding());
    return size;/* w w  w .j a  v a 2  s  . co  m*/
}

From source file:com.ewcms.publication.deploy.provider.DeployOperatorBaseTest.java

@Test
public void testCopyFileBytes() throws Exception {

    String rootPath = System.getProperty("java.io.tmpdir", "/tmp");
    DeployOperatorable operator = new DeployOperatorBaseImpl.Builder().setPath(rootPath).build();
    DeployOperatorBaseImpl operatorImpl = (DeployOperatorBaseImpl) operator;

    String content = "test-byte";
    operatorImpl.copy(content.getBytes(), "/docuemnt/2/write.jpg");
    FileObject fileObject = operatorImpl.getFileObject();

    ByteArrayOutputStream stream = (ByteArrayOutputStream) fileObject.getContent().getOutputStream();
    Assert.assertEquals(content.getBytes().length, stream.size());
}

From source file:com.ewcms.publication.deploy.provider.DeployOperatorBaseTest.java

@Test
public void testCopyFilePath() throws Exception {

    String rootPath = System.getProperty("java.io.tmpdir", "/tmp");
    DeployOperatorable operator = new DeployOperatorBaseImpl.Builder().setPath(rootPath).build();
    DeployOperatorBaseImpl operatorImpl = (DeployOperatorBaseImpl) operator;

    String source = DeployOperatorBaseTest.class.getResource("write.jpg").getPath();
    operatorImpl.copy(source, "/docuemnt/1/write.jpg");
    FileObject fileObject = operatorImpl.getFileObject();

    ByteArrayOutputStream content = (ByteArrayOutputStream) fileObject.getContent().getOutputStream();
    Assert.assertEquals(335961, content.size());
}