List of usage examples for com.squareup.okhttp Request header
public String header(String name)
From source file:org.dataconservancy.cos.osf.client.support.ApiVersionInterceptorTest.java
License:Apache License
/** * Insures the HTTP header is added with the supplied version. * * @throws Exception/* w ww .j a v a 2s . c om*/ */ @Test public void testChain() throws Exception { final String version = "foo"; final Interceptor.Chain chain = mock(Interceptor.Chain.class); final Request req = new Request.Builder().url("http://example.org").build(); when(chain.request()).thenReturn(req); when(chain.proceed(any(Request.class))).then(invocation -> { final Request request = (Request) invocation.getArguments()[0]; assertTrue(request.header("Accept").endsWith(version)); return null; // response is ignored in this test. }); new ApiVersionInterceptor(version).intercept(chain); verify(chain).proceed(any(Request.class)); }
From source file:org.dataconservancy.cos.osf.client.support.AuthInterceptor.java
License:Apache License
@Override public Response intercept(final Chain chain) throws IOException { String localAuthheader = null; // Check the value of the authHeader field, which may have been set on construction if (authHeader != null && !authHeader.trim().equals("")) { localAuthheader = authHeader;/* w ww . j av a 2 s . co m*/ } // If it wasn't set, see if there's a configuration service and use that if (osfConfigurationService != null && osfConfigurationService.getConfiguration().getAuthHeader() != null && !osfConfigurationService.getConfiguration().getAuthHeader().trim().equals("")) { localAuthheader = osfConfigurationService.getConfiguration().getAuthHeader(); } // if the auth header has been set, use it, otherwise simply proceed with the chain. if (localAuthheader == null) { return chain.proceed(chain.request()); } Request req = chain.request(); if (req.header("Authorization") == null) { req = req.newBuilder().addHeader("Authorization", localAuthheader).build(); } return chain.proceed(req); }
From source file:retrofit.KOkHttpCall.java
License:Apache License
@Override public void enqueue(final Callback<T> callback) { synchronized (this) { if (executed) throw new IllegalStateException("Already executed"); executed = true;//from w w w . j av a2 s . c o m } final com.squareup.okhttp.Request request = createRequest(); // ----------------------------------------------------------------------cgp String headerValue = request.header("RequestMode"); if (!TextUtils.isEmpty(headerValue)) { switch (headerValue) { case RequestMode.LOAD_NETWORK_ELSE_CACHE:// ?? exeRequest(callback, request, true); return; case RequestMode.LOAD_CACHE_ELSE_NETWORK:// ? // ---------------------? Response<T> response = execCacheRequest(request); if (response != null) { callback.onResponse(response); return; } // ---------------------? // case RequestMode.LOAD_DEFAULT: case RequestMode.LOAD_NETWORK_ONLY: default: break;// } } // ----------------------------------------------------------------------cgp // new ExecutorCallAdapterFactory(null). exeRequest(callback, request, false); }
From source file:retrofit.KOkHttpCall.java
License:Apache License
public Response<T> execute() throws IOException { synchronized (this) { if (executed) throw new IllegalStateException("Already executed"); executed = true;//from ww w . j a v a 2 s. c om } com.squareup.okhttp.Request request = createRequest(); // ----------------------------------------------------------------------cgp String headerValue = request.header("RequestMode"); if (!TextUtils.isEmpty(headerValue)) { switch (headerValue) { case RequestMode.LOAD_NETWORK_ELSE_CACHE:// ?? com.squareup.okhttp.Call rawCall = client.newCall(request); if (canceled) { rawCall.cancel(); } this.rawCall = rawCall; Response<T> response; try { response = parseResponse(rawCall.execute(), request); } catch (Exception e) { response = execCacheRequest(request); } return response; case RequestMode.LOAD_CACHE_ELSE_NETWORK:// ? // ---------------------? response = execCacheRequest(request); if (response != null) { return response; } // ---------------------? // case RequestMode.LOAD_DEFAULT: case RequestMode.LOAD_NETWORK_ONLY: default: break;// } } // ----------------------------------------------------------------------cgp com.squareup.okhttp.Call rawCall = client.newCall(request); if (canceled) { rawCall.cancel(); } this.rawCall = rawCall; return parseResponse(rawCall.execute(), request); }