httpclient的disableConnectionState方法工作流程
作者:codecraft
这篇文章主要为大家介绍了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的资料请关注脚本之家其它相关文章!