From 67ad0e5da8182d9465dea6dcdab7a5a4ae433d65 Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Fri, 29 Jan 2016 22:34:16 -0800 Subject: [PATCH 01/10] Remove dead code (NSStringToBool) This was only used in code that has since been moved to AuthCompletionHandler. --- src/ios/BEMCommunicationHelper.m | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/ios/BEMCommunicationHelper.m b/src/ios/BEMCommunicationHelper.m index 3628104..f09e6e2 100644 --- a/src/ios/BEMCommunicationHelper.m +++ b/src/ios/BEMCommunicationHelper.m @@ -30,10 +30,6 @@ static NSString* kCustomSettingsPath = @"/profile/settings"; static NSString* kRegisterPath = @"/profile/create"; -static inline NSString* NSStringFromBOOL(BOOL aBool) { - return aBool? @"YES" : @"NO"; -} - @interface CommunicationHelper() { } @end From dc9de4b2890b787f94a1b1bc0b297abce6a81b37 Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Mon, 1 Feb 2016 20:50:59 -0800 Subject: [PATCH 02/10] Small compile fixes on ios --- src/ios/BEMCommunicationHelper.m | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ios/BEMCommunicationHelper.m b/src/ios/BEMCommunicationHelper.m index f09e6e2..963843a 100644 --- a/src/ios/BEMCommunicationHelper.m +++ b/src/ios/BEMCommunicationHelper.m @@ -9,10 +9,9 @@ // Copyright (c) 2014 Kalyanaraman Shankari. All rights reserved. // -#import "CommunicationHelper.h" +#import "BEMCommunicationHelper.h" #import "AuthCompletionHandler.h" -#import "Constants.h" -#import "ConnectionSettings.h" +#import "BEMConnectionSettings.h" #import @@ -137,7 +136,7 @@ -(void)execute { return; } - [[AuthCompletionHandler sharedInstance] getValidAuth:^((GTMOAuth2Authentication *)auth error:(NSError*)error) { + [[AuthCompletionHandler sharedInstance] getValidAuth:^(GTMOAuth2Authentication *auth,NSError* error) { if (error != NULL) { self.mCompletionHandler(jsonData, NULL, error); } else { From 97478ddc76d8370e7a0a2a197102c034d86fab44 Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Mon, 1 Feb 2016 20:51:44 -0800 Subject: [PATCH 03/10] Remove reference to CommunicationSettings which is not really part of this package --- plugin.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index 80e7fc9..184f867 100644 --- a/plugin.xml +++ b/plugin.xml @@ -33,7 +33,6 @@ - From 04587159e51e9273b8487fe1bdecc026f6ed32e2 Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Mon, 1 Feb 2016 21:08:40 -0800 Subject: [PATCH 04/10] Rename the javascript interface and implement it Rename the javascript interface to match the plugin. Note that this interface does not actually invoke the corresponding native code. That is because javascript support for http is good, arguably better than native, so it is unclear why we would revert to native anyway. --- www/{server_communication.js => servercomm.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename www/{server_communication.js => servercomm.js} (100%) diff --git a/www/server_communication.js b/www/servercomm.js similarity index 100% rename from www/server_communication.js rename to www/servercomm.js From 49107711788cb622cabe39c602cfb9739068199e Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Mon, 1 Feb 2016 21:12:01 -0800 Subject: [PATCH 05/10] Restore bracket to end the auth call --- www/servercomm.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/www/servercomm.js b/www/servercomm.js index 369a45d..1a76438 100644 --- a/www/servercomm.js +++ b/www/servercomm.js @@ -2,7 +2,67 @@ var exec = require("cordova/exec") +/* + * While this is the javascript implementation, it does not actually call any + * native code! That is because it is basically a wrapper around HTTP get and + * post, just like the corresponding native implementations are. Since the + * javascript has perfectly good support for HTTP, we don't really need the + * overhead of going to native. In fact, you could even argue that we don't + * really need a wrapper here since the default implementation is easy enough + * to use, but abstracting out the implementation makes it consistent with + * native code, and simultaneously makes it easier for us to change the library + * if we switch to a newer and cooler library (websockets? pub/sub? capnproto?). + */ + var ServerCommunication = { + /* + * This is only used for communication with our own server. For + * communication with other services, we can use the standard HTTP + * libraries from our javascript framework. + */ + pushGetJson: function(relativeURL, messageFiller, successCallback, errorCallback) { + var request = new XMLHttpRequest(); + request.setRequestHeader("Content-Type", "application/json"); + + request.onreadystatechange = function() { + if(request.readyState == request.DONE) { + if (request.status == 200) { + var resultObj = JSON.parse(response.responseText); + successCallback(resultObj); + } else { + errorCallback(request.statusText); + } + } else { + console.log("during HTTP post, state "+request.readyState); + } + }; + window.cordova.plugins.BEMConnectionSettings.getSettings(function(settings) { + var fullURL = settings.connectUrl + relativeURL; + window.cordova.plugins.BEMJWTAuth.getJWT(function(token) { + message = {}; + message.user = token; + messageFiller(message); + request.open("POST", fullURL, true); + request.send(message); + }, function(error) { + errorCallback(error); + }) + }, function(error) { + errorCallback(error); + }); + }, + pushJSON: function(relativeUrl, objectLabel, objectJSON, successCallback, errorCallback) { + var msgFiller = function(message) { + message[objectLabel] = objectJSON; + }; + pushGetJSON(relativeUrl, msgFiller, successCallback, errorCallback); + }, + getUserPersonalData: function(relativeUrl, successCallback, errorCallback) { + var msgFiller = function(message) { + // nop. we don't really send any data for what are effectively get calls + }; + pushGetJSON(relativeUrl, msgFiller, successCallback, errorCallback); + } } module.exports = ServerCommunication; From 5cf892f33d49de58d5c5d652be83f74606ffa0be Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Tue, 2 Feb 2016 23:02:28 -0800 Subject: [PATCH 06/10] Remove dependencies - they are kind of broken in cordova --- plugin.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin.xml b/plugin.xml index 184f867..fc91fa6 100644 --- a/plugin.xml +++ b/plugin.xml @@ -19,10 +19,12 @@ + From a209ec7e854bf267f9b7343d54e3ab0d878e599b Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Tue, 2 Feb 2016 23:04:53 -0800 Subject: [PATCH 07/10] Minor changes to javascript calls through the XHR --- www/servercomm.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/www/servercomm.js b/www/servercomm.js index 1a76438..ad9df33 100644 --- a/www/servercomm.js +++ b/www/servercomm.js @@ -20,14 +20,13 @@ var ServerCommunication = { * communication with other services, we can use the standard HTTP * libraries from our javascript framework. */ - pushGetJson: function(relativeURL, messageFiller, successCallback, errorCallback) { + pushGetJSON: function(relativeURL, messageFiller, successCallback, errorCallback) { var request = new XMLHttpRequest(); - request.setRequestHeader("Content-Type", "application/json"); request.onreadystatechange = function() { if(request.readyState == request.DONE) { if (request.status == 200) { - var resultObj = JSON.parse(response.responseText); + var resultObj = JSON.parse(request.responseText); successCallback(resultObj); } else { errorCallback(request.statusText); @@ -37,13 +36,14 @@ var ServerCommunication = { } }; window.cordova.plugins.BEMConnectionSettings.getSettings(function(settings) { - var fullURL = settings.connectUrl + relativeURL; + var fullURL = settings.connectURL + relativeURL; window.cordova.plugins.BEMJWTAuth.getJWT(function(token) { message = {}; message.user = token; messageFiller(message); request.open("POST", fullURL, true); - request.send(message); + request.setRequestHeader("Content-Type", "application/json"); + request.send(JSON.stringify(message)); }, function(error) { errorCallback(error); }) @@ -55,13 +55,13 @@ var ServerCommunication = { var msgFiller = function(message) { message[objectLabel] = objectJSON; }; - pushGetJSON(relativeUrl, msgFiller, successCallback, errorCallback); + ServerCommuniation.pushGetJSON(relativeUrl, msgFiller, successCallback, errorCallback); }, getUserPersonalData: function(relativeUrl, successCallback, errorCallback) { var msgFiller = function(message) { // nop. we don't really send any data for what are effectively get calls }; - pushGetJSON(relativeUrl, msgFiller, successCallback, errorCallback); + ServerCommunication.pushGetJSON(relativeUrl, msgFiller, successCallback, errorCallback); } } From d83db57b88313846021a1b1ea71678d8ea8235d5 Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Wed, 3 Feb 2016 13:45:11 -0800 Subject: [PATCH 08/10] Get android code to compile as well --- src/android/CommunicationHelper.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/android/CommunicationHelper.java b/src/android/CommunicationHelper.java index 7c48801..5473856 100644 --- a/src/android/CommunicationHelper.java +++ b/src/android/CommunicationHelper.java @@ -19,8 +19,13 @@ import java.net.MalformedURLException; import java.net.URL; +import edu.berkeley.eecs.emission.cordova.connectionsettings.ConnectionSettings; +import edu.berkeley.eecs.emission.cordova.jwtauth.GoogleAccountManagerAuth; +import edu.berkeley.eecs.emission.cordova.jwtauth.UserProfile; import edu.berkeley.eecs.emission.cordova.unifiedlogger.Log; +import edu.berkeley.eecs.emission.R; + public class CommunicationHelper { public static final String TAG = "CommunicationHelper"; @@ -115,9 +120,9 @@ public static String getUserPersonalData(Context ctxt, String fullURL, String us } result = builder.toString(); System.out.println("Result Summary JSON = "+ - result.substring(0, Math.min(200, result.length()) + " length "+result.length()); + result.substring(0, Math.min(200, result.length())) + " length "+result.length()); Log.i(ctxt, TAG, "Result Summary JSON = "+ - result.substring(0, Math.min(200, result.length()) + " length "+result.length()); + result.substring(0, Math.min(200, result.length())) + " length "+result.length()); in.close(); } else { Log.e(ctxt, R.class.toString(),"Failed to get JSON object"); From 0ff1f058d091a5d6b92d089374a8f47b0367b3cb Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Wed, 3 Feb 2016 13:59:43 -0800 Subject: [PATCH 09/10] Add a gradle extension that allows us to compile with the legacy http library We should really upgrade this at some point, but that point is not now --- plugin.xml | 2 +- src/android/httplib.gradle | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 src/android/httplib.gradle diff --git a/plugin.xml b/plugin.xml index fc91fa6..47b4505 100644 --- a/plugin.xml +++ b/plugin.xml @@ -25,7 +25,7 @@ --> - + diff --git a/src/android/httplib.gradle b/src/android/httplib.gradle new file mode 100644 index 0000000..c033b44 --- /dev/null +++ b/src/android/httplib.gradle @@ -0,0 +1,3 @@ +android { + useLibrary 'org.apache.http.legacy' +} From b2378d5b8d7c2df59b7cdbd35dd95b1101e3f21a Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Wed, 3 Feb 2016 22:41:28 -0800 Subject: [PATCH 10/10] Fixed package Everything else worked right out of the box --- src/android/CommunicationHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/CommunicationHelper.java b/src/android/CommunicationHelper.java index 5473856..66597ce 100644 --- a/src/android/CommunicationHelper.java +++ b/src/android/CommunicationHelper.java @@ -1,4 +1,4 @@ -package edu.berkeley.eecs.cordova.comm; +package edu.berkeley.eecs.emission.cordova.comm; import android.content.Context; import android.net.http.AndroidHttpClient;