Example usage for org.apache.commons.vfs2.provider.ftp FtpFileSystemConfigBuilder setUserDirIsRoot

List of usage examples for org.apache.commons.vfs2.provider.ftp FtpFileSystemConfigBuilder setUserDirIsRoot

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.provider.ftp FtpFileSystemConfigBuilder setUserDirIsRoot.

Prototype

public void setUserDirIsRoot(final FileSystemOptions opts, final boolean userDirIsRoot) 

Source Link

Document

Use user directory as root (do not change to fs root).

Usage

From source file:fr.cls.atoll.motu.library.misc.ftp.TestFtp.java

public static void testVFS(String user, String pwd, String scheme, String host, String file) {

    StandardFileSystemManager fsManager = null;

    try {/*from ww w .j  ava2s  . c  om*/
        fsManager = new StandardFileSystemManager();
        fsManager.setLogger(_LOG);

        StaticUserAuthenticator auth = new StaticUserAuthenticator(null, user, pwd);

        fsManager.setConfiguration(ConfigLoader.getInstance().get(Organizer.getVFSProviderConfig()));
        fsManager.setCacheStrategy(CacheStrategy.ON_RESOLVE);
        // fsManager.addProvider("moi", new DefaultLocalFileProvider());
        fsManager.init();

        FileSystemOptions opts = new FileSystemOptions();
        FileSystemConfigBuilder fscb = fsManager.getFileSystemConfigBuilder(scheme);
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

        System.out.println(fsManager.getProviderCapabilities(scheme));

        if (fscb instanceof FtpFileSystemConfigBuilder) {
            FtpFileSystemConfigBuilder ftpFscb = (FtpFileSystemConfigBuilder) fscb;
            ftpFscb.setUserDirIsRoot(opts, true);
            ftpFscb.setPassiveMode(opts, true);

        }
        if (fscb instanceof HttpFileSystemConfigBuilder) {
            HttpFileSystemConfigBuilder httpFscb = (HttpFileSystemConfigBuilder) fscb;
            httpFscb.setProxyHost(opts, "proxy.cls.fr");
            httpFscb.setProxyPort(opts, 8080);

        }
        if (fscb instanceof SftpFileSystemConfigBuilder) {
            SftpFileSystemConfigBuilder sftpFscb = (SftpFileSystemConfigBuilder) fscb;
            sftpFscb.setUserDirIsRoot(opts, false);

            // TrustEveryoneUserInfo trustEveryoneUserInfo = new TrustEveryoneUserInfo();
            // trustEveryoneUserInfo.promptYesNo("eddfsdfs");
            // sftpFscb.setUserInfo(opts, new TrustEveryoneUserInfo());
            sftpFscb.setTimeout(opts, 5000);
            // SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
            // SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

        }
        // FileObject fo =
        // fsManager.resolveFile("ftp://ftp.cls.fr/pub/oceano/AVISO/NRT-SLA/maps/rt/j2/h/msla_rt_j2_err_21564.nc.gz",
        // opts);

        // String uri = String.format("%s://%s/%s", scheme, host, file);
        // String uri = String.format("%s://%s/", scheme, host);
        // FileObject originBase = fsManager.resolveFile(uri, opts);
        // fsManager.setBaseFile(originBase);

        File tempDir = new File("c:/tempVFS");
        // File tempFile = File.createTempFile("AsciiEnvisat", ".txt", tempDir);
        File hostFile = new File(file);
        String fileName = hostFile.getName();
        File newFile = new File(tempDir, fileName);
        newFile.createNewFile();

        DefaultFileReplicator dfr = new DefaultFileReplicator(tempDir);
        fsManager.setTemporaryFileStore(dfr);
        // System.out.println(fsManager.getBaseFile());
        // System.out.println(dfr);
        // System.out.println(fsManager.getTemporaryFileStore());

        // FileObject ff = fsManager.resolveFile("sftp://t:t@CLS-EARITH.pc.cls.fr/AsciiEnvisat.txt",
        // opts);
        String uri = String.format("%s://%s/%s", scheme, host, file);
        FileObject ff = fsManager.resolveFile(uri, opts);
        FileObject dest = fsManager.toFileObject(newFile);

        //ff.getContent().getInputStream();
        dest.copyFrom(ff, Selectors.SELECT_ALL);
        //dest.copyFrom(ff, Selectors.SELECT_ALL);

        //            
        // URL url = ff.getURL();
        //            
        // url.openConnection();
        // URLConnection conn = url.openConnection();
        // InputStream in = conn.getInputStream();
        // in.close();

        // InputStream in = ff.getContent().getInputStream();

    } catch (FileSystemException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MotuException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        // fsManager.close();
        // fsManager.freeUnusedResources();
    }

}

From source file:com.sonicle.webtop.vfs.sfs.FtpSFS.java

@Override
protected void configureOptions() throws FileSystemException {
    FtpFileSystemConfigBuilder builder = FtpFileSystemConfigBuilder.getInstance();
    builder.setUserDirIsRoot(fso, false);
}

From source file:net.sourceforge.fullsync.fs.filesystems.FTPAuthenticationProvider.java

@Override
public final void authSetup(final ConnectionDescription description, final FileSystemOptions options)
        throws FileSystemException {
    String username = description.getUsername().orElse(""); //$NON-NLS-1$
    String password = description.getPassword().orElse(""); //$NON-NLS-1$
    StaticUserAuthenticator auth = new StaticUserAuthenticator(null, username, password);
    FtpFileSystemConfigBuilder cfg = FtpFileSystemConfigBuilder.getInstance();
    cfg.setPassiveMode(options, true);//w  w  w  .  j av a2 s.c  om
    cfg.setUserDirIsRoot(options, description.isUserDirIsRoot());
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, auth);
}

From source file:com.codehaus.mojo.vfs.FileSystemOptionsFactory.java

public FileSystemOptions getFileSystemOptions(String url, String username, String password)
        throws FileSystemException {

    FileSystemOptions opts = new FileSystemOptions();

    String[] tokens = StringUtils.split(url, ":");

    String protocol = tokens[0];/*from w  w  w.  j a va 2  s  . c o  m*/

    if ("ftp".equals(protocol)) {
        FtpFileSystemConfigBuilder builder = FtpFileSystemConfigBuilder.getInstance();
        builder.setPassiveMode(opts, ftpSettings.isPassiveMode());
        builder.setUserDirIsRoot(opts, ftpSettings.isUserDirIsRoot());
    }
    if ("sftp".equals(protocol)) {
        SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
        builder.setUserDirIsRoot(opts, sftpSettings.isUserDirIsRoot());
    }

    String domain = null;
    tokens = StringUtils.split("\\");
    if (tokens.length == 2) {
        domain = tokens[0];
        username = tokens[1];
    }

    StaticUserAuthenticator auth = new StaticUserAuthenticator(domain, username, password);

    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    return opts;
}

From source file:fr.cls.atoll.motu.library.misc.vfs.VFSManager.java

/**
 * Sets the scheme.//w w  w. j av  a2  s .co m
 * 
 * @param scheme the scheme
 * 
 * @return the file system options
 * 
 * @throws MotuException the motu exception
 */
public FileSystemOptions setSchemeOpts(String scheme, String host) throws MotuException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("setSchemeOpts(String, String) - start");
    }

    if (Organizer.isNullOrEmpty(scheme)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("setSchemeOpts(String, String) - end");
        }
        return opts;
    }

    if (opts == null) {
        opts = new FileSystemOptions();
    }

    FileSystemConfigBuilder fscb = null;
    MotuConfigFileSystemWrapper<Boolean> wrapperBoolean = new MotuConfigFileSystemWrapper<Boolean>();
    MotuConfigFileSystemWrapper<Period> wrapperPeriod = new MotuConfigFileSystemWrapper<Period>();
    MotuConfigFileSystemWrapper<String> wrapperString = new MotuConfigFileSystemWrapper<String>();

    try {
        try {
            fscb = standardFileSystemManager.getFileSystemConfigBuilder(scheme);
        } catch (FileSystemException e) {
            LOG.error("setSchemeOpts(String)", e);

            fscb = standardFileSystemManager.getFileSystemConfigBuilder(VFSManager.DEFAULT_SCHEME);
        }

        if (fscb instanceof FtpFileSystemConfigBuilder) {
            FtpFileSystemConfigBuilder ftpFscb = (FtpFileSystemConfigBuilder) fscb;
            Boolean userDirIsRoot = wrapperBoolean.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_FTPUSERDIRISROOT);
            if (userDirIsRoot != null) {
                ftpFscb.setUserDirIsRoot(opts, userDirIsRoot);
            } else {
                ftpFscb.setUserDirIsRoot(opts, false);
            }

            Boolean passiveMode = wrapperBoolean.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_FTPPASSIVEMODE);
            ;
            if (passiveMode != null) {
                ftpFscb.setPassiveMode(opts, passiveMode);
            }
            Period dataTimeOut = wrapperPeriod.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_FTPDATATIMEOUT);
            if (dataTimeOut != null) {
                long value = dataTimeOut.toStandardDuration().getMillis();
                if (value > Integer.MAX_VALUE) {
                    throw new MotuException(String.format(
                            "Motu Configuration : sftp timeout value is too large '%ld' milliseconds. Max is '%d'",
                            value, Integer.MAX_VALUE));
                }
                if (value > 0) {
                    ftpFscb.setDataTimeout(opts, (int) value);
                }
            }
        }

        if (fscb instanceof HttpFileSystemConfigBuilder) {
            HttpFileSystemConfigBuilder httpFscb = (HttpFileSystemConfigBuilder) fscb;

            Boolean isUseProxy = wrapperBoolean.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_USEHTTPPROXY);
            if ((isUseProxy != null) && (isUseProxy)) {
                String proxyHost = wrapperString.getFieldValue(host,
                        MotuConfigFileSystemWrapper.PROP_HTTPPROXYHOST);
                String proxyPort = wrapperString.getFieldValue(host,
                        MotuConfigFileSystemWrapper.PROP_HTTPPROXYPORT);
                httpFscb.setProxyHost(opts, proxyHost);
                httpFscb.setProxyPort(opts, Integer.parseInt(proxyPort));
            }

        }

        if (fscb instanceof SftpFileSystemConfigBuilder) {
            SftpFileSystemConfigBuilder sftpFscb = (SftpFileSystemConfigBuilder) fscb;

            Boolean userDirIsRoot = wrapperBoolean.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_SFTPUSERDIRISROOT);
            if (userDirIsRoot != null) {
                sftpFscb.setUserDirIsRoot(opts, userDirIsRoot);
            } else {
                sftpFscb.setUserDirIsRoot(opts, false);
            }

            String strictHostKeyChecking = wrapperString.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_STRICTHOSTKEYCHECKING);
            if (strictHostKeyChecking != null) {
                sftpFscb.setStrictHostKeyChecking(opts, strictHostKeyChecking);
            }

            Period SftpSessionTimeOut = wrapperPeriod.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_SFTPSESSIONTIMEOUT);
            if (SftpSessionTimeOut != null) {
                long value = SftpSessionTimeOut.toStandardDuration().getMillis();
                if (value > Integer.MAX_VALUE) {
                    throw new MotuException(String.format(
                            "Motu Configuration : sftp timeout value is too large '%ld' milliseconds. Max is '%d'",
                            value, Integer.MAX_VALUE));
                }
                if (value > 0) {
                    sftpFscb.setTimeout(opts, (int) value);
                }
            }

            Boolean isUseProxy = wrapperBoolean.getFieldValue(host,
                    MotuConfigFileSystemWrapper.PROP_USESFTPPROXY);
            if ((isUseProxy != null) && (isUseProxy)) {
                String proxyHost = wrapperString.getFieldValue(host,
                        MotuConfigFileSystemWrapper.PROP_SFTPPROXYHOST);
                String proxyPort = wrapperString.getFieldValue(host,
                        MotuConfigFileSystemWrapper.PROP_SFTPPROXYPORT);
                sftpFscb.setProxyHost(opts, proxyHost);
                sftpFscb.setProxyPort(opts, Integer.parseInt(proxyPort));
            }

        }

    } catch (FileSystemException e) {
        LOG.error("setSchemeOpts(String, String)", e);

        throw new MotuException("Error in VFSManager#setScheme", e);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("setSchemeOpts(String, String) - end");
    }
    return opts;
}

From source file:com.streamsets.pipeline.lib.remote.FTPRemoteConnector.java

@Override
protected void initAndConnect(List<Stage.ConfigIssue> issues, ConfigIssueContext context, URI remoteURI,
        Label remoteGroup, Label credGroup) {
    options = new FileSystemOptions();
    this.remoteURI = remoteURI;
    if (remoteConfig.strictHostChecking) {
        issues.add(context.createConfigIssue(credGroup.getLabel(), CONF_PREFIX + "strictHostChecking",
                Errors.REMOTE_07));/* www . j a  v  a  2s  .  co m*/
    }
    switch (remoteConfig.auth) {
    case PRIVATE_KEY:
        issues.add(
                context.createConfigIssue(credGroup.getLabel(), CONF_PREFIX + "privateKey", Errors.REMOTE_06));
        break;
    case PASSWORD:
        String username = resolveUsername(remoteURI, issues, context, credGroup);
        String password = resolvePassword(remoteURI, issues, context, credGroup);
        if (remoteURI.getUserInfo() != null) {
            remoteURI = UriBuilder.fromUri(remoteURI).userInfo(null).build();
        }
        StaticUserAuthenticator authenticator = new StaticUserAuthenticator(remoteURI.getHost(), username,
                password);
        try {
            DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, authenticator);
        } catch (FileSystemException e) {
            // This method doesn't actually ever throw a FileSystemException, it simply declares that it does...
            // This shouldn't happen, but just in case, we'll log it
            LOG.error("Unexpected Exception", e);
        }
        break;
    case NONE:
        break;
    default:
        break;
    }

    FtpFileSystemConfigBuilder configBuilder = FtpFileSystemConfigBuilder.getInstance();
    if (remoteURI.getScheme().equals(FTPS_SCHEME)) {
        configBuilder = FtpsFileSystemConfigBuilder.getInstance();
        FtpsFileSystemConfigBuilder.getInstance().setFtpsMode(options, remoteConfig.ftpsMode.getMode());
        FtpsFileSystemConfigBuilder.getInstance().setDataChannelProtectionLevel(options,
                remoteConfig.ftpsDataChannelProtectionLevel.getLevel());
        if (remoteConfig.useFTPSClientCert) {
            setFtpsUserKeyManagerOrTrustManager(remoteConfig.ftpsClientCertKeystoreFile,
                    CONF_PREFIX + "ftpsClientCertKeystoreFile", remoteConfig.ftpsClientCertKeystorePassword,
                    CONF_PREFIX + "ftpsClientCertKeystorePassword", remoteConfig.ftpsClientCertKeystoreType,
                    true, issues, context, credGroup);
        }
        switch (remoteConfig.ftpsTrustStoreProvider) {
        case FILE:
            setFtpsUserKeyManagerOrTrustManager(remoteConfig.ftpsTruststoreFile,
                    CONF_PREFIX + "ftpsTruststoreFile", remoteConfig.ftpsTruststorePassword,
                    CONF_PREFIX + "ftpsTruststorePassword", remoteConfig.ftpsTruststoreType, false, issues,
                    context, credGroup);
            break;
        case JVM_DEFAULT:
            try {
                FtpsFileSystemConfigBuilder.getInstance().setTrustManager(options,
                        TrustManagerUtils.getDefaultTrustManager(null));
            } catch (GeneralSecurityException e) {
                issues.add(context.createConfigIssue(credGroup.getLabel(), CONF_PREFIX + "ftpsTruststoreFile",
                        Errors.REMOTE_14, "trust", "JVM", e.getMessage(), e));
            }
            break;
        case ALLOW_ALL:
            // fall through
        default:
            FtpsFileSystemConfigBuilder.getInstance().setTrustManager(options,
                    TrustManagerUtils.getAcceptAllTrustManager());
            break;
        }
    }
    configBuilder.setPassiveMode(options, true);
    configBuilder.setUserDirIsRoot(options, remoteConfig.userDirIsRoot);

    // Only actually try to connect and authenticate if there were no issues
    if (issues.isEmpty()) {
        LOG.info("Connecting to {}", remoteURI.toString());
        try {
            remoteDir = VFS.getManager().resolveFile(remoteURI.toString(), options);
            remoteDir.refresh();
            if (remoteConfig.createPathIfNotExists && !remoteDir.exists()) {
                remoteDir.createFolder();
            }
            remoteDir.getChildren();
        } catch (FileSystemException e) {
            LOG.error(Errors.REMOTE_11.getMessage(), remoteURI.toString(), e.getMessage(), e);
            issues.add(context.createConfigIssue(remoteGroup.getLabel(), CONF_PREFIX + "remoteAddress",
                    Errors.REMOTE_11, remoteURI.toString(), e.getMessage(), e));
        }
    }
}