Example usage for org.apache.commons.vfs2.auth StaticUserAuthenticator StaticUserAuthenticator

List of usage examples for org.apache.commons.vfs2.auth StaticUserAuthenticator StaticUserAuthenticator

Introduction

In this page you can find the example usage for org.apache.commons.vfs2.auth StaticUserAuthenticator StaticUserAuthenticator.

Prototype

public StaticUserAuthenticator(final String domain, final String username, final String password) 

Source Link

Usage

From source file:net.sf.jabb.util.vfs.VfsUtility.java

/**
 * Configure FileSystemOptions for HttpFileSystem
 * @param fsOptions/*  w  w  w.  j  av a  2 s  . c om*/
 * @param webProxyHost
 * @param webProxyPort
 * @param webProxyUserName
 * @param webProxyPassword
 */
static public void configHttpFileSystemProxy(FileSystemOptions fsOptions, String webProxyHost,
        Integer webProxyPort, String webProxyUserName, String webProxyPassword) {
    if (webProxyHost != null && webProxyPort != null) {
        HttpFileSystemConfigBuilder.getInstance().setProxyHost(fsOptions, webProxyHost);
        HttpFileSystemConfigBuilder.getInstance().setProxyPort(fsOptions, webProxyPort);
        if (webProxyUserName != null) {
            StaticUserAuthenticator auth = new StaticUserAuthenticator(webProxyUserName, webProxyPassword,
                    null);
            HttpFileSystemConfigBuilder.getInstance().setProxyAuthenticator(fsOptions, auth);
        }
    }

}

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

@Override
public final void authSetup(final ConnectionDescription description, final FileSystemOptions options)
        throws org.apache.commons.vfs2.FileSystemException {
    StaticUserAuthenticator auth = new StaticUserAuthenticator(null,
            description.getParameter(ConnectionDescription.PARAMETER_USERNAME),
            description.getSecretParameter(ConnectionDescription.PARAMETER_PASSWORD));
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, auth);
    SftpFileSystemConfigBuilder cfg = SftpFileSystemConfigBuilder.getInstance();
    if (null != sshDirName) {
        cfg.setKnownHosts(options, new File(sshDirName, "known_hosts"));
    }/*from   w  w  w . j a  v a 2  s. c o  m*/
    logger.debug("SFTP using knownHosts: ", cfg.getKnownHosts(options));
    cfg.setUserInfo(options, this);
    cfg.setStrictHostKeyChecking(options, "ask");
    if ("enabled".equals(description.getParameter("publicKeyAuth"))) {
        cfg.setPreferredAuthentications(options, "publickey,password,keyboard-interactive");
    } else {
        cfg.setPreferredAuthentications(options, "password,keyboard-interactive");
    }
}

From source file:net.sourceforge.fullsync.fs.filesystems.SFTPAuthProvider.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);
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, auth);
    SftpFileSystemConfigBuilder cfg = SftpFileSystemConfigBuilder.getInstance();
    cfg.setUserDirIsRoot(options, desc.isUserDirIsRoot());
    if (null != SSH_DIR_NAME) {
        cfg.setKnownHosts(options, new File(SSH_DIR_NAME, "known_hosts")); //$NON-NLS-1$
    }/*from ww  w . j  a  v a 2s  . c o  m*/
    logger.debug("using knownHosts: ", cfg.getKnownHosts(options)); //$NON-NLS-1$
    cfg.setUserInfo(options, this);
    cfg.setStrictHostKeyChecking(options, "ask"); //$NON-NLS-1$
    if (description.getPublicKeyAuth().orElse(false).booleanValue()) {
        cfg.setPreferredAuthentications(options, "publickey,password,keyboard-interactive"); //$NON-NLS-1$
    } else {
        cfg.setPreferredAuthentications(options, "password,keyboard-interactive"); //$NON-NLS-1$
    }
}

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 w  w  . j a va 2s  .  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:com.streamsets.pipeline.stage.origin.remote.FTPRemoteDownloadSourceDelegate.java

protected void initAndConnectInternal(List<Stage.ConfigIssue> issues, Source.Context context, URI remoteURI,
        String archiveDir) throws IOException {
    options = new FileSystemOptions();
    this.remoteURI = remoteURI;
    if (conf.strictHostChecking) {
        issues.add(context.createConfigIssue(Groups.CREDENTIALS.getLabel(), CONF_PREFIX + "strictHostChecking",
                Errors.REMOTE_12));/*from  w w  w.  j  a  v a 2s . co  m*/
    }
    switch (conf.auth) {
    case PRIVATE_KEY:
        issues.add(context.createConfigIssue(Groups.CREDENTIALS.getLabel(), CONF_PREFIX + "privateKey",
                Errors.REMOTE_11));
        break;
    case PASSWORD:
        String username = resolveUsername(remoteURI, issues, context);
        String password = resolvePassword(remoteURI, issues, context);
        if (remoteURI.getUserInfo() != null) {
            remoteURI = UriBuilder.fromUri(remoteURI).userInfo(null).build();
        }
        StaticUserAuthenticator authenticator = new StaticUserAuthenticator(remoteURI.getHost(), username,
                password);
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, authenticator);
        break;
    case NONE:
        break;
    default:
        break;
    }
    FtpFileSystemConfigBuilder.getInstance().setPassiveMode(options, true);
    FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, conf.userDirIsRoot);

    // Only actually try to connect and authenticate if there were no issues
    if (issues.isEmpty()) {
        LOG.info("Connecting to {}", remoteURI.toString());
        // To ensure we can connect, else we fail validation.
        remoteDir = VFS.getManager().resolveFile(remoteURI.toString(), options);
        // Ensure we can assess the remote directory...
        remoteDir.refresh();
        // throw away the results.
        remoteDir.getChildren();

        setupModTime();
    }

    if (archiveDir != null) {
        archiveURI = UriBuilder.fromUri(remoteURI).replacePath(archiveDir).build();
        archiveOptions = (FileSystemOptions) options.clone();
        FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(archiveOptions, conf.archiveDirUserDirIsRoot);
    }
}

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));//  w w  w . ja  va 2 s.com
    }
    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));
        }
    }
}

From source file:com.sludev.commons.vfs2.provider.azure.AzFileProviderTest.java

/**
 * /*ww  w. j a  va2s. co m*/
 */
@Test
public void A001_uploadFile() throws Exception {
    String currAccountStr = testProperties.getProperty("azure.account.name");
    String currKey = testProperties.getProperty("azure.account.key");
    String currContainerStr = testProperties.getProperty("azure.test0001.container.name");
    String currHost = testProperties.getProperty("azure.host"); // <account>.blob.core.windows.net
    String currFileNameStr;

    File temp = File.createTempFile("uploadFile01", ".tmp");
    try (FileWriter fw = new FileWriter(temp)) {
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append("testing...");
        bw.flush();
    }

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, new AzFileProvider());
    currMan.addProvider("file", new DefaultLocalFileProvider());
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "test01.tmp";
    String currUriStr = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);
    FileObject currFile2 = currMan.resolveFile(String.format("file://%s", temp.getAbsolutePath()));

    currFile.copyFrom(currFile2, Selectors.SELECT_SELF);
    temp.delete();
}

From source file:com.sludev.commons.vfs.simpleshell.SimpleShell.java

private void Init(String accnt, String key, String cont) throws IOException {
    conReader = new ConsoleReader();
    conReader.setPrompt("AzureShell> ");

    List<Completer> completors = new LinkedList<>();

    String currFileNameStr = "dir1";

    AzFileProvider azfp = new AzFileProvider();
    StaticUserAuthenticator auth = new StaticUserAuthenticator("", accnt, key);
    AzFileSystemConfigBuilder.getInstance().setUserAuthenticator(azfp.getDefaultFileSystemOptions(), auth);

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(AzConstants.AZSBSCHEME, azfp);
    currMan.addProvider("file", new DefaultLocalFileProvider());
    currMan.init();/*  w  ww .java2s  .c  o  m*/

    mgr = currMan;
    //cwd = mgr.resolveFile(System.getProperty("user.dir"));c
    String currAzURL = String.format("%s://%s/%s/%s", AzConstants.AZSBSCHEME, accnt, cont, currFileNameStr);
    cwd = mgr.resolveFile(currAzURL);

    completors.add(new FileNameCompleter());
    completors.add(new StringsCompleter(AzConstants.AZSBSCHEME, "file://", currAzURL));
    AggregateCompleter aggComp = new AggregateCompleter(completors);
    ArgumentCompleter argComp = new ArgumentCompleter(aggComp);
    argComp.setStrict(false);
    conReader.addCompleter(argComp);

    Path histPath = Paths.get(System.getProperty("user.home"), ".simpleshellhist");
    File histFile = histPath.toFile();
    FileHistory fh = new FileHistory(histFile);
    conReader.setHistory(fh);
    conReader.setHistoryEnabled(true);

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                ((FileHistory) conReader.getHistory()).flush();
            } catch (IOException ex) {
                log.error("Error saving history", ex);
            }
        }
    });

}

From source file:de.blizzy.backup.vfs.RemoteLocation.java

private FileSystemOptions getFileSystemOptions() throws FileSystemException {
    FileSystemOptions opts = new FileSystemOptions();
    setFileSystemOptions(opts);/* w w w . j a  v a 2s  .c o m*/
    if (StringUtils.isNotBlank(login) && StringUtils.isNotBlank(password)) {
        UserAuthenticator authenticator = new StaticUserAuthenticator(null, login, password);
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, authenticator);
    }
    return opts;
}

From source file:com.sludev.commons.vfs2.provider.s3.SS3FileProviderTest.java

/**
 * Upload a single file to the test bucket.
 * @throws java.lang.Exception// w w w.  java  2s .  c  o m
 */
@Test
public void A001_uploadFile() throws Exception {
    String currAccountStr = testProperties.getProperty("s3.access.id");
    String currKey = testProperties.getProperty("s3.access.secret");
    String currContainerStr = testProperties.getProperty("s3.test0001.bucket.name");
    String currHost = testProperties.getProperty("s3.host");
    String currRegion = testProperties.getProperty("s3.region");
    String currFileNameStr;

    File temp = File.createTempFile("uploadFile01", ".tmp");
    try (FileWriter fw = new FileWriter(temp)) {
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append("testing...");
        bw.flush();
    }

    SS3FileProvider currSS3 = new SS3FileProvider();

    // Optional set endpoint
    //currSS3.setEndpoint(currHost);

    // Optional set region
    //currSS3.setRegion(currRegion);

    DefaultFileSystemManager currMan = new DefaultFileSystemManager();
    currMan.addProvider(SS3Constants.S3SCHEME, currSS3);
    currMan.addProvider("file", new DefaultLocalFileProvider());
    currMan.init();

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", currAccountStr, currKey);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

    currFileNameStr = "test01.tmp";
    String currUriStr = String.format("%s://%s/%s/%s", SS3Constants.S3SCHEME, currHost, currContainerStr,
            currFileNameStr);
    FileObject currFile = currMan.resolveFile(currUriStr, opts);
    FileObject currFile2 = currMan.resolveFile(String.format("file://%s", temp.getAbsolutePath()));

    currFile.copyFrom(currFile2, Selectors.SELECT_SELF);
    temp.delete();
}