本文整理汇总了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;
}