-
Notifications
You must be signed in to change notification settings - Fork 2
/
soap.rex
513 lines (475 loc) · 19.9 KB
/
soap.rex
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*REXX 2.0.0
Copyright (c) 2009-2020, Andrew J. Armstrong
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* REXX ***************************************************************
** **
** NAME - SOAP **
** **
** FUNCTION - This exec invokes a Web Service and displays the **
** response. It assumes you are running the Apache Axis **
** SOAP server on the specified host and port. Other SOAP **
** servers should also work but have not been tested. **
** **
** Each time you invoke the client, you specify the SOAP **
** server hostname and port, and the name of the service **
** that you want to use. The client will then retrieve **
** the Web Services Definition Language (WSDL) definitions**
** for that service from the SOAP server. If there is **
** only one operation defined for that service then that **
** operation is invoked automatically, else you will be **
** prompted to select the operation to be invoked. If the **
** operation requires one or more parameters then you **
** will be prompted for each parameter. No attempt is **
** made to validate the input you supply. The operation **
** is invoked and the response is displayed. **
** **
** NOTES - 1. You will have to either append the PARSEXML and **
** PRETTY source files manually to this file, or run **
** this file through the REXX rexx pre-processor. **
** **
** To use the pre-processor, run: **
** **
** tso rexxpp your.rexx.lib(soap) **
** **
** 2. You can download Apache Axis from: **
** http://ws.apache.org/axis/ **
** **
** 3. Start the Apache Axis server by issuing the **
** following command (all on one line with no line **
** breaks): **
** **
** java org.apache.axis.transport.http.SimpleAxisServer**
** -p 8080 **
** **
** ...you will need to have the Axis jar files in your **
** classpath, of course. **
** **
** 4. If you need to go through a proxy server, then **
** specify the proxy server hostname and port number **
** in the z.!PROXYHOST and z.!PROXYPORT variables **
** (see below). **
** **
** 5. If you have access to the Internet, you can try the **
** SOAP client out by running: **
** **
** soap vizier.cfa.harvard.edu:8080 UCD **
** **
** **
** **
** **
** SYNTAX - SOAP url [service] [(options...] **
** **
** Where, **
** url = The URL of the Axis SOAP server. Format is:**
** hostname:port/path **
** hostname - Axis SOAP server hostname **
** port - default is 80 **
** path - default is axis/services **
** service = Service name (default is Version). This **
** name is case-sensitive. **
** options = One or more options: **
** TRACE - Trace network I/O **
** DEBUG - Dump message contents in hex **
** XML - Display messages contents in XML **
** NOPROXY - Do not use proxy host. **
** **
** AUTHOR - Andrew J. Armstrong <[email protected]> **
** **
** HISTORY - Date By Reason (most recent at the top please) **
** -------- --- ----------------------------------------- **
** 20090822 AJA Changed from GPL to BSD license. **
** 20050602 AJA Retrieve WSDL from SOAP server and prompt **
** user for the operation and parameters. **
** 20050601 AJA Added proxy host support. **
** 20050531 AJA Original version. **
** **
**********************************************************************/
parse arg sURL sService . '('sOptions')'
sOptions = translate(sOptions) /* convert to upper case */
parse value sourceline(1) with . sVersion
say 'SOAP000I Rexx SOAP client' sVersion
z. = '' /* g. is used by the XML parser */
z.!PROXYHOST = 'proxy.example.org'
z.!PROXYPORT = '8080'
if pos('://',sURL) > 0
then parse var sURL sScheme'://'sURL /* ignore protocol (scheme) */
parse var sURL sHost'/'sPath
parse var sHost sHost':'nPort
if sHost = '' then sHost = 'axisserver.example.org'
if \datatype(nPort,'WHOLE') then nPort = 80
if sPath = '' then sPath = 'axis/services'
if sService = '' then sService = 'Version'
say 'SOAP001I Host='sHost
say 'SOAP002I Port='nPort
say 'SOAP003I Path='sPath
say 'SOAP004I Service='sService
call initParser 'NOBLANKS' /* <-- This is in PARSEXML rexx */
z.!DEBUG = wordpos('DEBUG',sOptions) <> 0
z.!TRACE = wordpos('TRACE',sOptions) <> 0
z.!XML = wordpos('XML',sOptions) <> 0
z.!NOPROXY = wordpos('NOPROXY',sOptions) <> 0
if z.!NOPROXY
then do
z.!PROXYHOST = ''
z.!PROXYPORT = ''
end
say 'SOAP005I Proxy Host='z.!PROXYHOST
say 'SOAP006I Proxy Port='z.!PROXYPORT
call Prolog
/*
*--------------------------------------------------------------------*
* Contact the SOAP server and retrieve the WSDL for this service
*--------------------------------------------------------------------*
*/
nSocket = Connect(sHost,nPort)
say 'SOAP007I Retrieving WSDL for' sService 'service'
if z.!PROXYHOST <> ''
then sURL = 'http://'sHost':'nPort'/'sPath'/'sService'?wsdl'
else sURL = '/'sPath'/'sService'?wsdl'
say 'SOAP008I GET' sURL
sHeader = 'GET' sURL 'HTTP/1.1' || z.!CRLFCRLF
sMsg = sHeader
if z.!DEBUG then call Dump sMsg,'Sending'
sWrite = write(nSocket,sMsg)
/*
*--------------------------------------------------------------------*
* Read the response from the SOAP server
*--------------------------------------------------------------------*
*/
say 'SOAP009I Reading WSDL response'
sReply = Slurp(nSocket)
sDisc = Disconnect(nSocket)
parse var sReply sHeader (z.!CRLFCRLF) sXML
if z.!DEBUG then call Dump sHeader,'Header'
if z.!DEBUG then call Dump sXML,'Payload'
parse var sHeader . nCode . 0 . sReason (z.!CRLF)
if nCode <> 200
then call Abort 'SOAP021E HTTP GET failed:' sReason
/*
*--------------------------------------------------------------------*
* Parse the WSDL
*--------------------------------------------------------------------*
*/
rc = parseString(sXML)
doc = getDocumentElement()
if getName(doc) <> 'wsdl:definitions'
then do
say 'SOAP010E Could not retrieve WSDL for' sService,
'service on' sHost
say 'SOAP011I The reply received was:'
call showResponse doc
call Abort
end
porttype = getChildrenByName(doc,'wsdl:portType')
operations = getChildrenByName(porttype,'wsdl:operation')
messages = getChildrenByName(doc,'wsdl:message')
do i = 1 to words(messages)
message = word(messages,i)
sMsgName = getAttribute(message,'name')
z.!MSG.sMsgName = message
z.!MSG.message = sMsgName
end
z.!OP.0 = words(operations) /* number of operations */
if z.!OP.0 = 0
then call Abort 'Service' sService 'supports no operations'
do i = 1 to z.!OP.0 /* for each operation of this service...*/
operation = word(operations,i)
input = getChildrenByName(operation,'wsdl:input')
output = getChildrenByName(operation,'wsdl:output')
sInputMsgName = getAttribute(input,'name')
sOutputMsgName = getAttribute(output,'name')
z.!OP.i = operation /* operation node */
z.!IN.i = z.!MSG.sInputMsgName /* input parms node */
z.!OUT.i = z.!MSG.sOutputMsgName /* output parms node */
end
if z.!OP.0 > 1 /* More than one operation to choose from? */
then do
say 'SOAP012A Which' sService 'operation do you want to invoke?'
do i = 1 to z.!OP.0
say i'.' getAttribute(z.!OP.i,'name')
end
pull n
if n = '' then call Abort 'Operation cancelled'
if \datatype(n,'WHOLE') | n < 1 | n > z.!OP.0 then n = 1
end
else n = 1
sOperation = getAttribute(z.!OP.n,'name')
sExpectedResponse = getAttribute(z.!OUT.n,'name')
say 'SOAP013I About to invoke operation:' sOperation
parameters = getChildrenByName(z.!IN.n,'wsdl:part')
z.!PARM.0 = words(parameters) /* number of defined parameters */
do i = 1 to z.!PARM.0
parameter = word(parameters,i)
sParmName = getAttribute(parameter,'name')
sParmType = getAttribute(parameter,'type')
say 'SOAP014A Enter value for' sParmName '(type is' sParmType'):'
parse pull reply
z.!PARM.i = reply
z.!PARMNAME.i = sParmName
end
/*
*--------------------------------------------------------------------*
* Create a SOAP request message roughly like:
* <soapenv:Envelope>
* <soapenv:Body>
* <getVersion/>
* </soapenv:Body>
* </soapenv:Envelope>
*--------------------------------------------------------------------*
*/
call initParser 'NOBLANKS'
doc = createDocument('soapenv:Envelope')
call setAttribute doc,'xmlns:soapenv',,
'http://schemas.xmlsoap.org/soap/envelope/'
call setAttribute doc,'xmlns:xsd',,
'http://www.w3.org/2001/XMLSchema'
call setAttribute doc,'xmlns:xsi',,
'http://www.w3.org/2001/XMLSchema-instance'
body = createElement('soapenv:Body')
call appendChild body,doc
op = createElement(sOperation)
call appendChild op,body
do i = 1 to z.!PARM.0
sParmName = z.!PARMNAME.i
sParmValue = z.!PARM.i
parm = createElement(sParmName)
call appendChild createTextNode(sParmValue),parm
call appendChild parm,op
end
nSocket = Connect(sHost,nPort)
sContent = toString(getRoot())
say 'SOAP015I Sending' sOperation 'message to' sService 'service'
if z.!XML then say sContent
/*
*--------------------------------------------------------------------*
* Build an HTTP header and send the message
*--------------------------------------------------------------------*
*/
if z.!PROXYHOST <> ''
then sURL = 'http://'sHost':'nPort'/'sPath'/'sService
else sURL = '/'sPath'/'sService
say 'SOAP020I POST' sURL
sHeader = 'POST' sURL 'HTTP/1.0' || z.!CRLF ||,
'Content-Type: text/xml; charset=utf-8' || z.!CRLF ||,
'Accept: application/soap+xml, application/dime,',
'multipart/related, text/*' || z.!CRLF ||,
'User-Agent: EpistAxis/1.2' || z.!CRLF ||,
'Host:' sHost':'nPort || z.!CRLF ||,
'Cache-Control: no-cache' || z.!CRLF ||,
'Pragma: no-cache' || z.!CRLF ||,
'SOAPAction: ""' || z.!CRLF ||,
'Content-Length:' length(sContent) || z.!CRLF || z.!CRLF
sMsg = sHeader || sContent
if z.!DEBUG then call Dump sMsg,'Sending'
sWrite = write(nSocket,sMsg)
/*
*--------------------------------------------------------------------*
* Read the response from the SOAP server
*--------------------------------------------------------------------*
*/
sReply = Slurp(nSocket)
parse var sReply sHeader (z.!CRLFCRLF) sXML
if z.!DEBUG then call Dump sHeader,'Header'
if z.!DEBUG then call Dump sXML,'Payload'
parse var sHeader . nCode . 0 . sReason (z.!CRLF)
if nCode <> 200
then call Abort 'SOAP021E HTTP POST failed:' sReason
/*
*--------------------------------------------------------------------*
* Now parse the response message.
* If it worked, the response will look like:
*
* <soapenv:Envelope>
* <soapenv:Body>
* <getVersionResponse>
* <getVersionReturn>
* version information
* </getVersionReturn>
* </getVersionResponse>
* </soapenv:Body>
* </soapenv:Envelope>
*
* If it failed, the response will look like:
*
* <soapenv:Envelope>
* <soapenv:Body>
* <soapenv:Fault>
* <faultcode>code</faultcode>
* <faultstring>string</faultstring>
* <detail>
* <stackTrace>stack trace information</stackTrace>
* <hostname>hostname of SOAP server</hostname>
* </detail>
* </soapenv:Fault>
* </soapenv:Body>
* </soapenv:Envelope>
*--------------------------------------------------------------------*
*/
call initParser 'NOBLANKS'
rc = parseString(sXML)
if z.!XML then call prettyPrinter
/*
*--------------------------------------------------------------------*
* Extract the payload from the response
*--------------------------------------------------------------------*
*/
doc = getDocumentElement() /* SOAP envelope */
body = getFirstChild(doc) /* SOAP body */
resp = getFirstChild(body) /* SOAP server response */
select
when getName(resp) = sExpectedResponse then do
say 'SOAP016I Received "'sExpectedResponse'" response:'
call showResponse resp
end
when getName(resp) = 'soapenv:Fault' then do
say 'SOAP017E Received "'getName(resp)'" response:'
call showResponse resp
end
otherwise do
say 'SOAP018W Received unexpected response:'
say toString(resp)
end
end
call Epilog
exit
showResponse: procedure expose z. g.
parse arg node,sPad
if isElementNode(node)
then do
if hasAttribute(node,'xsi:type')
then do
say sPad || getName(node),
getAttribute(node,'xsi:type') ||,
'="'getText(getFirstChild(node))'"'
end
else do
say sPad || getName(node)
if isTextNode(getFirstChild(node))
then say sPad' 'getText(getFirstChild(node))
end
end
children = getChildren(node)
do i = 1 to words(children)
child = word(children,i)
call showResponse child,sPad' '
end
return
showParameters: procedure expose z. g.
parse arg sMsgName
message = z.!MSG.sMsgName
parts = getChildrenByName(message,'wsdl:part')
do k = 1 to words(parts)
part = word(parts,k)
say ' parameter:' getAttribute(part,'name'),
'type:' getAttribute(part,'type')
end
return
Connect: procedure expose z.
parse arg sHost,nPort
say 'SOAP019I Connecting to' sHost 'port' nPort
if z.!PROXYHOST <> '' then sHost = z.!PROXYHOST
if z.!PROXYPORT <> '' then nPort = z.!PROXYPORT
sShutdown = Socket('Shutdown', z.!SOCKET)
sClose = Socket('Close', z.!SOCKET)
nSocket = Socket('Socket','AF_INET','SOCK_STREAM','TCP')
sSockOpt = Socket('SetSockOpt',nSocket,'IPPROTO_TCP','SO_ASCII','ON')
sConnect = Socket('Connect',nSocket,'AF_INET' nPort sHost)
if z.!RC <> 0 then call ABORT sConnect
return nSocket
Slurp: procedure expose z.
parse arg nSocket
sReply = ''
sRead = read(nSocket)
do while length(sRead) > 0
sReply = sReply || sRead
sRead = read(nSocket)
end
return sReply
Disconnect: procedure expose z.
parse arg nSocket
sShutdown= Socket('Shutdown', nSocket)
sResp = Socket('Close', nSocket)
return sResp
Abort: procedure expose z.
parse arg sMsg
say sMsg
call Epilog
exit
return
Write: procedure expose z.
parse arg nSocket,sMsg
if z.!TRACE then call Dump sMsg,'-->'
sWrite = Socket('Write',nSocket,sMsg)
return sWrite
Read: procedure expose z.
parse arg nSocket
sRead = Socket('Read',nSocket)
parse var sRead nLen sData
if z.!TRACE then call Dump sData,'<--'
return sData
Dump: procedure expose z.
parse arg sData,sPrefix,nPad
if \datatype(nPad,'WHOLE') then nPad = 8
sPrefix = left(sPrefix,nPad)
lastline = length(sData)%16*16+1
do i = 1 to length(sData) by 16
if i = lastline
then sChunk = substr(sData,i)
else sChunk = substr(sData,i,16)
xChunk = left(c2x(sChunk),32)
say sPrefix,
substr(xChunk, 1,8),
substr(xChunk, 9,8),
substr(xChunk,17,8),
substr(xChunk,25,8),
'*'left(sChunk,16)'*'
sPrefix = right('+'d2x(i+15,6),nPad)
end
return
Prolog:
z.!CR = '0D'x /* EBCDIC Carriage Return */
z.!LF = '25'x /* EBCDIC Line Feed */
z.!CRLF = '0D25'x
z.!CRLFCRLF = z.!CRLF || z.!CRLF
sResp = Socket('Terminate') /* Kill any previous socket set */
sResp = Socket('Initialize', 'MySet')
if z.!RC <> 0 then exit z.!RC
return
Epilog:
sDisc = Disconnect(nSocket)
sResp = Socket('Terminate')
return sResp
Socket: procedure expose z.
parse arg a,c,d,e,f,g,h,i,j,k
if z.!TRACE
then say 'Socket('strip(a','c','d','e','f','g','h','i','j','k,'T',",")')'
parse value 'SOCKET'(a,c,d,e,f,g,h,i,j,k) with nRC sResp
z.!RC = nRC
if z.!TRACE
then do
say ' Return code <'nRC'>'
say ' Response <'sResp'>'
end
return sResp
/*INCLUDE pretty.rex */