Adding emm apis as common modules

merge-requests/7/head
Ace 8 years ago
parent ab5aa3e31c
commit ca10ad53bb

@ -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.
*/
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);
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);
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_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_POST"]:
serviceInvokers.XMLHttp.post(
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_PUT"]:
serviceInvokers.XMLHttp.put(
restAPIEndpoint,
requestPayload,
function (restAPIResponse) {
response["status"] = restAPIResponse["status"];
if (restAPIResponse["responseText"]) {
response["content"] = restAPIResponse["responseText"];
}
}
);
break;
case constants["HTTP_DELETE"]:
serviceInvokers.XMLHttp.delete(
restAPIEndpoint,
function (restAPIResponse) {
response["status"] = restAPIResponse["status"];
if (restAPIResponse["responseText"]) {
response["content"] = restAPIResponse["responseText"];
}
} catch (e) {
log.error("Exception occurred while accessing sevices", e);
}
);
break;
}
function getTargetUrl(serverUrl, actionUrl){
if(actionUrl == undefined || actionUrl.lastIndexOf("http", 0) === 0){
return actionUrl;
} else {
return serverUrl + actionUrl;
} catch (e) {
log.error("Exception occurred while trying to access backend " +
"REST API services from Jaggery API invoker layer", e);
}
}
%>

@ -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