Example usage for org.apache.commons.vfs2.provider.sftp SftpFileSystemConfigBuilder PROXY_HTTP

List of usage examples for org.apache.commons.vfs2.provider.sftp SftpFileSystemConfigBuilder PROXY_HTTP

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.provider.sftp SftpFileSystemConfigBuilder PROXY_HTTP.

Prototype

ProxyType PROXY_HTTP

To view the source code for org.apache.commons.vfs2.provider.sftp SftpFileSystemConfigBuilder PROXY_HTTP.

Click Source Link

Document

HTTP Proxy.

Usage

From source file:de.unioninvestment.portal.explorer.view.vfs.VFSMainView.java

public VFSMainView(ConfigBean cb, VFSFileExplorerPortlet instance) throws Exception {

    final String vfsUrl = cb.getVfsUrl();
    if (vfsUrl.length() != 0) {
        removeAllComponents();//from w ww.j  av a  2 s  .c om
        explorerPanel.setStyleName(Reindeer.PANEL_LIGHT);
        filePanel.setStyleName(Reindeer.PANEL_LIGHT);
        FileSystemOptions opts = new FileSystemOptions();

        logger.log(Level.INFO, "Check Type ");

        if (cb.getVfsType().equalsIgnoreCase("FTP") || cb.getVfsType().equalsIgnoreCase("SFTP")) {
            if (cb.getUsername() != null && cb.getUsername().length() > 0) {
                StaticUserAuthenticator auth = new StaticUserAuthenticator(null, cb.getUsername(),
                        cb.getPassword());
                DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
            }
        }

        if (cb.getVfsType().equalsIgnoreCase("FTP")) {
            FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
            FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
        }

        if (cb.getVfsType().equalsIgnoreCase("SFTP")) {
            SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
            if (cb.getKeyfile() != null && cb.getKeyfile().length() > 0) {
                logger.log(Level.INFO, "Keyfile " + cb.getKeyfile());
                SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
                File keyFile = new File(cb.getKeyfile());
                SftpFileSystemConfigBuilder.getInstance().setIdentities(opts, new File[] { keyFile });

            }

            SftpFileSystemConfigBuilder.getInstance().setProxyType(opts,
                    SftpFileSystemConfigBuilder.PROXY_HTTP);
            if (cb.getProxyHost() != null && cb.getProxyHost().length() > 0) {
                SftpFileSystemConfigBuilder.getInstance().setProxyHost(opts, cb.getProxyHost());
                logger.log(Level.INFO, "ProxyHost " + cb.getProxyHost());
            }
            if (cb.getProxyPort() != null && cb.getProxyPort().length() > 0) {
                SftpFileSystemConfigBuilder.getInstance().setProxyPort(opts,
                        Integer.valueOf(cb.getProxyPort()));
                logger.log(Level.INFO, "ProxyPort " + cb.getProxyPort());
            }
        }

        DefaultFileSystemManager fsManager = null;
        fsManager = getManager();

        final HorizontalSplitPanel panel = new HorizontalSplitPanel();
        panel.setHeight(500, UNITS_PIXELS);
        panel.setWidth(1400, UNITS_PIXELS);
        panel.setSplitPosition(350, Sizeable.UNITS_PIXELS);
        panel.setFirstComponent(explorerPanel);
        panel.setSecondComponent(filePanel);
        addComponent(panel);

        final Embedded image = new Embedded();
        image.setType(Embedded.TYPE_IMAGE);
        image.setSource(FOLDER);
        image.setHeight(15, Sizeable.UNITS_PIXELS);

        explorerPanel.setSizeFull();
        filePanel.setSizeFull();
        explorerPanel.addComponent(tree);

        filePanel.addComponent(new TableView(instance, fsManager, opts, cb));
        tree.setImmediate(true);

        tree.addListener(new ItemClickEvent.ItemClickListener() {

            private static final long serialVersionUID = 1L;

            @Override
            public void itemClick(ItemClickEvent event) {
                VFSFileExplorerPortlet app = (VFSFileExplorerPortlet) getApplication();
                String newDir = (String) event.getItemId();
                app.getEventBus().fireEvent(new TableChangedEvent(newDir));
            }
        });

        scanDirectory(fsManager, opts, vfsUrl);
    } else {

        addComponent(new Label("Please configure Portlet !"));
    }
}

From source file:SftpClientFactory.java

/**
 * Creates a new connection to the server.
 * @param hostname The name of the host to connect to.
 * @param port The port to use./*from  w w w .  ja va  2  s .  c  o  m*/
 * @param username The user's id.
 * @param password The user's password.
 * @param fileSystemOptions The FileSystem options.
 * @return A Session.
 * @throws FileSystemException if an error occurs.
 */
public static Session createConnection(String hostname, int port, char[] username, char[] password,
        FileSystemOptions fileSystemOptions) throws FileSystemException {
    JSch jsch = new JSch();

    File sshDir = null;

    // new style - user passed
    File knownHostsFile = SftpFileSystemConfigBuilder.getInstance().getKnownHosts(fileSystemOptions);
    File[] identities = SftpFileSystemConfigBuilder.getInstance().getIdentities(fileSystemOptions);

    if (knownHostsFile != null) {
        try {
            jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
        } catch (JSchException e) {
            throw new FileSystemException("vfs.provider.sftp/known-hosts.error",
                    knownHostsFile.getAbsolutePath(), e);
        }
    } else {
        sshDir = findSshDir();
        // Load the known hosts file
        knownHostsFile = new File(sshDir, "known_hosts");
        if (knownHostsFile.isFile() && knownHostsFile.canRead()) {
            try {
                jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
            } catch (JSchException e) {
                throw new FileSystemException("vfs.provider.sftp/known-hosts.error",
                        knownHostsFile.getAbsolutePath(), e);
            }
        }
    }

    if (identities != null) {
        for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++) {
            final File privateKeyFile = identities[iterIdentities];
            try {
                jsch.addIdentity(privateKeyFile.getAbsolutePath());
            } catch (final JSchException e) {
                throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
            }
        }
    } else {
        if (sshDir == null) {
            sshDir = findSshDir();
        }

        // Load the private key (rsa-key only)
        final File privateKeyFile = new File(sshDir, "id_rsa");
        if (privateKeyFile.isFile() && privateKeyFile.canRead()) {
            try {
                jsch.addIdentity(privateKeyFile.getAbsolutePath());
            } catch (final JSchException e) {
                throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
            }
        }
    }

    Session session;
    try {
        session = jsch.getSession(new String(username), hostname, port);
        if (password != null) {
            session.setPassword(new String(password));
        }

        Integer timeout = SftpFileSystemConfigBuilder.getInstance().getTimeout(fileSystemOptions);
        if (timeout != null) {
            session.setTimeout(timeout.intValue());
        }

        UserInfo userInfo = SftpFileSystemConfigBuilder.getInstance().getUserInfo(fileSystemOptions);
        if (userInfo != null) {
            session.setUserInfo(userInfo);
        }

        Properties config = new Properties();

        //set StrictHostKeyChecking property
        String strictHostKeyChecking = SftpFileSystemConfigBuilder.getInstance()
                .getStrictHostKeyChecking(fileSystemOptions);
        if (strictHostKeyChecking != null) {
            config.setProperty("StrictHostKeyChecking", strictHostKeyChecking);
        }
        //set PreferredAuthentications property
        String preferredAuthentications = SftpFileSystemConfigBuilder.getInstance()
                .getPreferredAuthentications(fileSystemOptions);
        if (preferredAuthentications != null) {
            config.setProperty("PreferredAuthentications", preferredAuthentications);
        }

        //set compression property
        String compression = SftpFileSystemConfigBuilder.getInstance().getCompression(fileSystemOptions);
        if (compression != null) {
            config.setProperty("compression.s2c", compression);
            config.setProperty("compression.c2s", compression);
        }

        String proxyHost = SftpFileSystemConfigBuilder.getInstance().getProxyHost(fileSystemOptions);
        if (proxyHost != null) {
            int proxyPort = SftpFileSystemConfigBuilder.getInstance().getProxyPort(fileSystemOptions);
            SftpFileSystemConfigBuilder.ProxyType proxyType = SftpFileSystemConfigBuilder.getInstance()
                    .getProxyType(fileSystemOptions);
            Proxy proxy = null;
            if (SftpFileSystemConfigBuilder.PROXY_HTTP.equals(proxyType)) {
                if (proxyPort != 0) {
                    proxy = new ProxyHTTP(proxyHost, proxyPort);
                } else {
                    proxy = new ProxyHTTP(proxyHost);
                }
            } else if (SftpFileSystemConfigBuilder.PROXY_SOCKS5.equals(proxyType)) {
                if (proxyPort != 0) {
                    proxy = new ProxySOCKS5(proxyHost, proxyPort);
                } else {
                    proxy = new ProxySOCKS5(proxyHost);
                }
            }

            if (proxy != null) {
                session.setProxy(proxy);
            }
        }

        //set properties for the session
        if (config.size() > 0) {
            session.setConfig(config);
        }
        session.setDaemonThread(true);
        session.connect();
    } catch (final Exception exc) {
        throw new FileSystemException("vfs.provider.sftp/connect.error", new Object[] { hostname }, exc);
    }

    return session;
}