Example usage for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials

List of usage examples for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials

Introduction

In this page you can find the example usage for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials.

Prototype

public UsernamePasswordCredentials(final String userName, final String password) 

Source Link

Document

The constructor with the username and password arguments.

Usage

From source file:com.sayar.requests.auth.HttpBasicAuthentication.java

public Header getHttpHeader(final HttpUriRequest request) throws AuthenticationException {
    final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(this.username, this.password);
    return new BasicScheme().authenticate(creds, request);
}

From source file:org.nekorp.workflow.desktop.rest.util.RestTemplateFactory.java

@PostConstruct
public void init() {
    targetHost = new HttpHost(host, port, protocol);
    //connectionPool = new PoolingHttpClientConnectionManager();
    //connectionPool.setDefaultMaxPerRoute(10);
    //connectionPool.setMaxTotal(20);

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(username, password));
    //wildcard ssl certificate
    SSLContext sslContext = SSLContexts.createDefault();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
            NoopHostnameVerifier.INSTANCE);

    httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
            //.setConnectionManager(connectionPool)
            .setSSLSocketFactory(sslsf).build();
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local
    // auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);

    // Add AuthCache to the execution context
    HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);

    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactoryBasicAuth(
            httpclient, localContext);//from   w  w  w.j av  a2 s .co m
    this.template = new RestTemplate();
    template.getMessageConverters().add(new BufferedImageHttpMessageConverter());
    template.setRequestFactory(factory);
}

From source file:com.ccreanga.bitbucket.rest.client.http.BitBucketHttpExecutor.java

public BitBucketHttpExecutor(String baseUrl, BitBucketCredentials credentials) {
    this.baseUrl = baseUrl;

    HttpHost targetHost = HttpHost.create(baseUrl);
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(5);/* ww  w .  j  a v a2s .c o  m*/
    connectionManager.setDefaultMaxPerRoute(4);

    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(targetHost),
            new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword()));

    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(targetHost, basicAuth);

    context = HttpClientContext.create();
    context.setCredentialsProvider(credentialsProvider);
    context.setAuthCache(authCache);

    httpClient = HttpClients.custom().setConnectionManager(connectionManager)
            .setDefaultCredentialsProvider(credentialsProvider).build();

}

From source file:org.dmfs.android.authenticator.handlers.BasicHttpClientAuthenticationHandler.java

@Override
public void authenticate(AbstractHttpClient client) {
    // just set the credentials assuming that proper authentication schemes are registered with the AbstractHttpClient.
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, mAuthToken.getRealm()),
            new UsernamePasswordCredentials(mAuthToken.getUsername(), mAuthToken.getPassword()));

    client.setCredentialsProvider(credsProvider);
}

From source file:com.apm4all.tracy.TracyAsyncHttpClientPublisher.java

TracyAsyncHttpClientPublisher(String hostname, int port, boolean waitForResponse, String resourcePath,
        HttpProxyConfig httpProxyConfig, boolean debug) {
    this.uri = "http://" + hostname + ":" + port + "/" + resourcePath;
    this.waitForResponse = waitForResponse;
    this.httpProxyConfig = httpProxyConfig;
    this.debug = debug;
    if (httpProxyConfig.isEnabled()) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(httpProxyConfig.getHost(), httpProxyConfig.getPort()),
                new UsernamePasswordCredentials(httpProxyConfig.getUsername(), httpProxyConfig.getPassword()));
        this.httpClient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider).build();

    } else {//  w w w  . j  a  v a  2  s  .c o m
        this.httpClient = HttpAsyncClients.custom().build();
    }
    this.httpClient.start();
}

From source file:org.zenoss.metrics.reporter.HttpPoster.java

private HttpPoster(final URL url, final String user, final String password, ObjectMapper mapper) {
    this.url = url;
    this.mapper = mapper;
    if (!Strings.nullToEmpty(user).trim().isEmpty()) {
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()),
                new UsernamePasswordCredentials(user, password));
        this.needsAuth = true;
    } else {//w  w  w  .  j a va 2  s. c  om
        this.needsAuth = false;
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.util.BasicAuthHttpClient.java

public BasicAuthHttpClient(URL url, String username, String password, ThreadSafeClientConnManager connManager) {
    super(connManager);
    this.url = url;

    BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(url.getHost(), AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(username, password));
    setCredentialsProvider(credsProvider);
}

From source file:com.microsoft.alm.plugin.authentication.AuthHelper.java

/**
 * Returns the NTCredentials or UsernamePasswordCredentials object
 *
 * @param type//from w  ww .j  a v a 2 s .co m
 * @param authenticationInfo
 * @return
 */
public static Credentials getCredentials(final ServerContext.Type type,
        final AuthenticationInfo authenticationInfo) {
    if (type == ServerContext.Type.TFS) {
        return getNTCredentials(authenticationInfo.getUserName(), authenticationInfo.getPassword());
    } else {
        return new UsernamePasswordCredentials(authenticationInfo.getUserName(),
                authenticationInfo.getPassword());
    }
}

From source file:org.commonjava.couch.io.CouchHttpClient.java

@PostConstruct
private void setupClient() {
    final ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager();
    ccm.setMaxTotal(config.getMaxConnections());

    final DefaultHttpClient c = new DefaultHttpClient(ccm);

    if (config.getDatabaseUser() != null) {
        final AuthScope scope = new AuthScope(config.getDatabaseHost(), config.getDatabasePort());
        final UsernamePasswordCredentials cred = new UsernamePasswordCredentials(config.getDatabaseUser(),
                config.getDatabasePassword());

        c.getCredentialsProvider().setCredentials(scope, cred);
    }//from  w  ww .j  av a2s  .c om

    client = c;
}

From source file:com.puppetlabs.geppetto.injectable.eclipse.impl.EclipseHttpClientProvider.java

@Override
public HttpClient get() {
    HttpParams params = new BasicHttpParams();
    if (connectonTimeout != null)
        HttpConnectionParams.setConnectionTimeout(params, connectonTimeout.intValue());
    if (soTimeout != null)
        HttpConnectionParams.setSoTimeout(params, soTimeout.intValue());

    DefaultHttpClient httpClient = new DefaultHttpClient(params);

    final SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
    if (sslSocketFactory != null)
        schemeRegistry.register(new Scheme("https", 443, sslSocketFactory));

    httpClient.setRoutePlanner(new ProxiedRoutePlanner(schemeRegistry));
    for (IProxyData proxyData : Activator.getInstance().getProxyService().getProxyData()) {
        String user = proxyData.getUserId();
        String pwd = proxyData.getPassword();
        if (user != null || pwd != null)
            httpClient.getCredentialsProvider().setCredentials(
                    new AuthScope(proxyData.getHost(), proxyData.getPort()),
                    new UsernamePasswordCredentials(user, pwd));
    }//www. j  av  a 2s .c om
    return httpClient;
}