/** * Called primarily from HTTPMethod to do the bulk of the execution. Assumes HTTPMethod has * inserted its headers into request. * * @param method * @param methoduri * @param rb * @return Request+Response pair * @throws HTTPException */ ExecState execute(HTTPMethod method, URI methoduri, RequestBuilder rb) throws HTTPException { this.execution = new ExecState(); this.requestURI = methoduri; AuthScope methodscope = HTTPAuthUtil.uriToAuthScope(methoduri); AuthScope target = HTTPAuthUtil.authscopeUpgrade(this.scope, methodscope); synchronized (this) { // keep coverity happy // Merge Settings; Settings merged = HTTPUtil.merge(globalsettings, localsettings); if (!this.cachevalid) { RequestConfig.Builder rcb = RequestConfig.custom(); this.cachedconfig = configureRequest(rcb, merged); HttpClientBuilder cb = HttpClients.custom(); configClient(cb, merged); setAuthenticationAndProxy(cb); this.cachedclient = cb.build(); rb.setConfig(this.cachedconfig); this.cachevalid = true; } } this.execution.request = (HttpRequestBase) rb.build(); try { HttpHost targethost = HTTPAuthUtil.authscopeToHost(target); this.execution.response = cachedclient.execute(targethost, this.execution.request, this.sessioncontext); } catch (IOException ioe) { throw new HTTPException(ioe); } return this.execution; }
protected void init(AuthScope scope, String actualurl) throws HTTPException { assert (scope != null); if (actualurl != null) this.sessionURI = actualurl; else this.sessionURI = HTTPAuthUtil.authscopeToURI(scope).toString(); this.scope = scope; this.scopeURI = HTTPAuthUtil.authscopeToURI(scope); this.cachevalid = false; // Force build on first use this.sessioncontext.setCookieStore(new BasicCookieStore()); this.sessioncontext.setAttribute(HttpClientContext.AUTH_CACHE, new BasicAuthCache()); }
@Deprecated public void setCredentialsProvider(String url, CredentialsProvider provider) throws HTTPException { assert (url != null && provider != null); AuthScope scope = HTTPAuthUtil.uriToAuthScope(url); setCredentialsProvider(provider, scope); }
@Deprecated public static void setGlobalCredentials(String url, Credentials creds) throws HTTPException { assert (url != null && creds != null); AuthScope scope = HTTPAuthUtil.uriToAuthScope(url); CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(scope, creds); setGlobalCredentialsProvider(provider, scope); }
/** * Handle authentication and Proxy'ing * * @param cb * @throws HTTPException */ protected synchronized void setAuthenticationAndProxy(HttpClientBuilder cb) throws HTTPException { // First, setup the ssl factory cb.setSSLSocketFactory(globalsslfactory); // Second, Construct a CredentialsProvider that is // the union of the Proxy credentials plus // either the global local credentials; local overrides global // Unfortunately, we cannot either clone or extract the contents // of the client supplied provider, so we are forced (for now) // to modify the client supplied provider. // Look in the local authcreds for best scope match AuthScope bestMatch = HTTPAuthUtil.bestmatch(scope, localcreds.keySet()); CredentialsProvider cp = null; if (bestMatch != null) { cp = localcreds.get(bestMatch); } else { bestMatch = HTTPAuthUtil.bestmatch(scope, globalcreds.keySet()); if (bestMatch != null) cp = globalcreds.get(bestMatch); } // Build the proxy credentials and AuthScope Credentials proxycreds = null; AuthScope proxyscope = null; if (proxyuser != null && (httpproxy != null || httpsproxy != null)) { if (httpproxy != null) proxyscope = HTTPAuthUtil.hostToAuthScope(httpproxy); else // httpsproxy != null proxyscope = HTTPAuthUtil.hostToAuthScope(httpsproxy); proxycreds = new UsernamePasswordCredentials(proxyuser, proxypwd); } if (cp == null && proxycreds != null && proxyscope != null) { // If client provider is null and proxycreds are not, // then use proxycreds alone cp = new BasicCredentialsProvider(); cp.setCredentials(proxyscope, proxycreds); } else if (cp != null && proxycreds != null && proxyscope != null) { // If client provider is not null and proxycreds are not, // then add proxycreds to the client provider cp.setCredentials(proxyscope, proxycreds); } if (cp != null) this.sessioncontext.setCredentialsProvider(cp); }
HTTPSession(HttpHost httphost) throws HTTPException { init(HTTPAuthUtil.hostToAuthScope(httphost), null); }
HTTPSession(String uri) throws HTTPException { init(HTTPAuthUtil.uriToAuthScope(uri), uri); }
@Deprecated public void setCredentials(String url, Credentials creds) throws HTTPException { assert (creds != null); AuthScope scope = HTTPAuthUtil.uriToAuthScope(url); setCredentials(creds, scope); }