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.pentaho.platform.repository.solution.filebased.MondrianVfsTest.java

private String runCatalogTest(final String expectedCatalog, final MondrianSchemaAnnotator annotator,
        final boolean annotationsExist) throws IOException {
    MondrianVfs vfs = new MondrianVfs() {
        @Override//from w ww  .ja v  a2 s  .  c o  m
        FileObject getCatalogFileObject(final String catalog, final String fileName) {
            assertEquals(expectedCatalog, catalog);
            try {
                if (fileName.equals("schema.xml")) {
                    return FileObjectTestHelper.mockFile(schemaContent, true);
                }
                if (fileName.equals("annotations.xml")) {
                    return FileObjectTestHelper.mockFile(annotationsContent, annotationsExist);
                }
                fail("unrecognized File");
                return null;
            } catch (FileSystemException e) {
                fail(e.getMessage());
                return null;
            }
        }

        @Override
        MondrianSchemaAnnotator getAnnotator() {
            return annotator;
        }
    };
    FileObject file = vfs.findFile(null, "mondrian:" + expectedCatalog, null);
    return IOUtils.toString(file.getContent().getInputStream());
}

From source file:org.pentaho.reporting.designer.extensions.pentaho.drilldown.PentahoParameterRefreshHandler.java

private static String getParameterServicePath(final AuthenticationData loginData,
        final PentahoPathModel pathModel) {
    try {//from ww  w  .java 2  s .  c o m
        final FileObject fileSystemRoot = PublishUtil.createVFSConnection(VFS.getManager(), loginData);
        final FileSystem fileSystem = fileSystemRoot.getFileSystem();

        // as of version 3.7 we do not need to check anything other than that the version information is there
        // later we may have to add additional checks in here to filter out known broken versions.

        final String localPath = pathModel.getLocalPath();
        final FileObject object = fileSystemRoot.resolveFile(localPath);
        final FileContent content = object.getContent();
        final String majorVersionText = (String) fileSystem.getAttribute(WebSolutionFileSystem.MAJOR_VERSION);

        if (StringUtils.isEmpty(majorVersionText) == false) {
            final String paramService = (String) content.getAttribute("param-service-url");
            if (StringUtils.isEmpty(paramService)) {
                return null;
            }
            if (paramService.startsWith("http://") || paramService.startsWith("https://")) {
                return paramService;
            }

            try {
                // Encode the URL (must use URI as URL encoding doesn't work on spaces correctly)
                final URL target = new URL(loginData.getUrl());
                final String host;
                if (target.getPort() != -1) {
                    host = target.getHost() + ":" + target.getPort();
                } else {
                    host = target.getHost();
                }

                return target.getProtocol() + "://" + host + paramService;
            } catch (MalformedURLException e) {
                UncaughtExceptionsModel.getInstance().addException(e);
                return null;
            }
        }

        final String extension = IOUtils.getInstance().getFileExtension(localPath);
        if (".prpt".equals(extension)) {
            logger.debug(
                    "Ancient pentaho system detected: parameter service does not deliver valid parameter values");

            final String name = pathModel.getName();
            final String path = pathModel.getPath();
            final String solution = pathModel.getSolution();

            final FastMessageFormat messageFormat = new FastMessageFormat(
                    "/content/reporting/?renderMode=XML&solution={0}&path={1}&name={2}");
            messageFormat.setNullString("");
            return loginData.getUrl() + messageFormat.format(new Object[] { solution, path, name });
        }

        logger.debug("Ancient pentaho system detected: We will not have access to a working parameter service");
        return null;
    } catch (FileSystemException e) {
        UncaughtExceptionsModel.getInstance().addException(e);
        return null;
    }
}

From source file:org.pentaho.reporting.designer.extensions.pentaho.repository.model.RepositoryTableModel.java

public Object getValueAt(final int row, final int column) {
    final FileObject node1 = getElementForRow(row);

    try {//  w  w w . j a v  a2  s  .  c  om
        switch (column) {
        case 0:
            return node1.getContent().getAttribute("localized-name");
        case 1:
            return URLDecoder.decode(node1.getName().getBaseName().replaceAll("\\+", "%2B"), "UTF-8");
        case 2:
            final long lastModifiedTime = node1.getContent().getLastModifiedTime();
            if (lastModifiedTime == -1) {
                return null;
            }
            return new Date(lastModifiedTime);
        case 3:
            return node1.getContent().getAttribute("description");
        default:
            throw new IndexOutOfBoundsException();
        }
    } catch (FileSystemException fse) {
        // ignre the exception, assume the file is not valid
        UncaughtExceptionsModel.getInstance().addException(fse);
        return null;
    } catch (UnsupportedEncodingException e) {
        UncaughtExceptionsModel.getInstance().addException(e);
        return null;
    }
}

From source file:org.pentaho.reporting.designer.extensions.pentaho.repository.util.PublishUtil.java

public static ReportRenderContext openReport(final ReportDesignerContext context,
        final AuthenticationData loginData, final String path)
        throws IOException, ReportDataFactoryException, ResourceException {
    if (StringUtils.isEmpty(path)) {
        throw new IOException("Path is empty.");
    }//  ww  w.  j av  a 2 s  .  com

    final String urlPath = path.replaceAll("%", "%25").replaceAll("%2B", "+").replaceAll("\\!", "%21")
            .replaceAll(":", "%3A");
    final FileObject connection = createVFSConnection(loginData);
    final FileObject object = connection.resolveFile(urlPath);
    if (object.exists() == false) {
        throw new FileNotFoundException(path);
    }

    final InputStream inputStream = object.getContent().getInputStream();
    try {
        final ByteArrayOutputStream out = new ByteArrayOutputStream(
                Math.max(8192, (int) object.getContent().getSize()));
        IOUtils.getInstance().copyStreams(inputStream, out);
        final MasterReport report = loadReport(out.toByteArray(), path);
        final int index = context.addMasterReport(report);
        return context.getReportRenderContext(index);
    } finally {
        inputStream.close();
    }
}

From source file:org.pentaho.reporting.designer.extensions.pentaho.repository.util.PublishUtil.java

public static int publish(final byte[] data, final String path, final AuthenticationData loginData)
        throws IOException {
    int responseCode = HTTP_RESPONSE_FAIL;
    final String versionText = loginData.getOption(SERVER_VERSION);
    final int version = ParserUtil.parseInt(versionText, SERVER_VERSION_SUGAR);

    if (SERVER_VERSION_SUGAR == version) {
        PublishRestUtil publishRestUtil = new PublishRestUtil(loginData.getUrl(), loginData.getUsername(),
                loginData.getPassword());
        responseCode = publishRestUtil.publishFile(path, data, true);

    } else {/*from   ww w. j a v  a 2  s.co m*/
        final FileObject connection = createVFSConnection(loginData);
        final FileObject object = connection.resolveFile(path);
        final OutputStream out = object.getContent().getOutputStream(false);
        try {
            out.write(data);
            responseCode = HTTP_RESPONSE_OK;
        } finally {
            out.close();
        }
    }
    return responseCode;
}

From source file:org.pentaho.vfs.ui.VfsBrowser.java

public void populateTreeItemText(TreeItem ti, FileObject fileObject) {
    try {/* ww w  . ja  v  a2s . c  o m*/
        String contentType = fileObject.getContent().getContentInfo().getContentType();
        DateFormat df = SimpleDateFormat.getDateTimeInstance();
        Date date = new Date(fileObject.getContent().getLastModifiedTime());
        if (contentType == null) {
            contentType = ""; //$NON-NLS-1$
        }
        ti.setText(new String[] { fileObject.getName().getBaseName(), contentType, df.format(date) });
    } catch (Throwable t) {
        // t.printStackTrace();
        ti.setText(fileObject.getName().getBaseName());
    }
}

From source file:org.pentaho.vfs.ui.VfsBrowser.java

public boolean setContent(TreeItem ti, byte[] data) {
    FileObject file = (FileObject) ti.getData();
    try {//from  w w w.j a  v a 2s.  com
        OutputStream os = file.getContent().getOutputStream();
        os.write(data);
        os.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.pentaho.vfs.util.VfsHelper.java

public FileObject saveFile(String uri, InputStream is) throws IOException {
    if (fsManager != null) {
        FileObject savedFile = fsManager.resolveFile(uri);
        if (!savedFile.exists()) {
            throw new FileSystemException(Messages.getString("VfsHelper.fileDoesNotExist")); //$NON-NLS-1$
        }/*from w  w  w . j a  va  2s. c om*/
        IOUtils.copy(is, savedFile.getContent().getOutputStream());
        return savedFile;
    }
    throw new FileSystemException(Messages.getString("VfsHelper.operationFailed")); //$NON-NLS-1$
}

From source file:org.pentaho.vfs.util.VfsHelper.java

public FileObject saveFileAs(String uri, InputStream is) throws FileSystemException, IOException {
    if (fsManager != null) {
        FileObject savedFile = fsManager.resolveFile(uri);
        if (!savedFile.exists()) {
            savedFile.createFile();//ww  w.j  a  v  a2 s.c  om
        }
        IOUtils.copy(is, savedFile.getContent().getOutputStream());
        return savedFile;
    }
    throw new FileSystemException(Messages.getString("VfsHelper.operationFailed")); //$NON-NLS-1$
}

From source file:org.pentaho.vfs.util.VfsHelper.java

public byte[] getFileContentAsByteArray(FileObject fileObject) throws IOException {
    if (fileObject != null && fileObject.exists()) {
        IOUtils.toByteArray(fileObject.getContent().getInputStream());
    }/*from   ww  w  . ja  v  a2  s .com*/
    throw new FileSystemException(Messages.getString("VfsHelper.operationFailed")); //$NON-NLS-1$
}