Skip to content

Commit

Permalink
fix(rest): use cordova-HTTP for permissive-SSL ReST calls (COMPASS-40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Apr 29, 2015
1 parent a7d1b77 commit c103e8a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 39 deletions.
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.opennms.compass" version="2.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<widget id="com.opennms.compass" version="2.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>OpenNMS</name>
<description>OpenNMS Compass</description>
<author email="ranger@opennms.com" href="http://www.opennms.com/">Benjamin Reed</author>
Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "OpenNMS",
"version": "2.0.0",
"version": "2.0.1",
"description": "OpenNMS Compass",
"repository": {
"type": "git",
Expand Down Expand Up @@ -40,6 +40,7 @@
"karma-phantomjs-launcher": "*",
"karma-safari-launcher": "*",
"karma-webdriver-launcher": "*",
"libxmljs": "^0.14.0",
"line-reader": "*",
"shelljs": "*"
},
Expand All @@ -50,15 +51,16 @@
"org.apache.cordova.network-information",
"org.apache.cordova.splashscreen",
"org.apache.cordova.statusbar",
"com.google.cordova.admob",
"cc.fovea.cordova.purchase",
"com.google.cordova.admob",
"com.google.playservices",
"com.ionic.keyboard",
{
"locator": "https://github.com/RangerRick/cordova-certificate-plugin.git",
"id": "com.raccoonfink.cordova.plugins.certificates"
},
"com.ohh2ahh.plugins.appavailability",
"com.google.playservices"
"com.rjfun.cordova.extension",
{
"locator": "https://github.com/RangerRick/cordova-HTTP.git",
"id": "com.synconset.cordovaHTTP"
}
],
"cordovaPlatforms": [
{
Expand Down
39 changes: 23 additions & 16 deletions www/scripts/opennms/services/Info.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'opennms.services.Rest',
'opennms.services.Settings'
])
.factory('Info', function($q, $rootScope, $http, $timeout, RestService, Settings) {
.factory('Info', function($q, $rootScope, $http, $window, $timeout, RestService, Settings) {
console.log('Info: Initializing.');

var defaultInfo = {
Expand All @@ -24,6 +24,20 @@
var currentInfo = angular.copy(defaultInfo);
var info = $q.defer();

var onSuccess = function(data) {
console.log('info success=' + angular.toJson(data));
data.numericVersion = parseFloat(data.version.replace('^(\\d+\\.\\d+).*$', '$1'));
currentInfo = angular.copy(data);
info.resolve(currentInfo);
initialized = true;
};

var onFailure = function() {
currentInfo = angular.copy(defaultInfo);
info.resolve(currentInfo);
initialized = true;
};

var updateInfo = function() {
if (!Settings.isServerConfigured()) {
console.log('Info.updateInfo: skipping update, server is not configured yet.');
Expand All @@ -36,21 +50,14 @@
info = $q.defer();
}

$http.get(RestService.url('/info'), {
headers: {
'Accept': 'application/json'
},
withCredentials: true,
}).success(function(results) {
results.numericVersion = parseFloat(results.version.replace('^(\\d+\\.\\d+).*$', '$1'));
currentInfo = angular.copy(results);
info.resolve(currentInfo);
initialized = true;
}).error(function(data, status, headers, config, statusText) {
console.log('Info.updateInfo failed: ' + status + ' ' + statusText, data);
currentInfo = angular.copy(defaultInfo);
info.resolve(currentInfo);
initialized = true;
RestService.get('/info', {'limit':0}, {'Accept': 'application/json'}).then(function(response) {
if (angular.isString(response)) {
response = angular.fromJson(response);
}
onSuccess(response);
}, function(err) {
console.log('Info.updateInfo failed: ' + angular.toJson(err));
onFailure();
});
};
$timeout(updateInfo);
Expand Down
67 changes: 52 additions & 15 deletions www/scripts/opennms/services/Rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@

angular.module('opennms.services.Rest', [
'ng',
'cordovaHTTP',
'opennms.services.Settings',
])

.factory('RestService', function($q, $http, $rootScope, $window, Settings) {
.factory('RestService', function($q, $http, $rootScope, $window, cordovaHTTP, Settings) {
console.log('RestService: Initializing.');

var requestTimeout = 10000;
var x2js = new X2JS();
/* jshint -W069 */ /* "better written in dot notation" */
$http.defaults.headers.common['Accept'] = 'application/xml';
cordovaHTTP.acceptAllCerts(true);

var updateAuthorization = function() {
var username = Settings.username();
Expand All @@ -26,8 +28,13 @@
console.log('RestService.updateAuthorization: username or password not set.');
delete $http.defaults.headers.common['Authorization'];
} else {
console.log('RestService.updateAuthorization: setting basic auth with username "' + username + '".');
//console.log('RestService.updateAuthorization: setting basic auth with username "' + username + '".');
$http.defaults.headers.common['Authorization'] = 'Basic ' + $window.btoa(username + ':' + password);
cordovaHTTP.useBasicAuth(username, password).then(function() {
console.log('RestService.updateAuthorization: configured basic auth with username "' + username + '".');
}, function(err) {
console.log('RestService.updateAuthorization: failed to configure basic auth with username "' + username + '".');
});
}
};

Expand All @@ -48,6 +55,12 @@
return url + restFragment;
};

var encodeData = function(data) {
return Object.keys(data).map(function(key) {
return [key, data[key]].map(encodeURIComponent).join("=");
}).join("&");
};

var doQuery = function(method, restFragment, params, headers) {
var deferred = $q.defer();
var url = getUrl(restFragment);
Expand All @@ -68,19 +81,43 @@
if (myparams.limit === 0) {
delete myparams.limit;
}
$http({
method: method,
url: url,
params: myparams,
headers: headers,
withCredentials: true,
timeout: requestTimeout,
}).success(function(data) {
//console.log('Rest.doQuery:',data);
deferred.resolve(data);
}).error(function(data, status, headers, config, statusText) {
deferred.reject(new RestError(url, data, status, statusText));
});

console.log('url=' + url + ', params=' + angular.toJson(myparams) + ', headers=' + angular.toJson(headers));
if ($window.cordova) {
if (method === 'GET') {
cordovaHTTP.get(url, myparams, headers).then(function(response) {
deferred.resolve(response.data);
}, function(response) {
deferred.reject(new RestError(url, response.data, response.status));
});
} else if (method === 'PUT') {
cordovaHTTP.put(url, myparams, headers).then(function(response) {
deferred.resolve(response.data);
}, function(response) {
deferred.reject(new RestError(url, response.data, response.status));
});
} else if (method === 'POST') {
cordovaHTTP.post(url, myparams, headers).then(function(response) {
deferred.resolve(response.data);
}, function(response) {
deferred.reject(new RestError(url, response.data, response.status));
});
}
} else {
$http({
method: method,
url: url,
params: myparams,
headers: headers,
withCredentials: true,
timeout: requestTimeout,
}).success(function(data) {
//console.log('Rest.doQuery:',data);
deferred.resolve(data);
}).error(function(data, status, headers, config, statusText) {
deferred.reject(new RestError(url, data, status, statusText));
});
}

return deferred.promise;
};
Expand Down

0 comments on commit c103e8a

Please sign in to comment.