mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2024-10-06 17:22:12 +08:00
- implemented configurable serializer for Android
- cleaned up Android source structure
This commit is contained in:
parent
50cda6a5af
commit
c8df1ebd8b
20
plugin.xml
20
plugin.xml
@ -62,7 +62,7 @@
|
||||
<platform name="android">
|
||||
<config-file target="res/xml/config.xml" parent="/*">
|
||||
<feature name="CordovaHttpPlugin">
|
||||
<param name="android-package" value="com.synconset.CordovaHttpPlugin"/>
|
||||
<param name="android-package" value="com.synconset.cordovahttp.CordovaHttpPlugin"/>
|
||||
</feature>
|
||||
</config-file>
|
||||
|
||||
@ -70,14 +70,14 @@
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
</config-file>
|
||||
|
||||
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttp.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpPost.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpHead.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpUpload.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java" target-dir="src/com/synconset" />
|
||||
<source-file src="src/android/com/synconset/CordovaHTTP/HttpRequest.java" target-dir="src/com/github/kevinsawicki/http" />
|
||||
<source-file src="src/android/com/synconset/CordovaHTTP/TLSSocketFactory.java" target-dir="src/com/github/kevinsawicki/http" />
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttp.java" target-dir="src/com/synconset/cordovahttp" />
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpGet.java" target-dir="src/com/synconset/cordovahttp" />
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPost.java" target-dir="src/com/synconset/cordovahttp" />
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpHead.java" target-dir="src/com/synconset/cordovahttp" />
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpUpload.java" target-dir="src/com/synconset/cordovahttp" />
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpDownload.java" target-dir="src/com/synconset/cordovahttp" />
|
||||
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpPlugin.java" target-dir="src/com/synconset/cordovahttp" />
|
||||
<source-file src="src/android/com/github/kevinsawicki/http/HttpRequest.java" target-dir="src/com/github/kevinsawicki/http" />
|
||||
<source-file src="src/android/com/github/kevinsawicki/http/TLSSocketFactory.java" target-dir="src/com/github/kevinsawicki/http" />
|
||||
</platform>
|
||||
</plugin>
|
||||
|
@ -259,11 +259,11 @@ public class HttpRequest {
|
||||
private static final String CRLF = "\r\n";
|
||||
|
||||
private static final String[] EMPTY_STRINGS = new String[0];
|
||||
|
||||
|
||||
private static SSLSocketFactory PINNED_FACTORY;
|
||||
|
||||
private static SSLSocketFactory TRUSTED_FACTORY;
|
||||
|
||||
|
||||
private static ArrayList<Certificate> PINNED_CERTS;
|
||||
|
||||
private static HostnameVerifier TRUSTED_VERIFIER;
|
||||
@ -274,7 +274,7 @@ public class HttpRequest {
|
||||
else
|
||||
return CHARSET_UTF8;
|
||||
}
|
||||
|
||||
|
||||
private static SSLSocketFactory getPinnedFactory()
|
||||
throws HttpRequestException {
|
||||
if (PINNED_FACTORY != null) {
|
||||
@ -305,7 +305,7 @@ public class HttpRequest {
|
||||
try {
|
||||
SSLContext context = SSLContext.getInstance("TLS");
|
||||
context.init(null, trustAllCerts, new SecureRandom());
|
||||
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT < 20) {
|
||||
TRUSTED_FACTORY = new TLSSocketFactory(context);
|
||||
} else {
|
||||
@ -429,8 +429,8 @@ public class HttpRequest {
|
||||
else
|
||||
CONNECTION_FACTORY = connectionFactory;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Add a certificate to test against when using ssl pinning.
|
||||
*
|
||||
@ -447,27 +447,27 @@ public class HttpRequest {
|
||||
String keyStoreType = KeyStore.getDefaultType();
|
||||
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
|
||||
keyStore.load(null, null);
|
||||
|
||||
|
||||
for (int i = 0; i < PINNED_CERTS.size(); i++) {
|
||||
keyStore.setCertificateEntry("CA" + i, PINNED_CERTS.get(i));
|
||||
}
|
||||
|
||||
|
||||
// Create a TrustManager that trusts the CAs in our KeyStore
|
||||
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
|
||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
|
||||
tmf.init(keyStore);
|
||||
|
||||
|
||||
// Create an SSLContext that uses our TrustManager
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(null, tmf.getTrustManagers(), null);
|
||||
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT < 20) {
|
||||
PINNED_FACTORY = new TLSSocketFactory(sslContext);
|
||||
} else {
|
||||
PINNED_FACTORY = sslContext.getSocketFactory();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a certificate to test against when using ssl pinning.
|
||||
*
|
||||
@ -3256,7 +3256,7 @@ public class HttpRequest {
|
||||
form(entry, charset);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Configure HTTPS connection to trust only certain certificates
|
||||
* <p>
|
||||
@ -3275,7 +3275,7 @@ public class HttpRequest {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Configure HTTPS connection to trust all certificates
|
||||
* <p>
|
@ -4,8 +4,6 @@ import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
@ -60,4 +58,4 @@ public class TLSSocketFactory extends SSLSocketFactory {
|
||||
}
|
||||
return socket;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,63 +1,58 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset;
|
||||
package com.synconset.cordovahttp;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.BufferedReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
|
||||
public abstract class CordovaHttp {
|
||||
|
||||
abstract class CordovaHttp {
|
||||
protected static final String TAG = "CordovaHTTP";
|
||||
protected static final String CHARSET = "UTF-8";
|
||||
|
||||
|
||||
private static AtomicBoolean sslPinning = new AtomicBoolean(false);
|
||||
private static AtomicBoolean acceptAllCerts = new AtomicBoolean(false);
|
||||
private static AtomicBoolean validateDomainName = new AtomicBoolean(true);
|
||||
|
||||
private String urlString;
|
||||
private Map<?, ?> params;
|
||||
private Map<String, String> headers;
|
||||
private JSONObject params;
|
||||
private String serializerName;
|
||||
private JSONObject headers;
|
||||
private CallbackContext callbackContext;
|
||||
|
||||
public CordovaHttp(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext) {
|
||||
|
||||
public CordovaHttp(String urlString, JSONObject params, JSONObject headers, CallbackContext callbackContext) {
|
||||
this.urlString = urlString;
|
||||
this.params = params;
|
||||
this.serializerName = "default";
|
||||
this.headers = headers;
|
||||
this.callbackContext = callbackContext;
|
||||
}
|
||||
|
||||
|
||||
public CordovaHttp(String urlString, JSONObject params, String serializerName, JSONObject headers, CallbackContext callbackContext) {
|
||||
this.urlString = urlString;
|
||||
this.params = params;
|
||||
this.serializerName = serializerName;
|
||||
this.headers = headers;
|
||||
this.callbackContext = callbackContext;
|
||||
}
|
||||
|
||||
public static void enableSSLPinning(boolean enable) {
|
||||
sslPinning.set(enable);
|
||||
if (enable) {
|
||||
acceptAllCerts.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void acceptAllCerts(boolean accept) {
|
||||
acceptAllCerts.set(accept);
|
||||
if (accept) {
|
||||
@ -72,19 +67,31 @@ public abstract class CordovaHttp {
|
||||
protected String getUrlString() {
|
||||
return this.urlString;
|
||||
}
|
||||
|
||||
protected Map<?, ?> getParams() {
|
||||
|
||||
protected JSONObject getParamsObject() {
|
||||
return this.params;
|
||||
}
|
||||
|
||||
protected Map<String, String> getHeaders() {
|
||||
|
||||
protected String getSerializerName() {
|
||||
return this.serializerName;
|
||||
}
|
||||
|
||||
protected HashMap<String, Object> getParamsMap() throws JSONException {
|
||||
return this.getMapFromJSONObject(this.params);
|
||||
}
|
||||
|
||||
protected JSONObject getHeadersObject() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
|
||||
protected HashMap<String, String> getHeadersMap() throws JSONException {
|
||||
return this.getStringMapFromJSONObject(this.headers);
|
||||
}
|
||||
|
||||
protected CallbackContext getCallbackContext() {
|
||||
return this.callbackContext;
|
||||
}
|
||||
|
||||
|
||||
protected HttpRequest setupSecurity(HttpRequest request) {
|
||||
if (acceptAllCerts.get()) {
|
||||
request.trustAllCerts();
|
||||
@ -97,7 +104,7 @@ public abstract class CordovaHttp {
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
|
||||
protected void respondWithError(int status, String msg) {
|
||||
try {
|
||||
JSONObject response = new JSONObject();
|
||||
@ -108,7 +115,7 @@ public abstract class CordovaHttp {
|
||||
this.callbackContext.error(msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void respondWithError(String msg) {
|
||||
this.respondWithError(500, msg);
|
||||
}
|
||||
@ -125,4 +132,26 @@ public abstract class CordovaHttp {
|
||||
}
|
||||
response.put("headers", new JSONObject(parsed_headers));
|
||||
}
|
||||
|
||||
protected HashMap<String, String> getStringMapFromJSONObject(JSONObject object) throws JSONException {
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
Iterator<?> i = object.keys();
|
||||
|
||||
while (i.hasNext()) {
|
||||
String key = (String)i.next();
|
||||
map.put(key, object.getString(key));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
protected HashMap<String, Object> getMapFromJSONObject(JSONObject object) throws JSONException {
|
||||
HashMap<String, Object> map = new HashMap<String, Object>();
|
||||
Iterator<?> i = object.keys();
|
||||
|
||||
while(i.hasNext()) {
|
||||
String key = (String)i.next();
|
||||
map.put(key, object.get(key));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
@ -1,9 +1,7 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset;
|
||||
|
||||
import android.util.Log;
|
||||
package com.synconset.cordovahttp;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
|
||||
@ -12,7 +10,6 @@ import java.io.File;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
@ -22,23 +19,23 @@ import org.apache.cordova.file.FileUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class CordovaHttpDownload extends CordovaHttp implements Runnable {
|
||||
class CordovaHttpDownload extends CordovaHttp implements Runnable {
|
||||
private String filePath;
|
||||
|
||||
public CordovaHttpDownload(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext, String filePath) {
|
||||
|
||||
public CordovaHttpDownload(String urlString, JSONObject params, JSONObject headers, CallbackContext callbackContext, String filePath) {
|
||||
super(urlString, params, headers, callbackContext);
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), true);
|
||||
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParamsMap(), true);
|
||||
this.setupSecurity(request);
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
request.headers(this.getHeadersMap());
|
||||
int code = request.code();
|
||||
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
this.addResponseHeaders(request, response);
|
||||
response.put("status", code);
|
@ -1,40 +1,41 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset;
|
||||
package com.synconset.cordovahttp;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import android.util.Log;
|
||||
import org.apache.cordova.CallbackContext;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
|
||||
|
||||
public class CordovaHttpPost extends CordovaHttp implements Runnable {
|
||||
public CordovaHttpPost(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext) {
|
||||
|
||||
class CordovaHttpGet extends CordovaHttp implements Runnable {
|
||||
public CordovaHttpGet(String urlString, JSONObject params, JSONObject headers, CallbackContext callbackContext) {
|
||||
super(urlString, params, headers, callbackContext);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.post(this.getUrlString());
|
||||
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParamsMap(), false);
|
||||
|
||||
this.setupSecurity(request);
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
request.form(this.getParams());
|
||||
request.headers(this.getHeadersMap());
|
||||
|
||||
int code = request.code();
|
||||
String body = request.body(CHARSET);
|
||||
JSONObject response = new JSONObject();
|
||||
|
||||
this.addResponseHeaders(request, response);
|
||||
response.put("status", code);
|
||||
|
||||
if (code >= 200 && code < 300) {
|
||||
response.put("data", body);
|
||||
this.getCallbackContext().success(response);
|
||||
@ -44,7 +45,7 @@ public class CordovaHttpPost extends CordovaHttp implements Runnable {
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
this.respondWithError("There was an error generating the response");
|
||||
} catch (HttpRequestException e) {
|
||||
} catch (HttpRequestException e) {
|
||||
if (e.getCause() instanceof UnknownHostException) {
|
||||
this.respondWithError(0, "The host could not be resolved");
|
||||
} else if (e.getCause() instanceof SSLHandshakeException) {
|
@ -1,18 +1,9 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset;
|
||||
package com.synconset.cordovahttp;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
@ -20,27 +11,29 @@ import org.apache.cordova.CallbackContext;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
|
||||
|
||||
public class CordovaHttpHead extends CordovaHttp implements Runnable {
|
||||
public CordovaHttpHead(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext) {
|
||||
|
||||
class CordovaHttpHead extends CordovaHttp implements Runnable {
|
||||
public CordovaHttpHead(String urlString, JSONObject params, JSONObject headers, CallbackContext callbackContext) {
|
||||
super(urlString, params, headers, callbackContext);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.head(this.getUrlString(), this.getParams(), true);
|
||||
HttpRequest request = HttpRequest.head(this.getUrlString(), this.getParamsMap(), true);
|
||||
|
||||
this.setupSecurity(request);
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
request.headers(this.getHeadersMap());
|
||||
|
||||
int code = request.code();
|
||||
JSONObject response = new JSONObject();
|
||||
|
||||
this.addResponseHeaders(request, response);
|
||||
response.put("status", code);
|
||||
|
||||
if (code >= 200 && code < 300) {
|
||||
// no 'body' to return for HEAD request
|
||||
this.getCallbackContext().success(response);
|
||||
@ -61,4 +54,4 @@ public class CordovaHttpHead extends CordovaHttp implements Runnable {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,39 +1,26 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset;
|
||||
package com.synconset.cordovahttp;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyStore;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Iterator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaInterface;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.CordovaWebView;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.content.res.AssetManager;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
|
||||
@ -51,25 +38,23 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
String urlString = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.getStringMapFromJSONObject(headers);
|
||||
CordovaHttpGet get = new CordovaHttpGet(urlString, paramsMap, headersMap, callbackContext);
|
||||
CordovaHttpGet get = new CordovaHttpGet(urlString, params, headers, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(get);
|
||||
} else if (action.equals("head")) {
|
||||
String urlString = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.getStringMapFromJSONObject(headers);
|
||||
CordovaHttpHead head = new CordovaHttpHead(urlString, paramsMap, headersMap, callbackContext);
|
||||
CordovaHttpHead head = new CordovaHttpHead(urlString, params, headers, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(head);
|
||||
} else if (action.equals("post")) {
|
||||
String urlString = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.getStringMapFromJSONObject(headers);
|
||||
CordovaHttpPost post = new CordovaHttpPost(urlString, paramsMap, headersMap, callbackContext);
|
||||
String serializerName = args.getString(2);
|
||||
JSONObject headers = args.getJSONObject(3);
|
||||
CordovaHttpPost post = new CordovaHttpPost(urlString, params, serializerName, headers, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(post);
|
||||
} else if (action.equals("enableSSLPinning")) {
|
||||
try {
|
||||
@ -82,30 +67,30 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
}
|
||||
} else if (action.equals("acceptAllCerts")) {
|
||||
boolean accept = args.getBoolean(0);
|
||||
|
||||
CordovaHttp.acceptAllCerts(accept);
|
||||
callbackContext.success();
|
||||
} else if (action.equals("validateDomainName")) {
|
||||
boolean accept = args.getBoolean(0);
|
||||
|
||||
CordovaHttp.validateDomainName(accept);
|
||||
callbackContext.success();
|
||||
} else if (action.equals("uploadFile")) {
|
||||
String urlString = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.getStringMapFromJSONObject(headers);
|
||||
String filePath = args.getString(3);
|
||||
String name = args.getString(4);
|
||||
CordovaHttpUpload upload = new CordovaHttpUpload(urlString, paramsMap, headersMap, callbackContext, filePath, name);
|
||||
CordovaHttpUpload upload = new CordovaHttpUpload(urlString, params, headers, callbackContext, filePath, name);
|
||||
|
||||
cordova.getThreadPool().execute(upload);
|
||||
} else if (action.equals("downloadFile")) {
|
||||
String urlString = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
HashMap<?, ?> paramsMap = this.getMapFromJSONObject(params);
|
||||
HashMap<String, String> headersMap = this.getStringMapFromJSONObject(headers);
|
||||
String filePath = args.getString(3);
|
||||
CordovaHttpDownload download = new CordovaHttpDownload(urlString, paramsMap, headersMap, callbackContext, filePath);
|
||||
CordovaHttpDownload download = new CordovaHttpDownload(urlString, params, headers, callbackContext, filePath);
|
||||
|
||||
cordova.getThreadPool().execute(download);
|
||||
} else {
|
||||
return false;
|
||||
@ -149,26 +134,4 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
CordovaHttp.enableSSLPinning(false);
|
||||
}
|
||||
}
|
||||
|
||||
private HashMap<String, String> getStringMapFromJSONObject(JSONObject object) throws JSONException {
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
Iterator<?> i = object.keys();
|
||||
|
||||
while (i.hasNext()) {
|
||||
String key = (String)i.next();
|
||||
map.put(key, object.getString(key));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private HashMap<String, Object> getMapFromJSONObject(JSONObject object) throws JSONException {
|
||||
HashMap<String, Object> map = new HashMap<String, Object>();
|
||||
Iterator<?> i = object.keys();
|
||||
|
||||
while(i.hasNext()) {
|
||||
String key = (String)i.next();
|
||||
map.put(key, object.get(key));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
@ -1,47 +1,47 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset;
|
||||
package com.synconset.cordovahttp;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
|
||||
|
||||
public class CordovaHttpGet extends CordovaHttp implements Runnable {
|
||||
public CordovaHttpGet(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext) {
|
||||
super(urlString, params, headers, callbackContext);
|
||||
|
||||
class CordovaHttpPost extends CordovaHttp implements Runnable {
|
||||
public CordovaHttpPost(String urlString, JSONObject params, String serializerName, JSONObject headers, CallbackContext callbackContext) {
|
||||
super(urlString, params, serializerName, headers, callbackContext);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), false);
|
||||
HttpRequest request = HttpRequest.post(this.getUrlString());
|
||||
|
||||
this.setupSecurity(request);
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
request.headers(this.getHeadersMap());
|
||||
|
||||
if (new String("json").equals(this.getSerializerName())) {
|
||||
request.contentType(request.CONTENT_TYPE_JSON, request.CHARSET_UTF8);
|
||||
request.send(this.getParamsObject().toString());
|
||||
} else {
|
||||
request.form(this.getParamsMap());
|
||||
}
|
||||
|
||||
int code = request.code();
|
||||
String body = request.body(CHARSET);
|
||||
JSONObject response = new JSONObject();
|
||||
|
||||
this.addResponseHeaders(request, response);
|
||||
response.put("status", code);
|
||||
|
||||
if (code >= 200 && code < 300) {
|
||||
response.put("data", body);
|
||||
this.getCallbackContext().success(response);
|
||||
@ -51,7 +51,7 @@ public class CordovaHttpGet extends CordovaHttp implements Runnable {
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
this.respondWithError("There was an error generating the response");
|
||||
} catch (HttpRequestException e) {
|
||||
} catch (HttpRequestException e) {
|
||||
if (e.getCause() instanceof UnknownHostException) {
|
||||
this.respondWithError(0, "The host could not be resolved");
|
||||
} else if (e.getCause() instanceof SSLHandshakeException) {
|
||||
@ -61,4 +61,4 @@ public class CordovaHttpGet extends CordovaHttp implements Runnable {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,57 +1,65 @@
|
||||
/**
|
||||
* A HTTP plugin for Cordova / Phonegap
|
||||
*/
|
||||
package com.synconset;
|
||||
package com.synconset.cordovahttp;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.cordova.CallbackContext;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import javax.net.ssl.SSLHandshakeException;
|
||||
|
||||
import android.util.Log;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.github.kevinsawicki.http.HttpRequest;
|
||||
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
|
||||
|
||||
public class CordovaHttpUpload extends CordovaHttp implements Runnable {
|
||||
|
||||
class CordovaHttpUpload extends CordovaHttp implements Runnable {
|
||||
private String filePath;
|
||||
private String name;
|
||||
|
||||
public CordovaHttpUpload(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext, String filePath, String name) {
|
||||
|
||||
public CordovaHttpUpload(String urlString, JSONObject params, JSONObject headers, CallbackContext callbackContext, String filePath, String name) {
|
||||
super(urlString, params, headers, callbackContext);
|
||||
this.filePath = filePath;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.post(this.getUrlString());
|
||||
|
||||
this.setupSecurity(request);
|
||||
request.acceptCharset(CHARSET);
|
||||
request.headers(this.getHeaders());
|
||||
request.headers(this.getHeadersMap());
|
||||
|
||||
URI uri = new URI(filePath);
|
||||
int index = filePath.lastIndexOf('/');
|
||||
String filename = filePath.substring(index + 1);
|
||||
index = filePath.lastIndexOf('.');
|
||||
String ext = filePath.substring(index + 1);
|
||||
|
||||
int filenameIndex = filePath.lastIndexOf('/');
|
||||
String filename = filePath.substring(filenameIndex + 1);
|
||||
|
||||
int extIndex = filePath.lastIndexOf('.');
|
||||
String ext = filePath.substring(extIndex + 1);
|
||||
|
||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||
String mimeType = mimeTypeMap.getMimeTypeFromExtension(ext);
|
||||
|
||||
request.part(this.name, filename, mimeType, new File(uri));
|
||||
|
||||
Set<?> set = (Set<?>)this.getParams().entrySet();
|
||||
|
||||
Set<?> set = (Set<?>)this.getParamsMap().entrySet();
|
||||
Iterator<?> i = set.iterator();
|
||||
|
||||
while (i.hasNext()) {
|
||||
Entry<?, ?> e = (Entry<?, ?>)i.next();
|
||||
String key = (String)e.getKey();
|
||||
@ -65,13 +73,15 @@ public class CordovaHttpUpload extends CordovaHttp implements Runnable {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int code = request.code();
|
||||
String body = request.body(CHARSET);
|
||||
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
|
||||
this.addResponseHeaders(request, response);
|
||||
response.put("status", code);
|
||||
|
||||
if (code >= 200 && code < 300) {
|
||||
response.put("data", body);
|
||||
this.getCallbackContext().success(response);
|
Loading…
Reference in New Issue
Block a user