List of usage examples for org.apache.http.auth AuthState setCredentials
@Deprecated public void setCredentials(final Credentials credentials)
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 {/*from w ww . ja v a 2 s. 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:net.homelinux.penecoptero.android.citybikes.app.RESTHelper.java
private static DefaultHttpClient setCredentials(DefaultHttpClient httpclient, String url, String username, String password) throws HttpException, IOException { HttpHost targetHost = new HttpHost(url); final UsernamePasswordCredentials access = new UsernamePasswordCredentials(username, password); httpclient.getCredentialsProvider()/*from w w w . ja v a 2 s. c o m*/ .setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), access); httpclient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(access); } } }, 0); return 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 {//from ww w. j av a 2 s . 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, 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.suse.studio.api.lib.HttpClient.java
private DefaultHttpClient getHttpClient(final String userName, final String apiKey) { HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(new UsernamePasswordCredentials(userName, apiKey)); }//w w w.j ava 2 s. c o m } }; DefaultHttpClient client = new DefaultHttpClient(); client.addRequestInterceptor(preemptiveAuth, 0); return client; }
From source file:se.vgregion.pubsub.twitter.impl.PreemptiveBasicAuth.java
public void process(HttpRequest request, HttpContext context) { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); // If not auth scheme has been initialized yet if (authState.getAuthScheme() == null) { Credentials creds = new UsernamePasswordCredentials(username, password); authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); }// www . j a v a 2 s .c om }
From source file:net.homelinux.penecoptero.android.citybikes.app.RESTHelper.java
private DefaultHttpClient setCredentials(DefaultHttpClient httpclient, String url) throws HttpException, IOException { HttpHost targetHost = new HttpHost(url); final UsernamePasswordCredentials access = new UsernamePasswordCredentials(USERNAME, PASSWORD); httpclient.getCredentialsProvider()//from w w w . j a v a 2 s . c o m .setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), access); httpclient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(access); } } }, 0); return httpclient; }
From source file:cloudMe.CloudMeAPI.java
private DefaultHttpClient getConnection() { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false); HttpProtocolParams.setUserAgent(httpClient.getParams(), USER_AGENT_INFO); httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, 80, "os@xcerion.com", "Digest"), new UsernamePasswordCredentials(userName, pass)); // Encrypts password & username HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (authState.getAuthScheme() == null) { if (creds != null && scheme != null) { authState.setAuthScheme(scheme); authState.setCredentials(creds); } else { scheme = authState.getAuthScheme(); }/*from w w w . j a v a 2 s .c om*/ } } }; httpClient.addRequestInterceptor(preemptiveAuth, 0); return httpClient; }
From source file:org.opendatakit.dwc.server.GreetingServiceImpl.java
private static HttpRequestInterceptor getPreemptiveAuth() { return new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); }/*from ww w . ja va2 s. com*/ } } }; }
From source file:com.cloudhopper.httpclient.util.PreemptiveBasicAuthHttpRequestInterceptor.java
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { ///*from w w w . j av a 2 s . co m*/ // get the authentication state for the request // AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); // // if no auth scheme avaialble yet, try to initialize it preemptively // if (authState.getAuthScheme() == null) { // check if credentials are set if (this.credentials == null) { throw new HttpException("No credentials for preemptive authentication"); } // add the credentials to the auth state authState.setAuthScheme(this.basicAuthScheme); authState.setCredentials(this.credentials); // HttpHost targetHost = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); //CredentialsProvider credsProvider = (CredentialsProvider)context.getAttribute(ClientContext.CREDS_PROVIDER); } }
From source file:at.bitfire.davdroid.webdav.PreemptiveAuthInterceptor.java
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.setAuthScheme(new BasicScheme()); authState.setCredentials(creds); }/*from w ww . java2s . c om*/ } }