Search in sources :

Example 1 with FileSystemOptions

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

the class KettleVFS method buildFsOptions.

private static FileSystemOptions buildFsOptions(VariableSpace varSpace, FileSystemOptions sourceOptions, String vfsFilename, String scheme) throws IOException {
    if (varSpace == null || vfsFilename == null) {
        // We cannot extract settings from a non-existant variable space
        return null;
    }
    IKettleFileSystemConfigBuilder configBuilder = KettleFileSystemConfigBuilderFactory.getConfigBuilder(varSpace, scheme);
    FileSystemOptions fsOptions = (sourceOptions == null) ? new FileSystemOptions() : sourceOptions;
    String[] varList = varSpace.listVariables();
    for (String var : varList) {
        if (var.startsWith("vfs.")) {
            String param = configBuilder.parseParameterName(var, scheme);
            String varScheme = KettleGenericFileSystemConfigBuilder.extractScheme(var);
            if (param != null) {
                if (varScheme == null || varScheme.equals("sftp") || varScheme.equals(scheme)) {
                    configBuilder.setParameter(fsOptions, param, varSpace.getVariable(var), var, vfsFilename);
                }
            } else {
                throw new IOException("FileSystemConfigBuilder could not parse parameter: " + var);
            }
        }
    }
    return fsOptions;
}
Also used : IKettleFileSystemConfigBuilder(org.pentaho.di.core.vfs.configuration.IKettleFileSystemConfigBuilder) IOException(java.io.IOException) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions)

Example 2 with FileSystemOptions

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

the class KettleVFS method getFileObject.

public static FileObject getFileObject(String vfsFilename, VariableSpace space, FileSystemOptions fsOptions) throws KettleFileException {
    try {
        FileSystemManager fsManager = getInstance().getFileSystemManager();
        // We have one problem with VFS: if the file is in a subdirectory of the current one: somedir/somefile
        // In that case, VFS doesn't parse the file correctly.
        // We need to put file: in front of it to make it work.
        // However, how are we going to verify this?
        // 
        // We are going to see if the filename starts with one of the known protocols like file: zip: ram: smb: jar: etc.
        // If not, we are going to assume it's a file.
        // 
        boolean relativeFilename = true;
        String[] schemes = fsManager.getSchemes();
        for (int i = 0; i < schemes.length && relativeFilename; i++) {
            if (vfsFilename.startsWith(schemes[i] + ":")) {
                relativeFilename = false;
                // We have a VFS URL, load any options for the file system driver
                fsOptions = buildFsOptions(space, fsOptions, vfsFilename, schemes[i]);
            }
        }
        String filename;
        if (vfsFilename.startsWith("\\\\")) {
            File file = new File(vfsFilename);
            filename = file.toURI().toString();
        } else {
            if (relativeFilename) {
                File file = new File(vfsFilename);
                filename = file.getAbsolutePath();
            } else {
                filename = vfsFilename;
            }
        }
        if (fsOptions != null) {
            return fsManager.resolveFile(filename, fsOptions);
        } else {
            return fsManager.resolveFile(filename);
        }
    } catch (IOException e) {
        throw new KettleFileException("Unable to get VFS File object for filename '" + cleanseFilename(vfsFilename) + "' : " + e.getMessage(), e);
    }
}
Also used : KettleFileException(org.pentaho.di.core.exception.KettleFileException) IOException(java.io.IOException) DefaultFileSystemManager(org.apache.commons.vfs2.impl.DefaultFileSystemManager) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) StandardFileSystemManager(org.apache.commons.vfs2.impl.StandardFileSystemManager) LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) File(java.io.File)

Example 3 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)

Example 4 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 5 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)

Aggregations

FileSystemOptions (org.apache.commons.vfs2.FileSystemOptions)17 FileObject (org.apache.commons.vfs2.FileObject)10 IOException (java.io.IOException)9 FileSystemException (org.apache.commons.vfs2.FileSystemException)7 File (java.io.File)5 FileName (org.apache.commons.vfs2.FileName)4 Test (org.junit.Test)4 KettleFileException (org.pentaho.di.core.exception.KettleFileException)4 ArrayList (java.util.ArrayList)3 SftpFileSystemConfigBuilder (org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder)3 DelegatingFileSystemOptionsBuilder (org.apache.commons.vfs2.util.DelegatingFileSystemOptionsBuilder)3 Method (java.lang.reflect.Method)2 Date (java.util.Date)2 RepositoryException (javax.jcr.RepositoryException)2 ParseException (javax.mail.internet.ParseException)2 AxisFault (org.apache.axis2.AxisFault)2 FileDepthSelector (org.apache.commons.vfs2.FileDepthSelector)2 FileNotFolderException (org.apache.commons.vfs2.FileNotFolderException)2 FileNotFoundException (org.apache.commons.vfs2.FileNotFoundException)2 FileSelector (org.apache.commons.vfs2.FileSelector)2