Adding emm apis as common modules

revert-70aa11f8
Ace 8 years ago committed by prabathabey
parent d28054a39d
commit 8a54c5a9cb

@ -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"];
}
);
}

@ -17,94 +17,88 @@
* under the License. * under the License.
*/ */
var log = new Log("api/invoker-api.jag");
var uri = request.getRequestURI(); var uri = request.getRequestURI();
var uriMatcher = new URIMatcher(String(uri)); var uriMatcher = new URIMatcher(String(uri));
var log = new Log("api/invoker-api.jag");
var constants = require("/app/modules/constants.js"); var constants = require("/app/modules/constants.js");
var devicemgtProps = require('/app/conf/devicemgt-props.js').config(); var devicemgtProps = require('/app/conf/devicemgt-props.js').config();
var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker; var serviceInvokers = require("/app/modules/backend-service-invoker.js")["backendServiceInvoker"];
var result;
if (uriMatcher.match("/{context}/api/invoker/execute/")) { if (uriMatcher.match("/{context}/api/invoker/execute/")) {
var method = request.getContent().actionMethod; var restAPIRequestDetails = request.getContent();
var targetURL = getTargetUrl(devicemgtProps.httpsURL, request.getContent().actionUrl);
var payload = request.getContent().actionPayload; var requestMethod = restAPIRequestDetails["requestMethod"];
var contentType = request.getHeader(constants.CONTENT_TYPE_IDENTIFIER); var requestURL = restAPIRequestDetails["requestURL"];
var acceptType = request.getHeader(constants.ACCEPT_IDENTIFIER); var requestPayload = restAPIRequestDetails["requestPayload"];
if (method == undefined && payload == undefined) {
method = parse(request.getContent()).actionMethod; if (!requestMethod) {
targetURL = getTargetUrl(devicemgtProps.httpsURL, parse(request.getContent()).actionUrl); requestMethod = parse(restAPIRequestDetails)["requestMethod"];
payload = parse(request.getContent()).actionPayload;
} }
if (!requestURL) {
requestURL = parse(restAPIRequestDetails)["requestURL"];
}
if (!requestPayload) {
requestPayload = parse(restAPIRequestDetails)["requestPayload"];
}
var restAPIEndpoint = devicemgtProps["httpsURL"] + requestURL;
try { try {
switch (method) { switch (requestMethod) {
case constants.HTTP_GET: case constants["HTTP_GET"]:
var responseData = serviceInvokers.XMLHttp.get( serviceInvokers.XMLHttp.get(
targetURL, function (responsePayload) { restAPIEndpoint,
response.status = 200; function (restAPIResponse) {
response.content = responsePayload; response["status"] = restAPIResponse["status"];
}, if (restAPIResponse["responseText"]) {
function (responsePayload) { response["content"] = restAPIResponse["responseText"];
response.status = responsePayload.status; }
response.content = responsePayload.responseText; }
}, );
contentType,
acceptType);
break; break;
case constants.HTTP_POST: case constants["HTTP_POST"]:
var responseData = serviceInvokers.XMLHttp.post( serviceInvokers.XMLHttp.post(
targetURL, payload, function (responsePayload) { restAPIEndpoint,
response.status = 200; requestPayload,
response.content = responsePayload; function (restAPIResponse) {
}, response["status"] = restAPIResponse["status"];
function (responsePayload) { if (restAPIResponse["responseText"]) {
response.status = responsePayload.status; response["content"] = restAPIResponse["responseText"];
response.content = responsePayload.responseText; }
}, }
contentType, );
acceptType);
break; break;
case constants.HTTP_PUT: case constants["HTTP_PUT"]:
var responseData = serviceInvokers.XMLHttp.put( serviceInvokers.XMLHttp.put(
targetURL, payload, function (responsePayload) { restAPIEndpoint,
response.status = 200; requestPayload,
response.content = responsePayload; function (restAPIResponse) {
}, response["status"] = restAPIResponse["status"];
function (responsePayload) { if (restAPIResponse["responseText"]) {
response.status = responsePayload.status; response["content"] = restAPIResponse["responseText"];
response.content = responsePayload.responseText; }
}, }
contentType, );
acceptType);
break; break;
case constants.HTTP_DELETE: case constants["HTTP_DELETE"]:
var responseData = serviceInvokers.XMLHttp.delete( serviceInvokers.XMLHttp.delete(
targetURL, function (responsePayload) { restAPIEndpoint,
response.status = 200; function (restAPIResponse) {
response.content = responsePayload; response["status"] = restAPIResponse["status"];
}, if (restAPIResponse["responseText"]) {
function (responsePayload) { response["content"] = restAPIResponse["responseText"];
response.status = responsePayload.status; }
response.content = responsePayload.responseText; }
}, );
contentType,
acceptType);
break; break;
} }
} catch (e) { } 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;
} }
} }
%> %>

@ -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"]);
}
}
}
);
}
%>

@ -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;
}
%>
Loading…
Cancel
Save