Configuration

  1. Create a new project and name it config_project
  2. Select the ScriptingServices sub-folder of the project and open the pop-up menu.
  3. Choose New -> Scripting Service.
  4. Choose Server-Side JavaScript Service from the list of available templates.
  5. Give the service a meaningful name (e.g config.js).
  6. Replace the generated code in config.js with the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
	/* globals $ */
	/* eslint-env node, dirigible */
	
	processRequest();
	
	function processRequest() {
		var request = require('net/http/request');
		var response = require('net/http/response');
		var xss = require('utils/xss');
		var config = require('core/config');
		
		var method = request.getMethod();
		
		if (method === "GET") {
		    var list = xss.escapeSql(request.getParameter("list"));
		    var path = xss.escapeSql(request.getParameter("path"));
		    var key = xss.escapeSql(request.getParameter("key"));   
		
			if (list) {
				response.println(readConfiguration(config, path));
			} else {
				response.println(readConfigurationProperty(config, path, key));
			}
		} else if (method === "POST") {
		    var data = JSON.parse(request.readInputText());
			storeConfiguration(config, data)
			response.println("Configuration stored");
		} else if (method === "DELETE") {
		   	config.clear();
		    response.println("Config cleared!");
		}
	
		response.flush();
		response.close();
	}
	
	
	function readConfiguration(config, path) {
		return config.getJson(path);
	}
	
	function readConfigurationProperty(config, path, key) {
		return config.get(path, key);
	}
	function storeConfiguration(config, data) {
		config.set(data.path, data.key, data.value);
	}

get(path, key) - get property value by given path

getJson(path) - retrieves properties by given path in a JSON format

set(path, key, value) - add property at given path

setJson(path, properties) - add properties at given path

delete(path) - removes properties by given path

clear() - removes all properties from the storage

With REST client, send a POST request to the service, with the following body:

1
2
3
4
5
6
7
	{
	   "path": "properties",
	   "key": "key",
	   "value": "test"
	}

Then access the config.js service in the following manner:

http //[host]:[port]/services/js/[project-name]/[scripting-service-name]?path=properties&list=true

Edit