This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
149 lines (131 loc) · 5.31 KB
/
server.js
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
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var fs = require('fs');
var xml2js = require('xml2js');
var SAPRestService = require('./model/SAPRestService.js');
var bo = require('./model/bo.js');
var log4js = require('log4js');
var log = log4js.getLogger("Web server");
log.setLevel("DEBUG");
var port = process.env.port || process.argv[2] || 1338 ;
var app = express();
app.use(log4js.connectLogger(log4js.getLogger("http"), { level: 'auto', format: ':method :url'}));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(express.static('public'));
//app.listen(port);
var os = require("os");
app.listen(8081, os.hostname());
//-------------------------------------------------------------------
//Remove below comments in case you want to enable cross domain call
//And give the URL of domain from where you are expecting the call.
//-------------------------------------------------------------------
//var allowCrossDomain = function (req, res, next) {
// res.header('Access-Control-Allow-Origin', 'http://amit-pc:8000');
// res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
// res.header('Access-Control-Allow-Headers', 'Content-Type');
// next();
//}
//app.use(allowCrossDomain);
var selectedObjects;
// TODO: some kind of persistent data containing username and password.
var userName = "boUserName";
var password = "boPassword";
// TODO: some kind of persistent data containing accepted username and passwords for authentication to WDC.
var acceptedUsers = {
"kbui" : "kbui",
"skalki" : "skalki",
"kritiv" : "kritiv"
}
app.get('/', function (request, response) {
response.sendFile("views/BOT.html", {root: __dirname});
});
app.post('/api/cms', async function (req, response) {
serverIP = req.body["Server"];
cmsName = req.body["CMS"];
universeName = req.body["UnxName"];
universeId = req.body["UnxId"];
if (acceptedUsers[req.body["UserName"]] == req.body["Password"]) {
var metadataFile = "./UniverseMetdata/" + universeName + ".json";
fs.access(metadataFile, fs.R_OK, function(e) {
if (!e) {
response.sendStatus(200);
} else {
log.error("Couldn't access metadata file " + metadataFile);
response.sendStatus(500);
}
});
} else {
log.error("Invalid username or password");
response.statusMessage = "Invalid username or password";
response.sendStatus(500);
}
});
app.get('/api/bo-fav/:favName', function(req, response) {
selectedObjects = bo.getFavorite(req.params.favName);
response.send(selectedObjects);
});
app.post('/api/bo-fav', function(req, response) {
var favName = req.body["favName"];
selectedObjects = JSON.parse(req.body.data);
bo.addToFavorite(favName, selectedObjects);
response.send(true);
});
app.get('/api/bo-metadata', function (req, response) {
console.log("get api/bo metadata:");
var metadataFile = "./UniverseMetdata/" + universeName + ".json";
var universeMetadata = JSON.parse(fs.readFileSync(metadataFile));
var metadataResponse = bo.getBOData(universeMetadata);
response.json(metadataResponse);
});
app.post('/api/bo-ids', function (req, response) {
selectedObjects = JSON.parse(req.body.data);
response.send(true);
});
app.get('/api/bo-schema', async function (req, response) {
var sapRestSvc = new SAPRestService(userName, password, serverIP, cmsName, universeId, selectedObjects);
try {
// node.js closes socket after 2 minutes, causing browser to resend ajax. This is to prevent it from resubmitting ajax call.
response.setTimeout(0);
await sapRestSvc.logon();
var queryId = await sapRestSvc.createQuery();
var schemas = await sapRestSvc.getQuerySchema(queryId);
response.send(schemas); // not really understan
await sapRestSvc.logoff();
} catch (err) {
handleError(err, response);
await sapRestSvc.logoff();
}
});
app.get('/api/bo-data', async function (req, response) {
var sapRestSvc = new SAPRestService(userName, password, serverIP, cmsName, universeId, selectedObjects);
try {
// node.js closes socket after 2 minutes, causing browser to resend ajax. This is to prevent it from resubmitting ajax call.
response.setTimeout(0);
await sapRestSvc.logon();
var queryId = await sapRestSvc.createQuery();
var data = await sapRestSvc.getQueryData(queryId);
response.send(data);
await sapRestSvc.logoff();
} catch (err) {
console.log((err.response ? err.response.data.message : err.message));
await sapRestSvc.logoff();
}
});
function handleError(err, response) {
var errMsg;
var status = 500; // default generic code
if (err.response) {
status = err.response.status;
if (err.response.data.message) {
errMsg = err.response.data.message;
} else {
errMsg = err.response.data;
}
} else {
errMsg = err.message;
}
response.statusMessage = errMsg;
response.status(status).end();
}