List of usage examples for com.squareup.okhttp OkHttpClient networkInterceptors
List networkInterceptors
To view the source code for com.squareup.okhttp OkHttpClient networkInterceptors.
Click Source Link
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/* ww w.j ava2 s . com*/ 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/*from w ww . j a va 2 s.co 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;//from ww w .j ava2s. c o m 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 ww. j a va 2s. c o m * @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.linroid.pushapp.module.DataModule.java
License:Apache License
@Provides @Singleton// ww w. jav a2 s .c o m OkHttpClient provideOkHttp(Cache cache) { OkHttpClient okHttp = new OkHttpClient(); okHttp.setCache(cache); okHttp.setConnectTimeout(30, TimeUnit.SECONDS); okHttp.setReadTimeout(30, TimeUnit.SECONDS); okHttp.setWriteTimeout(30, TimeUnit.SECONDS); okHttp.networkInterceptors().add(new StethoInterceptor()); return okHttp; }
From source file:com.mobimvp.cliques.service.RequestManager.java
License:Apache License
private static RequestQueue newRequestQueue() { OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.networkInterceptors().add(new StethoInterceptor()); RequestQueue requestQueue = new RequestQueue(openCache(), new BasicNetwork(new OkHttpStack(okHttpClient))); requestQueue.start();/* www.j av a 2 s . c om*/ return requestQueue; }
From source file:com.moesif.android.okhttp2.MoesifOkHttp2Stack.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); client.networkInterceptors().add(new MoesifOkHttp2Interceptor()); 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 av a 2 s . c om 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.mycelium.wallet.external.cashila.api.CashilaService.java
License:Microsoft Reference Source License
public CashilaService(String baseUrl, String apiVersion, Bus eventBus) { this.baseUrl = baseUrl; this.eventBus = eventBus; OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(5000, TimeUnit.MILLISECONDS); client.setReadTimeout(5000, TimeUnit.MILLISECONDS); client.networkInterceptors().add(hmacInterceptor); CertificatePinner certPinner = new CertificatePinner.Builder().add("cashila.com", CASHILA_CERT) .add("cashila-staging.com", CASHILA_CERT).build(); client.setCertificatePinner(certPinner); // use jackson as json mapper, as we already use it in the rest of the project objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); objectMapper.registerModule(new WapiJsonModule()); RestAdapter adapter = new RestAdapter.Builder().setEndpoint(baseUrl + apiVersion + "/") .setLogLevel(RestAdapter.LogLevel.BASIC) //.setLogLevel(RestAdapter.LogLevel.FULL) .setConverter(new JacksonConverter(objectMapper)).setClient(new OkClient(client)) .setRequestInterceptor(apiIdInterceptor).build(); cashila = adapter.create(Cashila.class); // initialise nonce with current time and increase by one on each call lastNonce = System.currentTimeMillis(); }
From source file:com.nbonnec.mediaseb.di.modules.ApiModule.java
License:Apache License
private static OkHttpClient createOkHttpClient(Application app) { OkHttpClient client = new OkHttpClient(); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); client.setCookieHandler(cookieManager); client.networkInterceptors().add(new UserAgentInterceptor(USER_AGENT)); try {//from w ww.j a va 2 s. c om File cacheDir = new File(app.getCacheDir(), "http"); Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE); client.setCache(cache); } catch (NullPointerException e) { Log.e(TAG, "Unable to initialize OkHttpclient with disk cache", e); } return client; }
From source file:com.pacoworks.dereference.dependencies.modules.NetworkModule.java
License:Open Source License
@Provides @Singleton//from w ww . ja va 2 s . com 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; }