/** * 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 configClient(HttpClientBuilder cb, Settings settings) throws HTTPException { cb.useSystemProperties(); String agent = (String) settings.get(Prop.USER_AGENT); if (agent != null) cb.setUserAgent(agent); setInterceptors(cb); cb.setContentDecoderRegistry(contentDecoderMap); }
protected static void setInterceptors(HttpClientBuilder cb) { for (HttpRequestInterceptor hrq : reqintercepts) { cb.addInterceptorLast(hrq); } for (HttpResponseInterceptor hrs : rspintercepts) { cb.addInterceptorLast(hrs); } // Add debug interceptors for (HttpRequestInterceptor hrq : dbgreq) { cb.addInterceptorFirst(hrq); } for (HttpResponseInterceptor hrs : dbgrsp) { cb.addInterceptorFirst(hrs); } // Hack: add Content-Encoding suppressor cb.addInterceptorFirst(CEKILL); }
/** * 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); }