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:org.kalypso.commons.io.VFSUtilities.java

/**
 * This function will check the string for protocol, and if neccessary applys an proxy object to it.
 *
 * @param absoluteFile//from  w w w  .  ja va2s.c  om
 *          The absolute file path to the file. It should be absolute, because this function was not testet against
 *          relative files.
 * @return The file object.
 */
public static FileObject checkProxyFor(final String absoluteFile, final FileSystemManager fsManager)
        throws FileSystemException {
    final Proxy proxy = ProxyUtilities.getProxy();
    KalypsoCommonsDebug.DEBUG.printf("Should use proxy: %s%n", String.valueOf(proxy.useProxy())); //$NON-NLS-1$

    // TODO: VFS usually accepts file-pathes (without protocoll), but creating an url here will prohibit this.
    // TODO: handle file pathes differently
    if (proxy.useProxy() && !ProxyUtilities.isNonProxyHost(absoluteFile)) {
        final String proxyHost = proxy.getProxyHost();
        final int proxyPort = proxy.getProxyPort();
        KalypsoCommonsDebug.DEBUG.printf("Proxy host: %s%n", proxyHost); //$NON-NLS-1$
        KalypsoCommonsDebug.DEBUG.printf("Proxy port: %s%n", String.valueOf(proxyPort)); //$NON-NLS-1$

        /* Get the credentials. */
        final String user = proxy.getUser();
        final String password = proxy.getPassword();

        final Pattern p = Pattern.compile("(.+)://.+"); //$NON-NLS-1$
        final Matcher m = p.matcher(absoluteFile);
        if (m.find() == true) {
            KalypsoCommonsDebug.DEBUG.printf("File: %s%n", absoluteFile); //$NON-NLS-1$
            KalypsoCommonsDebug.DEBUG.printf("Protocol: %s%n", m.group(1)); //$NON-NLS-1$

            if (m.group(1).equals("webdav")) //$NON-NLS-1$
            {
                WebdavFileSystemConfigBuilder.getInstance().setProxyHost(THE_WEBDAV_OPTIONS, proxyHost);
                WebdavFileSystemConfigBuilder.getInstance().setProxyPort(THE_WEBDAV_OPTIONS, proxyPort);

                /* If there are credentials given, set them. */
                if (user != null && password != null) {
                    final UserAuthenticator authenticator = new StaticUserAuthenticator(null, user, password);
                    WebdavFileSystemConfigBuilder.getInstance().setProxyAuthenticator(THE_WEBDAV_OPTIONS,
                            authenticator);
                }

                return fsManager.resolveFile(absoluteFile, THE_WEBDAV_OPTIONS);
            } else if (m.group(1).equals("http")) //$NON-NLS-1$
            {
                HttpFileSystemConfigBuilder.getInstance().setProxyHost(THE_HTTP_OPTIONS, proxyHost);
                HttpFileSystemConfigBuilder.getInstance().setProxyPort(THE_HTTP_OPTIONS, proxyPort);

                /* If there are credentials given, set them. */
                if (user != null && password != null) {
                    final UserAuthenticator authenticator = new StaticUserAuthenticator(null, user, password);
                    HttpFileSystemConfigBuilder.getInstance().setProxyAuthenticator(THE_HTTP_OPTIONS,
                            authenticator);
                }

                return fsManager.resolveFile(absoluteFile, THE_HTTP_OPTIONS);
            } else if (m.group(1).equals("https")) //$NON-NLS-1$
            {
                HttpFileSystemConfigBuilder.getInstance().setProxyHost(THE_HTTPS_OPTIONS, proxyHost);
                HttpFileSystemConfigBuilder.getInstance().setProxyPort(THE_HTTPS_OPTIONS, proxyPort);

                /* If there are credentials given, set them. */
                if (user != null && password != null) {
                    final UserAuthenticator authenticator = new StaticUserAuthenticator(null, user, password);
                    HttpFileSystemConfigBuilder.getInstance().setProxyAuthenticator(THE_HTTPS_OPTIONS,
                            authenticator);
                }

                return fsManager.resolveFile(absoluteFile, THE_HTTPS_OPTIONS);
            }
        }
    }

    return fsManager.resolveFile(absoluteFile);
}

From source file:org.pentaho.amazon.AbstractAmazonJobExecutor.java

private String getS3FileObjectPath() throws FileSystemException, KettleFileException {
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts,
            new StaticUserAuthenticator(null, getAWSAccessKeyId(), getAWSSecretKey()));
    FileObject stagingDirFileObject = KettleVFS.getFileObject(stagingDir, getVariables(), opts);

    return stagingDirFileObject.getName().getPath();
}

From source file:org.pentaho.amazon.AbstractAmazonJobExecutorController.java

protected FileSystemOptions getFileSystemOptions() throws FileSystemException {
    FileSystemOptions opts = new FileSystemOptions();

    if (!Const.isEmpty(getAccessKey()) || !Const.isEmpty(getSecretKey())) {
        // create a FileSystemOptions with user & password
        StaticUserAuthenticator userAuthenticator = new StaticUserAuthenticator(null,
                getVariableSpace().environmentSubstitute(getAccessKey()),
                getVariableSpace().environmentSubstitute(getSecretKey()));

        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, userAuthenticator);
    }//from w  w  w .  j  av  a  2s  .c  om
    return opts;
}

From source file:org.pentaho.amazon.AbstractAmazonJobExecutorController.java

public FileObject resolveFile(String fileUri) throws FileSystemException, KettleFileException {
    VariableSpace vs = getVariableSpace();
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts,
            new StaticUserAuthenticator(null, getAccessKey(), getSecretKey()));
    FileObject file = KettleVFS.getFileObject(fileUri, vs, opts);
    return file;//  www .  j av  a  2 s .co m
}

From source file:org.pentaho.amazon.s3.S3FileOutputDialog.java

protected FileSystemOptions getFileSystemOptions() throws FileSystemException {
    FileSystemOptions opts = new FileSystemOptions();
    AWSCredentials credentials = S3CredentialsProvider.getAWSCredentials();
    if (credentials != null) {
        StaticUserAuthenticator userAuthenticator = new StaticUserAuthenticator(null,
                credentials.getAWSAccessKeyId(), credentials.getAWSSecretKey());
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, userAuthenticator);
    }//from  w  w w  .  j  a v a2 s.c  o m
    return opts;
}

From source file:org.pentaho.amazon.s3.S3NVfsFileChooserDialog.java

private FileSystemOptions getFileSystemOptions() throws FileSystemException {
    FileSystemOptions opts = new FileSystemOptions();
    try {//from w ww  . j  av  a 2s .  c om
        AWSCredentials credentials = S3CredentialsProvider.getAWSCredentials();
        if (credentials != null) {
            StaticUserAuthenticator userAuthenticator = new StaticUserAuthenticator(null,
                    credentials.getAWSAccessKeyId(), credentials.getAWSSecretKey());
            DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, userAuthenticator);
        }
    } catch (SdkClientException e) {
        throw new FileSystemException(e);
    }
    return opts;
}

From source file:org.pentaho.amazon.s3.S3VfsFileChooserDialog.java

private FileSystemOptions getFileSystemOptions() throws FileSystemException {
    FileSystemOptions opts = new FileSystemOptions();
    AWSCredentials credentials = S3CredentialsProvider.getAWSCredentials();
    if (credentials != null) {
        StaticUserAuthenticator userAuthenticator = new StaticUserAuthenticator(null,
                credentials.getAWSAccessKeyId(), credentials.getAWSSecretKey());
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, userAuthenticator);
    }/*from  ww  w  .  j a  v  a  2  s.  co  m*/
    return opts;
}

From source file:org.pentaho.di.connections.vfs.providers.other.OtherConnectionDetailsProvider.java

@Override
public FileSystemOptions getOpts(OtherConnectionDetails otherConnectionDetails) {
    if (otherConnectionDetails == null) {
        return null;
    }//from w  w  w.  j ava 2 s  .  c o  m
    StaticUserAuthenticator auth = new StaticUserAuthenticator(otherConnectionDetails.getHost(),
            otherConnectionDetails.getUsername(), otherConnectionDetails.getPassword());
    FileSystemOptions opts = new FileSystemOptions();
    try {
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
    } catch (FileSystemException fse) {
        // Ignore and return default options
    }
    return opts;
}

From source file:org.pentaho.reporting.designer.extensions.pentaho.repository.util.PublishUtil.java

public static FileObject createVFSConnection(final FileSystemManager fileSystemManager,
        final AuthenticationData loginData) throws FileSystemException {
    if (fileSystemManager == null) {
        throw new NullPointerException();
    }//  www  .  ja v  a  2s  .c om
    if (loginData == null) {
        throw new NullPointerException();
    }

    final String versionText = loginData.getOption(SERVER_VERSION);
    final int version = ParserUtil.parseInt(versionText, SERVER_VERSION_SUGAR);

    final String normalizedUrl = normalizeURL(loginData.getUrl(), version);
    final FileSystemOptions fileSystemOptions = new FileSystemOptions();
    final PentahoSolutionsFileSystemConfigBuilder configBuilder = new PentahoSolutionsFileSystemConfigBuilder();
    configBuilder.setTimeOut(fileSystemOptions, getTimeout(loginData) * 1000);
    configBuilder.setUserAuthenticator(fileSystemOptions,
            new StaticUserAuthenticator(normalizedUrl, loginData.getUsername(), loginData.getPassword()));
    return fileSystemManager.resolveFile(normalizedUrl, fileSystemOptions);
}

From source file:org.schedoscope.export.ftp.upload.Uploader.java

/**
 * The constructor to initialize a user name / password (s)ftp connection.
 *
 * @param user       The username to use.
 * @param pass       The password to use.
 * @param conf       The Hadoop Configuration object, needed to initialize HDFS.
 * @param passive    A flag to use FTP passive mode (only for ftp connections).
 * @param userIsRoot A flag indicating the user dir is (s)ftp root dir.
 * @throws IOException Is thrown if an error occures.
 *//* ww w.j  a v  a 2 s  . c o  m*/
public Uploader(String user, String pass, Configuration conf, boolean passive, boolean userIsRoot)
        throws IOException {

    initFileSystem(conf, passive, userIsRoot);

    // set up authentication - user/pass for ftp and sftp
    LOG.debug("setting up user/pass authentication");

    UserAuthenticator auth = new StaticUserAuthenticator(null, user, pass);
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
}