httpclient的disableConnectionState方法工作流程

 更新时间:2023年11月20日 08:54:57   作者: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的资料请关注脚本之家其它相关文章!

相关文章

  • Java设计模式之单例模式示例详解

    Java设计模式之单例模式示例详解

    单例模式是最简单也是最基础的设计模式之一,单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例。本文将通过一些示例代码为大家详细介绍一下单例模式,感兴趣的可以了解一下
    2021-12-12
  • 数据结构与算法之手撕排序算法

    数据结构与算法之手撕排序算法

    排序算法看似简单,其实不同的算法中蕴涵着经典的算法策略。通过熟练掌握排序算法,就可以掌握基本的算法设计思想,本文主要介绍了Java中的排序算法,需要的朋友欢迎阅读
    2023-04-04
  • Spring Boot 与 Kotlin 使用JdbcTemplate连接MySQL数据库的方法

    Spring Boot 与 Kotlin 使用JdbcTemplate连接MySQL数据库的方法

    本文介绍在Spring Boot基础下配置数据源和通过 JdbcTemplate 编写数据访问的示例。感兴趣的朋友跟随脚本之家小编一起学习吧
    2018-01-01
  • SpringBoot集成WebSocket实现前后端消息互传的方法

    SpringBoot集成WebSocket实现前后端消息互传的方法

    这篇文章主要介绍了SpringBoot集成WebSocket实现前后端消息互传的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • SpringBoot所管理的依赖和需要的依赖冲突问题及解决

    SpringBoot所管理的依赖和需要的依赖冲突问题及解决

    这篇文章主要介绍了SpringBoot所管理的依赖和需要的依赖冲突问题及解决过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2026-05-05
  • idea报错之找不到符号:类的问题及解决

    idea报错之找不到符号:类的问题及解决

    这篇文章主要介绍了idea报错之找不到符号:类的问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • SpringBoot实现自定义Redis的连接的流程步骤

    SpringBoot实现自定义Redis的连接的流程步骤

    Spring Boot 自定义 Redis 主要是指在基于 Spring Boot 的应用程序中,当你需要更深入地控制或扩展对 Redis 数据库的操作,而不是仅仅依赖 Spring Data Redis 的默认配置,本文给大家介绍了SpringBoot实现自定义Redis的连接的流程步骤,需要的朋友可以参考下
    2024-09-09
  • SpringBoot三种打包方法举例详解

    SpringBoot三种打包方法举例详解

    这篇文章主要给大家介绍了关于SpringBoot三种打包方法的相关资料,Spring Boot是一个开发框架,目的是简化Spring应用的初始搭建过程和开发过程,文中提供了3种打包方法,需要的朋友可以参考下
    2023-12-12
  • 详解Spring Boot中如何自定义SpringMVC配置

    详解Spring Boot中如何自定义SpringMVC配置

    这篇文章主要给大家介绍了关于Spring Boot中如何自定义SpringMVC配置的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-09-09
  • MapStruct内部错误:NullPointerException的解决方案

    MapStruct内部错误:NullPointerException的解决方案

    在Java开发中,MapStruct是一个非常流行的对象映射工具,它通过注解处理器在编译时生成映射代码,极大地简化了对象之间的转换操作,本文将详细分析一个常见的MapStruct内部错误——NullPointerException,并提供一系列解决方案,需要的朋友可以参考下
    2025-02-02

最新评论