Search in sources :

Example 1 with OkHttpResponseBody

use of com.dtflys.forest.backend.okhttp3.response.OkHttpResponseBody in project forest by dromara.

the class OkHttp3ConnectionManager method getClient.

public OkHttpClient getClient(ForestRequest request, LifeCycleHandler lifeCycleHandler) {
    Integer timeout = request.getTimeout();
    Integer connectTimeout = request.connectTimeout();
    Integer readTimeout = request.readTimeout();
    if (TimeUtils.isNone(connectTimeout)) {
        connectTimeout = timeout;
    }
    if (TimeUtils.isNone(readTimeout)) {
        readTimeout = timeout;
    }
    OkHttpClient.Builder builder = new OkHttpClient.Builder().connectionPool(pool).dispatcher(dispatcher).connectTimeout(connectTimeout, TimeUnit.MILLISECONDS).readTimeout(readTimeout, TimeUnit.MILLISECONDS).protocols(getProtocols(request)).followRedirects(false).followSslRedirects(false).cookieJar(new CookieJar() {

        @Override
        public void saveFromResponse(HttpUrl url, List<Cookie> okCookies) {
            ForestCookies cookies = new ForestCookies();
            for (Cookie okCookie : okCookies) {
                long currentTime = System.currentTimeMillis();
                ForestCookie cookie = ForestCookie.createFromOkHttpCookie(currentTime, okCookie);
                cookies.addCookie(cookie);
            }
            lifeCycleHandler.handleSaveCookie(request, cookies);
        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl url) {
            ForestCookies cookies = new ForestCookies();
            lifeCycleHandler.handleLoadCookie(request, cookies);
            List<ForestCookie> forestCookies = cookies.allCookies();
            List<Cookie> okCookies = new ArrayList<>(forestCookies.size());
            for (ForestCookie cookie : forestCookies) {
                Duration maxAge = cookie.getMaxAge();
                Date createTime = cookie.getCreateTime();
                long expiresAt = createTime.getTime() + maxAge.toMillis();
                Cookie.Builder cookieBuilder = new Cookie.Builder();
                cookieBuilder.name(cookie.getName()).value(cookie.getValue()).expiresAt(expiresAt).path(cookie.getPath());
                if (cookie.isHostOnly()) {
                    cookieBuilder.hostOnlyDomain(cookie.getDomain());
                } else {
                    cookieBuilder.domain(cookie.getDomain());
                }
                if (cookie.isHttpOnly()) {
                    cookieBuilder.httpOnly();
                }
                if (cookie.isSecure()) {
                    cookieBuilder.secure();
                }
                Cookie okCookie = cookieBuilder.build();
                okCookies.add(okCookie);
            }
            return okCookies;
        }
    });
    // set proxy
    ForestProxy proxy = request.getProxy();
    if (proxy != null) {
        Proxy okProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy.getHost(), proxy.getPort()));
        builder.proxy(okProxy);
        if (StringUtils.isNotEmpty(proxy.getUsername())) {
            builder.proxyAuthenticator(new Authenticator() {

                @Nullable
                @Override
                public Request authenticate(@Nullable Route route, Response response) {
                    Request.Builder proxyBuilder = response.request().newBuilder();
                    String credential = Credentials.basic(proxy.getUsername(), proxy.getPassword());
                    proxyBuilder.addHeader("Proxy-Authorization", credential);
                    return proxyBuilder.build();
                }
            });
        }
    }
    if (request.isSSL()) {
        SSLSocketFactory sslSocketFactory = request.getSSLSocketFactory();
        builder.sslSocketFactory(sslSocketFactory, getX509TrustManager(request)).hostnameVerifier(request.hostnameVerifier());
    }
    // add default interceptor
    builder.addNetworkInterceptor(chain -> {
        Response response = chain.proceed(chain.request());
        return response.newBuilder().body(new OkHttpResponseBody(request, response.body(), lifeCycleHandler)).build();
    });
    return builder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) InetSocketAddress(java.net.InetSocketAddress) ForestCookie(com.dtflys.forest.http.ForestCookie) ForestProxy(com.dtflys.forest.http.ForestProxy) Proxy(java.net.Proxy) OkHttpResponseBody(com.dtflys.forest.backend.okhttp3.response.OkHttpResponseBody) ArrayList(java.util.ArrayList) List(java.util.List) CookieJar(okhttp3.CookieJar) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) ForestProxy(com.dtflys.forest.http.ForestProxy) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route) Cookie(okhttp3.Cookie) ForestCookie(com.dtflys.forest.http.ForestCookie) ForestRequest(com.dtflys.forest.http.ForestRequest) Request(okhttp3.Request) Duration(java.time.Duration) HttpUrl(okhttp3.HttpUrl) Date(java.util.Date) Response(okhttp3.Response) ForestCookies(com.dtflys.forest.http.ForestCookies) Nullable(javax.annotation.Nullable)

Aggregations

OkHttpResponseBody (com.dtflys.forest.backend.okhttp3.response.OkHttpResponseBody)1 ForestCookie (com.dtflys.forest.http.ForestCookie)1 ForestCookies (com.dtflys.forest.http.ForestCookies)1 ForestProxy (com.dtflys.forest.http.ForestProxy)1 ForestRequest (com.dtflys.forest.http.ForestRequest)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Nullable (javax.annotation.Nullable)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 Authenticator (okhttp3.Authenticator)1 Cookie (okhttp3.Cookie)1 CookieJar (okhttp3.CookieJar)1 HttpUrl (okhttp3.HttpUrl)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1