Skip to content

Update for OkHttp3 #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Parse/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ android {
dependencies {
compile 'com.parse.bolts:bolts-tasks:1.4.0'

provided 'com.squareup.okhttp:okhttp:2.4.0'
provided 'com.facebook.stetho:stetho:1.1.1'
provided 'com.squareup.okhttp3:okhttp:3.2.0'
provided 'com.facebook.stetho:stetho:1.3.0'

testCompile 'org.robolectric:robolectric:3.0'
testCompile 'org.skyscreamer:jsonassert:1.2.3'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'com.squareup.okhttp:mockwebserver:2.4.0'
testCompile 'com.squareup.okhttp3:mockwebserver:3.2.0'
}

android.libraryVariants.all { variant ->
Expand Down
4 changes: 2 additions & 2 deletions Parse/src/main/java/com/parse/ParseHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@

private static final String APACHE_HTTPCLIENT_NAME = "org.apache.http";
private static final String URLCONNECTION_NAME = "net.java.URLConnection";
private static final String OKHTTP_NAME = "com.squareup.okhttp";
private static final String OKHTTP_NAME = "com.squareup.okhttp3";

private static final String OKHTTPCLIENT_PATH = "com.squareup.okhttp.OkHttpClient";
private static final String OKHTTPCLIENT_PATH = "okhttp3.OkHttpClient";

private static final String MAX_CONNECTIONS_PROPERTY_NAME = "http.maxConnections";
private static final String KEEP_ALIVE_PROPERTY_NAME = "http.keepAlive";
Expand Down
41 changes: 21 additions & 20 deletions Parse/src/main/java/com/parse/ParseOkHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,12 @@
*/
package com.parse;

import android.net.SSLCertificateSocketFactory;
import android.net.SSLSessionCache;

import com.parse.http.ParseHttpBody;
import com.parse.http.ParseHttpRequest;
import com.parse.http.ParseHttpResponse;
import com.parse.http.ParseNetworkInterceptor;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -33,6 +23,15 @@
import java.util.concurrent.TimeUnit;

import bolts.Capture;
import okhttp3.Call;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
Expand All @@ -48,17 +47,16 @@

public ParseOkHttpClient(int socketOperationTimeout, SSLSessionCache sslSessionCache) {

okHttpClient = new OkHttpClient();
OkHttpClient.Builder builder = new OkHttpClient.Builder();

okHttpClient.setConnectTimeout(socketOperationTimeout, TimeUnit.MILLISECONDS);
okHttpClient.setReadTimeout(socketOperationTimeout, TimeUnit.MILLISECONDS);
builder.connectTimeout(socketOperationTimeout, TimeUnit.MILLISECONDS);
builder.readTimeout(socketOperationTimeout, TimeUnit.MILLISECONDS);

// Don't handle redirects. We copy the setting from AndroidHttpClient.
// For detail, check https://quip.com/Px8jAxnaun2r
okHttpClient.setFollowRedirects(false);
builder.followRedirects(false);

okHttpClient.setSslSocketFactory(SSLCertificateSocketFactory.getDefault(
socketOperationTimeout, sslSessionCache));
okHttpClient = builder.build();
}

@Override
Expand Down Expand Up @@ -183,7 +181,7 @@ private ParseHttpRequest getParseHttpRequest(Request okHttpRequest) {
}

// Set url
parseRequestBuilder.setUrl(okHttpRequest.urlString());
parseRequestBuilder.setUrl(okHttpRequest.url().toString());

// Set Header
for (Map.Entry<String, List<String>> entry : okHttpRequest.headers().toMultimap().entrySet()) {
Expand All @@ -206,7 +204,8 @@ private ParseHttpRequest getParseHttpRequest(Request okHttpRequest) {
*/
@Override
/* package */ void addExternalInterceptor(final ParseNetworkInterceptor parseNetworkInterceptor) {
okHttpClient.networkInterceptors().add(new Interceptor() {
OkHttpClient.Builder builder = okHttpClient.newBuilder();
builder.networkInterceptors().add(new Interceptor() {
@Override
public Response intercept(final Chain okHttpChain) throws IOException {
Request okHttpRequest = okHttpChain.request();
Expand Down Expand Up @@ -257,12 +256,12 @@ public MediaType contentType() {
}

@Override
public long contentLength() throws IOException {
public long contentLength() {
return parseResponse.getTotalSize();
}

@Override
public BufferedSource source() throws IOException {
public BufferedSource source() {
// 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.
Expand All @@ -276,6 +275,8 @@ public BufferedSource source() throws IOException {
return newOkHttpResponseBuilder.build();
}
});

okHttpClient = builder.build();
}

private static class ParseOkHttpRequestBody extends RequestBody {
Expand Down
12 changes: 6 additions & 6 deletions Parse/src/test/java/com/parse/ParseHttpClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import com.parse.http.ParseHttpRequest;
import com.parse.http.ParseHttpResponse;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;
import okhttp3.Headers;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;

import org.json.JSONObject;
import org.junit.Test;
Expand Down Expand Up @@ -108,7 +108,7 @@ private void doSingleParseHttpClientExecuteWithResponse(int responseCode, String
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("User-Agent", "Parse Android SDK");

String requestUrl = server.getUrl("/").toString();
String requestUrl = server.url("/").toString();
JSONObject json = new JSONObject();
json.put("key", "value");
String requestContent = json.toString();
Expand Down Expand Up @@ -183,7 +183,7 @@ private void doSingleParseHttpClientExecuteWithGzipResponse(
server.start();

// We do not need to add Accept-Encoding header manually, httpClient library should do that.
String requestUrl = server.getUrl("/").toString();
String requestUrl = server.url("/").toString();
ParseHttpRequest parseRequest = new ParseHttpRequest.Builder()
.setUrl(requestUrl)
.setMethod(ParseHttpRequest.Method.GET)
Expand Down
32 changes: 16 additions & 16 deletions Parse/src/test/java/com/parse/ParseOkHttpClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import com.parse.http.ParseHttpRequest;
import com.parse.http.ParseHttpResponse;
import com.parse.http.ParseNetworkInterceptor;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;
import okhttp3.MediaType;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;

import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -116,7 +116,7 @@ public void testGetOkHttpRequest() throws IOException {
// Verify method
assertEquals(ParseHttpRequest.Method.POST.toString(), okHttpRequest.method());
// Verify URL
assertEquals(url, okHttpRequest.urlString());
assertEquals(url, okHttpRequest.url().toString());
// Verify Headers
assertEquals(1, okHttpRequest.headers(headerName).size());
assertEquals(headerValue, okHttpRequest.headers(headerName).get(0));
Expand Down Expand Up @@ -173,12 +173,12 @@ public MediaType contentType() {
}

@Override
public long contentLength() throws IOException {
public long contentLength() {
return contentLength;
}

@Override
public BufferedSource source() throws IOException {
public BufferedSource source() {
Buffer buffer = new Buffer();
buffer.write(content.getBytes());
return buffer;
Expand Down Expand Up @@ -252,7 +252,7 @@ public ParseHttpResponse intercept(Chain chain) throws IOException {
});

// We do not need to add Accept-Encoding header manually, httpClient library should do that.
String requestUrl = server.getUrl("/").toString();
String requestUrl = server.url("/").toString();
ParseHttpRequest parseRequest = new ParseHttpRequest.Builder()
.setUrl(requestUrl)
.setMethod(ParseHttpRequest.Method.GET)
Expand Down Expand Up @@ -359,7 +359,7 @@ private ParseHttpRequest generateClientRequest() throws Exception {
JSONObject json = new JSONObject();
json.put("key", "value");
ParseHttpRequest parseRequest = new ParseHttpRequest.Builder()
.setUrl(server.getUrl("/").toString())
.setUrl(server.url("/").toString())
.setMethod(ParseHttpRequest.Method.POST)
.setBody(new ParseByteArrayHttpBody(json.toString().getBytes(), "application/json"))
.setHeaders(headers)
Expand All @@ -370,7 +370,7 @@ private ParseHttpRequest generateClientRequest() throws Exception {
// Verify the request from client, if you change the data in generateClientRequest, make
// sure you also change the condition in this method otherwise tests will fail
private void verifyClientRequest(ParseHttpRequest parseRequest) throws IOException {
assertEquals(server.getUrl("/").toString(), parseRequest.getUrl());
assertEquals(server.url("/").toString(), parseRequest.getUrl());
assertEquals(ParseHttpRequest.Method.POST, parseRequest.getMethod());
assertEquals("requestValue", parseRequest.getHeader("requestKey"));
assertEquals("application/json", parseRequest.getBody().getContentType());
Expand All @@ -390,7 +390,7 @@ private ParseHttpRequest generateInterceptorRequest() {
ParseHttpRequest requestAgain =
new ParseHttpRequest.Builder()
.addHeader("requestKeyAgain", "requestValueAgain")
.setUrl(server.getUrl("/test").toString())
.setUrl(server.url("/test").toString())
.setMethod(ParseHttpRequest.Method.GET)
.build();
return requestAgain;
Expand Down