All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nycjv321.http.CredentialsProviderBuilder Maven / Gradle / Ivy

Go to download

A simple wrapper of Apache's Http Client Library that tries to make interacting with HTTP Easy

The newest version!
package com.nycjv321.http;

import com.nycjv321.utilities.Builder;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;

/**
 * Created by fedora on 11/18/15.
 */
public class CredentialsProviderBuilder extends Builder {
    private CredentialsProvider credentialsProvider;

    public static CredentialsProviderBuilder BasicUserNamePasswordBuilder(String userName, String password) {
        CredentialsProviderBuilder builder = new CredentialsProviderBuilder();
        builder.credentialsProvider = new BasicCredentialsProvider();
        builder.credentialsProvider.setCredentials(
                AuthScope.ANY,
                createCredentials(userName, password, AUTHENTICATION_METHOD.USERNAME_PASSWORD)
        );
        return builder;
    }

    private static Credentials createCredentials(String userName, String password, AUTHENTICATION_METHOD authenticationMethod) {
        final String credentialsString = String.format("%s:%s", userName, password);
        switch (authenticationMethod) {
            case USERNAME_PASSWORD:
                return new UsernamePasswordCredentials(credentialsString);
            case NT:
                return new NTCredentials(credentialsString);
            default:
                throw new IllegalArgumentException("Invalid Authentication Method Provided");
        }
    }

    @Override
    protected CredentialsProviderBuilder getThis() {
        return this;
    }

    private enum AUTHENTICATION_METHOD {
        USERNAME_PASSWORD, NT
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy