httpclient的disableConnectionState方法工作流程
序
本文主要研究一下httpclient的disableConnectionState
disableConnectionState
org/apache/http/impl/client/HttpClientBuilder.java
/**
* Disables connection state tracking.
*/
public final HttpClientBuilder disableConnectionState() {
connectionStateDisabled = true;
return this;
}
public CloseableHttpClient build() {
//......
UserTokenHandler userTokenHandlerCopy = this.userTokenHandler;
if (userTokenHandlerCopy == null) {
if (!connectionStateDisabled) {
userTokenHandlerCopy = DefaultUserTokenHandler.INSTANCE;
} else {
userTokenHandlerCopy = NoopUserTokenHandler.INSTANCE;
}
}
ClientExecChain execChain = createMainExec(
requestExecCopy,
connManagerCopy,
reuseStrategyCopy,
keepAliveStrategyCopy,
new ImmutableHttpProcessor(new RequestTargetHost(), new RequestUserAgent(userAgentCopy)),
targetAuthStrategyCopy,
proxyAuthStrategyCopy,
userTokenHandlerCopy);
//......
}
}HttpClientBuilder提供了disableConnectionState方法可以设置connectionStateDisabled为true,在该值为true时userTokenHandlerCopy为NoopUserTokenHandler.INSTANCE,而userTokenHandlerCopy是作为创建ClientExecChain(MainClientExec)的参数之一
execute
org/apache/http/impl/execchain/MainClientExec.java
Object userToken = context.getUserToken();
if (userToken == null) {
userToken = userTokenHandler.getUserToken(context);
context.setAttribute(HttpClientContext.USER_TOKEN, userToken);
}
if (userToken != null) {
connHolder.setState(userToken);
}MainClientExec的execute方法会通过context.getUserToken()获取userToken,在userToken为null时会通过serTokenHandler.getUserToken(context)获取userToken然后设置到context中,最后将userToken设置到connHolder的state
UserTokenHandler
org/apache/http/client/UserTokenHandler.java
public interface UserTokenHandler {
/**
* The token object returned by this method is expected to uniquely
* identify the current user if the context is user specific or to be
* {@code null} if it is not.
*
* @param context the execution context
*
* @return user token that uniquely identifies the user or
* {@code null} if the context is not user specific.
*/
Object getUserToken(HttpContext context);
}UserTokenHandler定义了getUserToken方法
DefaultUserTokenHandler
org/apache/http/impl/client/DefaultUserTokenHandler.java
@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class DefaultUserTokenHandler implements UserTokenHandler {
public static final DefaultUserTokenHandler INSTANCE = new DefaultUserTokenHandler();
@Override
public Object getUserToken(final HttpContext context) {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
Principal userPrincipal = null;
final AuthState targetAuthState = clientContext.getTargetAuthState();
if (targetAuthState != null) {
userPrincipal = getAuthPrincipal(targetAuthState);
if (userPrincipal == null) {
final AuthState 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;
}
private static Principal getAuthPrincipal(final AuthState authState) {
final AuthScheme scheme = authState.getAuthScheme();
if (scheme != null && scheme.isComplete() && scheme.isConnectionBased()) {
final Credentials creds = authState.getCredentials();
if (creds != null) {
return creds.getUserPrincipal();
}
}
return null;
}
}DefaultUserTokenHandler从context中获取userPrincipal
NoopUserTokenHandler
org/apache/http/impl/client/NoopUserTokenHandler.java
@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class NoopUserTokenHandler implements UserTokenHandler {
public static final NoopUserTokenHandler INSTANCE = new NoopUserTokenHandler();
@Override
public Object getUserToken(final HttpContext context) {
return null;
}
}NoopUserTokenHandler的getUserToken则返回null
小结
httpclient的disableConnectionState设置了ClientExecChain(MainClientExec)的UserTokenHandler为NoopUserTokenHandler,而MainClientExec的execute方法会通过context.getUserToken()获取userToken,在userToken为null时会通过serTokenHandler.getUserToken(context)获取userToken然后设置到context中,最后将userToken设置到connHolder的state。
connHolder的state与userToken挂钩起来歧义挺大的
以上就是httpclient的disableConnectionState的详细内容,更多关于httpclient disableConnectionState的资料请关注脚本之家其它相关文章!
相关文章
Spring Boot 与 Kotlin 使用JdbcTemplate连接MySQL数据库的方法
本文介绍在Spring Boot基础下配置数据源和通过 JdbcTemplate 编写数据访问的示例。感兴趣的朋友跟随脚本之家小编一起学习吧2018-01-01
SpringBoot集成WebSocket实现前后端消息互传的方法
这篇文章主要介绍了SpringBoot集成WebSocket实现前后端消息互传的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-10-10
详解Spring Boot中如何自定义SpringMVC配置
这篇文章主要给大家介绍了关于Spring Boot中如何自定义SpringMVC配置的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2021-09-09
MapStruct内部错误:NullPointerException的解决方案
在Java开发中,MapStruct是一个非常流行的对象映射工具,它通过注解处理器在编译时生成映射代码,极大地简化了对象之间的转换操作,本文将详细分析一个常见的MapStruct内部错误——NullPointerException,并提供一系列解决方案,需要的朋友可以参考下2025-02-02


最新评论