List of usage examples for com.squareup.okhttp Response.Builder header
public String header(String name, String defaultValue)
From source file:com.parse.ParseOkHttpClient.java
License:Open Source License
/** * For OKHttpClient, since it does not expose any interface for us to check the raw response * stream, we have to use OKHttp networkInterceptors. Instead of using our own interceptor list, * we use OKHttp inner interceptor list. * @param parseNetworkInterceptor/*from w ww .j a va2s. c o m*/ */ @Override /* package */ void addExternalInterceptor(final ParseNetworkInterceptor parseNetworkInterceptor) { okHttpClient.networkInterceptors().add(new Interceptor() { @Override public Response intercept(final Chain okHttpChain) throws IOException { Request okHttpRequest = okHttpChain.request(); // Transfer OkHttpRequest to ParseHttpRequest final ParseHttpRequest parseRequest = getParseHttpRequest(okHttpRequest); // Capture OkHttpResponse final Capture<Response> okHttpResponseCapture = new Capture<>(); final ParseHttpResponse parseResponse = parseNetworkInterceptor .intercept(new ParseNetworkInterceptor.Chain() { @Override public ParseHttpRequest getRequest() { return parseRequest; } @Override public ParseHttpResponse proceed(ParseHttpRequest parseRequest) throws IOException { // Use OKHttpClient to send request Request okHttpRequest = ParseOkHttpClient.this.getRequest(parseRequest); Response okHttpResponse = okHttpChain.proceed(okHttpRequest); okHttpResponseCapture.set(okHttpResponse); return getResponse(okHttpResponse); } }); final Response okHttpResponse = okHttpResponseCapture.get(); // Ideally we should build newOkHttpResponse only based on parseResponse, however // ParseHttpResponse does not have all the info we need to build the newOkHttpResponse, so // we rely on the okHttpResponse to generate the builder and change the necessary info // inside Response.Builder newOkHttpResponseBuilder = okHttpResponse.newBuilder(); // Set status newOkHttpResponseBuilder.code(parseResponse.getStatusCode()) .message(parseResponse.getReasonPhrase()); // Set headers if (parseResponse.getAllHeaders() != null) { for (Map.Entry<String, String> entry : parseResponse.getAllHeaders().entrySet()) { newOkHttpResponseBuilder.header(entry.getKey(), entry.getValue()); } } // Set body newOkHttpResponseBuilder.body(new ResponseBody() { @Override public MediaType contentType() { if (parseResponse.getContentType() == null) { return null; } return MediaType.parse(parseResponse.getContentType()); } @Override public long contentLength() throws IOException { return parseResponse.getTotalSize(); } @Override public BufferedSource source() throws IOException { // We need to use the proxy stream from interceptor to replace the origin network // stream, so when the stream is read by Parse, the network stream is proxyed in the // interceptor. if (parseResponse.getContent() == null) { return null; } return Okio.buffer(Okio.source(parseResponse.getContent())); } }); return newOkHttpResponseBuilder.build(); } }); }
From source file:org.mariotaku.twidere.util.OAuthPasswordAuthenticator.java
License:Open Source License
public OAuthPasswordAuthenticator(final TwitterOAuth oauth, final LoginVerificationCallback loginVerificationCallback, final String userAgent) { final RestClient restClient = RestAPIFactory.getRestClient(oauth); this.oauth = oauth; this.client = (OkHttpRestClient) restClient.getRestClient(); final OkHttpClient okhttp = client.getClient(); okhttp.setCookieHandler(new CookieManager()); okhttp.networkInterceptors().add(new Interceptor() { @Override/*from w ww. j a v a 2 s .co m*/ public Response intercept(Chain chain) throws IOException { final Response response = chain.proceed(chain.request()); if (!response.isRedirect()) { return response; } final String location = response.header("Location"); final Response.Builder builder = response.newBuilder(); if (!TextUtils.isEmpty(location) && !endpoint.checkEndpoint(location)) { final HttpUrl originalLocation = HttpUrl .get(URI.create("https://api.twitter.com/").resolve(location)); final HttpUrl.Builder locationBuilder = HttpUrl.parse(endpoint.getUrl()).newBuilder(); for (String pathSegments : originalLocation.pathSegments()) { locationBuilder.addPathSegment(pathSegments); } for (int i = 0, j = originalLocation.querySize(); i < j; i++) { final String name = originalLocation.queryParameterName(i); final String value = originalLocation.queryParameterValue(i); locationBuilder.addQueryParameter(name, value); } final String encodedFragment = originalLocation.encodedFragment(); if (encodedFragment != null) { locationBuilder.encodedFragment(encodedFragment); } final HttpUrl newLocation = locationBuilder.build(); builder.header("Location", newLocation.toString()); } return builder.build(); } }); this.endpoint = restClient.getEndpoint(); this.loginVerificationCallback = loginVerificationCallback; this.userAgent = userAgent; }