Search in sources :

Example 6 with FileSystemOptions

use of org.apache.commons.vfs2.FileSystemOptions in project pentaho-metaverse by pentaho.

the class VfsLineageCollector method compressArtifacts.

@Override
public void compressArtifacts(List<String> paths, OutputStream os) {
    ZipOutputStream zos = null;
    try {
        FileSystemOptions opts = new FileSystemOptions();
        zos = new ZipOutputStream(os);
        for (String path : paths) {
            FileObject file = KettleVFS.getFileObject(path, opts);
            try {
                // register the file as an entry in the zip file
                ZipEntry zipEntry = new ZipEntry(file.getName().getPath());
                zos.putNextEntry(zipEntry);
                // write the file's bytes to the zip stream
                try (InputStream fis = file.getContent().getInputStream()) {
                    zos.write(IOUtils.toByteArray(fis));
                }
            } catch (IOException e) {
                log.error(Messages.getString("ERROR.FailedAddingFileToZip", file.getName().getPath()));
            } finally {
                // indicate we are done with this file
                try {
                    zos.closeEntry();
                } catch (IOException e) {
                    log.error(Messages.getString("ERROR.FailedToProperlyCloseZipEntry", file.getName().getPath()));
                }
            }
        }
    } catch (KettleFileException e) {
        log.error(Messages.getString("ERROR.UnexpectedVfsError", e.getMessage()));
    } finally {
        IOUtils.closeQuietly(zos);
    }
}
Also used : KettleFileException(org.pentaho.di.core.exception.KettleFileException) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions)

Example 7 with FileSystemOptions

use of org.apache.commons.vfs2.FileSystemOptions in project pentaho-kettle by pentaho.

the class KettleSftpFileSystemConfigBuilderTest method recognizesAndSetsUserHomeDirProperty.

@Test
public void recognizesAndSetsUserHomeDirProperty() throws Exception {
    final String fullName = Const.VFS_USER_DIR_IS_ROOT;
    final String name = fullName.substring("vfs.sftp.".length());
    final String vfsInternalName = SftpFileSystemConfigBuilder.class.getName() + ".USER_DIR_IS_ROOT";
    final FileSystemOptions opts = new FileSystemOptions();
    KettleSftpFileSystemConfigBuilder builder = KettleSftpFileSystemConfigBuilder.getInstance();
    builder.setParameter(opts, name, "true", fullName, "sftp://fake-url:22");
    Method getOption = ReflectionUtils.findMethod(opts.getClass(), "getOption", Class.class, String.class);
    getOption.setAccessible(true);
    Object value = ReflectionUtils.invokeMethod(getOption, opts, builder.getConfigClass(), vfsInternalName);
    assertEquals(true, value);
}
Also used : SftpFileSystemConfigBuilder(org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder) Method(java.lang.reflect.Method) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions) Test(org.junit.Test)

Example 8 with FileSystemOptions

use of org.apache.commons.vfs2.FileSystemOptions in project pentaho-kettle by pentaho.

the class KettleSftpFileSystemConfigBuilderTest method recognizesAndSetsIdentityKeyFile.

@Test
public void recognizesAndSetsIdentityKeyFile() throws Exception {
    File tempFile = File.createTempFile("KettleSftpFileSystemConfigBuilderTest", ".tmp");
    tempFile.deleteOnExit();
    final String fullName = "vfs.sftp.identity";
    final String name = fullName.substring("vfs.sftp.".length());
    final String vfsInternalName = SftpFileSystemConfigBuilder.class.getName() + ".IDENTITIES";
    final FileSystemOptions opts = new FileSystemOptions();
    KettleSftpFileSystemConfigBuilder builder = KettleSftpFileSystemConfigBuilder.getInstance();
    builder.setParameter(opts, name, tempFile.getAbsolutePath(), fullName, "sftp://fake-url:22");
    Method getOption = ReflectionUtils.findMethod(opts.getClass(), "getOption", Class.class, String.class);
    getOption.setAccessible(true);
    Object value = ReflectionUtils.invokeMethod(getOption, opts, builder.getConfigClass(), vfsInternalName);
    assertEquals(IdentityInfo[].class, value.getClass());
    assertEquals(tempFile.getAbsolutePath(), ((IdentityInfo[]) value)[0].getPrivateKey().getAbsolutePath());
}
Also used : IdentityInfo(org.apache.commons.vfs2.provider.sftp.IdentityInfo) SftpFileSystemConfigBuilder(org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder) Method(java.lang.reflect.Method) File(java.io.File) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions) Test(org.junit.Test)

Example 9 with FileSystemOptions

use of org.apache.commons.vfs2.FileSystemOptions in project pentaho-kettle by pentaho.

the class ConnectionManagerTest method testNullConnectionName.

@Test
public void testNullConnectionName() {
    FileSystemOptions fileSystemOptions = VFSHelper.getOpts("file://fakefile.ktr", null);
    Assert.assertNull(fileSystemOptions);
}
Also used : FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions) Test(org.junit.Test)

Example 10 with FileSystemOptions

use of org.apache.commons.vfs2.FileSystemOptions in project pentaho-kettle by pentaho.

the class KettleSftpFileSystemConfigBuilder method setParameter.

/**
 * Publicly expose a generic way to set parameters
 */
@Override
public void setParameter(FileSystemOptions opts, String name, String value, String fullParameterName, String vfsUrl) throws IOException {
    if (!fullParameterName.startsWith("vfs.sftp")) {
        // This is not an SFTP parameter. Delegate to the generic handler
        super.setParameter(opts, name, value, fullParameterName, vfsUrl);
    } else {
        // Check for the presence of a host in the full variable name
        try {
            // Parse server name from vfsFilename
            FileNameParser sftpFilenameParser = SftpFileNameParser.getInstance();
            URLFileName file = (URLFileName) sftpFilenameParser.parseUri(null, null, vfsUrl);
            if (!parameterContainsHost(fullParameterName) || fullParameterName.endsWith(file.getHostName())) {
                // Match special cases for parameter names
                if (name.equalsIgnoreCase("AuthKeyPassphrase")) {
                    setParam(opts, UserInfo.class.getName(), new PentahoUserInfo(value));
                } else if (name.equals("identity")) {
                    IdentityInfo[] identities = (IdentityInfo[]) this.getParam(opts, IDENTITY_KEY);
                    if (identities == null) {
                        identities = new IdentityInfo[] { new IdentityInfo(new File(value)) };
                    } else {
                        // Copy, in a Java 5 friendly manner, identities into a larger array
                        IdentityInfo[] temp = new IdentityInfo[identities.length + 1];
                        System.arraycopy(identities, 0, temp, 0, identities.length);
                        identities = temp;
                        identities[identities.length - 1] = new IdentityInfo(new File(value));
                    }
                    setParam(opts, IDENTITY_KEY, identities);
                } else {
                    super.setParameter(opts, name, value, fullParameterName, vfsUrl);
                }
            } else {
                // No host match found
                log.logDebug("No host match found for: " + fullParameterName);
            }
        } catch (IOException e) {
            log.logError("Failed to set VFS parameter: [" + fullParameterName + "] " + value, e);
        }
    }
}
Also used : URLFileName(org.apache.commons.vfs2.provider.URLFileName) FileNameParser(org.apache.commons.vfs2.provider.FileNameParser) SftpFileNameParser(org.apache.commons.vfs2.provider.sftp.SftpFileNameParser) IdentityInfo(org.apache.commons.vfs2.provider.sftp.IdentityInfo) UserInfo(com.jcraft.jsch.UserInfo) IOException(java.io.IOException) File(java.io.File)

Aggregations

FileSystemOptions (org.apache.commons.vfs2.FileSystemOptions)98 FileObject (org.apache.commons.vfs2.FileObject)47 FileSystemException (org.apache.commons.vfs2.FileSystemException)29 Test (org.junit.Test)25 IOException (java.io.IOException)17 FileName (org.apache.commons.vfs2.FileName)17 URL (java.net.URL)13 GenericFileName (org.apache.commons.vfs2.provider.GenericFileName)13 File (java.io.File)12 UserAuthenticationData (org.apache.commons.vfs2.UserAuthenticationData)12 StaticUserAuthenticator (org.apache.commons.vfs2.auth.StaticUserAuthenticator)9 FileSystem (org.apache.commons.vfs2.FileSystem)8 DefaultFileSystemManager (org.apache.commons.vfs2.impl.DefaultFileSystemManager)8 ArrayList (java.util.ArrayList)7 FileNotFolderException (org.apache.commons.vfs2.FileNotFolderException)6 Test (org.junit.jupiter.api.Test)6 OutputStream (java.io.OutputStream)5 UserAuthenticator (org.apache.commons.vfs2.UserAuthenticator)5 Before (org.junit.Before)5 AmazonS3 (com.amazonaws.services.s3.AmazonS3)4