org.graylog2.telemetry.providers.OkHttpClientProvider Maven / Gradle / Ivy
package org.graylog2.telemetry.providers;
import com.github.joschi.jadconfig.util.Duration;
import com.squareup.okhttp.OkHttpClient;
import org.graylog2.telemetry.okhttp.GzipRequestInterceptor;
import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
public class OkHttpClientProvider implements Provider {
private final OkHttpClient client;
@Inject
public OkHttpClientProvider(
@Named("telemetry_service_connect_timeout") Duration connectTimeout,
@Named("telemetry_service_read_timeout") Duration readTimeout,
@Named("telemetry_service_write_timeout") Duration writeTimeout,
@Named("telemetry_gzip_enabled") boolean gzipEnabled,
@Named("http_proxy_uri") @Nullable URI httpProxyUri
) {
client = new OkHttpClient();
client.setRetryOnConnectionFailure(true);
client.setConnectTimeout(connectTimeout.getQuantity(), connectTimeout.getUnit());
client.setWriteTimeout(writeTimeout.getQuantity(), writeTimeout.getUnit());
client.setReadTimeout(readTimeout.getQuantity(), readTimeout.getUnit());
if(gzipEnabled) {
client.interceptors().add(new GzipRequestInterceptor());
}
if (httpProxyUri != null) {
final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpProxyUri.getHost(), httpProxyUri.getPort()));
client.setProxy(proxy);
}
}
@Override
public OkHttpClient get() {
return client;
}
}