-
Notifications
You must be signed in to change notification settings - Fork 5
/
RESTMessageScripted.js
169 lines (138 loc) · 4.61 KB
/
RESTMessageScripted.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
var RESTMessageScripted = Class.create();
RESTMessageScripted.prototype = Object.extendsObject(RESTMessage, {
/*
* Constructor
* Takes in an http function name: get, post, put, delete
*/
initialize: function(funcName, endpoint) {
this.restMessageGr = new GlideRecord("sys_rest_message");
this.functionGr = new GlideRecord("sys_rest_message_fn");
this.headerGr = new GlideRecord("sys_rest_message_headers");
this.headerGrList = [];
this.fnHeaderGr = new GlideRecord("sys_rest_message_fn_headers");
this.fnHeaderGrList = [];
this.fnParamDefGr = new GlideRecord('sys_rest_message_fn_param_defs');
this.fnParamDefGrList = [];
this.valid = false;
this.name;
this.props = new Packages.java.util.Properties();
this.funcName = funcName.toLowerCase();
this.useBasicAuth = false;
this.userName;
this.userPassword;
this.midServer; // mid server name minus mid.server
this.use_ecc;
this.eccResponse;
this.httpStatus;
this.eccParameters = {};
this.eccCorrelator;
this.endPoint = endpoint;
},
execute: function(){
//OVERRIDING PARENT METHOD
var httpResponse = null;
var response = 'error';
this.httpStatus = null;
this._handleEndpoint();
var headers = this._handleHeaders();
var params = this._handleParameters();
var creds = this._handleBasicAuth();
if (this.use_ecc) {
// Build ECC queue payload
var payload = new GlideXMLDocument('parameters');
this._addParameterToPayload(payload, 'message_headers', this._getMessageFields(headers));
this._addParameterToPayload(payload, 'message_parameters', this._getMessageFields(params));
for (var name in this.eccParameters){
this._addParameterToPayload(payload, name, this.eccParameters[name]);
}
if (this.useBasicAuth) {
if (creds) {
var encrypter = new GlideEncrypter();
this._addParameterToPayload(payload, 'rest_user', creds.user);
this._addParameterToPayload(payload, 'rest_password', 'enc:' + encrypter.reencryptForAutomation(creds.password));
}
}
// if the function takes content
if (this.funcName == 'post' || this.funcName == 'put'){
this._addParameterToPayload(payload, 'content', this._handleContent());
}
this._createECCQueueEntry(payload.toString());
} else {
var httpRequest = new GlideHTTPRequest(this.endPoint);
if (this.useBasicAuth) {
if (creds) {
var Encrypter = new GlideEncrypter();
var userpassword = Encrypter.decrypt(creds.password);
httpRequest.setBasicAuth(creds.user, userpassword);
}
}
// Pass the headers through
for (var h = 0; h < headers.length; h++)
httpRequest.addHeader(headers[h].name, headers[h].value);
// Pass the parameters through
for (var i = 0; i < params.length; i++)
httpRequest.addParameter(params[i].name, params[i].value);
if (this.funcName == 'get')
httpResponse = this._handleGetRequest(httpRequest);
else if (this.funcName == 'post')
httpResponse = this._handlePostRequest(httpRequest, this._handleContent());
else if (this.funcName == 'put')
httpResponse = this._handlePutRequest(httpRequest, this._handleContent());
else if (this.funcName == 'delete')
httpResponse = this._handleDeleteRequest(httpRequest);
}
return httpResponse;
},
_handleHeaders: function() {
//OVERRIDING PARENT METHOD
var headers = [];
for(var i in this.fnHeaderGrList){
var hgr = this.fnHeaderGrList[i];
//headers['' + hgr.name] = '' + hgr.value;
var header = {};
header.name = ""+hgr.name;
header.value = ""+hgr.value;
headers.push(header);
}
return headers;
},
_handleParameters: function() {
//OVERRIDING PARENT METHOD
var params = [];
for(var i in this.fnParamDefGrList){
var pgr = this.fnParamDefGrList[i];
var value = '' + pgr.value;
var param = {};
param.name = '' + pgr.name;
param.value = '' + GlideStringUtil.urlEncode(value);
params.push(param);
}
return params;
},
_handleContent: function() {
//OVERRIDING PARENT FUNCTION
var content = '' + this.functionGr.content;
return content;
},
//Headers
addHeader: function (name, value){
var h = new GlideRecord("sys_rest_message_fn_headers");
h.initialize();
h.name = name;
h.value = value;
this.fnHeaderGrList.push(h);
},
//Parameters
addRequestParameter: function (name, value){
var p = new GlideRecord("sys_rest_message_fn_param_defs");
p.initialize();
p.name = name;
p.value = value;
this.fnParamDefGrList.push(p);
},
//Content
setContent: function (content){
this.functionGr.content = content;
},
type: 'RESTMessageScripted'
});