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.di.plugins.fileopensave.providers.vfs.VFSFileProvider.java

/**
 * @param file/*from   w  w  w .  ja  va  2  s  .c  o m*/
 * @return
 */
public InputStream readFile(VFSFile file) {
    try {
        FileObject fileObject = KettleVFS.getFileObject(file.getPath(), new Variables(),
                VFSHelper.getOpts(file.getPath(), file.getConnection()));
        return fileObject.getContent().getInputStream();
    } catch (KettleException | FileSystemException e) {
        return null;
    }
}

From source file:org.pentaho.di.plugins.fileopensave.providers.vfs.VFSFileProvider.java

/**
 * @param inputStream/*  ww w .j a v a 2s .  c om*/
 * @param destDir
 * @param path
 * @param overwrite
 * @return
 * @throws FileException
 */
@Override
public VFSFile writeFile(InputStream inputStream, VFSFile destDir, String path, boolean overwrite)
        throws FileException {
    FileObject fileObject = null;
    try {
        fileObject = KettleVFS.getFileObject(path, new Variables(),
                VFSHelper.getOpts(destDir.getPath(), destDir.getConnection()));
    } catch (KettleException ke) {
        throw new FileException();
    }
    if (fileObject != null) {
        try (OutputStream outputStream = fileObject.getContent().getOutputStream();) {
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            return VFSFile.create(destDir.getPath(), fileObject, destDir.getConnection());
        } catch (IOException e) {
            return null;
        }
    }
    return null;
}

From source file:org.pentaho.di.repository.pur.PurRepositoryIT.java

@Test
public void testExport() throws Exception {
    final String exportFileName = new File("test.export").getAbsolutePath(); //$NON-NLS-1$

    RepositoryDirectoryInterface rootDir = initRepo();
    String uniqueTransName = EXP_TRANS_NAME.concat(EXP_DBMETA_NAME);
    TransMeta transMeta = createTransMeta(EXP_DBMETA_NAME);

    // Create a database association
    DatabaseMeta dbMeta = createDatabaseMeta(EXP_DBMETA_NAME);
    repository.save(dbMeta, VERSION_COMMENT_V1, null);

    TableInputMeta tableInputMeta = new TableInputMeta();
    tableInputMeta.setDatabaseMeta(dbMeta);

    transMeta.addStep(new StepMeta(EXP_TRANS_STEP_1_NAME, tableInputMeta));

    RepositoryDirectoryInterface transDir = rootDir.findDirectory(DIR_TRANSFORMATIONS);
    repository.save(transMeta, VERSION_COMMENT_V1, null);
    deleteStack.push(transMeta); // So this transformation is cleaned up afterward
    assertNotNull(transMeta.getObjectId());
    ObjectRevision version = transMeta.getObjectRevision();
    assertNotNull(version);//from ww w.  j  a v  a2  s  . c  o  m
    assertTrue(hasVersionWithComment(transMeta, VERSION_COMMENT_V1));
    assertTrue(repository.exists(uniqueTransName, transDir, RepositoryObjectType.TRANSFORMATION));

    JobMeta jobMeta = createJobMeta(EXP_JOB_NAME);
    RepositoryDirectoryInterface jobsDir = rootDir.findDirectory(DIR_JOBS);
    repository.save(jobMeta, VERSION_COMMENT_V1, null);
    deleteStack.push(jobMeta);
    assertNotNull(jobMeta.getObjectId());
    version = jobMeta.getObjectRevision();
    assertNotNull(version);
    assertTrue(hasVersionWithComment(jobMeta, VERSION_COMMENT_V1));
    assertTrue(repository.exists(EXP_JOB_NAME, jobsDir, RepositoryObjectType.JOB));

    LogListener errorLogListener = new LogListener(LogLevel.ERROR);
    KettleLogStore.getAppender().addLoggingEventListener(errorLogListener);

    try {
        repository.getExporter().exportAllObjects(new MockProgressMonitorListener(), exportFileName, null,
                "all"); //$NON-NLS-1$
        FileObject exportFile = KettleVFS.getFileObject(exportFileName);
        assertFalse("file left open", exportFile.getContent().isOpen());
        assertNotNull(exportFile);
        MockRepositoryExportParser parser = new MockRepositoryExportParser();
        SAXParserFactory.newInstance().newSAXParser().parse(KettleVFS.getInputStream(exportFile), parser);
        if (parser.getFatalError() != null) {
            throw parser.getFatalError();
        }
        assertNotNull("No nodes found in export", parser.getNodeNames()); //$NON-NLS-1$
        assertTrue("No nodes found in export", !parser.getNodeNames().isEmpty()); //$NON-NLS-1$
        assertEquals("Incorrect number of nodes", 5, parser.getNodeNames().size()); //$NON-NLS-1$
        assertEquals("Incorrect number of transformations", 1, //$NON-NLS-1$
                parser.getNodesWithName("transformation").size()); //$NON-NLS-1$
        assertEquals("Incorrect number of jobs", 1, parser.getNodesWithName("job").size()); //$NON-NLS-1$ //$NON-NLS-2$
        assertTrue("log error", errorLogListener.getEvents().isEmpty());

    } finally {
        KettleVFS.getFileObject(exportFileName).delete();
        KettleLogStore.getAppender().removeLoggingEventListener(errorLogListener);
    }
}

From source file:org.pentaho.di.trans.steps.enhanced.jsoninput.JsonInput.java

public boolean onNewFile(FileObject file) throws FileSystemException {
    if (file == null) {
        String errMsg = BaseMessages.getString(PKG, "JsonInput.Log.IsNotAFile", "null");
        logError(errMsg);// ww w  .j av  a2s  .  com
        inputError(errMsg);
        return false;
    } else if (!file.exists()) {
        String errMsg = BaseMessages.getString(PKG, "JsonInput.Log.IsNotAFile",
                file.getName().getFriendlyURI());
        logError(errMsg);
        inputError(errMsg);
        return false;
    }
    if (hasAdditionalFileFields()) {
        fillFileAdditionalFields(data, file);
    }
    if (file.getContent().getSize() == 0) {
        // log only basic as a warning (was before logError)
        if (meta.isIgnoreEmptyFile()) {
            logBasic(BaseMessages.getString(PKG, "JsonInput.Error.FileSizeZero", "" + file.getName()));
        } else {
            logError(BaseMessages.getString(PKG, "JsonInput.Error.FileSizeZero", "" + file.getName()));
            incrementErrors();
            return false;
        }
    }
    return true;
}

From source file:org.pentaho.di.trans.steps.file.BaseFileInputStep.java

/**
 * Prepare file-dependent data for fill additional fields.
 */// w ww  .  jav  a 2 s .c  o  m
protected void fillFileAdditionalFields(D data, FileObject file) throws FileSystemException {
    data.shortFilename = file.getName().getBaseName();
    data.path = KettleVFS.getFilename(file.getParent());
    data.hidden = file.isHidden();
    data.extension = file.getName().getExtension();
    data.uriName = file.getName().getURI();
    data.rootUriName = file.getName().getRootURI();
    if (file.getType().hasContent()) {
        data.lastModificationDateTime = new Date(file.getContent().getLastModifiedTime());
        data.size = file.getContent().getSize();
    } else {
        data.lastModificationDateTime = null;
        data.size = null;
    }
}

From source file:org.pentaho.di.trans.steps.pentahoreporting.urlrepository.FileObjectContentLocation.java

/**
 * Creates a new data item in the current location. This method must never return null. This method will fail if an
 * entity with the same name exists in this location.
 *
 * @param name the name of the new entity.
 * @return the newly created entity, never null.
 * @throws ContentCreationException if the item could not be created.
 *//*w w w .j a  v a 2  s.  co  m*/
public ContentItem createItem(final String name) throws ContentCreationException {
    if (RepositoryUtilities.isInvalidPathName(name)) {
        throw new IllegalArgumentException("The name given is not valid.");
    }
    try {
        final FileObject file = getBackend();
        final FileObject child = file.resolveFile(name);
        if (child.exists()) {
            if (child.getContent().getSize() == 0) {
                // probably one of the temp files created by the pentaho-system
                return new FileObjectContentItem(this, child);
            }
            throw new ContentCreationException("File already exists: " + child);
        }
        try {
            child.createFile();
            return new FileObjectContentItem(this, child);
        } catch (IOException e) {
            throw new ContentCreationException("IOError while create", e);
        }
    } catch (FileSystemException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.pentaho.di.trans.steps.propertyinput.PropertyInputMetaTest.java

@Test
public void testOpenNextFile() throws Exception {

    PropertyInputMeta propertyInputMeta = Mockito.mock(PropertyInputMeta.class);
    PropertyInputData propertyInputData = new PropertyInputData();
    FileInputList fileInputList = new FileInputList();
    FileObject fileObject = Mockito.mock(FileObject.class);
    FileName fileName = Mockito.mock(FileName.class);
    Mockito.when(fileName.getRootURI()).thenReturn("testFolder");
    Mockito.when(fileName.getURI()).thenReturn("testFileName.ini");

    String header = "test ini data with umlauts";
    String key = "key";
    String testValue = "value-with-";
    String testData = "[" + header + "]\r\n" + key + "=" + testValue;
    String charsetEncode = "Windows-1252";

    InputStream inputStream = new ByteArrayInputStream(testData.getBytes(Charset.forName(charsetEncode)));
    FileContent fileContent = Mockito.mock(FileContent.class);
    Mockito.when(fileObject.getContent()).thenReturn(fileContent);
    Mockito.when(fileContent.getInputStream()).thenReturn(inputStream);
    Mockito.when(fileObject.getName()).thenReturn(fileName);
    fileInputList.addFile(fileObject);//from   w w  w .  j a va  2s .c om

    propertyInputData.files = fileInputList;
    propertyInputData.propfiles = false;
    propertyInputData.realEncoding = charsetEncode;

    PropertyInput propertyInput = Mockito.mock(PropertyInput.class);

    Field logField = BaseStep.class.getDeclaredField("log");
    logField.setAccessible(true);
    logField.set(propertyInput, Mockito.mock(LogChannelInterface.class));

    Mockito.doCallRealMethod().when(propertyInput).dispose(propertyInputMeta, propertyInputData);

    propertyInput.dispose(propertyInputMeta, propertyInputData);

    Method method = PropertyInput.class.getDeclaredMethod("openNextFile");
    method.setAccessible(true);
    method.invoke(propertyInput);

    Assert.assertEquals(testValue, propertyInputData.wini.get(header).get(key));
}

From source file:org.pentaho.di.trans.steps.ssh.SSHData.java

public static Connection OpenConnection(String serveur, int port, String username, String password,
        boolean useKey, String keyFilename, String passPhrase, int timeOut, VariableSpace space,
        String proxyhost, int proxyport, String proxyusername, String proxypassword) throws KettleException {
    Connection conn = null;/*from  ww  w . j a  v  a2  s .  c  o m*/
    char[] content = null;
    boolean isAuthenticated = false;
    try {
        // perform some checks
        if (useKey) {
            if (Utils.isEmpty(keyFilename)) {
                throw new KettleException(
                        BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyFileMissing"));
            }
            FileObject keyFileObject = KettleVFS.getFileObject(keyFilename);

            if (!keyFileObject.exists()) {
                throw new KettleException(
                        BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyNotExist", keyFilename));
            }

            FileContent keyFileContent = keyFileObject.getContent();

            CharArrayWriter charArrayWriter = new CharArrayWriter((int) keyFileContent.getSize());

            try (InputStream in = keyFileContent.getInputStream()) {
                IOUtils.copy(in, charArrayWriter);
            }

            content = charArrayWriter.toCharArray();
        }
        // Create a new connection
        conn = createConnection(serveur, port);

        /* We want to connect through a HTTP proxy */
        if (!Utils.isEmpty(proxyhost)) {
            /* Now connect */
            // if the proxy requires basic authentication:
            if (!Utils.isEmpty(proxyusername)) {
                conn.setProxyData(new HTTPProxyData(proxyhost, proxyport, proxyusername, proxypassword));
            } else {
                conn.setProxyData(new HTTPProxyData(proxyhost, proxyport));
            }
        }

        // and connect
        if (timeOut == 0) {
            conn.connect();
        } else {
            conn.connect(null, 0, timeOut * 1000);
        }
        // authenticate
        if (useKey) {
            isAuthenticated = conn.authenticateWithPublicKey(username, content,
                    space.environmentSubstitute(passPhrase));
        } else {
            isAuthenticated = conn.authenticateWithPassword(username, password);
        }
        if (isAuthenticated == false) {
            throw new KettleException(
                    BaseMessages.getString(SSHMeta.PKG, "SSH.Error.AuthenticationFailed", username));
        }
    } catch (Exception e) {
        // Something wrong happened
        // do not forget to disconnect if connected
        if (conn != null) {
            conn.close();
        }
        throw new KettleException(
                BaseMessages.getString(SSHMeta.PKG, "SSH.Error.ErrorConnecting", serveur, username), e);
    }
    return conn;
}

From source file:org.pentaho.di.trans.steps.textfileoutput.TextFileOutputSplittingIT.java

@SuppressWarnings("unchecked")
private static List<String> readContentOf(FileObject fileObject) throws Exception {
    return IOUtils.readLines(fileObject.getContent().getInputStream());
}

From source file:org.pentaho.di.trans.steps.xsdvalidator.XsdValidatorIntTest.java

private FileObject loadRamFile(String filename) throws Exception {
    String targetUrl = RAMDIR + "/" + filename;
    try (InputStream source = getFileInputStream(filename)) {
        FileObject fileObject = KettleVFS.getFileObject(targetUrl);
        try (OutputStream targetStream = fileObject.getContent().getOutputStream()) {
            IOUtils.copy(source, targetStream);
        }//ww  w . j a v a 2s  .c  om
        return fileObject;
    }
}