-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGlobal-REST Message Scriptable Client.xml
226 lines (195 loc) · 8.59 KB
/
Global-REST Message Scriptable Client.xml
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
<?xml version="1.0" encoding="UTF-8"?><unload unload_date="2018-11-04 02:45:13">
<sys_remote_update_set action="INSERT_OR_UPDATE">
<application display_value="Global">global</application>
<application_name>Global</application_name>
<application_scope>global</application_scope>
<application_version/>
<collisions/>
<commit_date/>
<deleted/>
<description>A purely scriptable REST Message client</description>
<inserted/>
<name>REST Message Scriptable Client</name>
<origin_sys_id/>
<parent display_value=""/>
<release_date/>
<remote_base_update_set display_value=""/>
<remote_parent_id/>
<remote_sys_id>92b089fadb112300236c2a9a48961993</remote_sys_id>
<state>loaded</state>
<summary/>
<sys_class_name>sys_remote_update_set</sys_class_name>
<sys_created_by>admin</sys_created_by>
<sys_created_on>2018-11-04 02:45:13</sys_created_on>
<sys_id>8c2fed3ddb612300236c2a9a48961913</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_updated_by>admin</sys_updated_by>
<sys_updated_on>2018-11-04 02:45:13</sys_updated_on>
<update_set display_value=""/>
<update_source display_value=""/>
<updated/>
</sys_remote_update_set>
<sys_update_xml action="INSERT_OR_UPDATE">
<action>INSERT_OR_UPDATE</action>
<application display_value="Global">global</application>
<category>customer</category>
<comments/>
<name>sys_script_include_f02ba3fde1e21100c3294e4a8036179a</name>
<payload><?xml version="1.0" encoding="UTF-8"?><record_update table="sys_script_include"><sys_script_include action="INSERT_OR_UPDATE"><active>true</active><client_callable>false</client_callable><description>A scriptable REST Message library. With this library you can create and consume REST web services without building out the RESTMessage records in the platform.</description><name>RESTMessageScripted</name><script><![CDATA[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'
});]]></script><sys_created_by>admin</sys_created_by><sys_created_on>2014-01-29 15:15:36</sys_created_on><sys_id>f02ba3fde1e21100c3294e4a8036179a</sys_id><sys_mod_count>17</sys_mod_count><sys_updated_by>admin</sys_updated_by><sys_updated_on>2014-01-29 21:36:18</sys_updated_on></sys_script_include><sys_app_file action="INSERT_OR_UPDATE"><customer_update>false</customer_update><publish_override/><replace_on_upgrade>false</replace_on_upgrade><restore/><sys_app/><sys_code>!!1WY/</sys_code><sys_created_by>admin</sys_created_by><sys_created_on>2014-01-29 15:15:36</sys_created_on><sys_id>cf8ce7b5e1e21100c3294e4a80361722</sys_id><sys_mod_count>0</sys_mod_count><sys_name>RESTMessageScripted</sys_name><sys_parent/><sys_path>!!1WY/</sys_path><sys_policy/><sys_source_deleted>false</sys_source_deleted><sys_source_id>f02ba3fde1e21100c3294e4a8036179a</sys_source_id><sys_source_table>sys_script_include</sys_source_table><sys_type>code</sys_type><sys_update_name>sys_script_include_f02ba3fde1e21100c3294e4a8036179a</sys_update_name><sys_updated_by>admin</sys_updated_by><sys_updated_on>2014-01-29 21:36:18</sys_updated_on></sys_app_file></record_update></payload>
<payload_hash/>
<remote_update_set display_value="REST Message Scriptable Client">8c2fed3ddb612300236c2a9a48961913</remote_update_set>
<replace_on_upgrade>false</replace_on_upgrade>
<sys_created_by>admin</sys_created_by>
<sys_created_on>2018-11-04 02:45:13</sys_created_on>
<sys_id>402fed3ddb612300236c2a9a48961914</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_recorded_at>166abe28a340000001</sys_recorded_at>
<sys_updated_by>admin</sys_updated_by>
<sys_updated_on>2018-11-04 02:45:13</sys_updated_on>
<table/>
<target_name>RESTMessageScripted</target_name>
<type>Script Include</type>
<update_domain>global</update_domain>
<update_guid/>
<update_guid_history/>
<update_set display_value=""/>
<view/>
</sys_update_xml>
</unload>