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

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

Introduction

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

Prototype

public AuthState() 

Source Link

Usage

From source file:neembuu.release1.httpclient.NHttpClient.java

public static DefaultHttpClient getNewInstance() {
    DefaultHttpClient new_httpClient = null;
    new_httpClient = new DefaultHttpClient();
    GlobalTestSettings.ProxySettings proxySettings = GlobalTestSettings.getGlobalProxySettings();
    HttpContext context = new BasicHttpContext();
    SchemeRegistry schemeRegistry = new SchemeRegistry();

    schemeRegistry.register(new Scheme("http", new PlainSocketFactory(), 80));

    try {/* w  ww  .j av a  2s  .  c o m*/
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        schemeRegistry.register(new Scheme("https", new SSLSocketFactory(keyStore), 8080));
    } catch (Exception a) {
        a.printStackTrace(System.err);
    }

    context.setAttribute(ClientContext.SCHEME_REGISTRY, schemeRegistry);
    context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY,
            new BasicScheme()/*file.httpClient.getAuthSchemes()*/);

    context.setAttribute(ClientContext.COOKIESPEC_REGISTRY,
            new_httpClient.getCookieSpecs()/*file.httpClient.getCookieSpecs()*/
    );

    BasicCookieStore basicCookieStore = new BasicCookieStore();

    context.setAttribute(ClientContext.COOKIE_STORE, basicCookieStore/*file.httpClient.getCookieStore()*/);
    context.setAttribute(ClientContext.CREDS_PROVIDER,
            new BasicCredentialsProvider()/*file.httpClient.getCredentialsProvider()*/);

    HttpConnection hc = new DefaultHttpClientConnection();
    context.setAttribute(ExecutionContext.HTTP_CONNECTION, hc);

    //System.out.println(file.httpClient.getParams().getParameter("http.useragent"));
    HttpParams httpParams = new BasicHttpParams();

    if (proxySettings != null) {
        AuthState as = new AuthState();
        as.setCredentials(new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password));
        as.setAuthScope(AuthScope.ANY);
        as.setAuthScheme(new BasicScheme());
        httpParams.setParameter(ClientContext.PROXY_AUTH_STATE, as);
        httpParams.setParameter("http.proxy_host", new HttpHost(proxySettings.host, proxySettings.port));
    }

    new_httpClient = new DefaultHttpClient(
            new SingleClientConnManager(httpParams/*file.httpClient.getParams()*/, schemeRegistry),
            httpParams/*file.httpClient.getParams()*/);

    if (proxySettings != null) {
        new_httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password));
    }

    return new_httpClient;
}

From source file:neembuu.vfs.test.FileNameAndSizeFinderService.java

private DefaultHttpClient newClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    GlobalTestSettings.ProxySettings proxySettings = GlobalTestSettings.getGlobalProxySettings();
    HttpContext context = new BasicHttpContext();
    SchemeRegistry schemeRegistry = new SchemeRegistry();

    schemeRegistry.register(new Scheme("http", new PlainSocketFactory(), 80));

    try {/* ww w.ja  va2s  .co  m*/
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        schemeRegistry.register(new Scheme("https", new SSLSocketFactory(keyStore), 8080));
    } catch (Exception a) {
        a.printStackTrace(System.err);
    }

    context.setAttribute(ClientContext.SCHEME_REGISTRY, schemeRegistry);
    context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY,
            new BasicScheme()/*file.httpClient.getAuthSchemes()*/);

    context.setAttribute(ClientContext.COOKIESPEC_REGISTRY,
            client.getCookieSpecs()/*file.httpClient.getCookieSpecs()*/
    );

    BasicCookieStore basicCookieStore = new BasicCookieStore();

    context.setAttribute(ClientContext.COOKIE_STORE, basicCookieStore/*file.httpClient.getCookieStore()*/);
    context.setAttribute(ClientContext.CREDS_PROVIDER,
            new BasicCredentialsProvider()/*file.httpClient.getCredentialsProvider()*/);

    HttpConnection hc = new DefaultHttpClientConnection();
    context.setAttribute(ExecutionContext.HTTP_CONNECTION, hc);

    //System.out.println(file.httpClient.getParams().getParameter("http.useragent"));
    HttpParams httpParams = new BasicHttpParams();

    if (proxySettings != null) {
        AuthState as = new AuthState();
        as.setCredentials(new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password));
        as.setAuthScope(AuthScope.ANY);
        as.setAuthScheme(new BasicScheme());
        httpParams.setParameter(ClientContext.PROXY_AUTH_STATE, as);
        httpParams.setParameter("http.proxy_host", new HttpHost(proxySettings.host, proxySettings.port));
    }

    client = new DefaultHttpClient(
            new SingleClientConnManager(httpParams/*file.httpClient.getParams()*/, schemeRegistry),
            httpParams/*file.httpClient.getParams()*/);

    if (proxySettings != null) {
        client.getCredentialsProvider().setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password));
    }

    return client;
}

From source file:com.grendelscan.commons.http.apache_overrides.client.CustomClientRequestDirector.java

public CustomClientRequestDirector(final HttpRequestExecutor requestExec, final ClientConnectionManager conman,
        final ConnectionReuseStrategy reustrat, final ConnectionKeepAliveStrategy kastrat,
        final HttpRoutePlanner rouplan, final HttpProcessor httpProcessor,
        final AuthenticationHandler proxyAuthHandler, final UserTokenHandler userTokenHandler,
        final HttpParams params) {

    if (requestExec == null) {
        throw new IllegalArgumentException("Request executor may not be null.");
    }/*from   w  ww  .j a va 2s .c o  m*/
    if (conman == null) {
        throw new IllegalArgumentException("Client connection manager may not be null.");
    }
    if (reustrat == null) {
        throw new IllegalArgumentException("Connection reuse strategy may not be null.");
    }
    if (kastrat == null) {
        throw new IllegalArgumentException("Connection keep alive strategy may not be null.");
    }
    if (rouplan == null) {
        throw new IllegalArgumentException("Route planner may not be null.");
    }
    if (httpProcessor == null) {
        throw new IllegalArgumentException("HTTP protocol processor may not be null.");
    }
    if (proxyAuthHandler == null) {
        throw new IllegalArgumentException("Proxy authentication handler may not be null.");
    }
    if (userTokenHandler == null) {
        throw new IllegalArgumentException("User token handler may not be null.");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    this.requestExec = requestExec;
    connManager = conman;
    reuseStrategy = reustrat;
    keepAliveStrategy = kastrat;
    routePlanner = rouplan;
    this.httpProcessor = httpProcessor;
    this.proxyAuthHandler = proxyAuthHandler;
    this.userTokenHandler = userTokenHandler;
    this.params = params;

    managedConn = null;
    targetAuthState = new AuthState();
    proxyAuthState = new AuthState();
}

From source file:org.robolectric.shadows.httpclient.DefaultRequestDirector.java

public DefaultRequestDirector(final Log log, final HttpRequestExecutor requestExec,
        final ClientConnectionManager conman, final ConnectionReuseStrategy reustrat,
        final ConnectionKeepAliveStrategy kastrat, final HttpRoutePlanner rouplan,
        final HttpProcessor httpProcessor, final HttpRequestRetryHandler retryHandler,
        final RedirectHandler redirectHandler, final AuthenticationHandler targetAuthHandler,
        final AuthenticationHandler proxyAuthHandler, final UserTokenHandler userTokenHandler,
        final HttpParams params) {

    if (log == null) {
        throw new IllegalArgumentException("Log may not be null.");
    }//  w w w . j  ava 2 s.  c o m
    if (requestExec == null) {
        throw new IllegalArgumentException("Request executor may not be null.");
    }
    if (conman == null) {
        throw new IllegalArgumentException("Client connection manager may not be null.");
    }
    if (reustrat == null) {
        throw new IllegalArgumentException("Connection reuse strategy may not be null.");
    }
    if (kastrat == null) {
        throw new IllegalArgumentException("Connection keep alive strategy may not be null.");
    }
    if (rouplan == null) {
        throw new IllegalArgumentException("Route planner may not be null.");
    }
    if (httpProcessor == null) {
        throw new IllegalArgumentException("HTTP protocol processor may not be null.");
    }
    if (retryHandler == null) {
        throw new IllegalArgumentException("HTTP request retry handler may not be null.");
    }
    if (redirectHandler == null) {
        throw new IllegalArgumentException("Redirect handler may not be null.");
    }
    if (targetAuthHandler == null) {
        throw new IllegalArgumentException("Target authentication handler may not be null.");
    }
    if (proxyAuthHandler == null) {
        throw new IllegalArgumentException("Proxy authentication handler may not be null.");
    }
    if (userTokenHandler == null) {
        throw new IllegalArgumentException("User token handler may not be null.");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    this.log = log;
    this.requestExec = requestExec;
    this.connManager = conman;
    this.reuseStrategy = reustrat;
    this.keepAliveStrategy = kastrat;
    this.routePlanner = rouplan;
    this.httpProcessor = httpProcessor;
    this.retryHandler = retryHandler;
    this.redirectHandler = redirectHandler;
    this.targetAuthHandler = targetAuthHandler;
    this.proxyAuthHandler = proxyAuthHandler;
    this.userTokenHandler = userTokenHandler;
    this.params = params;

    this.managedConn = null;

    this.redirectCount = 0;
    this.maxRedirects = this.params.getIntParameter(ClientPNames.MAX_REDIRECTS, 100);
    this.targetAuthState = new AuthState();
    this.proxyAuthState = new AuthState();
}

From source file:org.vietspider.net.apache.DefaultRequestDirector.java

/**
 * @since 4.1//from  ww  w. j  av  a2  s .com
 */
public DefaultRequestDirector(final Log log, final HttpRequestExecutor requestExec,
        final ClientConnectionManager conman, final ConnectionReuseStrategy reustrat,
        final ConnectionKeepAliveStrategy kastrat, final HttpRoutePlanner rouplan,
        final HttpProcessor httpProcessor, final HttpRequestRetryHandler retryHandler,
        final RedirectStrategy redirectStrategy, final AuthenticationHandler targetAuthHandler,
        final AuthenticationHandler proxyAuthHandler, final UserTokenHandler userTokenHandler,
        final HttpParams params) {

    if (log == null) {
        throw new IllegalArgumentException("Log may not be null.");
    }
    if (requestExec == null) {
        throw new IllegalArgumentException("Request executor may not be null.");
    }
    if (conman == null) {
        throw new IllegalArgumentException("Client connection manager may not be null.");
    }
    if (reustrat == null) {
        throw new IllegalArgumentException("Connection reuse strategy may not be null.");
    }
    if (kastrat == null) {
        throw new IllegalArgumentException("Connection keep alive strategy may not be null.");
    }
    if (rouplan == null) {
        throw new IllegalArgumentException("Route planner may not be null.");
    }
    if (httpProcessor == null) {
        throw new IllegalArgumentException("HTTP protocol processor may not be null.");
    }
    if (retryHandler == null) {
        throw new IllegalArgumentException("HTTP request retry handler may not be null.");
    }
    if (redirectStrategy == null) {
        throw new IllegalArgumentException("Redirect strategy may not be null.");
    }
    if (targetAuthHandler == null) {
        throw new IllegalArgumentException("Target authentication handler may not be null.");
    }
    if (proxyAuthHandler == null) {
        throw new IllegalArgumentException("Proxy authentication handler may not be null.");
    }
    if (userTokenHandler == null) {
        throw new IllegalArgumentException("User token handler may not be null.");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    this.log = log;
    this.requestExec = requestExec;
    this.connManager = conman;
    this.reuseStrategy = reustrat;
    this.keepAliveStrategy = kastrat;
    this.routePlanner = rouplan;
    this.httpProcessor = httpProcessor;
    this.retryHandler = retryHandler;
    this.redirectStrategy = redirectStrategy;
    this.targetAuthHandler = targetAuthHandler;
    this.proxyAuthHandler = proxyAuthHandler;
    this.userTokenHandler = userTokenHandler;
    this.params = params;

    this.managedConn = null;

    this.execCount = 0;
    this.redirectCount = 0;
    this.maxRedirects = this.params.getIntParameter(ClientPNames.MAX_REDIRECTS, 100);
    this.targetAuthState = new AuthState();
    this.proxyAuthState = new AuthState();
}

From source file:org.callimachusproject.client.HttpAuthenticator.java

private AuthState getTargetAuthState(final HttpContext context) {
    AuthState targetAuthState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    if (targetAuthState == null) {
        targetAuthState = new AuthState();
        context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, targetAuthState);
    }/*  w ww .  j  av  a2s .  c  o  m*/
    return targetAuthState;
}

From source file:org.callimachusproject.client.HttpAuthenticator.java

private AuthState getProxyAuthState(final HttpContext context) {
    AuthState proxyAuthState = (AuthState) context.getAttribute(HttpClientContext.PROXY_AUTH_STATE);
    if (proxyAuthState == null) {
        proxyAuthState = new AuthState();
        context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, proxyAuthState);
    }/*from  w w w.  jav a 2s .c  o  m*/
    return proxyAuthState;
}

From source file:org.opentravel.schemacompiler.repository.impl.RemoteRepositoryClient.java

/**
 * Configures the 'Authorization' header on the given HTTP request using the current user ID and
 * encrypted password that is configured for this repository.
 *//*ww w. ja va  2  s .c  o  m*/
private HttpClientContext createHttpContext() {
    HttpClientContext context = HttpClientContext.create();

    if ((userId != null) && (encryptedPassword != null)) {
        AuthState target = new AuthState();
        target.update(new BasicScheme(),
                new UsernamePasswordCredentials(userId, PasswordHelper.decrypt(encryptedPassword)));
        context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, target);
    }
    return context;
}

From source file:org.apache.http.impl.client.DefaultRequestDirector.java

/**
 * @since 4.2// ww  w .  j  av  a  2  s . co  m
 */
public DefaultRequestDirector(final Log log, final HttpRequestExecutor requestExec,
        final ClientConnectionManager conman, final ConnectionReuseStrategy reustrat,
        final ConnectionKeepAliveStrategy kastrat, final HttpRoutePlanner rouplan,
        final HttpProcessor httpProcessor, final HttpRequestRetryHandler retryHandler,
        final RedirectStrategy redirectStrategy, final AuthenticationStrategy targetAuthStrategy,
        final AuthenticationStrategy proxyAuthStrategy, final UserTokenHandler userTokenHandler,
        final HttpParams params) {

    Args.notNull(log, "Log");
    Args.notNull(requestExec, "Request executor");
    Args.notNull(conman, "Client connection manager");
    Args.notNull(reustrat, "Connection reuse strategy");
    Args.notNull(kastrat, "Connection keep alive strategy");
    Args.notNull(rouplan, "Route planner");
    Args.notNull(httpProcessor, "HTTP protocol processor");
    Args.notNull(retryHandler, "HTTP request retry handler");
    Args.notNull(redirectStrategy, "Redirect strategy");
    Args.notNull(targetAuthStrategy, "Target authentication strategy");
    Args.notNull(proxyAuthStrategy, "Proxy authentication strategy");
    Args.notNull(userTokenHandler, "User token handler");
    Args.notNull(params, "HTTP parameters");
    this.log = log;
    this.authenticator = new HttpAuthenticator(log);
    this.requestExec = requestExec;
    this.connManager = conman;
    this.reuseStrategy = reustrat;
    this.keepAliveStrategy = kastrat;
    this.routePlanner = rouplan;
    this.httpProcessor = httpProcessor;
    this.retryHandler = retryHandler;
    this.redirectStrategy = redirectStrategy;
    this.targetAuthStrategy = targetAuthStrategy;
    this.proxyAuthStrategy = proxyAuthStrategy;
    this.userTokenHandler = userTokenHandler;
    this.params = params;

    if (redirectStrategy instanceof DefaultRedirectStrategyAdaptor) {
        this.redirectHandler = ((DefaultRedirectStrategyAdaptor) redirectStrategy).getHandler();
    } else {
        this.redirectHandler = null;
    }
    if (targetAuthStrategy instanceof AuthenticationStrategyAdaptor) {
        this.targetAuthHandler = ((AuthenticationStrategyAdaptor) targetAuthStrategy).getHandler();
    } else {
        this.targetAuthHandler = null;
    }
    if (proxyAuthStrategy instanceof AuthenticationStrategyAdaptor) {
        this.proxyAuthHandler = ((AuthenticationStrategyAdaptor) proxyAuthStrategy).getHandler();
    } else {
        this.proxyAuthHandler = null;
    }

    this.managedConn = null;

    this.execCount = 0;
    this.redirectCount = 0;
    this.targetAuthState = new AuthState();
    this.proxyAuthState = new AuthState();
    this.maxRedirects = this.params.getIntParameter(ClientPNames.MAX_REDIRECTS, 100);
}

From source file:org.apache.http.impl.client.InternalHttpClient.java

private void setupContext(final HttpClientContext context) {
    if (context.getAttribute(HttpClientContext.TARGET_AUTH_STATE) == null) {
        context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, new AuthState());
    }//from w w w.  j  a va  2  s . co  m
    if (context.getAttribute(HttpClientContext.PROXY_AUTH_STATE) == null) {
        context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, new AuthState());
    }
    if (context.getAttribute(HttpClientContext.AUTHSCHEME_REGISTRY) == null) {
        context.setAttribute(HttpClientContext.AUTHSCHEME_REGISTRY, this.authSchemeRegistry);
    }
    if (context.getAttribute(HttpClientContext.COOKIESPEC_REGISTRY) == null) {
        context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
    }
    if (context.getAttribute(HttpClientContext.COOKIE_STORE) == null) {
        context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
    }
    if (context.getAttribute(HttpClientContext.CREDS_PROVIDER) == null) {
        context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credentialsProvider);
    }
    if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
        context.setAttribute(HttpClientContext.REQUEST_CONFIG, this.defaultConfig);
    }
}