Example usage for com.squareup.okhttp OkHttpClient clone

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

Introduction

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

Prototype

@Override
public OkHttpClient clone() 

Source Link

Document

Returns a shallow copy of this OkHttpClient.

Usage

From source file:cn.com.crcement.oa.base.download.helper.ProgressHelper.java

License:Apache License

/**
 * OkHttpClient//from  ww  w .  ja v a  2s  .c o m
 * 
 * @param client
 *            OkHttpClient
 * @param progressListener
 *            ?
 * @return ?OkHttpClientclone
 */
public static OkHttpClient addProgressResponseListener(OkHttpClient client,
        final ProgressListener progressListener) {
    // 
    OkHttpClient clone = client.clone();
    // 
    clone.networkInterceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            // 
            Response originalResponse = chain.proceed(chain.request());
            // ?
            return originalResponse.newBuilder()
                    .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
        }
    });
    return clone;
}

From source file:cn.edu.zafu.coreprogress.helper.ProgressHelper.java

License:Apache License

/**
 * OkHttpClient/*from  w  w w .j  av  a 2 s .  c o m*/
 * @param client OkHttpClient
 * @param progressListener ?
 * @return ?OkHttpClientclone
 */
public static OkHttpClient addProgressResponseListener(OkHttpClient client,
        final ProgressListener progressListener) {
    //
    OkHttpClient clone = client.clone();
    //
    clone.networkInterceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            //
            Response originalResponse = chain.proceed(chain.request());
            //?
            return originalResponse.newBuilder()
                    .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
        }
    });
    return clone;
}

From source file:com.burhan.udacity.popularmovies.data.api.ApiModule.java

License:Apache License

@Provides
@Singleton
@Named("Api")
OkHttpClient provideApiClient(OkHttpClient client) {
    return client.clone();
}

From source file:com.facebook.buck.artifact_cache.ArtifactCaches.java

License:Apache License

private static ArtifactCache createHttpArtifactCache(ArtifactCacheBuckConfig buckConfig,
        BuckEventBus buckEventBus, ProjectFilesystem projectFilesystem) {
    URI uri = buckConfig.getHttpCacheUrl();
    int timeoutSeconds = buckConfig.getHttpCacheTimeoutSeconds();
    boolean doStore = buckConfig.getHttpCacheReadMode();
    final String host = buckConfig.getHostToReportToRemoteCacheServer();

    // Setup the defaut client to use.
    OkHttpClient client = new OkHttpClient();
    client.networkInterceptors().add(new Interceptor() {
        @Override/*from www  .  jav a 2 s . c  om*/
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request().newBuilder()
                    .addHeader("X-BuckCache-User", System.getProperty("user.name", "<unknown>"))
                    .addHeader("X-BuckCache-Host", host).build());
        }
    });
    client.setConnectTimeout(timeoutSeconds, TimeUnit.SECONDS);
    client.setConnectionPool(new ConnectionPool(
            // It's important that this number is greater than the `-j` parallelism,
            // as if it's too small, we'll overflow the reusable connection pool and
            // start spamming new connections.  While this isn't the best location,
            // the other current option is setting this wherever we construct a `Build`
            // object and have access to the `-j` argument.  However, since that is
            // created in several places leave it here for now.
            /* maxIdleConnections */ 200, /* keepAliveDurationMs */ TimeUnit.MINUTES.toMillis(5)));

    // For fetches, use a client with a read timeout.
    OkHttpClient fetchClient = client.clone();
    fetchClient.setReadTimeout(timeoutSeconds, TimeUnit.SECONDS);

    return new HttpArtifactCache("http", fetchClient, client, uri, doStore, projectFilesystem, buckEventBus);
}

From source file:com.facebook.buck.cli.ArtifactCaches.java

License:Apache License

private static ArtifactCache createHttpArtifactCache(ArtifactCacheBuckConfig buckConfig,
        BuckEventBus buckEventBus, ProjectFilesystem projectFilesystem) {
    URL url = buckConfig.getHttpCacheUrl();
    int timeoutSeconds = buckConfig.getHttpCacheTimeoutSeconds();
    boolean doStore = buckConfig.getHttpCacheReadMode();
    final String host = buckConfig.getHostToReportToRemoteCacheServer();

    // Setup the defaut client to use.
    OkHttpClient client = new OkHttpClient();
    client.networkInterceptors().add(new Interceptor() {
        @Override/*  w w w  .java  2 s  .c  o m*/
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request().newBuilder()
                    .addHeader("X-BuckCache-User", System.getProperty("user.name", "<unknown>"))
                    .addHeader("X-BuckCache-Host", host).build());
        }
    });
    client.setConnectTimeout(timeoutSeconds, TimeUnit.SECONDS);
    client.setConnectionPool(new ConnectionPool(
            // It's important that this number is greater than the `-j` parallelism,
            // as if it's too small, we'll overflow the reusable connection pool and
            // start spamming new connections.  While this isn't the best location,
            // the other current option is setting this wherever we construct a `Build`
            // object and have access to the `-j` argument.  However, since that is
            // created in several places leave it here for now.
            /* maxIdleConnections */ 200, /* keepAliveDurationMs */ TimeUnit.MINUTES.toMillis(5)));

    // For fetches, use a client with a read timeout.
    OkHttpClient fetchClient = client.clone();
    fetchClient.setReadTimeout(timeoutSeconds, TimeUnit.SECONDS);

    return new HttpArtifactCache("http", fetchClient, client, url, doStore, projectFilesystem, buckEventBus,
            Hashing.crc32());
}

From source file:com.facebook.buck.cli.BuckConfig.java

License:Apache License

private ArtifactCache createHttpArtifactCache() {
    URL url;/*  www  . j a v  a 2s .com*/
    try {
        url = new URL(getValue("cache", "http_url").or(DEFAULT_HTTP_URL));
    } catch (MalformedURLException e) {
        throw new HumanReadableException(e, "Malformed [cache]http_url: %s", e.getMessage());
    }

    int timeoutSeconds = Integer
            .parseInt(getValue("cache", "http_timeout_seconds").or(DEFAULT_HTTP_CACHE_TIMEOUT_SECONDS));

    boolean doStore = readCacheMode("http_mode", DEFAULT_HTTP_CACHE_MODE);

    // Setup the defaut client to use.
    OkHttpClient client = new OkHttpClient();
    final String localhost = getLocalhost();
    client.networkInterceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request().newBuilder()
                    .addHeader("X-BuckCache-User", System.getProperty("user.name", "<unknown>"))
                    .addHeader("X-BuckCache-Host", localhost).build());
        }
    });
    client.setConnectTimeout(timeoutSeconds, TimeUnit.SECONDS);
    client.setConnectionPool(new ConnectionPool(
            // It's important that this number is greater than the `-j` parallelism,
            // as if it's too small, we'll overflow the reusable connection pool and
            // start spamming new connections.  While this isn't the best location,
            // the other current option is setting this wherever we construct a `Build`
            // object and have access to the `-j` argument.  However, since that is
            // created in several places leave it here for now.
            /* maxIdleConnections */ 200, /* keepAliveDurationMs */ TimeUnit.MINUTES.toMillis(5)));

    // For fetches, use a client with a read timeout.
    OkHttpClient fetchClient = client.clone();
    fetchClient.setReadTimeout(timeoutSeconds, TimeUnit.SECONDS);

    return new HttpArtifactCache(fetchClient, client, url, doStore, projectFilesystem, Hashing.crc32());
}

From source file:com.gdkm.sfk.utils.coreProgress.ProgressHelper.java

License:Apache License

/**
 * OkHttpClient/*from   w  w  w  .  j  a  v a2s.c  om*/
 * @param client OkHttpClient
 * @param progressListener ?
 * @return ?OkHttpClientclone
 */
public static OkHttpClient addProgressResponseListener(OkHttpClient client,
        final ProgressResponseListener progressListener) {
    //
    OkHttpClient clone = client.clone();
    //
    clone.networkInterceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            //
            Response originalResponse = chain.proceed(chain.request());
            //?
            return originalResponse.newBuilder()
                    .body(new ProgressResponseBody(originalResponse.body(), progressListener)).build();
        }
    });
    return clone;
}

From source file:com.tinspx.util.net.okhttp.OkHttpContext.java

License:Open Source License

private OkHttpContext(@NonNull OkHttpClient client, boolean debug) {
    //todo: always set no follow redirects, rather should the user be allowed
    //to set this? this will effect the request creation/submisstion
    //document info about the cookie handler
    client = client.clone();
    client.setFollowRedirects(false);/*from w  ww . j  av a2  s  . c  om*/
    if (client.getCookieHandler() == null) {
        client.setCookieHandler(new CookieManager());
    }
    this.client = client;
    this.debug = debug;
}

From source file:com.uele.examples.loginviewsmvp.injection.modules.NetworkModule.java

License:Apache License

@Provides
@Singleton
OkHttpClient provideApiClient(OkHttpClient client) {
    return client.clone();
}

From source file:io.fabric8.kubernetes.client.BaseClient.java

License:Apache License

public BaseClient(final OkHttpClient httpClient, Config config) throws KubernetesClientException {
    try {/*from ww  w  .  j  a v  a2 s.c o m*/
        this.httpClient = httpClient;
        this.namespace = config.getNamespace();
        this.configuration = config;
        this.apiVersion = config.getApiVersion();
        if (config.getMasterUrl() == null) {
            throw new KubernetesClientException("Unknown Kubernetes master URL - "
                    + "please set with the builder, or set with either system property \""
                    + Config.KUBERNETES_MASTER_SYSTEM_PROPERTY + "\"" + " or environment variable \""
                    + Utils.convertSystemPropertyNameToEnvVar(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY) + "\"");
        }
        this.masterUrl = new URL(config.getMasterUrl());

        Adapters.register(new ExtensionAdapter<OkHttpClient>() {
            @Override
            public Class<OkHttpClient> getExtensionType() {
                return OkHttpClient.class;
            }

            @Override
            public Boolean isAdaptable(Client client) {
                return client instanceof BaseClient;
            }

            @Override
            public OkHttpClient adapt(Client client) {
                return httpClient.clone();
            }
        });

    } catch (Exception e) {
        throw KubernetesClientException.launderThrowable(e);
    }
}