From ca10ad53bb88d210b800f14ad1cc341b04c8bc6f Mon Sep 17 00:00:00 2001 From: Ace Date: Fri, 15 Jul 2016 11:59:15 +0530 Subject: [PATCH] Adding emm apis as common modules --- .../devicemgt/api/data-tables-invoker-api.jag | 59 ++++++++ .../jaggeryapps/devicemgt/api/invoker-api.jag | 140 +++++++++--------- .../devicemgt/api/operation-api.jag | 60 ++++++++ .../jaggeryapps/devicemgt/api/policy-api.jag | 52 +++++++ 4 files changed, 238 insertions(+), 73 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/policy-api.jag diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag new file mode 100644 index 00000000000..160a7ff517c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/data-tables-invoker-api.jag @@ -0,0 +1,59 @@ +<% +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +var log = new Log("api/data-tables-invoker-api.jag"); + +var uri = request.getRequestURI(); +var uriMatcher = new URIMatcher(String(uri)); + +var devicemgtProps = require('/app/conf/devicemgt-props.js').config(); +var serviceInvokers = require("/app/modules/backend-service-invoker.js")["backendServiceInvoker"]; + +if (uriMatcher.match("/{context}/api/data-tables/invoker")) { + var url = request.getParameter("url"); + var targetURL; + var payload = request.getContent(); + + function appendQueryParam (url, queryParam , value) { + if (url.indexOf("?") > 0) { + return url + "&" + queryParam + "=" + value; + } + return url + "?" + queryParam + "=" + value; + } + targetURL = devicemgtProps["httpsURL"] + request.getParameter("url"); + + var allParams = request.getAllParameters(); + + for (var key in allParams) { + if (allParams.hasOwnProperty(key)) { + if(key == "limit" || key == "offset" || key == "filter"){ + targetURL = appendQueryParam(targetURL, key, allParams[key]); + } + } + } + + serviceInvokers.XMLHttp.get( + targetURL, + // response callback + function (backendResponse) { + response["status"] = backendResponse["status"]; + response["content"] = backendResponse["responseText"]; + } + ); +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag index ac587def72e..1fd232e3c77 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/invoker-api.jag @@ -17,94 +17,88 @@ * under the License. */ +var log = new Log("api/invoker-api.jag"); + var uri = request.getRequestURI(); var uriMatcher = new URIMatcher(String(uri)); -var log = new Log("api/invoker-api.jag"); - var constants = require("/app/modules/constants.js"); var devicemgtProps = require('/app/conf/devicemgt-props.js').config(); -var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker; - -var result; +var serviceInvokers = require("/app/modules/backend-service-invoker.js")["backendServiceInvoker"]; if (uriMatcher.match("/{context}/api/invoker/execute/")) { - var method = request.getContent().actionMethod; - var targetURL = getTargetUrl(devicemgtProps.httpsURL, request.getContent().actionUrl); - var payload = request.getContent().actionPayload; - var contentType = request.getHeader(constants.CONTENT_TYPE_IDENTIFIER); - var acceptType = request.getHeader(constants.ACCEPT_IDENTIFIER); - if (method == undefined && payload == undefined) { - method = parse(request.getContent()).actionMethod; - targetURL = getTargetUrl(devicemgtProps.httpsURL, parse(request.getContent()).actionUrl); - payload = parse(request.getContent()).actionPayload; + var restAPIRequestDetails = request.getContent(); + + var requestMethod = restAPIRequestDetails["requestMethod"]; + var requestURL = restAPIRequestDetails["requestURL"]; + var requestPayload = restAPIRequestDetails["requestPayload"]; + + if (!requestMethod) { + requestMethod = parse(restAPIRequestDetails)["requestMethod"]; + } + + if (!requestURL) { + requestURL = parse(restAPIRequestDetails)["requestURL"]; } + + if (!requestPayload) { + requestPayload = parse(restAPIRequestDetails)["requestPayload"]; + } + + var restAPIEndpoint = devicemgtProps["httpsURL"] + requestURL; + try { - switch (method) { - case constants.HTTP_GET: - var responseData = serviceInvokers.XMLHttp.get( - targetURL, function (responsePayload) { - response.status = 200; - response.content = responsePayload; - }, - function (responsePayload) { - response.status = responsePayload.status; - response.content = responsePayload.responseText; - }, - contentType, - acceptType); + switch (requestMethod) { + case constants["HTTP_GET"]: + serviceInvokers.XMLHttp.get( + restAPIEndpoint, + function (restAPIResponse) { + response["status"] = restAPIResponse["status"]; + if (restAPIResponse["responseText"]) { + response["content"] = restAPIResponse["responseText"]; + } + } + ); break; - case constants.HTTP_POST: - var responseData = serviceInvokers.XMLHttp.post( - targetURL, payload, function (responsePayload) { - response.status = 200; - response.content = responsePayload; - }, - function (responsePayload) { - response.status = responsePayload.status; - response.content = responsePayload.responseText; - }, - contentType, - acceptType); + case constants["HTTP_POST"]: + serviceInvokers.XMLHttp.post( + restAPIEndpoint, + requestPayload, + function (restAPIResponse) { + response["status"] = restAPIResponse["status"]; + if (restAPIResponse["responseText"]) { + response["content"] = restAPIResponse["responseText"]; + } + } + ); break; - case constants.HTTP_PUT: - var responseData = serviceInvokers.XMLHttp.put( - targetURL, payload, function (responsePayload) { - response.status = 200; - response.content = responsePayload; - }, - function (responsePayload) { - response.status = responsePayload.status; - response.content = responsePayload.responseText; - }, - contentType, - acceptType); + case constants["HTTP_PUT"]: + serviceInvokers.XMLHttp.put( + restAPIEndpoint, + requestPayload, + function (restAPIResponse) { + response["status"] = restAPIResponse["status"]; + if (restAPIResponse["responseText"]) { + response["content"] = restAPIResponse["responseText"]; + } + } + ); break; - case constants.HTTP_DELETE: - var responseData = serviceInvokers.XMLHttp.delete( - targetURL, function (responsePayload) { - response.status = 200; - response.content = responsePayload; - }, - function (responsePayload) { - response.status = responsePayload.status; - response.content = responsePayload.responseText; - }, - contentType, - acceptType); + case constants["HTTP_DELETE"]: + serviceInvokers.XMLHttp.delete( + restAPIEndpoint, + function (restAPIResponse) { + response["status"] = restAPIResponse["status"]; + if (restAPIResponse["responseText"]) { + response["content"] = restAPIResponse["responseText"]; + } + } + ); break; } } catch (e) { - log.error("Exception occurred while accessing sevices", e); + log.error("Exception occurred while trying to access backend " + + "REST API services from Jaggery API invoker layer", e); } } - -function getTargetUrl(serverUrl, actionUrl){ - if(actionUrl == undefined || actionUrl.lastIndexOf("http", 0) === 0){ - return actionUrl; - } else { - return serverUrl + actionUrl; - } -} - %> diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag new file mode 100644 index 00000000000..797eac1bd1a --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/operation-api.jag @@ -0,0 +1,60 @@ +<% +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +var uri = request.getRequestURI(); +var uriMatcher = new URIMatcher(String(uri)); + +var log = new Log("api/operation-api.jag"); + +var serviceInvokers = require("/app/modules/backend-service-invoker.js")["backendServiceInvoker"]; +var devicemgtProps = require('/app/conf/devicemgt-props.js').config(); + +if (uriMatcher.match("/{context}/api/operation/paginate")) { + var deviceType = request.getParameter("deviceType"); + var deviceId = request.getParameter("deviceId"); + var index = request.getParameter("start"); + var length = request.getParameter("length"); + var search = request.getParameter("search[value]"); + + var restAPIEndpoint = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/devices/" + + deviceType + "/" + deviceId + "/operations?offset=" + index + "&limit=" + length; + + serviceInvokers.XMLHttp.get( + restAPIEndpoint, + function (restAPIResponse) { + if (restAPIResponse["status"] == 200 && restAPIResponse["responseText"]) { + var responsePayload = parse(restAPIResponse["responseText"]); + + var paginatedResult = {}; + paginatedResult["recordsTotal"] = responsePayload["count"]; + paginatedResult["recordsFiltered"] = responsePayload["count"]; + paginatedResult["data"] = responsePayload["operations"]; + + response["status"] = 200; + response["content"] = paginatedResult; + } else { + response["status"] = restAPIResponse["status"]; + if (restAPIResponse["responseText"]) { + response["content"] = parse(restAPIResponse["responseText"]); + } + } + } + ); +} +%> \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/policy-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/policy-api.jag new file mode 100644 index 00000000000..d2a2630be7b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/policy-api.jag @@ -0,0 +1,52 @@ +<% +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + @Deprecated + */ + +var uri = request.getRequestURI(); +var uriMatcher = new URIMatcher(String(uri)); + +var log = new Log("api/policy-api.jag"); + +var constants = require("/modules/constants.js"); +var policyModule = require("/modules/policy.js").policyModule; + +var result; +if (uriMatcher.match("/{context}/api/policies/update")) { + payload = request.getContent(); + policyModule.updatePolicyPriorities(payload); +} else if (uriMatcher.match("/{context}/api/policies/{id}/delete")) { + elements = uriMatcher.elements(); + policyId = elements.id; + try { + result = policyModule.deletePolicy(policyId); + } catch (e) { + log.error("Exception occurred while trying to delete policy for id:" + policyId, e); + // http status code 500 refers to - Internal Server Error. + result = 500; + } +} + +// returning the result. +if (result) { + response.content = result; +} +%> \ No newline at end of file