summaryrefslogtreecommitdiff
path: root/lisp/net/soap-inspect.el
blob: 050be453db35179ef68b1dc3289a00f5a1a91091 (plain)
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
;;; soap-inspect.el --- Interactive WSDL inspector    -*- lexical-binding: t -*-

;; Copyright (C) 2010-2018 Free Software Foundation, Inc.

;; Author: Alexandru Harsanyi <AlexHarsanyi@gmail.com>
;; Created: October 2010
;; Keywords: soap, web-services, comm, hypermedia
;; Package: soap-client
;; Homepage: https://github.com/alex-hhh/emacs-soap-client

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; This package provides an inspector for a WSDL document loaded with
;; `soap-load-wsdl' or `soap-load-wsdl-from-url'.  To use it, evaluate:
;;
;; (soap-inspect *wsdl*)
;;
;; This will pop-up the inspector buffer.  You can click on ports, operations
;; and types to explore the structure of the wsdl document.
;;


;;; Code:

(require 'cl-lib)
(require 'soap-client)

;;; sample-value

(defun soap-sample-value (type)
  "Provide a sample value for TYPE, a WSDL type.
A sample value is a LISP value which soap-client.el will accept
for encoding it using TYPE when making SOAP requests.

This is a generic function, depending on TYPE a specific function
will be called."
  (let ((sample-value (get (aref type 0) 'soap-sample-value)))
    (if sample-value
        (funcall sample-value type)
      (error "Cannot provide sample value for type %s" (aref type 0)))))

(defun soap-sample-value-for-xs-basic-type (type)
  "Provide a sample value for TYPE, an xs-basic-type.
This is a specialization of `soap-sample-value' for xs-basic-type
objects."
  (cl-case (soap-xs-basic-type-kind type)
    (string "a string")
    (anyURI "an URI")
    (QName "a QName")
    (dateTime "a time-value-p or string")
    (boolean "t or nil")
    ((long int integer byte unsignedInt) 42)
    ((float double) 3.14)
    (base64Binary "a string")
    (t (format "%s" (soap-xs-basic-type-kind type)))))

(defun soap-sample-value-for-xs-element (element)
  "Provide a sample value for ELEMENT, a WSDL element.
This is a specialization of `soap-sample-value' for xs-element
objects."
  (if (soap-xs-element-name element)
      (cons (intern (soap-xs-element-name element))
            (soap-sample-value (soap-xs-element-type element)))
    (soap-sample-value (soap-xs-element-type element))))

(defun soap-sample-value-for-xs-attribute (attribute)
  "Provide a sample value for ATTRIBUTE, a WSDL attribute.
This is a specialization of `soap-sample-value' for
soap-xs-attribute objects."
  (if (soap-xs-attribute-name attribute)
      (cons (intern (soap-xs-attribute-name attribute))
            (soap-sample-value (soap-xs-attribute-type attribute)))
    (soap-sample-value (soap-xs-attribute-type attribute))))

(defun soap-sample-value-for-xs-attribute-group (attribute-group)
  "Provide a sample value for ATTRIBUTE-GROUP, a WSDL attribute group.
This is a specialization of `soap-sample-value' for
soap-xs-attribute objects."
  (let ((sample-values nil))
    (dolist (attribute (soap-xs-attribute-group-attributes attribute-group))
      (if (soap-xs-attribute-name attribute)
          (setq sample-values
                (append sample-values
                        (cons (intern (soap-xs-attribute-name attribute))
                              (soap-sample-value (soap-xs-attribute-type
                                                  attribute)))))
        (setq sample-values
              (append sample-values
                      (soap-sample-value
                       (soap-xs-attribute-type attribute))))))))

(defun soap-sample-value-for-xs-simple-type (type)
  "Provide a sample value for TYPE, a `soap-xs-simple-type'.
This is a specialization of `soap-sample-value' for
`soap-xs-simple-type' objects."
  (append
   (mapcar 'soap-sample-value-for-xs-attribute
           (soap-xs-type-attributes type))
   (cond
    ((soap-xs-simple-type-enumeration type)
     (let ((enumeration (soap-xs-simple-type-enumeration type)))
       (nth (random (length enumeration)) enumeration)))
    ((soap-xs-simple-type-pattern type)
     (format "a string matching %s" (soap-xs-simple-type-pattern type)))
    ((soap-xs-simple-type-length-range type)
     (cl-destructuring-bind (low . high) (soap-xs-simple-type-length-range type)
       (cond
        ((and low high)
         (format "a string between %d and %d chars long" low high))
        (low (format "a string at least %d chars long" low))
        (high (format "a string at most %d chars long" high))
        (t (format "a string OOPS")))))
    ((soap-xs-simple-type-integer-range type)
     (cl-destructuring-bind (min . max) (soap-xs-simple-type-integer-range type)
       (cond
        ((and min max) (+ min (random (- max min))))
        (min (+ min (random 10)))
        (max (random max))
        (t (random 100)))))
    ((consp (soap-xs-simple-type-base type)) ; an union of values
     (let ((base (soap-xs-simple-type-base type)))
       (soap-sample-value (nth (random (length base)) base))))
    ((soap-xs-basic-type-p (soap-xs-simple-type-base type))
     (soap-sample-value (soap-xs-simple-type-base type))))))

(defun soap-sample-value-for-xs-complex-type (type)
  "Provide a sample value for TYPE, a `soap-xs-complex-type'.
This is a specialization of `soap-sample-value' for
`soap-xs-complex-type' objects."
  (append
   (mapcar 'soap-sample-value-for-xs-attribute
           (soap-xs-type-attributes type))
   (cl-case (soap-xs-complex-type-indicator type)
     (array
      (let* ((element-type (soap-xs-complex-type-base type))
             (sample1 (soap-sample-value element-type))
             (sample2 (soap-sample-value element-type)))
        ;; Our sample value is a vector of two elements, but any number of
        ;; elements are permissible
        (vector sample1 sample2 '&etc)))
     ((sequence choice all)
      (let ((base (soap-xs-complex-type-base type)))
        (let ((value (append (and base (soap-sample-value base))
                             (mapcar #'soap-sample-value
                                     (soap-xs-complex-type-elements type)))))
          (if (eq (soap-xs-complex-type-indicator type) 'choice)
              (cons '***choice-of*** value)
            value)))))))

(defun soap-sample-value-for-message (message)
  "Provide a sample value for a WSDL MESSAGE.
This is a specialization of `soap-sample-value' for
`soap-message' objects."
  ;; NOTE: parameter order is not considered.
  (let (sample-value)
    (dolist (part (soap-message-parts message))
      (push (soap-sample-value (cdr part)) sample-value))
    (nreverse sample-value)))

(progn
  ;; Install soap-sample-value methods for our types
  (put (aref (make-soap-xs-basic-type) 0)
       'soap-sample-value
       'soap-sample-value-for-xs-basic-type)

  (put (aref (make-soap-xs-element) 0)
       'soap-sample-value
       'soap-sample-value-for-xs-element)

  (put (aref (make-soap-xs-attribute) 0)
       'soap-sample-value
       'soap-sample-value-for-xs-attribute)

  (put (aref (make-soap-xs-attribute) 0)
       'soap-sample-value
       'soap-sample-value-for-xs-attribute-group)

  (put (aref (make-soap-xs-simple-type) 0)
       'soap-sample-value
       'soap-sample-value-for-xs-simple-type)

  (put (aref (make-soap-xs-complex-type) 0)
       'soap-sample-value
       'soap-sample-value-for-xs-complex-type)

  (put (aref (make-soap-message) 0)
       'soap-sample-value
       'soap-sample-value-for-message))



;;; soap-inspect

(defvar soap-inspect-previous-items nil
  "A stack of previously inspected items in the *soap-inspect* buffer.
Used to implement the BACK button.")

(defvar soap-inspect-current-item nil
  "The current item being inspected in the *soap-inspect* buffer.")

(progn
  (make-variable-buffer-local 'soap-inspect-previous-items)
  (make-variable-buffer-local 'soap-inspect-current-item))

(defun soap-inspect (element)
  "Inspect a SOAP ELEMENT in the *soap-inspect* buffer.
The buffer is populated with information about ELEMENT with links
to its sub elements.  If ELEMENT is the WSDL document itself, the
entire WSDL can be inspected."
  (let ((inspect (get (aref element 0) 'soap-inspect)))
    (unless inspect
      (error "Soap-inspect: no inspector for element"))

    (with-current-buffer (get-buffer-create "*soap-inspect*")
      (setq buffer-read-only t)
      (let ((inhibit-read-only t))
        (erase-buffer)

        (when soap-inspect-current-item
          (push soap-inspect-current-item
                soap-inspect-previous-items))
        (setq soap-inspect-current-item element)

        (funcall inspect element)

        (unless (null soap-inspect-previous-items)
          (insert "\n\n")
          (insert-text-button
           "[back]"
           'type 'soap-client-describe-back-link
           'item element)
          (insert "\n"))
        (goto-char (point-min))
        (pop-to-buffer (current-buffer))))))


(define-button-type 'soap-client-describe-link
  'face 'link
  'help-echo "mouse-2, RET: describe item"
  'follow-link t
  'action (lambda (button)
            (let ((item (button-get button 'item)))
              (soap-inspect item)))
  'skip t)

(define-button-type 'soap-client-describe-back-link
  'face 'link
  'help-echo "mouse-2, RET: browse the previous item"
  'follow-link t
  'action (lambda (_button)
            (let ((item (pop soap-inspect-previous-items)))
              (when item
                (setq soap-inspect-current-item nil)
                (soap-inspect item))))
  'skip t)

(defun soap-insert-describe-button (element)
  "Insert a button to inspect ELEMENT when pressed."
  (insert-text-button
   (soap-element-fq-name element)
   'type 'soap-client-describe-link
   'item element))

(defun soap-inspect-xs-basic-type (type)
  "Insert information about TYPE, a soap-xs-basic-type, in the current buffer."
  (insert "Basic type: " (soap-element-fq-name type))
  (insert "\nSample value:\n")
  (pp (soap-sample-value type) (current-buffer)))

(defun soap-inspect-xs-element (element)
  "Insert information about ELEMENT, a soap-xs-element, in the current buffer."
  (insert "Element: " (soap-element-fq-name element))
  (insert "\nType: ")
  (soap-insert-describe-button (soap-xs-element-type element))
  (insert "\nAttributes:")
  (when (soap-xs-element-optional? element)
    (insert " optional"))
  (when (soap-xs-element-multiple? element)
    (insert " multiple"))
  (insert "\nSample value:\n")
  (pp (soap-sample-value element) (current-buffer)))

(defun soap-inspect-xs-attribute (attribute)
  "Insert information about ATTRIBUTE in the current buffer.
ATTRIBUTE is a soap-xs-attribute."
  (insert "Attribute: " (soap-element-fq-name attribute))
  (insert "\nType: ")
  (soap-insert-describe-button (soap-xs-attribute-type attribute))
  (insert "\nSample value:\n")
  (pp (soap-sample-value attribute) (current-buffer)))

(defun soap-inspect-xs-attribute-group (attribute-group)
  "Insert information about ATTRIBUTE-GROUP in the current buffer.
ATTRIBUTE is a soap-xs-attribute-group."
  (insert "Attribute group: " (soap-element-fq-name attribute-group))
  (insert "\nSample values:\n")
  (pp (soap-sample-value attribute-group) (current-buffer)))

(defun soap-inspect-xs-simple-type (type)
  "Insert information about TYPE, a soap-xs-simple-type, in the current buffer."
  (insert "Simple type: " (soap-element-fq-name type))
  (insert "\nBase: " )
  (if (listp (soap-xs-simple-type-base type))
      (let ((first-time t))
        (dolist (b (soap-xs-simple-type-base type))
          (unless first-time
            (insert ", ")
            (setq first-time nil))
          (soap-insert-describe-button b)))
    (soap-insert-describe-button (soap-xs-simple-type-base type)))
  (insert "\nAttributes: ")
  (dolist (attribute (soap-xs-simple-type-attributes type))
    (let ((name (or (soap-xs-attribute-name attribute) "*inline*"))
          (type (soap-xs-attribute-type attribute)))
      (insert "\n\t")
      (insert name)
      (insert "\t")
      (soap-insert-describe-button type)))
  (when (soap-xs-simple-type-enumeration type)
    (insert "\nEnumeration values: ")
    (dolist (e (soap-xs-simple-type-enumeration type))
      (insert "\n\t")
      (pp e)))
  (when (soap-xs-simple-type-pattern type)
    (insert "\nPattern: " (soap-xs-simple-type-pattern type)))
  (when (car (soap-xs-simple-type-length-range type))
    (insert "\nMin length: "
            (number-to-string (car (soap-xs-simple-type-length-range type)))))
  (when (cdr (soap-xs-simple-type-length-range type))
    (insert "\nMin length: "
            (number-to-string (cdr (soap-xs-simple-type-length-range type)))))
  (when (car (soap-xs-simple-type-integer-range type))
    (insert "\nMin value: "
            (number-to-string (car (soap-xs-simple-type-integer-range type)))))
  (when (cdr (soap-xs-simple-type-integer-range type))
    (insert "\nMin value: "
            (number-to-string (cdr (soap-xs-simple-type-integer-range type)))))
  (insert "\nSample value:\n")
  (pp (soap-sample-value type) (current-buffer)))

(defun soap-inspect-xs-complex-type (type)
  "Insert information about TYPE in the current buffer.
TYPE is a `soap-xs-complex-type'"
  (insert "Complex type: " (soap-element-fq-name type))
  (insert "\nKind: ")
  (cl-case (soap-xs-complex-type-indicator type)
    ((sequence all)
     (insert "a sequence ")
     (when (soap-xs-complex-type-base type)
       (insert "extending ")
       (soap-insert-describe-button (soap-xs-complex-type-base type)))
     (insert "\nAttributes: ")
     (dolist (attribute (soap-xs-complex-type-attributes type))
       (let ((name (or (soap-xs-attribute-name attribute) "*inline*"))
             (type (soap-xs-attribute-type attribute)))
         (insert "\n\t")
         (insert name)
         (insert "\t")
         (soap-insert-describe-button type)))
     (insert "\nElements: ")
     (let ((name-width 0)
           (type-width 0))
       (dolist (element (soap-xs-complex-type-elements type))
         (let ((name (or (soap-xs-element-name element) "*inline*"))
               (type (soap-xs-element-type element)))
           (setq name-width (max name-width (length name)))
           (setq type-width
                 (max type-width (length (soap-element-fq-name type))))))
       (setq name-width (+ name-width 2))
       (setq type-width (+ type-width 2))
       (dolist (element (soap-xs-complex-type-elements type))
         (let ((name (or (soap-xs-element-name element) "*inline*"))
               (type (soap-xs-element-type element)))
           (insert "\n\t")
           (insert name)
           (insert (make-string (- name-width (length name)) ?\ ))
           (soap-insert-describe-button type)
           (insert
            (make-string
             (- type-width (length (soap-element-fq-name type))) ?\ ))
           (when (soap-xs-element-multiple? element)
             (insert " multiple"))
           (when (soap-xs-element-optional? element)
             (insert " optional"))))))
    (choice
     (insert "a choice ")
     (when (soap-xs-complex-type-base type)
       (insert "extending ")
       (soap-insert-describe-button (soap-xs-complex-type-base type)))
     (insert "\nElements: ")
     (dolist (element (soap-xs-complex-type-elements type))
       (insert "\n\t")
       (soap-insert-describe-button element)))
    (array
     (insert "an array of ")
     (soap-insert-describe-button (soap-xs-complex-type-base type))))
  (insert "\nSample value:\n")
  (pp (soap-sample-value type) (current-buffer)))


(defun soap-inspect-message (message)
  "Insert information about MESSAGE into the current buffer."
  (insert "Message name: " (soap-element-fq-name message) "\n")
  (insert "Parts:\n")
  (dolist (part (soap-message-parts message))
    (insert "\t" (symbol-name (car part))
            " type: ")
    (soap-insert-describe-button (cdr part))
    (insert "\n")))

(defun soap-inspect-operation (operation)
  "Insert information about OPERATION into the current buffer."
  (insert "Operation name: " (soap-element-fq-name operation) "\n")
  (let ((input (soap-operation-input operation)))
    (insert "\tInput: " (symbol-name (car input)) " (" )
    (soap-insert-describe-button (cdr input))
    (insert ")\n"))
  (let ((output (soap-operation-output operation)))
    (insert "\tOutput: " (symbol-name (car output)) " (")
    (soap-insert-describe-button (cdr output))
    (insert ")\n"))

  (insert "\n\nSample invocation:\n")
  (let ((sample-message-value
         (soap-sample-value (cdr (soap-operation-input operation))))
        (funcall (list 'soap-invoke '*WSDL* "SomeService"
                       (soap-element-name operation))))
    (let ((sample-invocation
           (append funcall (mapcar 'cdr sample-message-value))))
      (pp sample-invocation (current-buffer)))))

(defun soap-inspect-port-type (port-type)
  "Insert information about PORT-TYPE into the current buffer."
  (insert "Port-type name: " (soap-element-fq-name port-type) "\n")
  (insert "Operations:\n")
  (cl-loop for o being the hash-values of
           (soap-namespace-elements (soap-port-type-operations port-type))
           do (progn
                (insert "\t")
                (soap-insert-describe-button (car o)))))

(defun soap-inspect-binding (binding)
  "Insert information about BINDING into the current buffer."
  (insert "Binding: " (soap-element-fq-name binding) "\n")
  (insert "\n")
  (insert "Bound operations:\n")
  (let* ((ophash (soap-binding-operations binding))
         (operations (cl-loop for o being the hash-keys of ophash
                              collect o))
         op-name-width)

    (setq operations (sort operations 'string<))

    (setq op-name-width (cl-loop for o in operations maximizing (length o)))

    (dolist (op operations)
      (let* ((bound-op (gethash op ophash))
             (soap-action (soap-bound-operation-soap-action bound-op))
             (use (soap-bound-operation-use bound-op)))
        (unless soap-action
          (setq soap-action ""))
        (insert "\t")
        (soap-insert-describe-button (soap-bound-operation-operation bound-op))
        (when (or use (not (equal soap-action "")))
          (insert (make-string (- op-name-width (length op)) ?\s))
          (insert " (")
          (insert soap-action)
          (when use
            (insert " " (symbol-name use)))
          (insert ")"))
        (insert "\n")))))

(defun soap-inspect-port (port)
  "Insert information about PORT into the current buffer."
  (insert "Port name:   " (soap-element-name port) "\n"
          "Service URL: " (soap-port-service-url port) "\n"
          "Binding:     ")
  (soap-insert-describe-button (soap-port-binding port)))

(defun soap-inspect-wsdl (wsdl)
  "Insert information about WSDL into the current buffer."
  (insert "WSDL Origin: " (soap-wsdl-origin wsdl) "\n")
  (insert "Ports:")
  (dolist (p (soap-wsdl-ports wsdl))
    (insert "\n--------------------\n")
    ;; (soap-insert-describe-button p)
    (soap-inspect-port p))
  (insert "\n--------------------\nNamespace alias table:\n")
  (dolist (a (soap-wsdl-alias-table wsdl))
    (insert "\t" (car a) " => " (cdr a) "\n")))

(progn
  ;; Install the soap-inspect methods for our types

  (put (aref (make-soap-xs-basic-type) 0) 'soap-inspect
       'soap-inspect-xs-basic-type)

  (put (aref (make-soap-xs-element) 0) 'soap-inspect
       'soap-inspect-xs-element)

  (put (aref (make-soap-xs-simple-type) 0) 'soap-inspect
       'soap-inspect-xs-simple-type)

  (put (aref (make-soap-xs-complex-type) 0) 'soap-inspect
       'soap-inspect-xs-complex-type)

  (put (aref (make-soap-xs-attribute) 0) 'soap-inspect
       'soap-inspect-xs-attribute)

  (put (aref (make-soap-xs-attribute-group) 0) 'soap-inspect
       'soap-inspect-xs-attribute-group)

  (put (aref (make-soap-message) 0) 'soap-inspect
       'soap-inspect-message)
  (put (aref (make-soap-operation) 0) 'soap-inspect
       'soap-inspect-operation)

  (put (aref (make-soap-port-type) 0) 'soap-inspect
       'soap-inspect-port-type)

  (put (aref (make-soap-binding) 0) 'soap-inspect
       'soap-inspect-binding)

  (put (aref (make-soap-port) 0) 'soap-inspect
       'soap-inspect-port)

  (put (aref (soap-make-wsdl "origin") 0) 'soap-inspect
       'soap-inspect-wsdl))

(provide 'soap-inspect)
;;; soap-inspect.el ends here