-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTEMCO_SSP_loftwarewsdlimplementation.js
260 lines (202 loc) · 10 KB
/
STEMCO_SSP_loftwarewsdlimplementation.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
/**
* STEMCO_SSP_loftwarewsdlimplementation.js
* @Developer Ivan M. Posadas Herrera
* @Email ivanmposadas@gmail.com
* @NApiVersion 2.0
* @Version alpha
*/
define(['N/http','N/xml'],
function(http,xml) {
var WSDL_ENDPOINT = 'http://www.domain.com/services/LoftwarePrintWS.LoftwarePrintWSHttpSoap11Endpoint/';
var USER="user";
var PASSWORD="password";
var DOMAIN="any";
var TOKEN = "";
function getDataByTag(srt,tagIn,tagOut,headers){
var pointer = 0;
var st = 0;
var np = 0;
var tagSub = "";
var results =[];
var token = [];
var itemsWithTokens = [];
while(pointer < srt.length){
st = srt.indexOf(tagIn,pointer) + tagIn.length ;
np = srt.indexOf(tagOut,pointer);
tagSub = srt.substring(st,np).trim();
results = tagSub.split(",");
token = {};
for(var i=0; i<results.length;i++){
token[headers[i]] = results[i];
}
itemsWithTokens.push(token);
pointer = np + tagOut.length;
}
return itemsWithTokens;
}
function makeToken()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 32; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function postRequestWS(request) {
var response = http.request({
method: http.Method.POST,
body: request,
url: WSDL_ENDPOINT
});
return response;
}
function endUserLogin() {
TOKEN = makeToken();
var authRequest = "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.loftware.com\">\n" +
" <x:Header/>\n" +
" <x:Body>\n" +
" <ser:endUserLogin>\n" +
" <ser:userName>"+USER+"</ser:userName>\n" +
" <ser:password>"+PASSWORD+"</ser:password>\n" +
" <ser:domain>"+DOMAIN+"</ser:domain>\n" +
" <ser:lwaUsername>"+DOMAIN+"/"+USER+"</ser:lwaUsername>\n" +
" <ser:lwaSessionID>"+TOKEN+"</ser:lwaSessionID>\n" +
" <ser:lwaUIServerIP>10.13.31.01</ser:lwaUIServerIP>\n" +
" </ser:endUserLogin>\n" +
" </x:Body>\n" +
"</x:Envelope>";
var securityString = postRequestWS(authRequest).body;
securityString = securityString.substring(204,securityString.lastIndexOf("Assertion>")+10);
return securityString;
}
function endUserLogout(securityString){
var desAuthRequest = "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.loftware.com\">"+
" <x:Header/>\n"+
" <x:Body>\n"+
" <ser:endUserLogout>\n"+
" <ser:securityObject>"+securityString+"</ser:securityObject>\n"+
" <ser:lwaUsername>None/iposadas</ser:lwaUsername>\n"+
" <ser:lwaSessionID>"+TOKEN+"</ser:lwaSessionID>\n"+
" <ser:lwaUIServerIP>10.13.31.01</ser:lwaUIServerIP>\n"+
" </ser:endUserLogout>\n"+
" </x:Body>\n"+
"</x:Envelope>\n";
return postRequestWS(desAuthRequest);
}
function sendJobNoWaitWS(printerNum,printerName,labelName,quantity,duplicates,parameters){
labelName = labelName.replace(/\\/g,'\\\\');
var securityString = endUserLogin();
var jobData = '<?xml version="1.0" encoding="utf-8" ?>'+
'<!DOCTYPE labels SYSTEM "C:\\Program Files\\Loftware Labeling\\Batch\\label.dtd">'+
'<labels _FORMAT="'+labelName+'" _QUANTITY="'+quantity+'" _DUPLICATES="'+duplicates+'">'+
'<label>';
for(var parameter in parameters){
jobData += '<variable name="'+parameter+'">'+parameters[parameter]+'</variable>';
}
jobData +='</label>'+
'</labels>';
jobData = xml.escape({ xmlText : jobData});
var sendPrintWS = "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.loftware.com\">\n" +
" <x:Header/>\n" +
" <x:Body>\n" +
" <ser:sendJobNoWaitWS>\n" +
" <ser:securityObject>" + securityString + "</ser:securityObject>\n" +
" <ser:printerNum>"+printerNum+"</ser:printerNum>\n" +
" <ser:printerName>"+printerName+"</ser:printerName>\n" +
" <ser:labelName>C:\\Program Files (x86)\\Loftware Labeling\\labels\\"+labelName+"</ser:labelName>\n" +
" <ser:jobData>" + jobData + "</ser:jobData>\n" +
" </ser:sendJobNoWaitWS>\n" +
" </x:Body>\n" +
"</x:Envelope>";
var response = postRequestWS(sendPrintWS);
endUserLogout(securityString);
return response;
}
function sendJobWaitWS(printerNum,printerName,labelName,quantity,duplicates,parameters){
labelName = labelName.replace(/\\/g,'\\\\');
var securityString = endUserLogin();
var jobData = '<?xml version="1.0" encoding="utf-8" ?>'+
'<!DOCTYPE labels SYSTEM "C:\\Program Files\\Loftware Labeling\\Batch\\label.dtd">'+
'<labels _FORMAT="'+labelName+'" _QUANTITY="'+quantity+'" _DUPLICATES="'+duplicates+'">'+
'<label>';
for(var parameter in parameters){
jobData += '<variable name="'+parameter+'">'+parameters[parameter]+'</variable>';
}
jobData +='</label>'+
'</labels>';
jobData = xml.escape({ xmlText : jobData});
var sendPrintWS = "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.loftware.com\">\n" +
" <x:Header/>\n" +
" <x:Body>\n" +
" <ser:sendJobWaitWS>\n" +
" <ser:securityObject>" + securityString + "</ser:securityObject>\n" +
" <ser:printerNum>"+printerNum+"</ser:printerNum>\n" +
" <ser:printerName>"+printerName+"</ser:printerName>\n" +
" <ser:labelName>C:\\Program Files (x86)\\Loftware Labeling\\labels\\"+labelName+"</ser:labelName>\n" +
" <ser:jobData>" + jobData + "</ser:jobData>\n" +
" </ser:sendJobWaitWS>\n" +
" </x:Body>\n" +
"</x:Envelope>";
var response = postRequestWS(sendPrintWS);
endUserLogout(securityString);
return response;
}
function getDeviceList(sortXML,filterXML){
var securityString = endUserLogin();
var sendPrintWS = "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.loftware.com\">\n" +
" <x:Header/>\n" +
" <x:Body>\n" +
" <ser:getDeviceList>\n" +
" <ser:securityObject>" + securityString + "</ser:securityObject>\n" +
" <ser:sortXML>" + sortXML + "</ser:sortXML>\n" +
" <ser:filterXML>" + filterXML +"</ser:filterXML>\n" +
" </ser:getDeviceList>\n" +
" </x:Body>\n" +
"</x:Envelope>";
var response = postRequestWS(sendPrintWS);
endUserLogout(securityString);
var str = response.body;
var startPosGral = str.indexOf("<ns:get") + '<ns:getDeviceListResponse xmlns:ns="http://services.loftware.com">'.length;
var endPos = str.indexOf("</ns:get");
var resultBody = str.substring(startPosGral,endPos).trim();
var headers = [
"number",
"model",
"ip",
"driver",
"name"
];
return getDataByTag(resultBody,'<ns:return>','</ns:return>',headers);
}
function getLabelList(sortXML,filterXML){
var securityString = endUserLogin();
var sendPrintWS = "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.loftware.com\">\n" +
" <x:Header/>\n" +
" <x:Body>\n" +
" <ser:getLabelList>\n" +
" <ser:securityObject>" + securityString + "</ser:securityObject>\n" +
" <ser:sortXML>" + sortXML + "</ser:sortXML>\n" +
" <ser:filterXML>" + filterXML +"</ser:filterXML>\n" +
" </ser:getLabelList>\n" +
" </x:Body>\n" +
"</x:Envelope>";
var response = postRequestWS(sendPrintWS);
endUserLogout(securityString);
var str = response.body;
var startPosGral = str.indexOf("<ns:get") + '<ns:getLabelListResponse xmlns:ns="http://services.loftware.com">'.length;
var endPos = str.indexOf("</ns:get");
var resultBody = str.substring(startPosGral,endPos).trim();
var headers = [
"name",
"driver",
"template"
];
return getDataByTag(resultBody,'<ns:return>','</ns:return>',headers);
}
return {
sendJobNoWaitWS:sendJobNoWaitWS,
sendJobWaitWS:sendJobWaitWS,
getDeviceList:getDeviceList,
getLabelList:getLabelList
};
});