本文整理匯總了Java中org.apache.http.client.protocol.HttpClientContext.adapt方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpClientContext.adapt方法的具體用法?Java HttpClientContext.adapt怎麽用?Java HttpClientContext.adapt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.protocol.HttpClientContext
的用法示例。
在下文中一共展示了HttpClientContext.adapt方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: authSucceeded
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
public void authSucceeded(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
Args.notNull(authhost, "Host");
Args.notNull(authScheme, "Auth scheme");
Args.notNull(context, "HTTP context");
final HttpClientContext clientContext = HttpClientContext.adapt(context);
if (isCachable(authScheme)) {
AuthCache authCache = clientContext.getAuthCache();
if (authCache == null) {
authCache = new BasicAuthCache();
clientContext.setAuthCache(authCache);
}
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Caching '" + authScheme.getSchemeName() +
"' auth scheme for " + authhost);
}
authCache.put(authhost, authScheme);
}
}
示例2: upgrade
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
public void upgrade(
final ManagedHttpClientConnection conn,
final HttpHost host,
final HttpContext context) throws IOException {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final Lookup<ConnectionSocketFactory> registry = getSocketFactoryRegistry(clientContext);
final ConnectionSocketFactory sf = registry.lookup(host.getSchemeName());
if (sf == null) {
throw new UnsupportedSchemeException(host.getSchemeName() +
" protocol is not supported");
}
if (!(sf instanceof LayeredConnectionSocketFactory)) {
throw new UnsupportedSchemeException(host.getSchemeName() +
" protocol does not support connection upgrade");
}
final LayeredConnectionSocketFactory lsf = (LayeredConnectionSocketFactory) sf;
Socket sock = conn.getSocket();
final int port = this.schemePortResolver.resolve(host);
sock = lsf.createLayeredSocket(sock, host.getHostName(), port, context);
conn.bind(sock);
}
示例3: getServicePrincipalName
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private String getServicePrincipalName(final HttpContext context) {
final String spn;
if (this.servicePrincipalName != null) {
spn = this.servicePrincipalName;
} else {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final HttpHost target = clientContext.getTargetHost();
if (target != null) {
spn = "HTTP/" + target.getHostName();
} else {
final RouteInfo route = clientContext.getHttpRoute();
if (route != null) {
spn = "HTTP/" + route.getTargetHost().getHostName();
} else {
// Should not happen
spn = null;
}
}
}
if (this.log.isDebugEnabled()) {
this.log.debug("Using SPN: " + spn);
}
return spn;
}
示例4: retryRequest
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 5) {// 如果已經重試了5次,就放棄
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服務器丟掉了連接,那麽就重試
return true;
}
if (exception instanceof InterruptedIOException) {// 超時
return false;
}
if (exception instanceof SSLHandshakeException) {// 不要重試SSL握手異常
return false;
}
if (exception instanceof UnknownHostException) {// 目標服務器不可達
return false;
}
if (exception instanceof ConnectTimeoutException) {// 連接被拒絕
return false;
}
if (exception instanceof SSLException) {// SSL握手異常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果請求是冪等的,就再次嘗試
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
示例5: retryRequest
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context){
if(logOnRetry){
HttpClientContext clientContext = HttpClientContext.adapt(context);
logger.warn("Request {} failure Nº {}", clientContext.getRequest().getRequestLine(), executionCount,
exception);
}
Object retrySafe = context.getAttribute(RETRY_SAFE_ATTRIBUTE);
if(retrySafe == null || !(retrySafe instanceof Boolean) || !(Boolean)retrySafe || executionCount > retryCount){
return false;
}
return true;
}
示例6: authSucceeded
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
public void authSucceeded(final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final Credentials credentials = clientContext.getAttribute(PROXY_CREDENTIALS_INPUT_ID, Credentials.class);
if(null != credentials) {
clientContext.removeAttribute(PROXY_CREDENTIALS_INPUT_ID);
if(log.isInfoEnabled()) {
log.info(String.format("Save passphrase for proxy %s", authhost));
}
keychain.addCredentials(authhost.getHostName(), credentials.getUsername(), credentials.getPassword());
}
super.authSucceeded(authhost, authScheme, context);
}
示例7: retryRequest
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 3) {// 如果已經重試了3次,就放棄
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服務器丟掉了連接,那麽就重試
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重試SSL握手異常
return false;
}
if (exception instanceof InterruptedIOException) {// 超時
return true;
}
if (exception instanceof UnknownHostException) {// 目標服務器不可達
return false;
}
if (exception instanceof ConnectTimeoutException) {// 連接被拒絕
return false;
}
if (exception instanceof SSLException) {// ssl握手異常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果請求是冪等的,就再次嘗試
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
示例8: determineProxy
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
protected HttpHost determineProxy(HttpHost host, HttpRequest request, HttpContext context) throws HttpException {
HttpClientContext httpClientContext = HttpClientContext.adapt(context);
Proxy proxy = proxyPlanner.determineProxy(host, request, context, ipPool, crawlerSession);
if (proxy == null) {
return null;
}
if (log.isDebugEnabled()) {
log.debug("{} 當前使用IP為:{}:{}", host.getHostName(), proxy.getIp(), proxy.getPort());
}
context.setAttribute(VSCRAWLER_AVPROXY_KEY, proxy);
if (proxy.getAuthenticationHeaders() != null) {
for (Header header : proxy.getAuthenticationHeaders()) {
request.addHeader(header);
}
}
if (StringUtils.isNotEmpty(proxy.getUsername()) && StringUtils.isNotEmpty(proxy.getPassword())) {
BasicCredentialsProvider credsProvider1 = new BasicCredentialsProvider();
httpClientContext.setCredentialsProvider(credsProvider1);
credsProvider1.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
}
return new HttpHost(proxy.getIp(), proxy.getPort());
}
示例9: determineProxy
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
public Proxy determineProxy(HttpHost host, HttpRequest request, HttpContext context, IPPool ipPool,
CrawlerSession crawlerSession) {
HttpClientContext httpClientContext = HttpClientContext.adapt(context);
Proxy proxy = (Proxy) crawlerSession.getExtInfo(VSCRAWLER_AVPROXY_KEY);
if (proxy == null) {
String accessUrl = null;
if (request instanceof HttpRequestWrapper || request instanceof HttpGet) {
accessUrl = HttpUriRequest.class.cast(request).getURI().toString();
}
if (!PoolUtil.isDungProxyEnabled(httpClientContext)) {
log.info("{}不會被代理", accessUrl);
return null;
}
proxy = ipPool.getIP(host.getHostName(), accessUrl);
if (proxy == null) {
return null;
}
crawlerSession.setExtInfo(VSCRAWLER_AVPROXY_KEY, proxy);
}
return proxy;
}
示例10: upgrade
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
public void upgrade(
final ManagedHttpClientConnection conn,
final HttpHost host,
final HttpContext context) throws IOException {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final Lookup<ConnectionSocketFactory> registry = getSocketFactoryRegistry(clientContext);
final ConnectionSocketFactory sf = registry.lookup(host.getSchemeName());
if (sf == null) {
throw new UnsupportedSchemeException(host.getSchemeName() +
" protocol is not supported");
}
if (!(sf instanceof LayeredConnectionSocketFactory)) {
throw new UnsupportedSchemeException(host.getSchemeName() +
" protocol does not support connection upgrade");
}
final LayeredConnectionSocketFactory lsf = (LayeredConnectionSocketFactory) sf;
Socket sock = conn.getSocket();
final int port = this.schemePortResolver.resolve(host);
sock = lsf.createLayeredSocket(sock, host.getHostName(), port, context);
conn.bind(sock);
}
示例11: retryRequest
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
public boolean retryRequest(
IOException exception,
int executionCount,
HttpContext context) {
if (executionCount >= 5) {
// Do not retry if over max retry count
return false;
}
if (exception instanceof InterruptedIOException) {
// Timeout
return false;
}
if (exception instanceof UnknownHostException) {
// Unknown host
return false;
}
if (exception instanceof ConnectTimeoutException) {
// Connection refused
return false;
}
if (exception instanceof SSLException) {
// SSL handshake exception
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// Retry if the request is considered idempotent
return true;
}
return false;
}
示例12: retryRequest
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
/**
* 自定義的恢複策略
*/
public boolean retryRequest(IOException exception, int exceptionCount,
HttpContext context) {
if (exceptionCount >= 3)
return false;
if (exception instanceof InterruptedIOException) {
return false;
}
if (exception instanceof UnknownHostException) {
return false;
}
if (exception instanceof ConnectTimeoutException) {
return false;
}
if (exception instanceof SSLException) {
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
return true;
}
return false;
}
示例13: authFailed
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
public void authFailed(
final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
Args.notNull(authhost, "Host");
Args.notNull(context, "HTTP context");
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final AuthCache authCache = clientContext.getAuthCache();
if (authCache != null) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Clearing cached auth scheme for " + authhost);
}
authCache.remove(authhost);
}
}
示例14: retryRequest
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
/**
* Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
* if the given method should be retried.
*/
public boolean retryRequest(
final IOException exception,
final int executionCount,
final HttpContext context) {
Args.notNull(exception, "Exception parameter");
Args.notNull(context, "HTTP context");
if (executionCount > this.retryCount) {
// Do not retry if over max retry count
return false;
}
if (this.nonRetriableClasses.contains(exception.getClass())) {
return false;
} else {
for (final Class<? extends IOException> rejectException : this.nonRetriableClasses) {
if (rejectException.isInstance(exception)) {
return false;
}
}
}
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final HttpRequest request = clientContext.getRequest();
if(requestIsAborted(request)){
return false;
}
if (handleAsIdempotent(request)) {
// Retry if the request is considered idempotent
return true;
}
if (!clientContext.isRequestSent() || this.requestSentRetryEnabled) {
// Retry if the request has not been sent fully or
// if it's OK to retry methods that have been sent
return true;
}
// otherwise do not retry
return false;
}
示例15: getUserToken
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
public Object getUserToken(final HttpContext context) {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
Principal userPrincipal = null;
final AuthStateHC4 targetAuthState = clientContext.getTargetAuthState();
if (targetAuthState != null) {
userPrincipal = getAuthPrincipal(targetAuthState);
if (userPrincipal == null) {
final AuthStateHC4 proxyAuthState = clientContext.getProxyAuthState();
userPrincipal = getAuthPrincipal(proxyAuthState);
}
}
if (userPrincipal == null) {
final HttpConnection conn = clientContext.getConnection();
if (conn.isOpen() && conn instanceof ManagedHttpClientConnection) {
final SSLSession sslsession = ((ManagedHttpClientConnection) conn).getSSLSession();
if (sslsession != null) {
userPrincipal = sslsession.getLocalPrincipal();
}
}
}
return userPrincipal;
}