-
Notifications
You must be signed in to change notification settings - Fork 10
/
callERS.js
299 lines (240 loc) · 7.64 KB
/
callERS.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
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
// Copyright (c) 2016 IBM Corp. All rights reserved.
// Use of this source code is governed by the Apache License,
// Version 2.0, a copy of which can be found in the LICENSE file.
/* callERS.js
* The following file represents an instance of a call to the Embeddable Reporting Service (ERS)
* Parses JSESSIONID cookies where necessary and routes requests to and from ERS
*/
// ERS connection constructor
function callERS(ersUri, username, password, bundleUri) {
var url = require('url');
var ersContextRoot = null;
var cookieJar = "";
// return appropriate connection depending on whether reporting
// service is listening on http or https
var getConnection = function(uri) {
if (url.parse(uri).protocol === 'https:') {
return require('https');
} else {
return require('http');
}
};
// returns the host name of the reporting service
var getHost = function(uri) {
return url.parse(uri).hostname;
};
// returns the port of the reporting service
var getPort = function(uri) {
if (url.parse(uri).protocol === 'https:') {
return (url.parse(uri).port || 443);
} else {
return (url.parse(uri).port || 80);
}
};
// set the context root that will be used as a prefix on URLs generated by ERS.
this.setContextRoot = function(contextRoot) {
ersContextRoot = contextRoot;
};
// setup connection information for reporting service
var ersHost = getHost(ersUri);
var ersPort = getPort(ersUri);
var ersConnection = getConnection(ersUri);
var ersAuth = username + ':' + password;
var ersBundleUri = bundleUri;
// connects to the reporting service and retrieves a connection id
// for the specified bundle uri. If a callback has been specified,
// the original request and response are provided so that a retry
// of the original request can be attempted.
this.doConnect = function(clientRequest, clientResponse, callback, ers) {
if(cookieJar != ""){
cookieJar = "";
}
var responseCallback = function(ersResponse) {
var str = '';
ersResponse.on('data', function(chunk) {
str += chunk;
});
ersResponse.on('end', function() {
var ersHeaders = ersResponse.headers;
for ( var headerName in ersHeaders) {
var headerValue = ersHeaders[headerName];
if (headerName === 'set-cookie') {
for (idx = 0; idx < headerValue.length; idx++) {
var cookie = headerValue[idx].substring(0, headerValue[idx].indexOf(';') + 1);
cookieJar += cookie;
}
}
}
if (callback) {
return callback(ersResponse.statusCode, clientRequest, clientResponse, ers);
}
});
};
var options = {
host : ersHost,
port : ersPort,
path : '/ers/v1/connection',
method : 'POST',
auth : ersAuth
};
var ersRequest = ersConnection.request(options, responseCallback);
ersRequest.setHeader('Content-Type', 'application/json');
ersRequest.on('error', function(e) {
if (callback) {
return callback(503, clientRequest, clientResponse, ers);
}
});
ersRequest.write("{}");
ersRequest.end();
};
// disconnects from the reporting service
this.doDisconnect = function() {
var responseCallback = function(ersResponse) {
ersResponse.on('data', function(chunk) {
});
ersResponse.on('end', function() {
});
};
var options = {
host : ersHost,
port : ersPort,
path : '/ers/v1/connection',
method : 'DELETE',
auth : ersAuth
};
var ersRequest = ersConnection.request(options, responseCallback);
cookieJar = "";
ersRequest.end();
};
//For Testing
this.getCookieJar = function(){
return cookieJar;
};
// Issues a request to the reporting service. If a callback has been
// specified, the original request and response are returned so that
// a retry can be attempted if desired.
this.doVerb = function(clientRequest, clientResponse, callback, ers) {
var queryString = url.parse(clientRequest.url).search;
var copyRequestHeaders = function(clientRequest, ersRequest) {
var clientHeaders = clientRequest.headers;
for ( var headerName in clientHeaders) {
headerValue = clientHeaders[headerName];
if (headerName === 'host') {
continue;
}
else{
ersRequest.setHeader(headerName, headerValue);
}
}
ersRequest.setHeader('cookie', cookieJar);
};
var copyRequestBody = function (clientRequest, ersRequest) {
clientRequest.on('data', function (chunk) {
ersRequest.write(chunk, 'binary');
});
};
var copyResponseHeaders = function(ersResponse, clientResponse) {
var ersHeaders = ersResponse.headers;
for ( var headerName in ersHeaders) {
var headerValue = ersHeaders[headerName];
if (headerName === 'set-cookie') {
continue;
} else {
clientResponse.setHeader(headerName, headerValue);
}
}
};
var responseCallback = function(ersResponse) {
if (!callback || ersResponse.statusCode !== 404) {
clientResponse.statusCode = ersResponse.statusCode;
copyResponseHeaders(ersResponse, clientResponse);
}
ersResponse.on('data', function(chunk) {
if (!callback || ersResponse.statusCode !== 404) {
clientResponse.write(chunk, 'binary');
}
});
ersResponse.on('end', function() {
if (ersResponse.statusCode !== 404) {
clientResponse.end();
}
if (callback && ersResponse.statusCode === 404) {
return callback(ersResponse.statusCode, clientRequest, clientResponse, ers);
}
});
};
var path = url.parse(clientRequest.url).pathname;
if (queryString != null) {
path = path + queryString;
}
var options = {
host : ersHost,
port : ersPort,
path : path,
method : clientRequest.method,
auth : ersAuth
};
var ersRequest = ersConnection.request(options, responseCallback);
copyRequestHeaders(clientRequest, ersRequest);
copyRequestBody(clientRequest, ersRequest);
if (ersContextRoot != null) {
ersRequest.setHeader('ERS-ContextRoot', ersContextRoot);
}
ersRequest.on('error', function(e) {
clientResponse.statusCode = 503;
if (callback) {
return callback(clientResponse.statusCode, clientRequest, clientResponse, ers);
} else {
clientResponse.end();
}
});
ersRequest.end();
};
}
//ers reconnect handler
//
// If a 404 is returned for the reconnect attempt, do not retry the request.
var ersReconnectHandler = function (statusCode, originalRequest, originalResponse, ers) {
if (statusCode === 404) {
originalResponse.statusCode = statusCode;
originalResponse.end();
return;
}
// try the request again, except this time do not specify
// a retry handler (otherwise an infinite loop could occur)
ers.doVerb(originalRequest, originalResponse, null, null);
};
// ers retry handler
//
// If a 404 is returned on the first attempt
var ersRetryHandler = function (statusCode, originalRequest, originalResponse, ers) {
if (statusCode === 404) {
ers.doConnect(originalRequest, originalResponse, ersReconnectHandler, ers);
return;
}
};
// public methods
// Connects to the reporting service and retrieves a JSESSIONID cookie
// for the specified bundle uri.
callERS.prototype.connect = function() {
this.doConnect(null, null, null, null);
};
// Disconnects from the reporting service and invalidates the
// session cookie
callERS.prototype.disconnect = function() {
this.doDisconnect();
};
// Issues a request to the reporting service.
callERS.prototype.execute = function(clientRequest, clientResponse) {
this.doVerb(clientRequest, clientResponse, ersRetryHandler, this);
};
// Set the context root.
callERS.prototype.setContextRoot = function(contextRoot) {
this.setContextRoot(contextRoot);
};
//for testing
callERS.prototype.getCookieJar = function() {
this.getCookieJar();
};
// export the class
module.exports = callERS;