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();
}
Aggregations