org.apache.commons.vfs2.UserAuthenticator Java Examples

The following examples show how to use org.apache.commons.vfs2.UserAuthenticator. 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: SimpleJsonExtractor.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
public SimpleJsonExtractor(WorkUnitState workUnitState) throws FileSystemException {
  this.workUnitState = workUnitState;

  // Resolve the file to pull
  if (workUnitState.getPropAsBoolean(ConfigurationKeys.SOURCE_CONN_USE_AUTHENTICATION, false)) {
    // Add authentication credential if authentication is needed
    UserAuthenticator auth =
        new StaticUserAuthenticator(workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_DOMAIN, ""),
            workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_USERNAME), PasswordManager.getInstance(workUnitState)
                .readPassword(workUnitState.getProp(ConfigurationKeys.SOURCE_CONN_PASSWORD)));
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
    this.fileObject = VFS.getManager().resolveFile(workUnitState.getProp(SOURCE_FILE_KEY), opts);
  } else {
    this.fileObject = VFS.getManager().resolveFile(workUnitState.getProp(SOURCE_FILE_KEY));
  }

  // Open the file for reading
  LOGGER.info("Opening file " + this.fileObject.getURL().toString());
  this.bufferedReader =
      this.closer.register(new BufferedReader(new InputStreamReader(this.fileObject.getContent().getInputStream(),
          ConfigurationKeys.DEFAULT_CHARSET_ENCODING)));
}
 
Example #2
Source File: S3FileSystemTest.java    From hop with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetS3Service() throws Exception {
  assertNotNull( fileSystem.getS3Client() );

  FileSystemOptions options = new FileSystemOptions();
  UserAuthenticator authenticator = mock( UserAuthenticator.class );
  UserAuthenticationData authData = mock( UserAuthenticationData.class );
  when( authenticator.requestAuthentication( S3FileProvider.AUTHENTICATOR_TYPES ) ).thenReturn( authData );
  when( authData.getData( UserAuthenticationData.USERNAME ) ).thenReturn( "username".toCharArray() );
  when( authData.getData( UserAuthenticationData.PASSWORD ) ).thenReturn( "password".toCharArray() );
  DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator( options, authenticator );

  fileSystem = new S3FileSystem( fileName, options );
  assertNotNull( fileSystem.getS3Client() );
}
 
Example #3
Source File: StaticUserAuthenticatorTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Test
public void testEquality() {
    final UserAuthenticator userAuthenticator = new StaticUserAuthenticator("DOMAIN", "USER", "PWD");
    assertEquals(new StaticUserAuthenticator("DOMAIN", "USER", "PWD"), userAuthenticator);
    assertNotEquals(new StaticUserAuthenticator("DOMAIN", "USER", null), userAuthenticator);
    assertNotEquals(new StaticUserAuthenticator("DOMAIN", null, "PWD"), userAuthenticator);
    assertNotEquals(new StaticUserAuthenticator(null, "USER", "PWD"), userAuthenticator);
    assertEquals(new StaticUserAuthenticator("DOMAIN", "USER", "PWD").hashCode(), userAuthenticator.hashCode());
}
 
Example #4
Source File: StaticUserAuthenticatorTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthenticationRequest() {
    final UserAuthenticator userAuthenticator = new StaticUserAuthenticator("DOMAIN", "USER", "PWD");
    UserAuthenticationData authenticationData = userAuthenticator
            .requestAuthentication(ArrayUtils.toArray(UserAuthenticationData.DOMAIN));
    assertArrayEquals("DOMAIN".toCharArray(), authenticationData.getData(UserAuthenticationData.DOMAIN));
    assertNull(authenticationData.getData(UserAuthenticationData.USERNAME));
    assertNull(authenticationData.getData(UserAuthenticationData.PASSWORD));
    authenticationData = userAuthenticator.requestAuthentication(
            ArrayUtils.toArray(UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD));
    assertNull(authenticationData.getData(UserAuthenticationData.DOMAIN));
    assertArrayEquals("USER".toCharArray(), authenticationData.getData(UserAuthenticationData.USERNAME));
    assertArrayEquals("PWD".toCharArray(), authenticationData.getData(UserAuthenticationData.PASSWORD));
}
 
Example #5
Source File: UserAuthenticatorUtils.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Authenticates if there is an authenticator, else returns null.
 *
 * @param auth The UserAuthenticator.
 * @param authenticatorTypes An array of types describing the data to be retrieved.
 * @return A UserAuthenticationData object containing the data requested.
 */
public static UserAuthenticationData authenticate(final UserAuthenticator auth,
        final UserAuthenticationData.Type[] authenticatorTypes) {
    if (auth == null) {
        return null;
    }

    return auth.requestAuthentication(authenticatorTypes);
}
 
Example #6
Source File: S3NFileSystemTest.java    From hop with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetS3Service() throws Exception {
  assertNotNull( fileSystem.getS3Client() );

  FileSystemOptions options = new FileSystemOptions();
  UserAuthenticator authenticator = mock( UserAuthenticator.class );
  UserAuthenticationData authData = mock( UserAuthenticationData.class );
  when( authenticator.requestAuthentication( S3FileProvider.AUTHENTICATOR_TYPES ) ).thenReturn( authData );
  when( authData.getData( UserAuthenticationData.USERNAME ) ).thenReturn( "username".toCharArray() );
  when( authData.getData( UserAuthenticationData.PASSWORD ) ).thenReturn( "password".toCharArray() );
  DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator( options, authenticator );

  fileSystem = new S3NFileSystem( fileName, options );
  assertNotNull( fileSystem.getS3Client() );
}
 
Example #7
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;
}
 
Example #8
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 #9
Source File: Http5FileSystemConfigBuilder.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the proxy authenticator where the system should get the credentials from.
 *
 * @param opts The FileSystem options.
 * @return The UserAuthenticator.
 */
public UserAuthenticator getProxyAuthenticator(final FileSystemOptions opts) {
    return (UserAuthenticator) getParam(opts, "proxyAuthenticator");
}
 
Example #10
Source File: HttpFileSystemConfigBuilder.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the proxy authenticator where the system should get the credentials from.
 *
 * @param opts The FileSystem options.
 * @param authenticator The UserAuthenticator.
 */
public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) {
    setParam(opts, "proxyAuthenticator", authenticator);
}
 
Example #11
Source File: HttpFileSystemConfigBuilder.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the proxy authenticator where the system should get the credentials from.
 *
 * @param opts The FileSystem options.
 * @return The UserAuthenticator.
 */
public UserAuthenticator getProxyAuthenticator(final FileSystemOptions opts) {
    return (UserAuthenticator) getParam(opts, "proxyAuthenticator");
}
 
Example #12
Source File: Http4FileSystemConfigBuilder.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the proxy authenticator where the system should get the credentials from.
 *
 * @param opts The FileSystem options.
 * @param authenticator The UserAuthenticator.
 */
public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) {
    setParam(opts, "proxyAuthenticator", authenticator);
}
 
Example #13
Source File: Http4FileSystemConfigBuilder.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the proxy authenticator where the system should get the credentials from.
 *
 * @param opts The FileSystem options.
 * @return The UserAuthenticator.
 */
public UserAuthenticator getProxyAuthenticator(final FileSystemOptions opts) {
    return (UserAuthenticator) getParam(opts, "proxyAuthenticator");
}
 
Example #14
Source File: UserAuthenticatorUtils.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Authenticates if there is an authenticator, else returns null.
 *
 * @param opts The FileSystemOptions.
 * @param authenticatorTypes An array of types describing the data to be retrieved.
 * @return A UserAuthenticationData object containing the data requested.
 */
public static UserAuthenticationData authenticate(final FileSystemOptions opts,
        final UserAuthenticationData.Type[] authenticatorTypes) {
    final UserAuthenticator auth = DefaultFileSystemConfigBuilder.getInstance().getUserAuthenticator(opts);
    return authenticate(auth, authenticatorTypes);
}
 
Example #15
Source File: Http5FileSystemConfigBuilder.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the proxy authenticator where the system should get the credentials from.
 *
 * @param opts The FileSystem options.
 * @param authenticator The UserAuthenticator.
 */
public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) {
    setParam(opts, "proxyAuthenticator", authenticator);
}
 
Example #16
Source File: DefaultFileSystemConfigBuilder.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the user authenticator to get authentication informations.
 *
 * @param opts The FileSystemOptions.
 * @param userAuthenticator The UserAuthenticator.
 * @throws FileSystemException if an error occurs setting the UserAuthenticator.
 */
public void setUserAuthenticator(final FileSystemOptions opts, final UserAuthenticator userAuthenticator)
        throws FileSystemException {
    setParam(opts, "userAuthenticator", userAuthenticator);
}
 
Example #17
Source File: DefaultFileSystemConfigBuilder.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * @see #setUserAuthenticator
 * @param opts The FileSystemOptions.
 * @return The UserAuthenticator.
 */
public UserAuthenticator getUserAuthenticator(final FileSystemOptions opts) {
    return (UserAuthenticator) getParam(opts, "userAuthenticator");
}