Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2017 HttpBuilder-NG Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package groovyx.net.http;
import com.burgstaller.okhttp.AuthenticationCacheInterceptor;
import com.burgstaller.okhttp.CachingAuthenticatorDecorator;
import com.burgstaller.okhttp.digest.CachingAuthenticator;
import com.burgstaller.okhttp.digest.DigestAuthenticator;
import groovy.lang.Closure;
import groovy.lang.DelegatesTo;
import okhttp3.*;
import okio.BufferedSink;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Function;
import static groovyx.net.http.FromServer.Header.keyValue;
import static groovyx.net.http.HttpBuilder.ResponseHandlerFunction.HANDLER_FUNCTION;
import static groovyx.net.http.HttpConfig.AuthType.DIGEST;
/**
* `HttpBuilder` implementation based on the http://square.github.io/okhttp/[OkHttp] client library.
*
* Generally, this class should not be used directly, the preferred method of instantiation is via one of the two static `configure()` methods of this
* class or using one of the `configure` methods of `HttpBuilder` with a factory function for this builder.
*/
public class OkHttpBuilder extends HttpBuilder {
private static final Function okFactory = OkHttpBuilder::new;
private final ChainedHttpConfig config;
private final HttpObjectConfig.Client clientConfig;
private final Executor executor;
private final OkHttpClient client;
protected OkHttpBuilder(final HttpObjectConfig config) {
super(config);
this.config = new HttpConfigs.ThreadSafeHttpConfig(config.getChainedConfig());
this.clientConfig = config.getClient();
this.executor = config.getExecution().getExecutor();
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
final SSLContext sslContext = config.getExecution().getSslContext();
if (sslContext != null) {
builder.sslSocketFactory(sslContext.getSocketFactory()/*, (X509TrustManager) TRUST_MANAGERS[0]*/);
builder.hostnameVerifier(config.getExecution().getHostnameVerifier());
}
// DIGEST support - defining this here only allows DIGEST config on the HttpBuilder configuration, not for individual methods.
final HttpConfig.Auth auth = config.getRequest().getAuth();
if (auth != null && auth.getAuthType() == DIGEST) {
Map authCache = new ConcurrentHashMap<>();
builder.addInterceptor(new AuthenticationCacheInterceptor(authCache));
builder.authenticator(new CachingAuthenticatorDecorator(
new DigestAuthenticator(new com.burgstaller.okhttp.digest.Credentials(auth.getUser(), auth.getPassword())), authCache)
);
}
final Consumer