Skip to content
This repository was archived by the owner on Feb 14, 2020. It is now read-only.

Commit 5cd3788

Browse files
committed
convert to HttpURLConnection/OkHttp
- use DefaultOAuthConsumer/Provider instead of CommonsHttpOauthConsumer/Provider - overloaded Twitter.signRequest to accept HttpURLConnection - use OkHttp instead of HttpURLConnection if OkHttp exists in user's classpath
1 parent a1449bc commit 5cd3788

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed

library/src/main/java/com/parse/internal/signpost/basic/DefaultOAuthProvider.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,26 @@ public class DefaultOAuthProvider extends AbstractOAuthProvider {
3131

3232
private static final long serialVersionUID = 1L;
3333

34+
private transient HttpURLConnectionClient httpURLConnectionClient;
35+
3436
public DefaultOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl,
3537
String authorizationWebsiteUrl) {
3638
super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl);
39+
this.httpURLConnectionClient = HttpURLConnectionClient.create();
40+
}
41+
42+
public DefaultOAuthProvider(String requestTokenEndpointUrl, String accessTokenEndpointUrl,
43+
String authorizationWebsiteUrl, HttpURLConnectionClient httpURLConnectionClient) {
44+
super(requestTokenEndpointUrl, accessTokenEndpointUrl, authorizationWebsiteUrl);
45+
this.httpURLConnectionClient = httpURLConnectionClient;
46+
}
47+
48+
public void setHttpURLConnectionClient(HttpURLConnectionClient httpURLConnectionClient) {
49+
this.httpURLConnectionClient = httpURLConnectionClient;
3750
}
3851

39-
protected HttpRequest createRequest(String endpointUrl) throws MalformedURLException,
40-
IOException {
41-
HttpURLConnection connection = (HttpURLConnection) new URL(endpointUrl).openConnection();
52+
protected HttpRequest createRequest(String endpointUrl) throws Exception {
53+
HttpURLConnection connection = httpURLConnectionClient.open(new URL(endpointUrl));
4254
connection.setRequestMethod("POST");
4355
connection.setAllowUserInteraction(false);
4456
connection.setRequestProperty("Content-Length", "0");
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.parse.internal.signpost.basic;
2+
3+
import java.io.IOException;
4+
import java.lang.reflect.Method;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
8+
/**
9+
* Adapter class to provide HttpURLConnection, using android HttpURLConnection or OkHttp
10+
*/
11+
public final class HttpURLConnectionClient {
12+
private final boolean isUsingOkHttp;
13+
private final Object okUrlFactory;
14+
private final Method okUrlFactoryOpen;
15+
16+
private HttpURLConnectionClient(boolean isUsingOkHttp, Object okUrlFactory, Method okUrlFactoryOpen) {
17+
this.isUsingOkHttp = isUsingOkHttp;
18+
this.okUrlFactory = okUrlFactory;
19+
this.okUrlFactoryOpen = okUrlFactoryOpen;
20+
}
21+
22+
public static HttpURLConnectionClient create() {
23+
try {
24+
final Class okHttpClientClass = Class.forName("com.squareup.okhttp.OkHttpClient");
25+
final Object okHttpClient = okHttpClientClass.getConstructor().newInstance();
26+
final Class okUrlFactoryClass = Class.forName("com.squareup.okhttp.OkUrlFactory");
27+
final Object okUrlFactory = okUrlFactoryClass.getConstructor(okHttpClientClass).newInstance(okHttpClient);
28+
final Method okUrlFactoryOpen = okUrlFactoryClass.getMethod("open", URL.class);
29+
return new HttpURLConnectionClient(true, okUrlFactory, okUrlFactoryOpen);
30+
} catch (Exception e) {
31+
return new HttpURLConnectionClient(true, null, null);
32+
}
33+
}
34+
35+
public HttpURLConnection open(URL url) throws Exception {
36+
if (isUsingOkHttp) {
37+
return (HttpURLConnection) okUrlFactoryOpen.invoke(okUrlFactory, url);
38+
} else {
39+
return (HttpURLConnection) url.openConnection();
40+
}
41+
}
42+
}

library/src/main/java/com/parse/twitter/Twitter.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
*/
99
package com.parse.twitter;
1010

11+
import com.parse.internal.signpost.basic.DefaultOAuthConsumer;
12+
import com.parse.internal.signpost.basic.DefaultOAuthProvider;
13+
import com.parse.internal.signpost.basic.HttpURLConnectionClient;
1114
import org.apache.http.client.methods.HttpUriRequest;
1215

1316
import android.app.ProgressDialog;
1417
import android.content.Context;
1518
import android.net.Uri;
16-
import android.net.http.AndroidHttpClient;
1719
import android.os.AsyncTask;
1820
import android.webkit.CookieSyncManager;
1921

@@ -23,9 +25,10 @@
2325
import com.parse.internal.signpost.OAuthConsumer;
2426
import com.parse.internal.signpost.OAuthProvider;
2527
import com.parse.internal.signpost.commonshttp.CommonsHttpOAuthConsumer;
26-
import com.parse.internal.signpost.commonshttp.CommonsHttpOAuthProvider;
2728
import com.parse.internal.signpost.http.HttpParameters;
2829

30+
import java.net.HttpURLConnection;
31+
2932
public class Twitter {
3033
private static final String USER_AGENT = "Parse Android SDK";
3134

@@ -49,6 +52,8 @@ public class Twitter {
4952
private String userId;
5053
private String screenName;
5154

55+
private final HttpURLConnectionClient httpURLConnectionClient = HttpURLConnectionClient.create();
56+
5257
public Twitter(String consumerKey, String consumerSecret) {
5358
this.consumerKey = consumerKey;
5459
this.consumerSecret = consumerSecret;
@@ -114,21 +119,27 @@ public void signRequest(HttpUriRequest request) {
114119
}
115120
}
116121

122+
public void signRequest(HttpURLConnection request) {
123+
OAuthConsumer consumer = new DefaultOAuthConsumer(getConsumerKey(), getConsumerSecret());
124+
consumer.setTokenWithSecret(getAuthToken(), getAuthTokenSecret());
125+
try {
126+
consumer.sign(request);
127+
} catch (Exception e) {
128+
throw new RuntimeException(e);
129+
}
130+
}
131+
117132
public void authorize(final Context context, final AsyncCallback callback) {
118133
if (getConsumerKey() == null || getConsumerKey().length() == 0 || getConsumerSecret() == null
119134
|| getConsumerSecret().length() == 0) {
120135
throw new IllegalStateException(
121136
"Twitter must be initialized with a consumer key and secret before authorization.");
122137
}
123138

124-
final OAuthProvider provider = new CommonsHttpOAuthProvider(
125-
REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZE_URL,
126-
// Use AndroidHttpClient due to a MITM vulnerability with Apache's default
127-
// HostnameVerifier through HttpClient.
128-
//TODO (grantland): Re-use same HttpClient everywhere. We can't do this now due to packaging.
129-
AndroidHttpClient.newInstance(USER_AGENT, context));
130-
final OAuthConsumer consumer = new CommonsHttpOAuthConsumer(getConsumerKey(),
131-
getConsumerSecret());
139+
final OAuthProvider provider = new DefaultOAuthProvider(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZE_URL,
140+
httpURLConnectionClient);
141+
provider.setRequestHeader("User-Agent", USER_AGENT);
142+
final OAuthConsumer consumer = new DefaultOAuthConsumer(getConsumerKey(), getConsumerSecret());
132143

133144
final ProgressDialog progress = new ProgressDialog(context);
134145
progress.setMessage("Loading...");

0 commit comments

Comments
 (0)