diff --git a/plugin.xml b/plugin.xml index 0f512b3..dfbbf7d 100644 --- a/plugin.xml +++ b/plugin.xml @@ -90,6 +90,7 @@ + diff --git a/src/android/com/synconset/cordovahttp/CordovaHttpPatch.java b/src/android/com/synconset/cordovahttp/CordovaHttpPatch.java new file mode 100644 index 0000000..a97fff0 --- /dev/null +++ b/src/android/com/synconset/cordovahttp/CordovaHttpPatch.java @@ -0,0 +1,69 @@ +/** + * A HTTP plugin for Cordova / Phonegap + */ +package com.synconset.cordovahttp; + +import java.net.SocketTimeoutException; +import java.net.UnknownHostException; + +import org.apache.cordova.CallbackContext; +import org.json.JSONException; +import org.json.JSONObject; + +import javax.net.ssl.SSLHandshakeException; + +import com.github.kevinsawicki.http.HttpRequest; +import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; + +class CordovaHttpPatch extends CordovaHttp implements Runnable { + public CordovaHttpPatch(String urlString, JSONObject data, String serializerName, JSONObject headers, CallbackContext callbackContext, int timeout) { + super(urlString, data, serializerName, headers, timeout, callbackContext); + } + + @Override + public void run() { + try { + HttpRequest request = HttpRequest.patch(this.getUrlString()); + + request.readTimeout(this.getRequestTimeout()); + this.setupSecurity(request); + request.acceptCharset(CHARSET); + request.headers(this.getHeadersMap()); + request.uncompress(true); + + 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.patch("status", code); + + if (code >= 200 && code < 300) { + response.patch("data", body); + this.getCallbackContext().success(response); + } else { + response.patch("error", body); + this.getCallbackContext().error(response); + } + } catch (JSONException e) { + this.respondWithError("There was an error generating the response"); + } catch (HttpRequestException e) { + if (e.getCause() instanceof UnknownHostException) { + this.respondWithError(0, "The host could not be resolved"); + } else if (e.getCause() instanceof SocketTimeoutException) { + this.respondWithError(1, "The request timed out"); + } else if (e.getCause() instanceof SSLHandshakeException) { + this.respondWithError("SSL handshake failed"); + } else { + this.respondWithError("There was an error with the request"); + } + } + } +} diff --git a/www/advanced-http.js b/www/advanced-http.js index 36b62d3..8af616e 100644 --- a/www/advanced-http.js +++ b/www/advanced-http.js @@ -198,6 +198,19 @@ var http = { return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'put', [url, data, this.dataSerializer, headers, this.timeoutInSeconds]); }, + patch: function (url, data, headers, success, failure) { + handleMissingCallbacks(success, failure); + + data = data || {}; + headers = headers || {}; + headers = mergeHeaders(this.headers, headers); + headers = mergeHeaders(getCookieHeader(url), headers); + + var onSuccess = injectCookieHandler(url, success); + var onFail = injectCookieHandler(url, failure); + + return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'patch', [url, data, this.dataSerializer, headers]); + }, delete: function (url, params, headers, success, failure) { handleMissingCallbacks(success, failure);