Skip to content

Commit 35dc020

Browse files
authored
Merge pull request silkimen#82 from chax/master
Replaced connectionFactory with OkHttp implementation
2 parents 6877fbd + 8a9381f commit 35dc020

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<uses-permission android:name="android.permission.INTERNET"/>
5757
</config-file>
5858
<source-file src="src/android/com/github/kevinsawicki/http/HttpRequest.java" target-dir="src/com/github/kevinsawicki/http"/>
59+
<source-file src="src/android/com/github/kevinsawicki/http/OkConnectionFactory.java" target-dir="src/com/github/kevinsawicki/http"/>
5960
<source-file src="src/android/com/github/kevinsawicki/http/TLSSocketFactory.java" target-dir="src/com/github/kevinsawicki/http"/>
6061
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttp.java" target-dir="src/com/synconset/cordovahttp"/>
6162
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpDelete.java" target-dir="src/com/synconset/cordovahttp"/>
@@ -67,5 +68,6 @@
6768
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPut.java" target-dir="src/com/synconset/cordovahttp"/>
6869
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPatch.java" target-dir="src/com/synconset/cordovahttp"/>
6970
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpUpload.java" target-dir="src/com/synconset/cordovahttp"/>
71+
<framework src="com.squareup.okhttp3:okhttp-urlconnection:3.9.1" />
7072
</platform>
7173
</plugin>

src/android/com/github/kevinsawicki/http/HttpRequest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,7 @@ public interface ConnectionFactory {
428428
* A {@link ConnectionFactory} which uses the built-in
429429
* {@link URL#openConnection()}
430430
*/
431-
ConnectionFactory DEFAULT = new ConnectionFactory() {
432-
public HttpURLConnection create(URL url) throws IOException {
433-
return (HttpURLConnection) url.openConnection();
434-
}
435-
436-
public HttpURLConnection create(URL url, Proxy proxy) throws IOException {
437-
return (HttpURLConnection) url.openConnection(proxy);
438-
}
439-
};
431+
ConnectionFactory DEFAULT = new OkConnectionFactory();
440432
}
441433

442434
private static ConnectionFactory CONNECTION_FACTORY = ConnectionFactory.DEFAULT;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.kevinsawicki.http;
2+
3+
import okhttp3.OkUrlFactory;
4+
import okhttp3.OkHttpClient;
5+
6+
import java.net.URL;
7+
import java.net.HttpURLConnection;
8+
import java.net.URLStreamHandler;
9+
import java.net.Proxy;
10+
11+
12+
public class OkConnectionFactory implements HttpRequest.ConnectionFactory {
13+
14+
protected OkHttpClient okHttpClient = new OkHttpClient();
15+
16+
public HttpURLConnection create(URL url) {
17+
OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClient);
18+
return (HttpURLConnection) okUrlFactory.open(url);
19+
}
20+
21+
public HttpURLConnection create(URL url, Proxy proxy) {
22+
OkHttpClient okHttpClientWithProxy = okHttpClient.newBuilder().proxy(proxy).build();
23+
OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClientWithProxy);
24+
return (HttpURLConnection) okUrlFactory.open(url);
25+
}
26+
}

src/android/com/github/kevinsawicki/http/TLSSocketFactory.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,45 @@
1111

1212
public class TLSSocketFactory extends SSLSocketFactory {
1313

14-
private SSLSocketFactory internalSSLSocketFactory;
14+
private SSLSocketFactory delegate;
1515

1616
public TLSSocketFactory(SSLContext context) {
17-
internalSSLSocketFactory = context.getSocketFactory();
17+
delegate = context.getSocketFactory();
1818
}
1919

2020
@Override
2121
public String[] getDefaultCipherSuites() {
22-
return internalSSLSocketFactory.getDefaultCipherSuites();
22+
return delegate.getDefaultCipherSuites();
2323
}
2424

2525
@Override
2626
public String[] getSupportedCipherSuites() {
27-
return internalSSLSocketFactory.getSupportedCipherSuites();
27+
return delegate.getSupportedCipherSuites();
2828
}
2929

3030
@Override
3131
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
32-
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose));
32+
return enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose));
3333
}
3434

3535
@Override
3636
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
37-
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
37+
return enableTLSOnSocket(delegate.createSocket(host, port));
3838
}
3939

4040
@Override
4141
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
42-
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort));
42+
return enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort));
4343
}
4444

4545
@Override
4646
public Socket createSocket(InetAddress host, int port) throws IOException {
47-
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
47+
return enableTLSOnSocket(delegate.createSocket(host, port));
4848
}
4949

5050
@Override
5151
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
52-
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort));
52+
return enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort));
5353
}
5454

5555
private Socket enableTLSOnSocket(Socket socket) {

src/android/com/synconset/cordovahttp/CordovaHttp.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,7 @@ abstract class CordovaHttp {
5252
private CallbackContext callbackContext;
5353

5454
public CordovaHttp(String urlString, Object params, JSONObject headers, int timeout, CallbackContext callbackContext) {
55-
this.urlString = urlString;
56-
this.params = params;
57-
this.serializerName = "default";
58-
this.headers = headers;
59-
this.timeoutInMilliseconds = timeout;
60-
this.callbackContext = callbackContext;
55+
this(urlString, params, "default", headers, timeout, callbackContext);
6156
}
6257

6358
public CordovaHttp(String urlString, Object params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) {
@@ -228,6 +223,7 @@ protected HashMap<String, Object> getMapFromJSONObject(JSONObject object) throws
228223
protected void prepareRequest(HttpRequest request) throws HttpRequestException, JSONException {
229224
this.setupRedirect(request);
230225
this.setupSecurity(request);
226+
231227
request.readTimeout(this.getRequestTimeout());
232228
request.acceptCharset(ACCEPTED_CHARSETS);
233229
request.headers(this.getHeadersMap());

0 commit comments

Comments
 (0)