最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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是作為創(chuàng)建ClientExecChain(MainClientExec)的參數(shù)之一

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

小結(jié)

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的詳細內(nèi)容,更多關(guān)于httpclient disableConnectionState的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java設計模式之單例模式示例詳解

    Java設計模式之單例模式示例詳解

    單例模式是最簡單也是最基礎的設計模式之一,單例模式確保某個類只有一個實例,而且自行實例化并向整個系統(tǒng)提供這個實例。本文將通過一些示例代碼為大家詳細介紹一下單例模式,感興趣的可以了解一下
    2021-12-12
  • 數(shù)據(jù)結(jié)構(gòu)與算法之手撕排序算法

    數(shù)據(jù)結(jié)構(gòu)與算法之手撕排序算法

    排序算法看似簡單,其實不同的算法中蘊涵著經(jīng)典的算法策略。通過熟練掌握排序算法,就可以掌握基本的算法設計思想,本文主要介紹了Java中的排序算法,需要的朋友歡迎閱讀
    2023-04-04
  • Spring Boot 與 Kotlin 使用JdbcTemplate連接MySQL數(shù)據(jù)庫的方法

    Spring Boot 與 Kotlin 使用JdbcTemplate連接MySQL數(shù)據(jù)庫的方法

    本文介紹在Spring Boot基礎下配置數(shù)據(jù)源和通過 JdbcTemplate 編寫數(shù)據(jù)訪問的示例。感興趣的朋友跟隨腳本之家小編一起學習吧
    2018-01-01
  • SpringBoot集成WebSocket實現(xiàn)前后端消息互傳的方法

    SpringBoot集成WebSocket實現(xiàn)前后端消息互傳的方法

    這篇文章主要介紹了SpringBoot集成WebSocket實現(xiàn)前后端消息互傳的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • SpringBoot所管理的依賴和需要的依賴沖突問題及解決

    SpringBoot所管理的依賴和需要的依賴沖突問題及解決

    這篇文章主要介紹了SpringBoot所管理的依賴和需要的依賴沖突問題及解決過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2026-05-05
  • idea報錯之找不到符號:類的問題及解決

    idea報錯之找不到符號:類的問題及解決

    這篇文章主要介紹了idea報錯之找不到符號:類的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • SpringBoot實現(xiàn)自定義Redis的連接的流程步驟

    SpringBoot實現(xiàn)自定義Redis的連接的流程步驟

    Spring Boot 自定義 Redis 主要是指在基于 Spring Boot 的應用程序中,當你需要更深入地控制或擴展對 Redis 數(shù)據(jù)庫的操作,而不是僅僅依賴 Spring Data Redis 的默認配置,本文給大家介紹了SpringBoot實現(xiàn)自定義Redis的連接的流程步驟,需要的朋友可以參考下
    2024-09-09
  • SpringBoot三種打包方法舉例詳解

    SpringBoot三種打包方法舉例詳解

    這篇文章主要給大家介紹了關(guān)于SpringBoot三種打包方法的相關(guān)資料,Spring Boot是一個開發(fā)框架,目的是簡化Spring應用的初始搭建過程和開發(fā)過程,文中提供了3種打包方法,需要的朋友可以參考下
    2023-12-12
  • 詳解Spring Boot中如何自定義SpringMVC配置

    詳解Spring Boot中如何自定義SpringMVC配置

    這篇文章主要給大家介紹了關(guān)于Spring Boot中如何自定義SpringMVC配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2021-09-09
  • MapStruct內(nèi)部錯誤:NullPointerException的解決方案

    MapStruct內(nèi)部錯誤:NullPointerException的解決方案

    在Java開發(fā)中,MapStruct是一個非常流行的對象映射工具,它通過注解處理器在編譯時生成映射代碼,極大地簡化了對象之間的轉(zhuǎn)換操作,本文將詳細分析一個常見的MapStruct內(nèi)部錯誤——NullPointerException,并提供一系列解決方案,需要的朋友可以參考下
    2025-02-02

最新評論

湘潭市| 漳平市| 平山县| 吉木乃县| 临沧市| 彩票| 新田县| 尼木县| 永修县| 甘孜县| 拜泉县| 宜兴市| 诸暨市| 临泉县| 建德市| 新河县| 临江市| 泰兴市| 西和县| 芜湖县| 永宁县| 东阳市| 景宁| 漾濞| 阿克苏市| 同仁县| 云龙县| 梓潼县| 天长市| 佛学| 专栏| 凤凰县| 贵德县| 梁山县| 枣强县| 南城县| 天气| 赤峰市| 修武县| 梁河县| 阿克苏市|