From a1b6ea94f0287a6c49a8e160aaa0061bdc97a74c Mon Sep 17 00:00:00 2001 From: Sefa Ilkimen Date: Fri, 5 Apr 2019 16:48:18 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20#200:=20remove=20string=20switch=20and=20?= =?UTF-8?q?use=20ugly=20if=20...=20else=20=F0=9F=98=A2=20to=20be=20compati?= =?UTF-8?q?ble=20with=20Java=20target=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 ++ .../cordovahttp/CordovaClientAuth.java | 11 ++----- .../silkimen/cordovahttp/CordovaHttpBase.java | 20 ++++-------- .../cordovahttp/CordovaHttpPlugin.java | 31 +++++++++---------- .../cordovahttp/CordovaServerTrust.java | 13 +++----- 5 files changed, 30 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64693bb..c7722a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 2.0.8 +- Fixed #200: compatibility with Java 6 is broken due to string switch on Android + - :warning: **Deprecation**: Deprecated "setSSLCertMode" in favor of "setServerTrustMode" ## 2.0.7 diff --git a/src/android/com/silkimen/cordovahttp/CordovaClientAuth.java b/src/android/com/silkimen/cordovahttp/CordovaClientAuth.java index 5897f2a..54c781a 100644 --- a/src/android/com/silkimen/cordovahttp/CordovaClientAuth.java +++ b/src/android/com/silkimen/cordovahttp/CordovaClientAuth.java @@ -40,18 +40,13 @@ class CordovaClientAuth implements Runnable, KeyChainAliasCallback { @Override public void run() { - switch (this.mode) { - case "systemstore": + if ("systemstore".equals(this.mode)) { KeyChain.choosePrivateKeyAlias(this.activity, this, null, null, null, -1, null); - break; - case "file": - // @todo use pfx in bundle + } else if ("file".equals(this.mode)) { this.callbackContext.error("Not implemented, yet"); - break; - default: + } else { this.tlsConfiguration.setKeyManagers(null); this.callbackContext.success(); - break; } } diff --git a/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java b/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java index 27e8a67..e41c49a 100644 --- a/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java +++ b/src/android/com/silkimen/cordovahttp/CordovaHttpBase.java @@ -131,16 +131,12 @@ abstract class CordovaHttpBase implements Runnable { } protected void setContentType(HttpRequest request) { - switch (this.serializer) { - case "json": + if ("json".equals(this.serializer)) { request.contentType("application/json", "UTF-8"); - break; - case "utf8": + } else if ("utf8".equals(this.serializer)) { request.contentType("text/plain", "UTF-8"); - break; - case "urlencoded": + } else if ("urlencoded".equals(this.serializer)) { // intentionally left blank, because content type is set in HttpRequest.form() - break; } } @@ -149,16 +145,12 @@ abstract class CordovaHttpBase implements Runnable { return; } - switch (this.serializer) { - case "json": + if ("json".equals(this.serializer)) { request.send(this.data.toString()); - break; - case "utf8": + } else if ("utf8".equals(this.serializer)) { request.send(((JSONObject) this.data).getString("text")); - break; - case "urlencoded": + } else if ("urlencoded".equals(this.serializer)) { request.form(JsonUtils.getObjectMap((JSONObject) this.data)); - break; } } diff --git a/src/android/com/silkimen/cordovahttp/CordovaHttpPlugin.java b/src/android/com/silkimen/cordovahttp/CordovaHttpPlugin.java index 96ccde5..cf83a4f 100644 --- a/src/android/com/silkimen/cordovahttp/CordovaHttpPlugin.java +++ b/src/android/com/silkimen/cordovahttp/CordovaHttpPlugin.java @@ -51,30 +51,29 @@ public class CordovaHttpPlugin extends CordovaPlugin { return false; } - switch (action) { - case "get": + if ("get".equals(action)) { return this.executeHttpRequestWithoutData(action, args, callbackContext); - case "post": - return this.executeHttpRequestWithData(action, args, callbackContext); - case "put": - return this.executeHttpRequestWithData(action, args, callbackContext); - case "patch": - return this.executeHttpRequestWithData(action, args, callbackContext); - case "head": + } else if ("head".equals(action)) { return this.executeHttpRequestWithoutData(action, args, callbackContext); - case "delete": + } else if ("delete".equals(action)) { return this.executeHttpRequestWithoutData(action, args, callbackContext); - case "uploadFile": + } else if ("post".equals(action)) { + return this.executeHttpRequestWithData(action, args, callbackContext); + } else if ("put".equals(action)) { + return this.executeHttpRequestWithData(action, args, callbackContext); + } else if ("patch".equals(action)) { + return this.executeHttpRequestWithData(action, args, callbackContext); + } else if ("uploadFile".equals(action)) { return this.uploadFile(args, callbackContext); - case "downloadFile": + } else if ("downloadFile".equals(action)) { return this.downloadFile(args, callbackContext); - case "setServerTrustMode": + } else if ("setServerTrustMode".equals(action)) { return this.setServerTrustMode(args, callbackContext); - case "setClientAuthMode": + } else if ("setClientAuthMode".equals(action)) { return this.setClientAuthMode(args, callbackContext); - case "disableRedirect": + } else if ("disableRedirect".equals(action)) { return this.disableRedirect(args, callbackContext); - default: + } else { return false; } } diff --git a/src/android/com/silkimen/cordovahttp/CordovaServerTrust.java b/src/android/com/silkimen/cordovahttp/CordovaServerTrust.java index cfd4b49..822079e 100644 --- a/src/android/com/silkimen/cordovahttp/CordovaServerTrust.java +++ b/src/android/com/silkimen/cordovahttp/CordovaServerTrust.java @@ -63,23 +63,18 @@ class CordovaServerTrust implements Runnable { @Override public void run() { try { - switch (this.mode) { - case "legacy": + if ("legacy".equals(this.mode)) { this.tlsConfiguration.setHostnameVerifier(null); this.tlsConfiguration.setTrustManagers(null); - break; - case "nocheck": + } else if ("nocheck".equals(this.mode)) { this.tlsConfiguration.setHostnameVerifier(this.noOpVerifier); this.tlsConfiguration.setTrustManagers(this.noOpTrustManagers); - break; - case "pinned": + } else if ("pinned".equals(this.mode)) { this.tlsConfiguration.setHostnameVerifier(null); this.tlsConfiguration.setTrustManagers(this.getTrustManagers(this.getCertsFromBundle("www/certificates"))); - break; - default: + } else { this.tlsConfiguration.setHostnameVerifier(null); this.tlsConfiguration.setTrustManagers(this.getTrustManagers(this.getCertsFromKeyStore("AndroidCAStore"))); - break; } callbackContext.success();