List of usage examples for com.squareup.okhttp Interceptor Interceptor
Interceptor
From source file:au.com.wallaceit.reddinator.RedditData.java
License:Open Source License
private boolean createHttpClient() { httpClient = new OkHttpClient(); httpClient.setConnectTimeout(10, TimeUnit.SECONDS); httpClient.setReadTimeout(10, TimeUnit.SECONDS); httpClient.networkInterceptors().add(new Interceptor() { @Override//from ww w . jav a 2s. c om public Response intercept(Chain chain) throws IOException { Request originalRequest = chain.request(); Request requestWithUserAgent = originalRequest.newBuilder().removeHeader("User-Agent") .addHeader("User-Agent", userAgent).build(); return chain.proceed(requestWithUserAgent); } }); return true; }
From source file:bitrefill.retrofit.Generator.java
static void configAuth(OkHttpClient client, Config config) { final String auth = getAuth(config); if (auth == null) { return;/* w w w. j a va 2s . c o m*/ } client.interceptors().clear(); client.interceptors().add(new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); Request.Builder requestBuilder = original.newBuilder().header("Authorization", "Basic " + auth) .header("Accept", "applicaton/json").method(original.method(), original.body()); Request request = requestBuilder.build(); return chain.proceed(request); } }); }
From source file:cn.com.crcement.oa.base.download.helper.ProgressHelper.java
License:Apache License
/** * OkHttpClient//from w w w . j a va 2 s .co 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 www .j a va 2 s.c om * @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:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void cancelWithInterceptor() throws Exception { client.interceptors().add(new Interceptor() { @Override/*from www . j a v a 2s.co m*/ public Response intercept(Chain chain) throws IOException { chain.proceed(chain.request()); throw new AssertionError(); // We expect an exception. } }); Call call = client.newCall(new Request.Builder().url(server.getUrl("/a")).build()); call.cancel(); try { call.execute(); fail(); } catch (IOException expected) { } assertEquals(0, server.getRequestCount()); }
From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java
License:Open Source License
@Test public void applicationInterceptorsCanShortCircuitResponses() throws Exception { server.get().shutdown(); // Accept no connections. Request request = new Request.Builder().url("https://localhost:1/").build(); final Response interceptorResponse = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1) .code(200).message("Intercepted!") .body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).build(); client.interceptors().add(new Interceptor() { @Override/*from w w w. j a v a2s . c om*/ public Response intercept(Chain chain) throws IOException { return interceptorResponse; } }); Response response = FiberOkHttpUtil.executeInFiber(client, request); assertSame(interceptorResponse, response); }
From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java
License:Open Source License
@Ignore @Test/*from w w w. j a v a 2s.c o m*/ public void networkInterceptorsCannotShortCircuitResponses() throws Exception { server.enqueue(new MockResponse().setResponseCode(500)); Interceptor interceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { return new Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1).code(200) .message("Intercepted!") .body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).build(); } }; client.networkInterceptors().add(interceptor); Request request = new Request.Builder().url(server.getUrl("/")).build(); try { FiberOkHttpUtil.executeInFiber(client, request); fail(); } catch (IllegalStateException expected) { assertEquals("network interceptor " + interceptor + " must call proceed() exactly once", expected.getMessage()); } }
From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java
License:Open Source License
@Ignore @Test/*from w w w .j av a 2s.c o m*/ public void networkInterceptorsCannotCallProceedMultipleTimes() throws Exception { server.enqueue(new MockResponse()); server.enqueue(new MockResponse()); Interceptor interceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { chain.proceed(chain.request()); return chain.proceed(chain.request()); } }; client.networkInterceptors().add(interceptor); Request request = new Request.Builder().url(server.getUrl("/")).build(); try { FiberOkHttpUtil.executeInFiber(client, request); fail(); } catch (IllegalStateException expected) { assertEquals("network interceptor " + interceptor + " must call proceed() exactly once", expected.getMessage()); } }
From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java
License:Open Source License
@Ignore @Test//from w ww . ja va 2s. c o m public void networkInterceptorsCannotChangeServerAddress() throws Exception { server.enqueue(new MockResponse().setResponseCode(500)); Interceptor interceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Address address = chain.connection().getRoute().getAddress(); String sameHost = address.getUriHost(); int differentPort = address.getUriPort() + 1; return chain.proceed(chain.request().newBuilder() .url(new URL("http://" + sameHost + ":" + differentPort + "/")).build()); } }; client.networkInterceptors().add(interceptor); Request request = new Request.Builder().url(server.getUrl("/")).build(); try { FiberOkHttpUtil.executeInFiber(client, request); fail(); } catch (IllegalStateException expected) { assertEquals("network interceptor " + interceptor + " must retain the same host and port", expected.getMessage()); } }
From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java
License:Open Source License
@Test public void networkInterceptorsHaveConnectionAccess() throws Exception { server.enqueue(new MockResponse()); client.networkInterceptors().add(new Interceptor() { @Override/*from www.j av a2s . c o m*/ public Response intercept(Chain chain) throws IOException { Connection connection = chain.connection(); assertNotNull(connection); return chain.proceed(chain.request()); } }); Request request = new Request.Builder().url(server.getUrl("/")).build(); FiberOkHttpUtil.executeInFiber(client, request); }