Example usage for com.squareup.okhttp OkHttpClient setReadTimeout

List of usage examples for com.squareup.okhttp OkHttpClient setReadTimeout

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient setReadTimeout.

Prototype

public void setReadTimeout(long timeout, TimeUnit unit) 

Source Link

Document

Sets the default read timeout for new connections.

Usage

From source file:com.netflix.spinnaker.clouddriver.docker.registry.api.v2.client.DefaultDockerOkClientProvider.java

License:Apache License

@Override
public OkClient provide(String address, long timeoutMs, boolean insecure) {
    OkHttpClient client = new OkHttpClient();
    client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);

    if (insecure) {
        SSLContext sslContext;/*from w w  w  .  j  a va2  s.  c  om*/
        try {
            sslContext = SSLContext.getInstance("SSL");
            TrustManager[] trustManagers = { new TrustAllX509TrustManager() };
            sslContext.init(null, trustManagers, new SecureRandom());
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            throw new IllegalStateException("Failed configuring insecure SslSocketFactory", e);
        }
        client.setSslSocketFactory(sslContext.getSocketFactory());
    }

    return new OkClient(client);
}

From source file:com.netflix.spinnaker.igor.concourse.client.OkHttpClientBuilder.java

License:Apache License

public static OkHttpClient retryingClient(Supplier<Token> refreshToken) {
    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.interceptors().add(chain -> OkHttpClientBuilder.createRetryInterceptor(chain, refreshToken));
    okHttpClient.setHostnameVerifier((s, sslSession) -> true);
    okHttpClient.setSslSocketFactory(getSslContext().getSocketFactory());
    okHttpClient.setConnectTimeout(15, TimeUnit.SECONDS);
    okHttpClient.setReadTimeout(15, TimeUnit.SECONDS);
    return okHttpClient;
}

From source file:com.netflix.spinnaker.igor.config.GitlabCiConfig.java

License:Apache License

public static GitlabCiClient gitlabCiClient(String address, String privateToken, int timeout,
        ObjectMapper objectMapper) {/*from   w  ww .ja v a  2 s  .c o m*/
    OkHttpClient client = new OkHttpClient();
    client.setReadTimeout(timeout, TimeUnit.MILLISECONDS);

    //Need this code because without FULL log level, fetching logs will fail. Ref https://github.com/square/retrofit/issues/953.
    RestAdapter.Log fooLog = message -> {
    };
    return new RestAdapter.Builder().setEndpoint(Endpoints.newFixedEndpoint(address))
            .setRequestInterceptor(new GitlabCiHeaders(privateToken)).setClient(new OkClient(client))
            .setLog(fooLog).setLogLevel(RestAdapter.LogLevel.FULL)
            .setConverter(new JacksonConverter(objectMapper)).build().create(GitlabCiClient.class);
}

From source file:com.netflix.spinnaker.igor.config.TravisConfig.java

License:Apache License

public static TravisClient travisClient(String address, int timeout, ObjectMapper objectMapper) {
    OkHttpClient client = new OkHttpClient();
    client.setReadTimeout(timeout, TimeUnit.MILLISECONDS);

    //Need this code because without FULL log level, fetching logs will fail. Ref https://github.com/square/retrofit/issues/953.
    RestAdapter.Log fooLog = message -> {
    };//from w w  w  .j  a v  a2s.  com

    return new RestAdapter.Builder().setEndpoint(Endpoints.newFixedEndpoint(address))
            .setRequestInterceptor(new TravisHeader()).setClient(new OkClient(client)).setLog(fooLog)
            .setLogLevel(RestAdapter.LogLevel.FULL).setConverter(new JacksonConverter(objectMapper)).build()
            .create(TravisClient.class);
}

From source file:com.pacoworks.dereference.dependencies.modules.NetworkModule.java

License:Open Source License

@Provides
@Singleton//  w w  w .java  2  s  .  c o  m
OkHttpClient provideOkHttp(final Cache cache, LoggerInterceptor loggerInterceptor,
        StethoInterceptor stethoInterceptor) {
    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setCache(cache);
    okHttpClient.networkInterceptors().add(loggerInterceptor);
    okHttpClient.networkInterceptors().add(stethoInterceptor);
    okHttpClient.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    okHttpClient.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    okHttpClient.setWriteTimeout(DEFAULT_WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    return okHttpClient;
}

From source file:com.peach.masktime.module.net.OkHttpStack.java

License:Open Source License

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {

    OkHttpClient client = mClient.clone();
    int timeoutMs = request.getTimeoutMs();
    LogUtils.i(TAG, "timeoutMs = " + timeoutMs);
    client.setProxy(Proxy.NO_PROXY);
    client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);

    com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder();
    okHttpRequestBuilder.url(request.getUrl());

    Map<String, String> headers = request.getHeaders();
    for (final String name : headers.keySet()) {
        okHttpRequestBuilder.addHeader(name, headers.get(name));
    }/*from  w  w  w  .  j  a  va  2 s .co m*/
    for (final String name : additionalHeaders.keySet()) {
        okHttpRequestBuilder.addHeader(name, additionalHeaders.get(name));
    }

    setConnectionParametersForRequest(okHttpRequestBuilder, request);

    com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
    Call okHttpCall = client.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();

    StatusLine responseStatus = new BasicStatusLine(parseProtocol(okHttpResponse.protocol()),
            okHttpResponse.code(), okHttpResponse.message());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromOkHttpResponse(okHttpResponse));

    Headers responseHeaders = okHttpResponse.headers();
    for (int i = 0, len = responseHeaders.size(); i < len; i++) {
        final String name = responseHeaders.name(i), value = responseHeaders.value(i);
        if (name != null) {
            response.addHeader(new BasicHeader(name, value));
        }
    }
    return response;
}

From source file:com.phattn.vnexpressnews.io.OkHttpStack.java

License:Open Source License

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {

    OkHttpClient client = mClient.clone();
    int timeoutMs = request.getTimeoutMs();
    client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);

    com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder();
    okHttpRequestBuilder.url(request.getUrl());

    Map<String, String> headers = request.getHeaders();
    for (final String name : headers.keySet()) {
        okHttpRequestBuilder.addHeader(name, headers.get(name));
    }//from www .  ja v a  2  s.c o m
    for (final String name : additionalHeaders.keySet()) {
        okHttpRequestBuilder.addHeader(name, additionalHeaders.get(name));
    }

    setConnectionParametersForRequest(okHttpRequestBuilder, request);

    com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
    Call okHttpCall = client.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();

    StatusLine responseStatus = new BasicStatusLine(parseProtocol(okHttpResponse.protocol()),
            okHttpResponse.code(), okHttpResponse.message());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromOkHttpResponse(okHttpResponse));

    Headers responseHeaders = okHttpResponse.headers();
    for (int i = 0, length = responseHeaders.size(); i < length; i++) {
        final String name = responseHeaders.name(i);
        final String value = responseHeaders.value(i);
        if (name != null) {
            response.addHeader(new BasicHeader(name, value));
        }
    }

    return response;
}

From source file:com.raskasa.dropwizard.okhttp.OkHttpClientBuilder.java

License:Apache License

/** Builds the {@link OkHttpClient}. */
public OkHttpClient build() {
    final OkHttpClient rawClient = new OkHttpClient();
    if (configuration.getConnectTimeout() > 0L) {
        rawClient.setConnectTimeout(configuration.getConnectTimeout(), TimeUnit.MILLISECONDS);
    }/*  ww  w  .java2  s.  com*/
    if (configuration.getReadTimeout() > 0L) {
        rawClient.setReadTimeout(configuration.getReadTimeout(), TimeUnit.MILLISECONDS);
    }
    if (configuration.getWriteTimeout() > 0L) {
        rawClient.setWriteTimeout(configuration.getWriteTimeout(), TimeUnit.MILLISECONDS);
    }
    if (configuration.getCacheDir() != null && configuration.getCacheSize() > 0L) {
        rawClient.setCache(new Cache(configuration.getCacheDir(), configuration.getCacheSize()));
    }
    final OkHttpClient client = InstrumentedOkHttpClients.create(registry, rawClient, name);

    // If the environment is present, we tie the client with the server lifecycle
    if (environment != null) {
        environment.lifecycle().manage(new Managed() {
            @Override
            public void start() throws Exception {
            }

            @Override
            public void stop() throws Exception {
                client.getConnectionPool().evictAll();
            }
        });
    }
    return client;
}

From source file:com.raskasa.oksolr.SolrClient.java

License:Apache License

private OkHttpClient createOkHttpClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(10, SECONDS);
    client.setReadTimeout(10, SECONDS);
    client.setWriteTimeout(10, SECONDS);
    return client;
}

From source file:com.raskasa.oksolr.SolrClientTest.java

License:Apache License

@Test
public void testCustomTimeout() throws Exception {
    OkHttpClient okClient = new OkHttpClient();
    okClient.setConnectTimeout(20, SECONDS);
    okClient.setReadTimeout(20, SECONDS);
    okClient.setWriteTimeout(20, SECONDS);
    SolrClient solrClient = new SolrClient(okClient);

    assertThat(solrClient.getConnectTimeout()).isEqualTo(20000); // 20 secs
    assertThat(solrClient.getReadTimeout()).isEqualTo(20000); // 20 secs
    assertThat(solrClient.getWriteTimeout()).isEqualTo(20000); // 20 secs
}