// load rest client lib
loadScript("/rewe/templates/js/restClient.js");
/**
* Javascript model.
*
* @constructor
*/
var Model = function () {
/**
* See https://jsonplaceholder.typicode.com/
*
* @type {string}
*/
var baseUrl = "https://jsonplaceholder.typicode.com";
var restClient = new RestClient();
/**
* Returns all posts from the JSON Placeholder service.
*
* @returns {JSON object}
*/
this.getPosts = function () {
return JSON.parse(restClient.get(baseUrl + "/posts"));
};
/**
* Creates new post in the JSON Placeholder service.
*
* @param data
* @returns {JSON object}
*/
this.createPost = function (data) {
return JSON.parse(restClient.post(baseUrl + "/posts", data));
};
}
new Model();
Add the REST client javascript:
/rewe/templates/js/restClient.js
/**
* Simple implementation of REST library. Since Nashorn doesn't support XMLHttpRequest, it's implemented using
* Apache HTTP client.
*
* Be aware that this implementation of http requests is synchronous.
* Using these methods could have negative performance implications for your site.
*
* Consider making http requests from the client side if possible.
* Consider a more sophisticated implementation in Java with caching, buffering requests, etc.
*
* @constructor
*/
var RestClient = function () {
var HttpClients = Java.type("org.apache.http.impl.client.HttpClients");
var HttpGet = Java.type("org.apache.http.client.methods.HttpGet");
var HttpPost = Java.type("org.apache.http.client.methods.HttpPost");
var ArrayList = Java.type("java.util.ArrayList");
var UrlEncodedFormEntity = Java.type("org.apache.http.client.entity.UrlEncodedFormEntity");
var BasicNameValuePair = Java.type("org.apache.http.message.BasicNameValuePair");
var EntityUtils = Java.type("org.apache.http.util.EntityUtils");
this.get = function (url) {
var httpClient = HttpClients.createDefault();
var httpGet = new HttpGet(url);
var response = httpClient.execute(httpGet);
try {
entity = response.getEntity();
return EntityUtils.toString(entity, "utf-8");
} finally {
response.close();
}
}
this.post = function (url, params) {
var httpClient = HttpClients.createDefault();
var httpPost = new HttpPost(url);
var data = new ArrayList();
for (var key in params) {
data.add(new BasicNameValuePair(key, params[key]));
}
httpPost.setEntity(new UrlEncodedFormEntity(data));
response = httpClient.execute(httpPost);
try {
entity = response.getEntity();
return EntityUtils.toString(entity, "utf-8");
} finally {
response.close();
}
}
Make that component available on some page, then add it: