org.apache.commons.vfs2.util.UserAuthenticatorUtils Java Examples

The following examples show how to use org.apache.commons.vfs2.util.UserAuthenticatorUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PentahoSolutionFileProvider.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private FileSystem createJCRFileSystem( final LayeredFileName genericRootName,
                                        final FileSystemOptions fileSystemOptions ) {
  UserAuthenticationData authData = null;
  try {
    authData = UserAuthenticatorUtils.authenticate( fileSystemOptions, AUTHENTICATOR_TYPES );
    final GenericFileName outerName = (GenericFileName) genericRootName.getOuterName();

    final String username = UserAuthenticatorUtils.toString( UserAuthenticatorUtils
      .getData( authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar( outerName.getUserName() ) ) );

    final String password = UserAuthenticatorUtils.toString( UserAuthenticatorUtils
      .getData( authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar( outerName.getPassword() ) ) );
    final PentahoSolutionsFileSystemConfigBuilder configBuilder = new PentahoSolutionsFileSystemConfigBuilder();
    final int timeOut = configBuilder.getTimeOut( fileSystemOptions );

    final JCRSolutionFileModel model = new JCRSolutionFileModel( outerName.getURI(), username, password, timeOut );
    return new JCRSolutionFileSystem( genericRootName, fileSystemOptions, model );
  } finally {
    UserAuthenticatorUtils.cleanup( authData );
  }
}
 
Example #2
Source File: WebdavFileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link FileSystem}.
 * <p>
 * If you're looking at this method and wondering how to get a FileSystemOptions object bearing the proxy host and
 * credentials configuration through to this method so it's used for resolving a
 * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure to use correct signature of
 * the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} resolveFile method.
 * </p>
 *
 * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
 */
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    // Create the file system
    final GenericFileName rootName = (GenericFileName) name;
    final FileSystemOptions fsOpts = fileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;

    UserAuthenticationData authData = null;
    HttpClient httpClient;
    try {
        authData = UserAuthenticatorUtils.authenticate(fsOpts, AUTHENTICATOR_TYPES);

        httpClient = HttpClientFactory.createConnection(WebdavFileSystemConfigBuilder.getInstance(), "http",
                rootName.getHostName(), rootName.getPort(),
                UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                        UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
                UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                        UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
                fsOpts);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return new WebdavFileSystem(rootName, httpClient, fsOpts);
}
 
Example #3
Source File: StaticUserAuthenticator.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
public UserAuthenticationData requestAuthentication(final UserAuthenticationData.Type[] types) {
    final UserAuthenticationData data = new UserAuthenticationData();
    for (final UserAuthenticationData.Type type : types) {
        if (type == UserAuthenticationData.DOMAIN) {
            data.setData(UserAuthenticationData.DOMAIN, UserAuthenticatorUtils.toChar(domain));
        } else if (type == UserAuthenticationData.USERNAME) {
            data.setData(UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(username));
        } else if (type == UserAuthenticationData.PASSWORD) {
            data.setData(UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(password));
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug(StaticUserAuthenticator.class.getSimpleName()
                        + " does not support authentication data type '" + type
                        + "'; authentication request for this type ignored.");
            }
        }
    }
    return data;
}
 
Example #4
Source File: Http5FileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    final GenericFileName rootName = (GenericFileName) name;

    UserAuthenticationData authData = null;
    HttpClient httpClient = null;
    HttpClientContext httpClientContext = null;

    try {
        final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance();
        authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
        httpClientContext = createHttpClientContext(builder, rootName, fileSystemOptions, authData);
        httpClient = createHttpClient(builder, rootName, fileSystemOptions);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return new Http5FileSystem(rootName, fileSystemOptions, httpClient, httpClientContext);
}
 
Example #5
Source File: SftpFileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new Session.
 *
 * @return A Session, never null.
 */
static Session createSession(final GenericFileName rootName, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    UserAuthenticationData authData = null;
    try {
        authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);

        return SftpClientFactory.createConnection(rootName.getHostName(), rootName.getPort(),
                UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME,
                        UserAuthenticatorUtils.toChar(rootName.getUserName())),
                UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD,
                        UserAuthenticatorUtils.toChar(rootName.getPassword())),
                fileSystemOptions);
    } catch (final Exception e) {
        throw new FileSystemException("vfs.provider.sftp/connect.error", rootName, e);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }
}
 
Example #6
Source File: Http4FileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    final GenericFileName rootName = (GenericFileName) name;

    UserAuthenticationData authData = null;
    HttpClient httpClient = null;
    HttpClientContext httpClientContext = null;

    try {
        final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
        authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
        httpClientContext = createHttpClientContext(builder, rootName, fileSystemOptions, authData);
        httpClient = createHttpClient(builder, rootName, fileSystemOptions);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return new Http4FileSystem(rootName, fileSystemOptions, httpClient, httpClientContext);
}
 
Example #7
Source File: Webdav4sFileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link FileSystem}.
 * <p>
 * If you're looking at this method and wondering how to get a FileSystemOptions object bearing the proxy host and
 * credentials configuration through to this method so it's used for resolving a
 * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure to use correct signature of
 * the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} resolveFile method.
 *
 * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
 */
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    // Create the file system
    final GenericFileName rootName = (GenericFileName) name;
    // TODO: need to check null to create a non-null here???
    final FileSystemOptions fsOpts = fileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;

    UserAuthenticationData authData = null;
    HttpClient httpClient = null;
    HttpClientContext httpClientContext = null;

    try {
        final Webdav4FileSystemConfigBuilder builder = Webdav4FileSystemConfigBuilder.getInstance();
        authData = UserAuthenticatorUtils.authenticate(fsOpts, Webdav4FileProvider.AUTHENTICATOR_TYPES);
        httpClientContext = createHttpClientContext(builder, rootName, fsOpts, authData);
        httpClient = createHttpClient(builder, rootName, fsOpts);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return new Webdav4FileSystem(rootName, fsOpts, httpClient, httpClientContext) {
    };
}
 
Example #8
Source File: Webdav4FileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link FileSystem}.
 * <p>
 * If you're looking at this method and wondering how to get a FileSystemOptions object bearing the proxy host and
 * credentials configuration through to this method so it's used for resolving a
 * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure to use correct signature of
 * the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} resolveFile method.
 *
 * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
 */
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    // Create the file system
    final GenericFileName rootName = (GenericFileName) name;
    // TODO: need to check null to create a non-null here???
    final FileSystemOptions fsOpts = fileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;

    UserAuthenticationData authData = null;
    HttpClient httpClient = null;
    HttpClientContext httpClientContext = null;

    try {
        final Webdav4FileSystemConfigBuilder builder = Webdav4FileSystemConfigBuilder.getInstance();
        authData = UserAuthenticatorUtils.authenticate(fsOpts, AUTHENTICATOR_TYPES);
        httpClientContext = createHttpClientContext(builder, rootName, fsOpts, authData);
        httpClient = createHttpClient(builder, rootName, fsOpts);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return new Webdav4FileSystem(rootName, fsOpts, httpClient, httpClientContext);
}
 
Example #9
Source File: FTPClientWrapper.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private FTPClient createClient() throws FileSystemException {
    final GenericFileName rootName = getRoot();

    UserAuthenticationData authData = null;
    try {
        authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, FtpFileProvider.AUTHENTICATOR_TYPES);

        return createClient(rootName, authData);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }
}
 
Example #10
Source File: FTPClientWrapper.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
protected FTPClient createClient(final GenericFileName rootName, final UserAuthenticationData authData)
        throws FileSystemException {
    return FtpClientFactory.createConnection(rootName.getHostName(), rootName.getPort(),
            UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME,
                    UserAuthenticatorUtils.toChar(rootName.getUserName())),
            UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD,
                    UserAuthenticatorUtils.toChar(rootName.getPassword())),
            rootName.getPath(), getFileSystemOptions());
}
 
Example #11
Source File: HttpFileProvider.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link FileSystem}.
 */
@Override
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    // Create the file system
    final GenericFileName rootName = (GenericFileName) name;

    UserAuthenticationData authData = null;
    HttpClient httpClient;
    try {
        authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);

        final String fileScheme = rootName.getScheme();
        final char lastChar = fileScheme.charAt(fileScheme.length() - 1);
        final String internalScheme = (lastChar == 's' || lastChar == 'S') ? "https" : "http";

        httpClient = HttpClientFactory.createConnection(internalScheme, rootName.getHostName(),
                rootName.getPort(),
                UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                        UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
                UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                        UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
                fileSystemOptions);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return new HttpFileSystem(rootName, httpClient, fileSystemOptions);
}
 
Example #12
Source File: FtpsClientWrapper.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Override
protected FTPClient createClient(final GenericFileName rootName, final UserAuthenticationData authData)
        throws FileSystemException {
    return FtpsClientFactory.createConnection(rootName.getHostName(), rootName.getPort(),
            UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME,
                    UserAuthenticatorUtils.toChar(rootName.getUserName())),
            UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD,
                    UserAuthenticatorUtils.toChar(rootName.getPassword())),
            rootName.getPath(), getFileSystemOptions());
}
 
Example #13
Source File: Http5FileProvider.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Create an {@link HttpClientContext} object for an http4 file system.
 *
 * @param builder Configuration options builder for http4 provider
 * @param rootName The root path
 * @param fileSystemOptions The FileSystem options
 * @param authData The {@code UserAuthentiationData} object
 * @return an {@link HttpClientContext} object
 * @throws FileSystemException if an error occurs
 */
protected HttpClientContext createHttpClientContext(final Http5FileSystemConfigBuilder builder,
        final GenericFileName rootName, final FileSystemOptions fileSystemOptions,
        final UserAuthenticationData authData) throws FileSystemException {

    final HttpClientContext clientContext = HttpClientContext.create();
    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    clientContext.setCredentialsProvider(credsProvider);

    final String username = UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
            UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())));
    final char[] password = UserAuthenticatorUtils.getData(authData,
            UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()));

    if (username != null && !username.isEmpty()) {
        // -1 for any port
        credsProvider.setCredentials(new AuthScope(rootName.getHostName(), -1),
                new UsernamePasswordCredentials(username, password));
    }

    final HttpHost proxyHost = getProxyHttpHost(builder, fileSystemOptions);

    if (proxyHost != null) {
        final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);

        if (proxyAuth != null) {
            final UserAuthenticationData proxyAuthData = UserAuthenticatorUtils.authenticate(proxyAuth,
                    new UserAuthenticationData.Type[] { UserAuthenticationData.USERNAME,
                            UserAuthenticationData.PASSWORD });

            if (proxyAuthData != null) {
                final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials(
                        UserAuthenticatorUtils.toString(
                                UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, null)),
                        UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null));

                // -1 for any port
                credsProvider.setCredentials(new AuthScope(proxyHost.getHostName(), -1),
                        proxyCreds);
            }

            if (builder.isPreemptiveAuth(fileSystemOptions)) {
                final AuthCache authCache = new BasicAuthCache();
                final BasicScheme basicAuth = new BasicScheme();
                authCache.put(proxyHost, basicAuth);
                clientContext.setAuthCache(authCache);
            }
        }
    }

    return clientContext;
}
 
Example #14
Source File: Http4FileProvider.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Create an {@link HttpClientContext} object for an http4 file system.
 *
 * @param builder Configuration options builder for http4 provider
 * @param rootName The root path
 * @param fileSystemOptions The FileSystem options
 * @param authData The {@code UserAuthentiationData} object
 * @return an {@link HttpClientContext} object
 * @throws FileSystemException if an error occurs
 */
protected HttpClientContext createHttpClientContext(final Http4FileSystemConfigBuilder builder,
        final GenericFileName rootName, final FileSystemOptions fileSystemOptions,
        final UserAuthenticationData authData) throws FileSystemException {

    final HttpClientContext clientContext = HttpClientContext.create();
    final CredentialsProvider credsProvider = new BasicCredentialsProvider();
    clientContext.setCredentialsProvider(credsProvider);

    final String username = UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
            UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())));
    final String password = UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
            UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword())));

    if (username != null && !username.isEmpty()) {
        credsProvider.setCredentials(new AuthScope(rootName.getHostName(), AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(username, password));
    }

    final HttpHost proxyHost = getProxyHttpHost(builder, fileSystemOptions);

    if (proxyHost != null) {
        final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);

        if (proxyAuth != null) {
            final UserAuthenticationData proxyAuthData = UserAuthenticatorUtils.authenticate(proxyAuth,
                    new UserAuthenticationData.Type[] { UserAuthenticationData.USERNAME,
                            UserAuthenticationData.PASSWORD });

            if (proxyAuthData != null) {
                final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials(
                        UserAuthenticatorUtils.toString(
                                UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, null)),
                        UserAuthenticatorUtils.toString(
                                UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null)));

                credsProvider.setCredentials(new AuthScope(proxyHost.getHostName(), AuthScope.ANY_PORT),
                        proxyCreds);
            }

            if (builder.isPreemptiveAuth(fileSystemOptions)) {
                final AuthCache authCache = new BasicAuthCache();
                final BasicScheme basicAuth = new BasicScheme();
                authCache.put(proxyHost, basicAuth);
                clientContext.setAuthCache(authCache);
            }
        }
    }

    return clientContext;
}