Repository navigation
Expand file tree
/
Copy pathrequestHandler.js
More file actions
504 lines (454 loc) · 19.3 KB
/
Copy pathrequestHandler.js
File metadata and controls
504 lines (454 loc) · 19.3 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*!
GPII pilotsConfig
Copyright 2014 Technische Universität Dresden (TUD)
Copyright 2014 Raising the Floor - International (RtF)
Copyright 2014 Hochschule der Medien (HdM)
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
You may obtain a copy of the License at
https://github.com/GPII/universal/blob/master/LICENSE.txt
Acknowledgements:
The research leading to these results has received funding from
the European Union's Seventh Framework Programme (FP7/2007-2013)
under grant agreement no. 289016.
*/
var util = require('util');
var http = require('http');
var querystring = require("querystring");
var fs = require('fs');
function start(response, postData){
console.log("Request handler 'start' was called");
//TODO: load html from template (split logic from content)
var body = '<!DOCTYPE html><html lang="en">'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'<title>Cloud4all Pilots Configuration Tool</title>'+
'</head>'+
'<body>'+
'<h1>Pilots Configuration Panel</h1>'+
'<h2>Select Matchmaker</h2>'+
'<p>Note: Cloud4all/GPII server must be started before selecting a Matchmaker.</p>'+
'<form action="/selectMM" method="post">'+
'<fieldset><legend>Select Matchmaker</legend>'+
'For auto-configuration scenario:<br>'+
'<input type="radio" name="matchmaker" id="mm_default" checked="checked" value="flat"><label for="mm_default">Rule-based Matchmaker (without solution selection option)</label><br>'+
'<input type="radio" name="matchmaker" id="mm_stmm" value="statistical"><label for="mm_stmm">Statistical Matchmaker</label><br>'+
// '<p>For demo solution selection scenario:<br>'+
// '<input type="radio" name="matchmaker" value="ruleBased">Rule-based Matchmaker<br>'+
// '</p>'+
'<input type="submit" value="Set Matchmaker strategy" /></fieldset>'+
'</form>'+
// snapshot to prefs
'<h2>Snapshot preferences</h2>'+
'<form action="/snapshotToPrefs" method="post">'+
'<fieldset><legend id="snapshottoserver">Snapshot local settings to server</legend>'+
createSnapshotterSelection()+
'<input type="submit" value="Snapshot" aria-labelledby="snapshottoserver" />'+
'</fieldset></form>'+
// log to device
'<h2>Log preferences on local system</h2>'+
'<form action="/logging" method="post">'+
'<fieldset><legend id="writelocalsettingslog">Log current/settings on local file system</legend>'+
'<input type="submit" value="Logging" />'+
'</fieldset></form>'+
// select device spec
'<h2>Select Device Specification</h2><div id="selectDeviceSpec" style="float: none; clear: both;">'+
'<form action="/setDetailedDeviceinfo" method="post" style="width: 50%; float: left;">'+
'<fieldset><legend>For platform A/B scenario</legend>'+
createDetailedDeviceSelection()+
'<input type="submit" value="Set device configuration" />'+
'</fieldset>'+
'</form>\n'+
'<form action="/setDeviceinfo" method="post" style="width: 50%; float: left;">'+
'<fieldset><legend>For Demos</legend>'+
'<input type="radio" name="device" id="demo_SmartHouse" value="demo_SmartHouse"><label for="demo_SmartHouse">SmartHouse</label><br>'+
'<input type="radio" name="device" id="demo_Maavis" value="demo_Maavis"><label for="demo_Maavis">Maavis</label><br>'+
'<input type="radio" name="device" id="demo_GoogleChrome" value="demo_GoogleChrome"><label for="demo_GoogleChrome">Google Chrome extension</label><br>'+
'<input type="radio" name="device" id="demo_Library" value="demo_Library"><label for="demo_Library">WebAnywhere</label><br>'+
// '<input type="radio" name="device" value="demo_MultipleSolutions">Mulitple solutions in computer lab<br>'+
'</fieldset>\n'+
'<fieldset><legend>Default configuration</legend>'+
'<input type="radio" name="device" id="installedSolutions" value="installedSolutions"><label for="installedSolutions">Set device specification to default</label>'+
'</fieldset>'+
'<input type="submit" value="Set device configuration" />'+
'</form>\n</div>\n'+
// feedback pages
'<h2 style="clear: left">Proposing a new Solution - Feedback</h2>'+
'<p>NVDA:<br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/NVDA/RBMMFeedbackGerman.html" target="_blank">NVDA feedback German</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/NVDA/RBMMFeedbackSpanish.html" target="_blank">NVDA feedback Spain</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/NVDA/RBMMFeedbackGreek.html" target="_blank">NVDA feedback Greek</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/NVDA/RBMMFeedbackEnglish.html" target="_blank">NVDA feedback Englisch</a><br>'+
'</p>'+
'<p>SuperNova:<br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/SuperNova/RBMMFeedbackGerman.html" target="_blank">SuperNova feedback German</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/SuperNova/RBMMFeedbackSpanish.html" target="_blank">SuperNova feedback Spain</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/SuperNova/RBMMFeedbackGreek.html" target="_blank">SuperNova feedback Greek</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/SuperNova/RBMMFeedbackEnglish.html" target="_blank">SuperNova feedback Englisch</a><br>'+
'</p>'+
'<p>WebAnywhere:<br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/WebAnywhere/RBMMFeedbackGerman.html" target="_blank">WebAnywhere feedback German</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/WebAnywhere/RBMMFeedbackSpanish.html" target="_blank">WebAnywhere feedback Spain</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/WebAnywhere/RBMMFeedbackGreek.html" target="_blank">WebAnywhere feedback Greek</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/WebAnywhere/RBMMFeedbackEnglish.html" target="_blank">WebAnywhere feedback Englisch</a><br>'+
'</p>'+
'<p>ORCA:<br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/ORCA/RBMMFeedbackGerman.html" target="_blank">ORCA feedback German</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/ORCA/RBMMFeedbackSpanish.html" target="_blank">ORCA feedback Spain</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/ORCA/RBMMFeedbackGreek.html" target="_blank">ORCA feedback Greek</a><br>'+
'<a href="http://wwwpub.zih.tu-dresden.de/~loitsch/feedback/ORCA/RBMMFeedbackEnglish.html" target="_blank">ORCA feedback Englisch</a><br>'+
'</p>'+
'</body>'+
'</html>';
response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}
function createDetailedDeviceSelection() {
var result = "";
for (i in snapshotterSolutionsList) {
var solution = snapshotterSolutionsList[i];
var id = solution["id"];
var name = solution["name"];
if (!name) { name = id; }
result += '<input type="checkbox" id="DeviceReporter_' + id + '" name="' + id + '" value="1"><label for="DeviceReporter_' + id + '">' + name + '</label><br>\n';
}
return result;
}
function setDetailedDeviceinfo(response, postData){
var parsedPost = querystring.parse(postData);
// Select solutions
var multipleSolutionsPresent = false;
var deviceText = '[';
for (i in snapshotterSolutionsList) {
var id = snapshotterSolutionsList[i]["id"];
if (parsedPost[id] == 1) {
if (multipleSolutionsPresent) { deviceText += ',' }
deviceText += '{"id": "' + id + '"}'
multipleSolutionsPresent = true;
}
}
deviceText += ']';
// Write installedSolutions.json
fs.writeFile('../node_modules/universal/testData/deviceReporter/installedSolutions.json', deviceText, function(err) {
if (err) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html><title>Pilots Configuration Tool Feedback</title><body><p>" + err + "</p>");
response.write("<p><a href='javascript:history.go(-1)'>Go back to the pilotsConfig tool</a>.</p></body></html>");
response.end();
} else {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html><title>Pilots Configuration Tool Feedback</title><body><p>Device information successfully set!</p>");
response.write("<p>(Device info: " + deviceText + ")</p>");
response.write("<p><a href='javascript:history.go(-1)'>Go back to the pilotsConfig tool</a>.</p></body></html>");
response.end();
}
});
}
function setDeviceinfo(response, postData){
var device = querystring.parse(postData)["device"];
if (device == "installedSolutions"){
var filePath = './'+ device +'.json' ;
}
else{
var filePath = '../node_modules/universal/testData/deviceReporter/secondPilots/'+ device +'.json' ;
}
fs.createReadStream(filePath).pipe(fs.createWriteStream('../node_modules/universal/testData/deviceReporter/installedSolutions.json'));
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html><title>Pilots Configuration Tool Feedback</title><body><p>Device information successfully set! (" + device + ")</p>");
response.write("<p><a href='javascript:history.go(-1)'>Go back to the pilotsConfig tool</a>.</p></body></html>");
response.end();
}
function getJSONRequest (url, callback) {
var reply = "";
// fetch preference set from the preference server
http.get(url, function(res) {
res.on('data', function(dat){
reply += dat;
});
res.on('end', function () {
callback(reply);
});
}).on('error', function(e) {
console.log("Error on get request to "+url+": " + e.message);
callback(undefined);
});
};
function getCurrentToken (callback) {
return getJSONRequest("http://localhost:8081/token", callback);
}
function getPreferences (token, callback) {
return getJSONRequest("http://preferences.gpii.net/user/" + token, callback);
}
function getSnapshot (callback) {
return getJSONRequest("http://localhost:8081/snapshot", callback);
}
function selectMM (response, postData) {
var matchmaker = querystring.parse(postData)["matchmaker"];
getCurrentToken(function (token) {
if (token) {
token = token.replace(/\s/, "");
token = token.replace(/\[\"/, "");
token = token.replace(/\"\]/, "");
getPreferences(token, function (reply) {
var preferences = JSON.parse(reply);
if (preferences && preferences['preferences']) {
preferences['preferences']['http://registry.gpii.org/common/matchMakerType'] = [{ "value": matchmaker}];
saveModifiedPreferences(token, preferences['preferences'], "matchmaker successfully selected", response);
} else {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html><title>Pilots Configuration Tool Feedback</title><body><p>Error on fetching preferences!</p>");
response.write("<p><a href='javascript:history.go(-1)'>Go back to the pilotsConfig tool</a>.</p></body></html>");
response.end();
}
});
} else {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html><title>Pilots Configuration Tool Feedback</title><body><p>Error on fetching user token!</p>");
response.write("<p><a href='javascript:history.go(-1)'>Go back to the pilotsConfig tool</a>.</p></body></html>");
response.end();
}
});
};
function logging(response) {
var sys = require('sys');
var exec = require('child_process').exec;
var path = require('path');
console.log ("platform" + process.platform);
if(process.platform === 'linux')
var logScript = path.join(process.cwd(), '../linux/LogLocalSettings.sh');
else
var logScript = path.join(process.cwd(), './LogLocalSettings.cmd');
fs.exists(logScript, function (exists) {
var child = exec(logScript, function (error, stdout, stderr) {
console.log ("logging script: " + logScript);
console.log ("logging results: " + stdout);
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html><title>Logging results</title><body><p>Logging results: " + stdout + "</p>");
if (stderr) {
response.write("<p>Error: " + stderr + "</p>");
}
response.write("<p><a href='javascript:history.go(-1)'>Go back to the pilotsConfig tool</a>.</p></body></html>");
response.end();
});
});
};
function snapshotToLog(response) {
var data = new Date();
var timestamp = data.getDate()+'.'+data.getMonth()+'_time_'+data.getHours()+'.'+data.getMinutes();
var path = './logs/';
var log = { };
//Read the currently logged in token
getCurrentToken(function (token) {
if (token) {
getSnapshot(function (reply) {
var snapshotted = JSON.parse(reply);
log['token'] = token;
log['timestamp'] = timestamp;
log['preferences'] = snapshotted;
fs.mkdir(path,function(e){
if(!e || (e && e.code === 'EEXIST')){
fs.writeFile('./logs/'+token+'_'+timestamp, JSON.stringify(log), function(err) {
if(err) {
console.log(err);
response.end();
} else {
console.log("Logfile saved!");
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html><title>Pilots Configuration Tool Feedback</title><body><p>Log successfully saved in directory pilotsConfig/logs!</p>");
response.write("<p><a href='javascript:history.go(-1)'>Go back to the pilotsConfig tool</a>.</p></body></html>");
response.end();
}
});
} else {
//debug
console.log(e);
}
});
});
} else {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html><title>Pilots Configuration Tool Feedback</title><body><p>Error on fetching user token!</p>");
response.write("<p><a href='javascript:history.go(-1)'>Go back to the pilotsConfig tool</a>.</p></body></html>");
response.end();
}
});
};
function snapshotToPrefs(response, postData) {
//Read the currently logged in token
var parsedPost = querystring.parse(postData);
getCurrentToken(function (token) {
if (token) {
token = token.replace(/\s/, "");
token = token.replace(/\[\"/, "");
token = token.replace(/\"\]/, "");
//If a token is logged in, get that token's preferences
getPreferences(token, function (reply) {
var preferences = JSON.parse(reply);
if (preferences && preferences['preferences']) {
//Get and add the snapshotted settings to the preference set
getSnapshot(function (reply) {
var snapshotted = JSON.parse(reply);
for (solution in snapshotted) {
if ((parsedPost) && isAllowedSnapshotSolution(solution, parsedPost)) {
preferences['preferences'][solution] = snapshotted[solution];
};
};
saveModifiedPreferences(token, preferences['preferences'] , "Snapshot saved to preferences server", response);
});
} else {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html><title>Pilots Configuration Tool Feedback</title><body><p>Error on fetching preferences!</p>");
response.write("<p><a href='javascript:history.go(-1)'>Go back to the pilotsConfig tool</a>.</p></body></html>");
response.end();
}
});
} else {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html><title>Pilots Configuration Tool Feedback</title><body><p>Error on fetching user token!</p>");
response.write("<p><a href='javascript:history.go(-1)'>Go back to the pilotsConfig tool</a>.</p></body></html>");
response.end();
}
});
};
function getFeedback(response, postData){
var file = querystring.parse(postData)['feedback'];
fs.readFile('./feedback/'+file, function(err, feedback){
if(err){
throw err;
}
response.writeHead(200, {"Content-Type": "text/html"});
response.write(feedback);
response.end();
});
}
function saveModifiedPreferences(token, preferencesObject, usrMsg, response) {
var data = JSON.stringify(preferencesObject);
// object of options to indicate where to POST to
var post_options = {
host: 'preferences.gpii.net',
port: '80',
path: '/user/' + token,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
// set up the http.request POST
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
res.on('end', function(){
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<html><title>Pilots Configuration Tool Feedback</title><body><p>" + usrMsg + "</p>");
response.write("<p><a href='javascript:history.go(-1)'>Go back to the pilotsConfig tool</a>.</p></body></html>");
response.end();
});
});
// writes http request body data; Server replies and response callback 'res' gets activated
post_req.write(data);
post_req.end();
}
exports.start = start;
exports.selectMM = selectMM;
exports.setDetailedDeviceinfo = setDetailedDeviceinfo;
exports.setDeviceinfo = setDeviceinfo;
exports.snapshotToPrefs = snapshotToPrefs;
exports.getFeedback = getFeedback;
exports.logging = logging;
// outdated logging
//exports.snapshotToLog = snapshotToLog;
function isAllowedSnapshotSolution(solution, parsedPost) {
for (key in parsedPost) {
if ((solution.indexOf(key) > -1) && (parsedPost[key] == 1)) {
return true;
}
}
return false;
}
function createSnapshotterSelection() {
var result = "";
for (i in snapshotterSolutionsList) {
var solution = snapshotterSolutionsList[i];
var id = solution["id"];
var name = solution["name"];
if (!name) { name = id; }
result += '<input type="checkbox" id="SnapShotter_' + id + '" name="' + id + '" value="1"><label for="SnapShotter_' + id + '">' + name + '</label><br>\n';
}
return result;
}
snapshotterSolutionsList =
[
{
"id": "org.nvda-project",
"name": "NVDA"
},
{
"id": "com.microsoft.windows.magnifier",
"name": "Windows Built-in Screen Magnifier"
},
{
"id": "com.microsoft.windows.highContrast",
"name": "Windows High Contrast"
},
{
"id": "com.microsoft.windows.cursors",
"name": "Windows Cursors"
},
{
"id": "com.microsoft.windows.mouseTrailing",
"name": "Windows Mouse Trailing"
},
{
"id": "com.microsoft.windows.onscreenKeyboard",
"name": "Windows Built-in Onscreen Keyboard"
},
{
"id": "org.gnome.orca",
"name": "ORCA Screen Reader"
},
{
"id": "org.gnome.desktop.a11y.magnifier",
"name": "GNOME Shell Magnifier"
},
{
"id": "org.gnome.desktop.interface",
"name": "GNOME Interface Settings (font size, cursor size)"
},
{
"id": "org.gnome.shell.overrides",
"name": "GNOME Shell overrides"
},
{
"id": "org.gnome.desktop.wm.preferences",
"name": "GNOME desktop Window Manager preferences"
},
{
"id": "org.gnome.nautilus",
"name": "GNOME Nautilus Settings"
},
{
"id": "org.gnome.desktop.a11y.keyboard",
"name": "GNOME Shell Keyboard Settings"
},
{
"id": "org.gnome.desktop.a11y.applications.onscreen-keyboard",
"name": "GNOME Assistive Technology - Screen Keyboard"
},
{
"id": "org.alsa-project",
"name": "Linux System Volume (ALSA)"
},
{
"id": "org.freedesktop.xrandr",
"name": "XRandR (for brightness, screen height and screen width)"
},
{
"id": "org.chrome.cloud4chrome",
"name": "Cloud4Chrome"
}
];