Search in sources :

Example 1 with HttpFileObject

use of org.apache.commons.vfs2.provider.http.HttpFileObject in project mondrian by pentaho.

the class Util method readVirtualFile.

/**
 * Gets content via Apache VFS. File must exist and have content
 *
 * @param url String
 * @return Apache VFS FileContent for further processing
 * @throws FileSystemException on error
 */
public static 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 newError("Cannot get virtual file system manager");
    }
    // Workaround VFS bug.
    if (url.startsWith("file://localhost")) {
        url = url.substring("file://localhost".length());
    }
    if (url.startsWith("file:")) {
        url = url.substring("file:".length());
    }
    // (Mondrian-585)
    if (url.startsWith("http")) {
        try {
            return new URL(url).openStream();
        } catch (IOException e) {
            throw newError("Could not read URL: " + url);
        }
    }
    File userDir = new File("").getAbsoluteFile();
    FileObject file = fsManager.resolveFile(userDir, url);
    FileContent fileContent = null;
    try {
        // Because of VFS caching, make sure we refresh to get the latest
        // file content. This refresh may possibly solve the following
        // workaround for defect MONDRIAN-508, but cannot be tested, so we
        // will leave the work around for now.
        file.refresh();
        // http://blah.com?param=B)
        if (file instanceof HttpFileObject && !file.getName().getURI().equals(url)) {
            fsManager.getFilesCache().removeFile(file.getFileSystem(), file.getName());
            file = fsManager.resolveFile(userDir, url);
        }
        if (!file.isReadable()) {
            throw newError("Virtual file is not readable: " + url);
        }
        fileContent = file.getContent();
    } finally {
        file.close();
    }
    if (fileContent == null) {
        throw newError("Cannot get virtual file content: " + url);
    }
    return fileContent.getInputStream();
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) HttpFileObject(org.apache.commons.vfs2.provider.http.HttpFileObject) FileObject(org.apache.commons.vfs2.FileObject) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) URL(java.net.URL) HttpFileObject(org.apache.commons.vfs2.provider.http.HttpFileObject)

Aggregations

URL (java.net.URL)1 FileContent (org.apache.commons.vfs2.FileContent)1 FileObject (org.apache.commons.vfs2.FileObject)1 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)1 HttpFileObject (org.apache.commons.vfs2.provider.http.HttpFileObject)1