]> code.delx.au - gnu-emacs/blob - lisp/net/soap-client.el
Backslash cleanup in Elisp source files
[gnu-emacs] / lisp / net / soap-client.el
1 ;;;; soap-client.el -- Access SOAP web services from Emacs
2
3 ;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
4
5 ;; Author: Alexandru Harsanyi <AlexHarsanyi@gmail.com>
6 ;; Created: December, 2009
7 ;; Keywords: soap, web-services, comm, hypermedia
8 ;; Package: soap-client
9 ;; Homepage: http://code.google.com/p/emacs-soap-client
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27 ;;
28 ;; To use the SOAP client, you first need to load the WSDL document for the
29 ;; service you want to access, using `soap-load-wsdl-from-url'. A WSDL
30 ;; document describes the available operations of the SOAP service, how their
31 ;; parameters and responses are encoded. To invoke operations, you use the
32 ;; `soap-invoke' method passing it the WSDL, the service name, the operation
33 ;; you wish to invoke and any required parameters.
34 ;;
35 ;; Ideally, the service you want to access will have some documentation about
36 ;; the operations it supports. If it does not, you can try using
37 ;; `soap-inspect' to browse the WSDL document and see the available operations
38 ;; and their parameters.
39 ;;
40
41 ;;; Code:
42
43 (eval-when-compile (require 'cl))
44
45 (require 'xml)
46 (require 'warnings)
47 (require 'url)
48 (require 'url-http)
49 (require 'url-util)
50 (require 'mm-decode)
51
52 (defsubst soap-warning (message &rest args)
53 "Display a warning MESSAGE with ARGS, using the 'soap-client warning type."
54 (display-warning 'soap-client (apply #'format-message message args)
55 :warning))
56
57 (defgroup soap-client nil
58 "Access SOAP web services from Emacs."
59 :version "24.1"
60 :group 'tools)
61
62 ;;;; Support for parsing XML documents with namespaces
63
64 ;; XML documents with namespaces are difficult to parse because the names of
65 ;; the nodes depend on what "xmlns" aliases have been defined in the document.
66 ;; To work with such documents, we introduce a translation layer between a
67 ;; "well known" namespace tag and the local namespace tag in the document
68 ;; being parsed.
69
70 (defconst soap-well-known-xmlns
71 '(("apachesoap" . "http://xml.apache.org/xml-soap")
72 ("soapenc" . "http://schemas.xmlsoap.org/soap/encoding/")
73 ("wsdl" . "http://schemas.xmlsoap.org/wsdl/")
74 ("wsdlsoap" . "http://schemas.xmlsoap.org/wsdl/soap/")
75 ("xsd" . "http://www.w3.org/2001/XMLSchema")
76 ("xsi" . "http://www.w3.org/2001/XMLSchema-instance")
77 ("soap" . "http://schemas.xmlsoap.org/soap/envelope/")
78 ("soap12" . "http://schemas.xmlsoap.org/wsdl/soap12/")
79 ("http" . "http://schemas.xmlsoap.org/wsdl/http/")
80 ("mime" . "http://schemas.xmlsoap.org/wsdl/mime/"))
81 "A list of well known xml namespaces and their aliases.")
82
83 (defvar soap-local-xmlns nil
84 "A list of local namespace aliases.
85 This is a dynamically bound variable, controlled by
86 `soap-with-local-xmlns'.")
87
88 (defvar soap-default-xmlns nil
89 "The default XML namespaces.
90 Names in this namespace will be unqualified. This is a
91 dynamically bound variable, controlled by
92 `soap-with-local-xmlns'")
93
94 (defvar soap-target-xmlns nil
95 "The target XML namespace.
96 New XSD elements will be defined in this namespace, unless they
97 are fully qualified for a different namespace. This is a
98 dynamically bound variable, controlled by
99 `soap-with-local-xmlns'")
100
101 (defun soap-wk2l (well-known-name)
102 "Return local variant of WELL-KNOWN-NAME.
103 This is done by looking up the namespace in the
104 `soap-well-known-xmlns' table and resolving the namespace to
105 the local name based on the current local translation table
106 `soap-local-xmlns'. See also `soap-with-local-xmlns'."
107 (let ((wk-name-1 (if (symbolp well-known-name)
108 (symbol-name well-known-name)
109 well-known-name)))
110 (cond
111 ((string-match "^\\(.*\\):\\(.*\\)$" wk-name-1)
112 (let ((ns (match-string 1 wk-name-1))
113 (name (match-string 2 wk-name-1)))
114 (let ((namespace (cdr (assoc ns soap-well-known-xmlns))))
115 (cond ((equal namespace soap-default-xmlns)
116 ;; Name is unqualified in the default namespace
117 (if (symbolp well-known-name)
118 (intern name)
119 name))
120 (t
121 (let* ((local-ns (car (rassoc namespace soap-local-xmlns)))
122 (local-name (concat local-ns ":" name)))
123 (if (symbolp well-known-name)
124 (intern local-name)
125 local-name)))))))
126 (t well-known-name))))
127
128 (defun soap-l2wk (local-name)
129 "Convert LOCAL-NAME into a well known name.
130 The namespace of LOCAL-NAME is looked up in the
131 `soap-well-known-xmlns' table and a well known namespace tag is
132 used in the name.
133
134 nil is returned if there is no well-known namespace for the
135 namespace of LOCAL-NAME."
136 (let ((l-name-1 (if (symbolp local-name)
137 (symbol-name local-name)
138 local-name))
139 namespace name)
140 (cond
141 ((string-match "^\\(.*\\):\\(.*\\)$" l-name-1)
142 (setq name (match-string 2 l-name-1))
143 (let ((ns (match-string 1 l-name-1)))
144 (setq namespace (cdr (assoc ns soap-local-xmlns)))
145 (unless namespace
146 (error "Soap-l2wk(%s): no namespace for alias %s" local-name ns))))
147 (t
148 (setq name l-name-1)
149 (setq namespace soap-default-xmlns)))
150
151 (if namespace
152 (let ((well-known-ns (car (rassoc namespace soap-well-known-xmlns))))
153 (if well-known-ns
154 (let ((well-known-name (concat well-known-ns ":" name)))
155 (if (symbol-name local-name)
156 (intern well-known-name)
157 well-known-name))
158 (progn
159 ;; (soap-warning "soap-l2wk(%s): namespace %s has no well-known tag"
160 ;; local-name namespace)
161 nil)))
162 ;; if no namespace is defined, just return the unqualified name
163 name)))
164
165
166 (defun soap-l2fq (local-name &optional use-tns)
167 "Convert LOCAL-NAME into a fully qualified name.
168 A fully qualified name is a cons of the namespace name and the
169 name of the element itself. For example \"xsd:string\" is
170 converted to \(\"http://www.w3.org/2001/XMLSchema\" . \"string\").
171
172 The USE-TNS argument specifies what to do when LOCAL-NAME has no
173 namespace tag. If USE-TNS is non-nil, the `soap-target-xmlns'
174 will be used as the element's namespace, otherwise
175 `soap-default-xmlns' will be used.
176
177 This is needed because different parts of a WSDL document can use
178 different namespace aliases for the same element."
179 (let ((local-name-1 (if (symbolp local-name)
180 (symbol-name local-name)
181 local-name)))
182 (cond ((string-match "^\\(.*\\):\\(.*\\)$" local-name-1)
183 (let ((ns (match-string 1 local-name-1))
184 (name (match-string 2 local-name-1)))
185 (let ((namespace (cdr (assoc ns soap-local-xmlns))))
186 (if namespace
187 (cons namespace name)
188 (error "Soap-l2fq(%s): unknown alias %s" local-name ns)))))
189 (t
190 (cons (if use-tns
191 soap-target-xmlns
192 soap-default-xmlns)
193 local-name)))))
194
195 (defun soap-extract-xmlns (node &optional xmlns-table)
196 "Return a namespace alias table for NODE by extending XMLNS-TABLE."
197 (let (xmlns default-ns target-ns)
198 (dolist (a (xml-node-attributes node))
199 (let ((name (symbol-name (car a)))
200 (value (cdr a)))
201 (cond ((string= name "targetNamespace")
202 (setq target-ns value))
203 ((string= name "xmlns")
204 (setq default-ns value))
205 ((string-match "^xmlns:\\(.*\\)$" name)
206 (push (cons (match-string 1 name) value) xmlns)))))
207
208 (let ((tns (assoc "tns" xmlns)))
209 (cond ((and tns target-ns)
210 ;; If a tns alias is defined for this node, it must match
211 ;; the target namespace.
212 (unless (equal target-ns (cdr tns))
213 (soap-warning
214 "soap-extract-xmlns(%s): tns alias and targetNamespace mismatch"
215 (xml-node-name node))))
216 ((and tns (not target-ns))
217 (setq target-ns (cdr tns)))
218 ((and (not tns) target-ns)
219 ;; a tns alias was not defined in this node. See if the node has
220 ;; a "targetNamespace" attribute and add an alias to this. Note
221 ;; that we might override an existing tns alias in XMLNS-TABLE,
222 ;; but that is intended.
223 (push (cons "tns" target-ns) xmlns))))
224
225 (list default-ns target-ns (append xmlns xmlns-table))))
226
227 (defmacro soap-with-local-xmlns (node &rest body)
228 "Install a local alias table from NODE and execute BODY."
229 (declare (debug (form &rest form)) (indent 1))
230 (let ((xmlns (make-symbol "xmlns")))
231 `(let ((,xmlns (soap-extract-xmlns ,node soap-local-xmlns)))
232 (let ((soap-default-xmlns (or (nth 0 ,xmlns) soap-default-xmlns))
233 (soap-target-xmlns (or (nth 1 ,xmlns) soap-target-xmlns))
234 (soap-local-xmlns (nth 2 ,xmlns)))
235 ,@body))))
236
237 (defun soap-get-target-namespace (node)
238 "Return the target namespace of NODE.
239 This is the namespace in which new elements will be defined."
240 (or (xml-get-attribute-or-nil node 'targetNamespace)
241 (cdr (assoc "tns" soap-local-xmlns))
242 soap-target-xmlns))
243
244 (defun soap-xml-get-children1 (node child-name)
245 "Return the children of NODE named CHILD-NAME.
246 This is the same as `xml-get-children', but CHILD-NAME can have
247 namespace tag."
248 (let (result)
249 (dolist (c (xml-node-children node))
250 (when (and (consp c)
251 (soap-with-local-xmlns c
252 ;; We use `ignore-errors' here because we want to silently
253 ;; skip nodes for which we cannot convert them to a
254 ;; well-known name.
255 (eq (ignore-errors (soap-l2wk (xml-node-name c)))
256 child-name)))
257 (push c result)))
258 (nreverse result)))
259
260 (defun soap-xml-get-attribute-or-nil1 (node attribute)
261 "Return the NODE's ATTRIBUTE, or nil if it does not exist.
262 This is the same as `xml-get-attribute-or-nil', but ATTRIBUTE can
263 be tagged with a namespace tag."
264 (catch 'found
265 (soap-with-local-xmlns node
266 (dolist (a (xml-node-attributes node))
267 ;; We use `ignore-errors' here because we want to silently skip
268 ;; attributes for which we cannot convert them to a well-known name.
269 (when (eq (ignore-errors (soap-l2wk (car a))) attribute)
270 (throw 'found (cdr a)))))))
271
272 \f
273 ;;;; XML namespaces
274
275 ;; An element in an XML namespace, "things" stored in soap-xml-namespaces will
276 ;; be derived from this object.
277
278 (defstruct soap-element
279 name
280 ;; The "well-known" namespace tag for the element. For example, while
281 ;; parsing XML documents, we can have different tags for the XMLSchema
282 ;; namespace, but internally all our XMLSchema elements will have the "xsd"
283 ;; tag.
284 namespace-tag)
285
286 (defun soap-element-fq-name (element)
287 "Return a fully qualified name for ELEMENT.
288 A fq name is the concatenation of the namespace tag and the
289 element name."
290 (concat (soap-element-namespace-tag element)
291 ":" (soap-element-name element)))
292
293 ;; a namespace link stores an alias for an object in once namespace to a
294 ;; "target" object possibly in a different namespace
295
296 (defstruct (soap-namespace-link (:include soap-element))
297 target)
298
299 ;; A namespace is a collection of soap-element objects under a name (the name
300 ;; of the namespace).
301
302 (defstruct soap-namespace
303 (name nil :read-only t) ; e.g "http://xml.apache.org/xml-soap"
304 (elements (make-hash-table :test 'equal) :read-only t))
305
306 (defun soap-namespace-put (element ns)
307 "Store ELEMENT in NS.
308 Multiple elements with the same name can be stored in a
309 namespace. When retrieving the element you can specify a
310 discriminant predicate to `soap-namespace-get'"
311 (let ((name (soap-element-name element)))
312 (push element (gethash name (soap-namespace-elements ns)))))
313
314 (defun soap-namespace-put-link (name target ns &optional replace)
315 "Store a link from NAME to TARGET in NS.
316 An error will be signaled if an element by the same name is
317 already present in NS, unless REPLACE is non nil.
318
319 TARGET can be either a SOAP-ELEMENT or a string denoting an
320 element name into another namespace.
321
322 If NAME is nil, an element with the same name as TARGET will be
323 added to the namespace."
324
325 (unless (and name (not (equal name "")))
326 ;; if name is nil, use TARGET as a name...
327 (cond ((soap-element-p target)
328 (setq name (soap-element-name target)))
329 ((consp target) ; a fq name: (namespace . name)
330 (setq name (cdr target)))
331 ((stringp target)
332 (cond ((string-match "^\\(.*\\):\\(.*\\)$" target)
333 (setq name (match-string 2 target)))
334 (t
335 (setq name target))))))
336
337 ;; by now, name should be valid
338 (assert (and name (not (equal name "")))
339 nil
340 "Cannot determine name for namespace link")
341 (push (make-soap-namespace-link :name name :target target)
342 (gethash name (soap-namespace-elements ns))))
343
344 (defun soap-namespace-get (name ns &optional discriminant-predicate)
345 "Retrieve an element with NAME from the namespace NS.
346 If multiple elements with the same name exist,
347 DISCRIMINANT-PREDICATE is used to pick one of them. This allows
348 storing elements of different types (like a message type and a
349 binding) but the same name."
350 (assert (stringp name))
351 (let ((elements (gethash name (soap-namespace-elements ns))))
352 (cond (discriminant-predicate
353 (catch 'found
354 (dolist (e elements)
355 (when (funcall discriminant-predicate e)
356 (throw 'found e)))))
357 ((= (length elements) 1) (car elements))
358 ((> (length elements) 1)
359 (error
360 "Soap-namespace-get(%s): multiple elements, discriminant needed"
361 name))
362 (t
363 nil))))
364
365 \f
366 ;;;; WSDL documents
367 ;;;;; WSDL document elements
368
369 (defstruct (soap-basic-type (:include soap-element))
370 kind ; a symbol of: string, dateTime, long, int
371 )
372
373 (defstruct (soap-simple-type (:include soap-basic-type))
374 enumeration)
375
376 (defstruct soap-sequence-element
377 name type nillable? multiple?)
378
379 (defstruct (soap-sequence-type (:include soap-element))
380 parent ; OPTIONAL WSDL-TYPE name
381 elements ; LIST of SOAP-SEQUENCE-ELEMENT
382 )
383
384 (defstruct (soap-array-type (:include soap-element))
385 element-type ; WSDL-TYPE of the array elements
386 )
387
388 (defstruct (soap-message (:include soap-element))
389 parts ; ALIST of NAME => WSDL-TYPE name
390 )
391
392 (defstruct (soap-operation (:include soap-element))
393 parameter-order
394 input ; (NAME . MESSAGE)
395 output ; (NAME . MESSAGE)
396 faults) ; a list of (NAME . MESSAGE)
397
398 (defstruct (soap-port-type (:include soap-element))
399 operations) ; a namespace of operations
400
401 ;; A bound operation is an operation which has a soap action and a use
402 ;; method attached -- these are attached as part of a binding and we
403 ;; can have different bindings for the same operations.
404 (defstruct soap-bound-operation
405 operation ; SOAP-OPERATION
406 soap-action ; value for SOAPAction HTTP header
407 use ; 'literal or 'encoded, see
408 ; http://www.w3.org/TR/wsdl#_soap:body
409 )
410
411 (defstruct (soap-binding (:include soap-element))
412 port-type
413 (operations (make-hash-table :test 'equal) :readonly t))
414
415 (defstruct (soap-port (:include soap-element))
416 service-url
417 binding)
418
419 (defun soap-default-xsd-types ()
420 "Return a namespace containing some of the XMLSchema types."
421 (let ((ns (make-soap-namespace :name "http://www.w3.org/2001/XMLSchema")))
422 (dolist (type '("string" "dateTime" "boolean"
423 "long" "int" "integer" "unsignedInt" "byte" "float" "double"
424 "base64Binary" "anyType" "anyURI" "Array" "byte[]"))
425 (soap-namespace-put
426 (make-soap-basic-type :name type :kind (intern type))
427 ns))
428 ns))
429
430 (defun soap-default-soapenc-types ()
431 "Return a namespace containing some of the SOAPEnc types."
432 (let ((ns (make-soap-namespace
433 :name "http://schemas.xmlsoap.org/soap/encoding/")))
434 (dolist (type '("string" "dateTime" "boolean"
435 "long" "int" "integer" "unsignedInt" "byte" "float" "double"
436 "base64Binary" "anyType" "anyURI" "Array" "byte[]"))
437 (soap-namespace-put
438 (make-soap-basic-type :name type :kind (intern type))
439 ns))
440 ns))
441
442 (defun soap-type-p (element)
443 "Return t if ELEMENT is a SOAP data type (basic or complex)."
444 (or (soap-basic-type-p element)
445 (soap-sequence-type-p element)
446 (soap-array-type-p element)))
447
448
449 ;;;;; The WSDL document
450
451 ;; The WSDL data structure used for encoding/decoding SOAP messages
452 (defstruct soap-wsdl
453 origin ; file or URL from which this wsdl was loaded
454 ports ; a list of SOAP-PORT instances
455 alias-table ; a list of namespace aliases
456 namespaces ; a list of namespaces
457 )
458
459 (defun soap-wsdl-add-alias (alias name wsdl)
460 "Add a namespace ALIAS for NAME to the WSDL document."
461 (push (cons alias name) (soap-wsdl-alias-table wsdl)))
462
463 (defun soap-wsdl-find-namespace (name wsdl)
464 "Find a namespace by NAME in the WSDL document."
465 (catch 'found
466 (dolist (ns (soap-wsdl-namespaces wsdl))
467 (when (equal name (soap-namespace-name ns))
468 (throw 'found ns)))))
469
470 (defun soap-wsdl-add-namespace (ns wsdl)
471 "Add the namespace NS to the WSDL document.
472 If a namespace by this name already exists in WSDL, individual
473 elements will be added to it."
474 (let ((existing (soap-wsdl-find-namespace (soap-namespace-name ns) wsdl)))
475 (if existing
476 ;; Add elements from NS to EXISTING, replacing existing values.
477 (maphash (lambda (key value)
478 (dolist (v value)
479 (soap-namespace-put v existing)))
480 (soap-namespace-elements ns))
481 (push ns (soap-wsdl-namespaces wsdl)))))
482
483 (defun soap-wsdl-get (name wsdl &optional predicate use-local-alias-table)
484 "Retrieve element NAME from the WSDL document.
485
486 PREDICATE is used to differentiate between elements when NAME
487 refers to multiple elements. A typical value for this would be a
488 structure predicate for the type of element you want to retrieve.
489 For example, to retrieve a message named \"foo\" when other
490 elements named \"foo\" exist in the WSDL you could use:
491
492 (soap-wsdl-get \"foo\" WSDL \\='soap-message-p)
493
494 If USE-LOCAL-ALIAS-TABLE is not nil, `soap-local-xmlns' will be
495 used to resolve the namespace alias."
496 (let ((alias-table (soap-wsdl-alias-table wsdl))
497 namespace element-name element)
498
499 (when (symbolp name)
500 (setq name (symbol-name name)))
501
502 (when use-local-alias-table
503 (setq alias-table (append soap-local-xmlns alias-table)))
504
505 (cond ((consp name) ; a fully qualified name, as returned by `soap-l2fq'
506 (setq element-name (cdr name))
507 (when (symbolp element-name)
508 (setq element-name (symbol-name element-name)))
509 (setq namespace (soap-wsdl-find-namespace (car name) wsdl))
510 (unless namespace
511 (error "Soap-wsdl-get(%s): unknown namespace: %s" name namespace)))
512
513 ((string-match "^\\(.*\\):\\(.*\\)$" name)
514 (setq element-name (match-string 2 name))
515
516 (let* ((ns-alias (match-string 1 name))
517 (ns-name (cdr (assoc ns-alias alias-table))))
518 (unless ns-name
519 (error "Soap-wsdl-get(%s): cannot find namespace alias %s"
520 name ns-alias))
521
522 (setq namespace (soap-wsdl-find-namespace ns-name wsdl))
523 (unless namespace
524 (error
525 "Soap-wsdl-get(%s): unknown namespace %s, referenced by alias %s"
526 name ns-name ns-alias))))
527 (t
528 (error "Soap-wsdl-get(%s): bad name" name)))
529
530 (setq element (soap-namespace-get
531 element-name namespace
532 (if predicate
533 (lambda (e)
534 (or (funcall 'soap-namespace-link-p e)
535 (funcall predicate e)))
536 nil)))
537
538 (unless element
539 (error "Soap-wsdl-get(%s): cannot find element" name))
540
541 (if (soap-namespace-link-p element)
542 ;; NOTE: don't use the local alias table here
543 (soap-wsdl-get (soap-namespace-link-target element) wsdl predicate)
544 element)))
545
546 ;;;;; Resolving references for wsdl types
547
548 ;; See `soap-wsdl-resolve-references', which is the main entry point for
549 ;; resolving references
550
551 (defun soap-resolve-references-for-element (element wsdl)
552 "Resolve references in ELEMENT using the WSDL document.
553 This is a generic function which invokes a specific function
554 depending on the element type.
555
556 If ELEMENT has no resolver function, it is silently ignored.
557
558 All references are resolved in-place, that is the ELEMENT is
559 updated."
560 (let ((resolver (get (aref element 0) 'soap-resolve-references)))
561 (when resolver
562 (funcall resolver element wsdl))))
563
564 (defun soap-resolve-references-for-simple-type (type wsdl)
565 "Resolve the base type for the simple TYPE using the WSDL
566 document."
567 (let ((kind (soap-basic-type-kind type)))
568 (unless (symbolp kind)
569 (let ((basic-type (soap-wsdl-get kind wsdl 'soap-basic-type-p)))
570 (setf (soap-basic-type-kind type)
571 (soap-basic-type-kind basic-type))))))
572
573 (defun soap-resolve-references-for-sequence-type (type wsdl)
574 "Resolve references for a sequence TYPE using WSDL document.
575 See also `soap-resolve-references-for-element' and
576 `soap-wsdl-resolve-references'"
577 (let ((parent (soap-sequence-type-parent type)))
578 (when (or (consp parent) (stringp parent))
579 (setf (soap-sequence-type-parent type)
580 (soap-wsdl-get
581 parent wsdl
582 ;; Prevent self references, see Bug#9
583 (lambda (e) (and (not (eq e type)) (soap-type-p e)))))))
584 (dolist (element (soap-sequence-type-elements type))
585 (let ((element-type (soap-sequence-element-type element)))
586 (cond ((or (consp element-type) (stringp element-type))
587 (setf (soap-sequence-element-type element)
588 (soap-wsdl-get
589 element-type wsdl
590 ;; Prevent self references, see Bug#9
591 (lambda (e) (and (not (eq e type)) (soap-type-p e))))))
592 ((soap-element-p element-type)
593 ;; since the element already has a child element, it
594 ;; could be an inline structure. we must resolve
595 ;; references in it, because it might not be reached by
596 ;; scanning the wsdl names.
597 (soap-resolve-references-for-element element-type wsdl))))))
598
599 (defun soap-resolve-references-for-array-type (type wsdl)
600 "Resolve references for an array TYPE using WSDL.
601 See also `soap-resolve-references-for-element' and
602 `soap-wsdl-resolve-references'"
603 (let ((element-type (soap-array-type-element-type type)))
604 (when (or (consp element-type) (stringp element-type))
605 (setf (soap-array-type-element-type type)
606 (soap-wsdl-get
607 element-type wsdl
608 ;; Prevent self references, see Bug#9
609 (lambda (e) (and (not (eq e type)) (soap-type-p e))))))))
610
611 (defun soap-resolve-references-for-message (message wsdl)
612 "Resolve references for a MESSAGE type using the WSDL document.
613 See also `soap-resolve-references-for-element' and
614 `soap-wsdl-resolve-references'"
615 (let (resolved-parts)
616 (dolist (part (soap-message-parts message))
617 (let ((name (car part))
618 (type (cdr part)))
619 (when (stringp name)
620 (setq name (intern name)))
621 (when (or (consp type) (stringp type))
622 (setq type (soap-wsdl-get type wsdl 'soap-type-p)))
623 (push (cons name type) resolved-parts)))
624 (setf (soap-message-parts message) (nreverse resolved-parts))))
625
626 (defun soap-resolve-references-for-operation (operation wsdl)
627 "Resolve references for an OPERATION type using the WSDL document.
628 See also `soap-resolve-references-for-element' and
629 `soap-wsdl-resolve-references'"
630 (let ((input (soap-operation-input operation))
631 (counter 0))
632 (let ((name (car input))
633 (message (cdr input)))
634 ;; Name this part if it was not named
635 (when (or (null name) (equal name ""))
636 (setq name (format "in%d" (incf counter))))
637 (when (or (consp message) (stringp message))
638 (setf (soap-operation-input operation)
639 (cons (intern name)
640 (soap-wsdl-get message wsdl 'soap-message-p))))))
641
642 (let ((output (soap-operation-output operation))
643 (counter 0))
644 (let ((name (car output))
645 (message (cdr output)))
646 (when (or (null name) (equal name ""))
647 (setq name (format "out%d" (incf counter))))
648 (when (or (consp message) (stringp message))
649 (setf (soap-operation-output operation)
650 (cons (intern name)
651 (soap-wsdl-get message wsdl 'soap-message-p))))))
652
653 (let ((resolved-faults nil)
654 (counter 0))
655 (dolist (fault (soap-operation-faults operation))
656 (let ((name (car fault))
657 (message (cdr fault)))
658 (when (or (null name) (equal name ""))
659 (setq name (format "fault%d" (incf counter))))
660 (if (or (consp message) (stringp message))
661 (push (cons (intern name)
662 (soap-wsdl-get message wsdl 'soap-message-p))
663 resolved-faults)
664 (push fault resolved-faults))))
665 (setf (soap-operation-faults operation) resolved-faults))
666
667 (when (= (length (soap-operation-parameter-order operation)) 0)
668 (setf (soap-operation-parameter-order operation)
669 (mapcar 'car (soap-message-parts
670 (cdr (soap-operation-input operation))))))
671
672 (setf (soap-operation-parameter-order operation)
673 (mapcar (lambda (p)
674 (if (stringp p)
675 (intern p)
676 p))
677 (soap-operation-parameter-order operation))))
678
679 (defun soap-resolve-references-for-binding (binding wsdl)
680 "Resolve references for a BINDING type using the WSDL document.
681 See also `soap-resolve-references-for-element' and
682 `soap-wsdl-resolve-references'"
683 (when (or (consp (soap-binding-port-type binding))
684 (stringp (soap-binding-port-type binding)))
685 (setf (soap-binding-port-type binding)
686 (soap-wsdl-get (soap-binding-port-type binding)
687 wsdl 'soap-port-type-p)))
688
689 (let ((port-ops (soap-port-type-operations (soap-binding-port-type binding))))
690 (maphash (lambda (k v)
691 (setf (soap-bound-operation-operation v)
692 (soap-namespace-get k port-ops 'soap-operation-p)))
693 (soap-binding-operations binding))))
694
695 (defun soap-resolve-references-for-port (port wsdl)
696 "Resolve references for a PORT type using the WSDL document.
697 See also `soap-resolve-references-for-element' and
698 `soap-wsdl-resolve-references'"
699 (when (or (consp (soap-port-binding port))
700 (stringp (soap-port-binding port)))
701 (setf (soap-port-binding port)
702 (soap-wsdl-get (soap-port-binding port) wsdl 'soap-binding-p))))
703
704 ;; Install resolvers for our types
705 (progn
706 (put (aref (make-soap-simple-type) 0) 'soap-resolve-references
707 'soap-resolve-references-for-simple-type)
708 (put (aref (make-soap-sequence-type) 0) 'soap-resolve-references
709 'soap-resolve-references-for-sequence-type)
710 (put (aref (make-soap-array-type) 0) 'soap-resolve-references
711 'soap-resolve-references-for-array-type)
712 (put (aref (make-soap-message) 0) 'soap-resolve-references
713 'soap-resolve-references-for-message)
714 (put (aref (make-soap-operation) 0) 'soap-resolve-references
715 'soap-resolve-references-for-operation)
716 (put (aref (make-soap-binding) 0) 'soap-resolve-references
717 'soap-resolve-references-for-binding)
718 (put (aref (make-soap-port) 0) 'soap-resolve-references
719 'soap-resolve-references-for-port))
720
721 (defun soap-wsdl-resolve-references (wsdl)
722 "Resolve all references inside the WSDL structure.
723
724 When the WSDL elements are created from the XML document, they
725 refer to each other by name. For example, the ELEMENT-TYPE slot
726 of an SOAP-ARRAY-TYPE will contain the name of the element and
727 the user would have to call `soap-wsdl-get' to obtain the actual
728 element.
729
730 After the entire document is loaded, we resolve all these
731 references to the actual elements they refer to so that at
732 runtime, we don't have to call `soap-wsdl-get' each time we
733 traverse an element tree."
734 (let ((nprocessed 0)
735 (nstag-id 0)
736 (alias-table (soap-wsdl-alias-table wsdl)))
737 (dolist (ns (soap-wsdl-namespaces wsdl))
738 (let ((nstag (car-safe (rassoc (soap-namespace-name ns) alias-table))))
739 (unless nstag
740 ;; If this namespace does not have an alias, create one for it.
741 (catch 'done
742 (while t
743 (setq nstag (format "ns%d" (incf nstag-id)))
744 (unless (assoc nstag alias-table)
745 (soap-wsdl-add-alias nstag (soap-namespace-name ns) wsdl)
746 (throw 'done t)))))
747
748 (maphash (lambda (name element)
749 (cond ((soap-element-p element) ; skip links
750 (incf nprocessed)
751 (soap-resolve-references-for-element element wsdl)
752 (setf (soap-element-namespace-tag element) nstag))
753 ((listp element)
754 (dolist (e element)
755 (when (soap-element-p e)
756 (incf nprocessed)
757 (soap-resolve-references-for-element e wsdl)
758 (setf (soap-element-namespace-tag e) nstag))))))
759 (soap-namespace-elements ns)))))
760 wsdl)
761
762 ;;;;; Loading WSDL from XML documents
763
764 (defun soap-load-wsdl-from-url (url)
765 "Load a WSDL document from URL and return it.
766 The returned WSDL document needs to be used for `soap-invoke'
767 calls."
768 (let ((url-request-method "GET")
769 (url-package-name "soap-client.el")
770 (url-package-version "1.0")
771 (url-mime-charset-string "utf-8;q=1, iso-8859-1;q=0.5")
772 (url-request-coding-system 'utf-8)
773 (url-http-attempt-keepalives nil))
774 (let ((buffer (url-retrieve-synchronously url)))
775 (with-current-buffer buffer
776 (declare (special url-http-response-status))
777 (if (> url-http-response-status 299)
778 (error "Error retrieving WSDL: %s" url-http-response-status))
779 (let ((mime-part (mm-dissect-buffer t t)))
780 (unless mime-part
781 (error "Failed to decode response from server"))
782 (unless (equal (car (mm-handle-type mime-part)) "text/xml")
783 (error "Server response is not an XML document"))
784 (with-temp-buffer
785 (mm-insert-part mime-part)
786 (let ((wsdl-xml (car (xml-parse-region (point-min) (point-max)))))
787 (prog1
788 (let ((wsdl (soap-parse-wsdl wsdl-xml)))
789 (setf (soap-wsdl-origin wsdl) url)
790 wsdl)
791 (kill-buffer buffer)))))))))
792
793 (defun soap-load-wsdl (file)
794 "Load a WSDL document from FILE and return it."
795 (with-temp-buffer
796 (insert-file-contents file)
797 (let ((xml (car (xml-parse-region (point-min) (point-max)))))
798 (let ((wsdl (soap-parse-wsdl xml)))
799 (setf (soap-wsdl-origin wsdl) file)
800 wsdl))))
801
802 (defun soap-parse-wsdl (node)
803 "Construct a WSDL structure from NODE, which is an XML document."
804 (soap-with-local-xmlns node
805
806 (assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:definitions)
807 nil
808 "soap-parse-wsdl: expecting wsdl:definitions node, got %s"
809 (soap-l2wk (xml-node-name node)))
810
811 (let ((wsdl (make-soap-wsdl)))
812
813 ;; Add the local alias table to the wsdl document -- it will be used for
814 ;; all types in this document even after we finish parsing it.
815 (setf (soap-wsdl-alias-table wsdl) soap-local-xmlns)
816
817 ;; Add the XSD types to the wsdl document
818 (let ((ns (soap-default-xsd-types)))
819 (soap-wsdl-add-namespace ns wsdl)
820 (soap-wsdl-add-alias "xsd" (soap-namespace-name ns) wsdl))
821
822 ;; Add the soapenc types to the wsdl document
823 (let ((ns (soap-default-soapenc-types)))
824 (soap-wsdl-add-namespace ns wsdl)
825 (soap-wsdl-add-alias "soapenc" (soap-namespace-name ns) wsdl))
826
827 ;; Find all the 'xsd:schema nodes which are children of wsdl:types nodes
828 ;; and build our type-library
829
830 (let ((types (car (soap-xml-get-children1 node 'wsdl:types))))
831 (dolist (node (xml-node-children types))
832 ;; We cannot use (xml-get-children node (soap-wk2l 'xsd:schema))
833 ;; because each node can install its own alias type so the schema
834 ;; nodes might have a different prefix.
835 (when (consp node)
836 (soap-with-local-xmlns node
837 (when (eq (soap-l2wk (xml-node-name node)) 'xsd:schema)
838 (soap-wsdl-add-namespace (soap-parse-schema node) wsdl))))))
839
840 (let ((ns (make-soap-namespace :name (soap-get-target-namespace node))))
841 (dolist (node (soap-xml-get-children1 node 'wsdl:message))
842 (soap-namespace-put (soap-parse-message node) ns))
843
844 (dolist (node (soap-xml-get-children1 node 'wsdl:portType))
845 (let ((port-type (soap-parse-port-type node)))
846 (soap-namespace-put port-type ns)
847 (soap-wsdl-add-namespace
848 (soap-port-type-operations port-type) wsdl)))
849
850 (dolist (node (soap-xml-get-children1 node 'wsdl:binding))
851 (soap-namespace-put (soap-parse-binding node) ns))
852
853 (dolist (node (soap-xml-get-children1 node 'wsdl:service))
854 (dolist (node (soap-xml-get-children1 node 'wsdl:port))
855 (let ((name (xml-get-attribute node 'name))
856 (binding (xml-get-attribute node 'binding))
857 (url (let ((n (car (soap-xml-get-children1
858 node 'wsdlsoap:address))))
859 (xml-get-attribute n 'location))))
860 (let ((port (make-soap-port
861 :name name :binding (soap-l2fq binding 'tns)
862 :service-url url)))
863 (soap-namespace-put port ns)
864 (push port (soap-wsdl-ports wsdl))))))
865
866 (soap-wsdl-add-namespace ns wsdl))
867
868 (soap-wsdl-resolve-references wsdl)
869
870 wsdl)))
871
872 (defun soap-parse-schema (node)
873 "Parse a schema NODE.
874 Return a SOAP-NAMESPACE containing the elements."
875 (soap-with-local-xmlns node
876 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:schema)
877 nil
878 "soap-parse-schema: expecting an xsd:schema node, got %s"
879 (soap-l2wk (xml-node-name node)))
880 (let ((ns (make-soap-namespace :name (soap-get-target-namespace node))))
881 ;; NOTE: we only extract the complexTypes from the schema, we wouldn't
882 ;; know how to handle basic types beyond the built in ones anyway.
883 (dolist (node (soap-xml-get-children1 node 'xsd:simpleType))
884 (soap-namespace-put (soap-parse-simple-type node) ns))
885
886 (dolist (node (soap-xml-get-children1 node 'xsd:complexType))
887 (soap-namespace-put (soap-parse-complex-type node) ns))
888
889 (dolist (node (soap-xml-get-children1 node 'xsd:element))
890 (soap-namespace-put (soap-parse-schema-element node) ns))
891
892 ns)))
893
894 (defun soap-parse-simple-type (node)
895 "Parse NODE and construct a simple type from it."
896 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:simpleType)
897 nil
898 "soap-parse-complex-type: expecting xsd:simpleType node, got %s"
899 (soap-l2wk (xml-node-name node)))
900 (let ((name (xml-get-attribute-or-nil node 'name))
901 type
902 enumeration
903 (restriction (car-safe
904 (soap-xml-get-children1 node 'xsd:restriction))))
905 (unless restriction
906 (error "simpleType %s has no base type" name))
907
908 (setq type (xml-get-attribute-or-nil restriction 'base))
909 (dolist (e (soap-xml-get-children1 restriction 'xsd:enumeration))
910 (push (xml-get-attribute e 'value) enumeration))
911
912 (make-soap-simple-type :name name :kind type :enumeration enumeration)))
913
914 (defun soap-parse-schema-element (node)
915 "Parse NODE and construct a schema element from it."
916 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:element)
917 nil
918 "soap-parse-schema-element: expecting xsd:element node, got %s"
919 (soap-l2wk (xml-node-name node)))
920 (let ((name (xml-get-attribute-or-nil node 'name))
921 type)
922 ;; A schema element that contains an inline complex type --
923 ;; construct the actual complex type for it.
924 (let ((type-node (soap-xml-get-children1 node 'xsd:complexType)))
925 (when (> (length type-node) 0)
926 (assert (= (length type-node) 1)) ; only one complex type
927 ; definition per element
928 (setq type (soap-parse-complex-type (car type-node)))))
929 (setf (soap-element-name type) name)
930 type))
931
932 (defun soap-parse-complex-type (node)
933 "Parse NODE and construct a complex type from it."
934 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:complexType)
935 nil
936 "soap-parse-complex-type: expecting xsd:complexType node, got %s"
937 (soap-l2wk (xml-node-name node)))
938 (let ((name (xml-get-attribute-or-nil node 'name))
939 ;; Use a dummy type for the complex type, it will be replaced
940 ;; with the real type below, except when the complex type node
941 ;; is empty...
942 (type (make-soap-sequence-type :elements nil)))
943 (dolist (c (xml-node-children node))
944 (when (consp c) ; skip string nodes, which are whitespace
945 (let ((node-name (soap-l2wk (xml-node-name c))))
946 (cond
947 ;; The difference between xsd:all and xsd:sequence is that fields
948 ;; in xsd:all are not ordered and they can occur only once. We
949 ;; don't care about that difference in soap-client.el
950 ((or (eq node-name 'xsd:sequence)
951 (eq node-name 'xsd:all))
952 (setq type (soap-parse-complex-type-sequence c)))
953 ((eq node-name 'xsd:complexContent)
954 (setq type (soap-parse-complex-type-complex-content c)))
955 ((eq node-name 'xsd:attribute)
956 ;; The name of this node comes from an attribute tag
957 (let ((n (xml-get-attribute-or-nil c 'name)))
958 (setq name n)))
959 (t
960 (error "Unknown node type %s" node-name))))))
961 (setf (soap-element-name type) name)
962 type))
963
964 (defun soap-parse-sequence (node)
965 "Parse NODE and a list of sequence elements that it defines.
966 NODE is assumed to be an xsd:sequence node. In that case, each
967 of its children is assumed to be a sequence element. Each
968 sequence element is parsed constructing the corresponding type.
969 A list of these types is returned."
970 (assert (let ((n (soap-l2wk (xml-node-name node))))
971 (memq n '(xsd:sequence xsd:all)))
972 nil
973 "soap-parse-sequence: expecting xsd:sequence or xsd:all node, got %s"
974 (soap-l2wk (xml-node-name node)))
975 (let (elements)
976 (dolist (e (soap-xml-get-children1 node 'xsd:element))
977 (let ((name (xml-get-attribute-or-nil e 'name))
978 (type (xml-get-attribute-or-nil e 'type))
979 (nillable? (or (equal (xml-get-attribute-or-nil e 'nillable) "true")
980 (let ((e (xml-get-attribute-or-nil e 'minOccurs)))
981 (and e (equal e "0")))))
982 (multiple? (let ((e (xml-get-attribute-or-nil e 'maxOccurs)))
983 (and e (not (equal e "1"))))))
984 (if type
985 (setq type (soap-l2fq type 'tns))
986
987 ;; The node does not have a type, maybe it has a complexType
988 ;; defined inline...
989 (let ((type-node (soap-xml-get-children1 e 'xsd:complexType)))
990 (when (> (length type-node) 0)
991 (assert (= (length type-node) 1)
992 nil
993 "only one complex type definition per element supported")
994 (setq type (soap-parse-complex-type (car type-node))))))
995
996 (push (make-soap-sequence-element
997 :name (intern name) :type type :nillable? nillable?
998 :multiple? multiple?)
999 elements)))
1000 (nreverse elements)))
1001
1002 (defun soap-parse-complex-type-sequence (node)
1003 "Parse NODE as a sequence type."
1004 (let ((elements (soap-parse-sequence node)))
1005 (make-soap-sequence-type :elements elements)))
1006
1007 (defun soap-parse-complex-type-complex-content (node)
1008 "Parse NODE as a xsd:complexContent node.
1009 A sequence or an array type is returned depending on the actual
1010 contents."
1011 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:complexContent)
1012 nil
1013 "soap-parse-complex-type-complex-content: expecting xsd:complexContent node, got %s"
1014 (soap-l2wk (xml-node-name node)))
1015 (let (array? parent elements)
1016 (let ((extension (car-safe (soap-xml-get-children1 node 'xsd:extension)))
1017 (restriction (car-safe
1018 (soap-xml-get-children1 node 'xsd:restriction))))
1019 ;; a complex content node is either an extension or a restriction
1020 (cond (extension
1021 (setq parent (xml-get-attribute-or-nil extension 'base))
1022 (setq elements (soap-parse-sequence
1023 (car (soap-xml-get-children1
1024 extension 'xsd:sequence)))))
1025 (restriction
1026 (let ((base (xml-get-attribute-or-nil restriction 'base)))
1027 (assert (equal base (soap-wk2l "soapenc:Array"))
1028 nil
1029 "restrictions supported only for soapenc:Array types, this is a %s"
1030 base))
1031 (setq array? t)
1032 (let ((attribute (car (soap-xml-get-children1
1033 restriction 'xsd:attribute))))
1034 (let ((array-type (soap-xml-get-attribute-or-nil1
1035 attribute 'wsdl:arrayType)))
1036 (when (string-match "^\\(.*\\)\\[\\]$" array-type)
1037 (setq parent (match-string 1 array-type))))))
1038
1039 (t
1040 (error "Unknown complex type"))))
1041
1042 (if parent
1043 (setq parent (soap-l2fq parent 'tns)))
1044
1045 (if array?
1046 (make-soap-array-type :element-type parent)
1047 (make-soap-sequence-type :parent parent :elements elements))))
1048
1049 (defun soap-parse-message (node)
1050 "Parse NODE as a wsdl:message and return the corresponding type."
1051 (assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:message)
1052 nil
1053 "soap-parse-message: expecting wsdl:message node, got %s"
1054 (soap-l2wk (xml-node-name node)))
1055 (let ((name (xml-get-attribute-or-nil node 'name))
1056 parts)
1057 (dolist (p (soap-xml-get-children1 node 'wsdl:part))
1058 (let ((name (xml-get-attribute-or-nil p 'name))
1059 (type (xml-get-attribute-or-nil p 'type))
1060 (element (xml-get-attribute-or-nil p 'element)))
1061
1062 (when type
1063 (setq type (soap-l2fq type 'tns)))
1064
1065 (when element
1066 (setq element (soap-l2fq element 'tns)))
1067
1068 (push (cons name (or type element)) parts)))
1069 (make-soap-message :name name :parts (nreverse parts))))
1070
1071 (defun soap-parse-port-type (node)
1072 "Parse NODE as a wsdl:portType and return the corresponding port."
1073 (assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:portType)
1074 nil
1075 "soap-parse-port-type: expecting wsdl:portType node got %s"
1076 (soap-l2wk (xml-node-name node)))
1077 (let ((ns (make-soap-namespace
1078 :name (concat "urn:" (xml-get-attribute node 'name)))))
1079 (dolist (node (soap-xml-get-children1 node 'wsdl:operation))
1080 (let ((o (soap-parse-operation node)))
1081
1082 (let ((other-operation (soap-namespace-get
1083 (soap-element-name o) ns 'soap-operation-p)))
1084 (if other-operation
1085 ;; Unfortunately, the Confluence WSDL defines two operations
1086 ;; named "search" which differ only in parameter names...
1087 (soap-warning "Discarding duplicate operation: %s"
1088 (soap-element-name o))
1089
1090 (progn
1091 (soap-namespace-put o ns)
1092
1093 ;; link all messages from this namespace, as this namespace
1094 ;; will be used for decoding the response.
1095 (destructuring-bind (name . message) (soap-operation-input o)
1096 (soap-namespace-put-link name message ns))
1097
1098 (destructuring-bind (name . message) (soap-operation-output o)
1099 (soap-namespace-put-link name message ns))
1100
1101 (dolist (fault (soap-operation-faults o))
1102 (destructuring-bind (name . message) fault
1103 (soap-namespace-put-link name message ns 'replace)))
1104
1105 )))))
1106
1107 (make-soap-port-type :name (xml-get-attribute node 'name)
1108 :operations ns)))
1109
1110 (defun soap-parse-operation (node)
1111 "Parse NODE as a wsdl:operation and return the corresponding type."
1112 (assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:operation)
1113 nil
1114 "soap-parse-operation: expecting wsdl:operation node, got %s"
1115 (soap-l2wk (xml-node-name node)))
1116 (let ((name (xml-get-attribute node 'name))
1117 (parameter-order (split-string
1118 (xml-get-attribute node 'parameterOrder)))
1119 input output faults)
1120 (dolist (n (xml-node-children node))
1121 (when (consp n) ; skip string nodes which are whitespace
1122 (let ((node-name (soap-l2wk (xml-node-name n))))
1123 (cond
1124 ((eq node-name 'wsdl:input)
1125 (let ((message (xml-get-attribute n 'message))
1126 (name (xml-get-attribute n 'name)))
1127 (setq input (cons name (soap-l2fq message 'tns)))))
1128 ((eq node-name 'wsdl:output)
1129 (let ((message (xml-get-attribute n 'message))
1130 (name (xml-get-attribute n 'name)))
1131 (setq output (cons name (soap-l2fq message 'tns)))))
1132 ((eq node-name 'wsdl:fault)
1133 (let ((message (xml-get-attribute n 'message))
1134 (name (xml-get-attribute n 'name)))
1135 (push (cons name (soap-l2fq message 'tns)) faults)))))))
1136 (make-soap-operation
1137 :name name
1138 :parameter-order parameter-order
1139 :input input
1140 :output output
1141 :faults (nreverse faults))))
1142
1143 (defun soap-parse-binding (node)
1144 "Parse NODE as a wsdl:binding and return the corresponding type."
1145 (assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:binding)
1146 nil
1147 "soap-parse-binding: expecting wsdl:binding node, got %s"
1148 (soap-l2wk (xml-node-name node)))
1149 (let ((name (xml-get-attribute node 'name))
1150 (type (xml-get-attribute node 'type)))
1151 (let ((binding (make-soap-binding :name name
1152 :port-type (soap-l2fq type 'tns))))
1153 (dolist (wo (soap-xml-get-children1 node 'wsdl:operation))
1154 (let ((name (xml-get-attribute wo 'name))
1155 soap-action
1156 use)
1157 (dolist (so (soap-xml-get-children1 wo 'wsdlsoap:operation))
1158 (setq soap-action (xml-get-attribute-or-nil so 'soapAction)))
1159
1160 ;; Search a wsdlsoap:body node and find a "use" tag. The
1161 ;; same use tag is assumed to be present for both input and
1162 ;; output types (although the WDSL spec allows separate
1163 ;; "use"-s for each of them...
1164
1165 (dolist (i (soap-xml-get-children1 wo 'wsdl:input))
1166 (dolist (b (soap-xml-get-children1 i 'wsdlsoap:body))
1167 (setq use (or use
1168 (xml-get-attribute-or-nil b 'use)))))
1169
1170 (unless use
1171 (dolist (i (soap-xml-get-children1 wo 'wsdl:output))
1172 (dolist (b (soap-xml-get-children1 i 'wsdlsoap:body))
1173 (setq use (or use
1174 (xml-get-attribute-or-nil b 'use))))))
1175
1176 (puthash name (make-soap-bound-operation :operation name
1177 :soap-action soap-action
1178 :use (and use (intern use)))
1179 (soap-binding-operations binding))))
1180 binding)))
1181
1182 ;;;; SOAP type decoding
1183
1184 (defvar soap-multi-refs nil
1185 "The list of multi-ref nodes in the current SOAP response.
1186 This is a dynamically bound variable used during decoding the
1187 SOAP response.")
1188
1189 (defvar soap-decoded-multi-refs nil
1190 "List of decoded multi-ref nodes in the current SOAP response.
1191 This is a dynamically bound variable used during decoding the
1192 SOAP response.")
1193
1194 (defvar soap-current-wsdl nil
1195 "The current WSDL document used when decoding the SOAP response.
1196 This is a dynamically bound variable.")
1197
1198 (defun soap-decode-type (type node)
1199 "Use TYPE (an xsd type) to decode the contents of NODE.
1200
1201 NODE is an XML node, representing some SOAP encoded value or a
1202 reference to another XML node (a multiRef). This function will
1203 resolve the multiRef reference, if any, than call a TYPE specific
1204 decode function to perform the actual decoding."
1205 (let ((href (xml-get-attribute-or-nil node 'href)))
1206 (cond (href
1207 (catch 'done
1208 ;; NODE is actually a HREF, find the target and decode that.
1209 ;; Check first if we already decoded this multiref.
1210
1211 (let ((decoded (cdr (assoc href soap-decoded-multi-refs))))
1212 (when decoded
1213 (throw 'done decoded)))
1214
1215 (string-match "^#\\(.*\\)$" href) ; TODO: check that it matched
1216
1217 (let ((id (match-string 1 href)))
1218 (dolist (mr soap-multi-refs)
1219 (let ((mrid (xml-get-attribute mr 'id)))
1220 (when (equal id mrid)
1221 ;; recurse here, in case there are multiple HREF's
1222 (let ((decoded (soap-decode-type type mr)))
1223 (push (cons href decoded) soap-decoded-multi-refs)
1224 (throw 'done decoded)))))
1225 (error "Cannot find href %s" href))))
1226 (t
1227 (soap-with-local-xmlns node
1228 (if (equal (soap-xml-get-attribute-or-nil1 node 'xsi:nil) "true")
1229 nil
1230 (let ((decoder (get (aref type 0) 'soap-decoder)))
1231 (assert decoder nil "no soap-decoder for %s type"
1232 (aref type 0))
1233 (funcall decoder type node))))))))
1234
1235 (defun soap-decode-any-type (node)
1236 "Decode NODE using type information inside it."
1237 ;; If the NODE has type information, we use that...
1238 (let ((type (soap-xml-get-attribute-or-nil1 node 'xsi:type)))
1239 (if type
1240 (let ((wtype (soap-wsdl-get type soap-current-wsdl 'soap-type-p)))
1241 (if wtype
1242 (soap-decode-type wtype node)
1243 ;; The node has type info encoded in it, but we don't know how
1244 ;; to decode it...
1245 (error "Soap-decode-any-type: node has unknown type: %s" type)))
1246
1247 ;; No type info in the node...
1248
1249 (let ((contents (xml-node-children node)))
1250 (if (and (= (length contents) 1) (stringp (car contents)))
1251 ;; contents is just a string
1252 (car contents)
1253
1254 ;; we assume the NODE is a sequence with every element a
1255 ;; structure name
1256 (let (result)
1257 (dolist (element contents)
1258 (let ((key (xml-node-name element))
1259 (value (soap-decode-any-type element)))
1260 (push (cons key value) result)))
1261 (nreverse result)))))))
1262
1263 (defun soap-decode-array (node)
1264 "Decode NODE as an Array using type information inside it."
1265 (let ((type (soap-xml-get-attribute-or-nil1 node 'soapenc:arrayType))
1266 (wtype nil)
1267 (contents (xml-node-children node))
1268 result)
1269 (when type
1270 ;; Type is in the format "someType[NUM]" where NUM is the number of
1271 ;; elements in the array. We discard the [NUM] part.
1272 (setq type (replace-regexp-in-string "\\[[0-9]+\\]\\'" "" type))
1273 (setq wtype (soap-wsdl-get type soap-current-wsdl 'soap-type-p))
1274 (unless wtype
1275 ;; The node has type info encoded in it, but we don't know how to
1276 ;; decode it...
1277 (error "Soap-decode-array: node has unknown type: %s" type)))
1278 (dolist (e contents)
1279 (when (consp e)
1280 (push (if wtype
1281 (soap-decode-type wtype e)
1282 (soap-decode-any-type e))
1283 result)))
1284 (nreverse result)))
1285
1286 (defun soap-decode-basic-type (type node)
1287 "Use TYPE to decode the contents of NODE.
1288 TYPE is a `soap-basic-type' struct, and NODE is an XML document.
1289 A LISP value is returned based on the contents of NODE and the
1290 type-info stored in TYPE."
1291 (let ((contents (xml-node-children node))
1292 (type-kind (soap-basic-type-kind type)))
1293
1294 (if (null contents)
1295 nil
1296 (ecase type-kind
1297 ((string anyURI) (car contents))
1298 (dateTime (car contents)) ; TODO: convert to a date time
1299 ((long int integer unsignedInt byte float double) (string-to-number (car contents)))
1300 (boolean (string= (downcase (car contents)) "true"))
1301 (base64Binary (base64-decode-string (car contents)))
1302 (anyType (soap-decode-any-type node))
1303 (Array (soap-decode-array node))))))
1304
1305 (defun soap-decode-sequence-type (type node)
1306 "Use TYPE to decode the contents of NODE.
1307 TYPE is assumed to be a sequence type and an ALIST with the
1308 contents of the NODE is returned."
1309 (let ((result nil)
1310 (parent (soap-sequence-type-parent type)))
1311 (when parent
1312 (setq result (nreverse (soap-decode-type parent node))))
1313 (dolist (element (soap-sequence-type-elements type))
1314 (let ((instance-count 0)
1315 (e-name (soap-sequence-element-name element))
1316 (e-type (soap-sequence-element-type element)))
1317 (dolist (node (xml-get-children node e-name))
1318 (incf instance-count)
1319 (push (cons e-name (soap-decode-type e-type node)) result))
1320 ;; Do some sanity checking
1321 (cond ((and (= instance-count 0)
1322 (not (soap-sequence-element-nillable? element)))
1323 (soap-warning "While decoding %s: missing non-nillable slot %s"
1324 (soap-element-name type) e-name))
1325 ((and (> instance-count 1)
1326 (not (soap-sequence-element-multiple? element)))
1327 (soap-warning "While decoding %s: multiple slots named %s"
1328 (soap-element-name type) e-name)))))
1329 (nreverse result)))
1330
1331 (defun soap-decode-array-type (type node)
1332 "Use TYPE to decode the contents of NODE.
1333 TYPE is assumed to be an array type. Arrays are decoded as lists.
1334 This is because it is easier to work with list results in LISP."
1335 (let ((result nil)
1336 (element-type (soap-array-type-element-type type)))
1337 (dolist (node (xml-node-children node))
1338 (when (consp node)
1339 (push (soap-decode-type element-type node) result)))
1340 (nreverse result)))
1341
1342 (progn
1343 (put (aref (make-soap-basic-type) 0)
1344 'soap-decoder 'soap-decode-basic-type)
1345 ;; just use the basic type decoder for the simple type -- we accept any
1346 ;; value and don't do any validation on it.
1347 (put (aref (make-soap-simple-type) 0)
1348 'soap-decoder 'soap-decode-basic-type)
1349 (put (aref (make-soap-sequence-type) 0)
1350 'soap-decoder 'soap-decode-sequence-type)
1351 (put (aref (make-soap-array-type) 0)
1352 'soap-decoder 'soap-decode-array-type))
1353
1354 ;;;; Soap Envelope parsing
1355
1356 (define-error 'soap-error "SOAP error")
1357
1358 (defun soap-parse-envelope (node operation wsdl)
1359 "Parse the SOAP envelope in NODE and return the response.
1360 OPERATION is the WSDL operation for which we expect the response,
1361 WSDL is used to decode the NODE"
1362 (soap-with-local-xmlns node
1363 (assert (eq (soap-l2wk (xml-node-name node)) 'soap:Envelope)
1364 nil
1365 "soap-parse-envelope: expecting soap:Envelope node, got %s"
1366 (soap-l2wk (xml-node-name node)))
1367 (let ((body (car (soap-xml-get-children1 node 'soap:Body))))
1368
1369 (let ((fault (car (soap-xml-get-children1 body 'soap:Fault))))
1370 (when fault
1371 (let ((fault-code (let ((n (car (xml-get-children
1372 fault 'faultcode))))
1373 (car-safe (xml-node-children n))))
1374 (fault-string (let ((n (car (xml-get-children
1375 fault 'faultstring))))
1376 (car-safe (xml-node-children n))))
1377 (detail (xml-get-children fault 'detail)))
1378 (while t
1379 (signal 'soap-error (list fault-code fault-string detail))))))
1380
1381 ;; First (non string) element of the body is the root node of he
1382 ;; response
1383 (let ((response (if (eq (soap-bound-operation-use operation) 'literal)
1384 ;; For 'literal uses, the response is the actual body
1385 body
1386 ;; ...otherwise the first non string element
1387 ;; of the body is the response
1388 (catch 'found
1389 (dolist (n (xml-node-children body))
1390 (when (consp n)
1391 (throw 'found n)))))))
1392 (soap-parse-response response operation wsdl body)))))
1393
1394 (defun soap-parse-response (response-node operation wsdl soap-body)
1395 "Parse RESPONSE-NODE and return the result as a LISP value.
1396 OPERATION is the WSDL operation for which we expect the response,
1397 WSDL is used to decode the NODE.
1398
1399 SOAP-BODY is the body of the SOAP envelope (of which
1400 RESPONSE-NODE is a sub-node). It is used in case RESPONSE-NODE
1401 reference multiRef parts which are external to RESPONSE-NODE."
1402 (let* ((soap-current-wsdl wsdl)
1403 (op (soap-bound-operation-operation operation))
1404 (use (soap-bound-operation-use operation))
1405 (message (cdr (soap-operation-output op))))
1406
1407 (soap-with-local-xmlns response-node
1408
1409 (when (eq use 'encoded)
1410 (let* ((received-message-name (soap-l2fq (xml-node-name response-node)))
1411 (received-message (soap-wsdl-get
1412 received-message-name wsdl 'soap-message-p)))
1413 (unless (eq received-message message)
1414 (error "Unexpected message: got %s, expecting %s"
1415 received-message-name
1416 (soap-element-name message)))))
1417
1418 (let ((decoded-parts nil)
1419 (soap-multi-refs (xml-get-children soap-body 'multiRef))
1420 (soap-decoded-multi-refs nil))
1421
1422 (dolist (part (soap-message-parts message))
1423 (let ((tag (car part))
1424 (type (cdr part))
1425 node)
1426
1427 (setq node
1428 (cond
1429 ((eq use 'encoded)
1430 (car (xml-get-children response-node tag)))
1431
1432 ((eq use 'literal)
1433 (catch 'found
1434 (let* ((ns-aliases (soap-wsdl-alias-table wsdl))
1435 (ns-name (cdr (assoc
1436 (soap-element-namespace-tag type)
1437 ns-aliases)))
1438 (fqname (cons ns-name (soap-element-name type))))
1439 (dolist (c (xml-node-children response-node))
1440 (when (consp c)
1441 (soap-with-local-xmlns c
1442 (when (equal (soap-l2fq (xml-node-name c))
1443 fqname)
1444 (throw 'found c))))))))))
1445
1446 (unless node
1447 (error "Soap-parse-response(%s): cannot find message part %s"
1448 (soap-element-name op) tag))
1449 (push (soap-decode-type type node) decoded-parts)))
1450
1451 decoded-parts))))
1452
1453 ;;;; SOAP type encoding
1454
1455 (defvar soap-encoded-namespaces nil
1456 "A list of namespace tags used during encoding a message.
1457 This list is populated by `soap-encode-value' and used by
1458 `soap-create-envelope' to add aliases for these namespace to the
1459 XML request.
1460
1461 This variable is dynamically bound in `soap-create-envelope'.")
1462
1463 (defun soap-encode-value (xml-tag value type)
1464 "Encode inside an XML-TAG the VALUE using TYPE.
1465 The resulting XML data is inserted in the current buffer
1466 at (point)/
1467
1468 TYPE is one of the soap-*-type structures which defines how VALUE
1469 is to be encoded. This is a generic function which finds an
1470 encoder function based on TYPE and calls that encoder to do the
1471 work."
1472 (let ((encoder (get (aref type 0) 'soap-encoder)))
1473 (assert encoder nil "no soap-encoder for %s type" (aref type 0))
1474 ;; XML-TAG can be a string or a symbol, but we pass only string's to the
1475 ;; encoders
1476 (when (symbolp xml-tag)
1477 (setq xml-tag (symbol-name xml-tag)))
1478 (funcall encoder xml-tag value type))
1479 (add-to-list 'soap-encoded-namespaces (soap-element-namespace-tag type)))
1480
1481 (defun soap-encode-basic-type (xml-tag value type)
1482 "Encode inside XML-TAG the LISP VALUE according to TYPE.
1483 Do not call this function directly, use `soap-encode-value'
1484 instead."
1485 (let ((xsi-type (soap-element-fq-name type))
1486 (basic-type (soap-basic-type-kind type)))
1487
1488 ;; try to classify the type based on the value type and use that type when
1489 ;; encoding
1490 (when (eq basic-type 'anyType)
1491 (cond ((stringp value)
1492 (setq xsi-type "xsd:string" basic-type 'string))
1493 ((integerp value)
1494 (setq xsi-type "xsd:int" basic-type 'int))
1495 ((memq value '(t nil))
1496 (setq xsi-type "xsd:boolean" basic-type 'boolean))
1497 (t
1498 (error
1499 "Soap-encode-basic-type(%s, %s, %s): cannot classify anyType value"
1500 xml-tag value xsi-type))))
1501
1502 (insert "<" xml-tag " xsi:type=\"" xsi-type "\"")
1503
1504 ;; We have some ambiguity here, as a nil value represents "false" when the
1505 ;; type is boolean, we will never have a "nil" boolean type...
1506
1507 (if (or value (eq basic-type 'boolean))
1508 (progn
1509 (insert ">")
1510 (case basic-type
1511 ((string anyURI)
1512 (unless (stringp value)
1513 (error "Soap-encode-basic-type(%s, %s, %s): not a string value"
1514 xml-tag value xsi-type))
1515 (insert (url-insert-entities-in-string value)))
1516
1517 (dateTime
1518 (cond ((and (consp value) ; is there a time-value-p ?
1519 (>= (length value) 2)
1520 (numberp (nth 0 value))
1521 (numberp (nth 1 value)))
1522 ;; Value is a (current-time) style value, convert
1523 ;; to a string
1524 (insert (format-time-string "%Y-%m-%dT%H:%M:%S" value)))
1525 ((stringp value)
1526 (insert (url-insert-entities-in-string value)))
1527 (t
1528 (error
1529 "Soap-encode-basic-type(%s, %s, %s): not a dateTime value"
1530 xml-tag value xsi-type))))
1531
1532 (boolean
1533 (unless (memq value '(t nil))
1534 (error "Soap-encode-basic-type(%s, %s, %s): not a boolean value"
1535 xml-tag value xsi-type))
1536 (insert (if value "true" "false")))
1537
1538 ((long int integer byte unsignedInt)
1539 (unless (integerp value)
1540 (error "Soap-encode-basic-type(%s, %s, %s): not an integer value"
1541 xml-tag value xsi-type))
1542 (when (and (eq basic-type 'unsignedInt) (< value 0))
1543 (error "Soap-encode-basic-type(%s, %s, %s): not a positive integer"
1544 xml-tag value xsi-type))
1545 (insert (number-to-string value)))
1546
1547 ((float double)
1548 (unless (numberp value)
1549 (error "Soap-encode-basic-type(%s, %s, %s): not a number"
1550 xml-tag value xsi-type))
1551 (insert (number-to-string value)))
1552
1553 (base64Binary
1554 (unless (stringp value)
1555 (error "Soap-encode-basic-type(%s, %s, %s): not a string value"
1556 xml-tag value xsi-type))
1557 (insert (base64-encode-string value)))
1558
1559 (otherwise
1560 (error
1561 "Soap-encode-basic-type(%s, %s, %s): don't know how to encode"
1562 xml-tag value xsi-type))))
1563
1564 (insert " xsi:nil=\"true\">"))
1565 (insert "</" xml-tag ">\n")))
1566
1567 (defun soap-encode-simple-type (xml-tag value type)
1568 "Encode inside XML-TAG the LISP VALUE according to TYPE."
1569
1570 ;; Validate VALUE against the simple type's enumeration, than just encode it
1571 ;; using `soap-encode-basic-type'
1572
1573 (let ((enumeration (soap-simple-type-enumeration type)))
1574 (unless (and (> (length enumeration) 1)
1575 (member value enumeration))
1576 (error "soap-encode-simple-type(%s, %s, %s): bad value, should be one of %s"
1577 xml-tag value (soap-element-fq-name type) enumeration)))
1578
1579 (soap-encode-basic-type xml-tag value type))
1580
1581 (defun soap-encode-sequence-type (xml-tag value type)
1582 "Encode inside XML-TAG the LISP VALUE according to TYPE.
1583 Do not call this function directly, use `soap-encode-value'
1584 instead."
1585 (let ((xsi-type (soap-element-fq-name type)))
1586 (insert "<" xml-tag " xsi:type=\"" xsi-type "\"")
1587 (if value
1588 (progn
1589 (insert ">\n")
1590 (let ((parents (list type))
1591 (parent (soap-sequence-type-parent type)))
1592
1593 (while parent
1594 (push parent parents)
1595 (setq parent (soap-sequence-type-parent parent)))
1596
1597 (dolist (type parents)
1598 (dolist (element (soap-sequence-type-elements type))
1599 (let ((instance-count 0)
1600 (e-name (soap-sequence-element-name element))
1601 (e-type (soap-sequence-element-type element)))
1602 (dolist (v value)
1603 (when (equal (car v) e-name)
1604 (incf instance-count)
1605 (soap-encode-value e-name (cdr v) e-type)))
1606
1607 ;; Do some sanity checking
1608 (cond ((and (= instance-count 0)
1609 (not (soap-sequence-element-nillable? element)))
1610 (soap-warning
1611 "While encoding %s: missing non-nillable slot %s"
1612 (soap-element-name type) e-name))
1613 ((and (> instance-count 1)
1614 (not (soap-sequence-element-multiple? element)))
1615 (soap-warning
1616 "While encoding %s: multiple slots named %s"
1617 (soap-element-name type) e-name))))))))
1618 (insert " xsi:nil=\"true\">"))
1619 (insert "</" xml-tag ">\n")))
1620
1621 (defun soap-encode-array-type (xml-tag value type)
1622 "Encode inside XML-TAG the LISP VALUE according to TYPE.
1623 Do not call this function directly, use `soap-encode-value'
1624 instead."
1625 (unless (vectorp value)
1626 (error "Soap-encode: %s(%s) expects a vector, got: %s"
1627 xml-tag (soap-element-fq-name type) value))
1628 (let* ((element-type (soap-array-type-element-type type))
1629 (array-type (concat (soap-element-fq-name element-type)
1630 "[" (format "%s" (length value)) "]")))
1631 (insert "<" xml-tag
1632 " soapenc:arrayType=\"" array-type "\" "
1633 " xsi:type=\"soapenc:Array\">\n")
1634 (loop for i below (length value)
1635 do (soap-encode-value xml-tag (aref value i) element-type))
1636 (insert "</" xml-tag ">\n")))
1637
1638 (progn
1639 (put (aref (make-soap-basic-type) 0)
1640 'soap-encoder 'soap-encode-basic-type)
1641 (put (aref (make-soap-simple-type) 0)
1642 'soap-encoder 'soap-encode-simple-type)
1643 (put (aref (make-soap-sequence-type) 0)
1644 'soap-encoder 'soap-encode-sequence-type)
1645 (put (aref (make-soap-array-type) 0)
1646 'soap-encoder 'soap-encode-array-type))
1647
1648 (defun soap-encode-body (operation parameters wsdl)
1649 "Create the body of a SOAP request for OPERATION in the current buffer.
1650 PARAMETERS is a list of parameters supplied to the OPERATION.
1651
1652 The OPERATION and PARAMETERS are encoded according to the WSDL
1653 document."
1654 (let* ((op (soap-bound-operation-operation operation))
1655 (use (soap-bound-operation-use operation))
1656 (message (cdr (soap-operation-input op)))
1657 (parameter-order (soap-operation-parameter-order op)))
1658
1659 (unless (= (length parameter-order) (length parameters))
1660 (error "Wrong number of parameters for %s: expected %d, got %s"
1661 (soap-element-name op)
1662 (length parameter-order)
1663 (length parameters)))
1664
1665 (insert "<soap:Body>\n")
1666 (when (eq use 'encoded)
1667 (add-to-list 'soap-encoded-namespaces (soap-element-namespace-tag op))
1668 (insert "<" (soap-element-fq-name op) ">\n"))
1669
1670 (let ((param-table (loop for formal in parameter-order
1671 for value in parameters
1672 collect (cons formal value))))
1673 (dolist (part (soap-message-parts message))
1674 (let* ((param-name (car part))
1675 (type (cdr part))
1676 (tag-name (if (eq use 'encoded)
1677 param-name
1678 (soap-element-name type)))
1679 (value (cdr (assoc param-name param-table)))
1680 (start-pos (point)))
1681 (soap-encode-value tag-name value type)
1682 (when (eq use 'literal)
1683 ;; hack: add the xmlns attribute to the tag, the only way
1684 ;; ASP.NET web services recognize the namespace of the
1685 ;; element itself...
1686 (save-excursion
1687 (goto-char start-pos)
1688 (when (re-search-forward " ")
1689 (let* ((ns (soap-element-namespace-tag type))
1690 (namespace (cdr (assoc ns
1691 (soap-wsdl-alias-table wsdl)))))
1692 (when namespace
1693 (insert "xmlns=\"" namespace "\" ")))))))))
1694
1695 (when (eq use 'encoded)
1696 (insert "</" (soap-element-fq-name op) ">\n"))
1697 (insert "</soap:Body>\n")))
1698
1699 (defun soap-create-envelope (operation parameters wsdl)
1700 "Create a SOAP request envelope for OPERATION using PARAMETERS.
1701 WSDL is the wsdl document used to encode the PARAMETERS."
1702 (with-temp-buffer
1703 (let ((soap-encoded-namespaces '("xsi" "soap" "soapenc"))
1704 (use (soap-bound-operation-use operation)))
1705
1706 ;; Create the request body
1707 (soap-encode-body operation parameters wsdl)
1708
1709 ;; Put the envelope around the body
1710 (goto-char (point-min))
1711 (insert "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<soap:Envelope\n")
1712 (when (eq use 'encoded)
1713 (insert " soapenc:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\n"))
1714 (dolist (nstag soap-encoded-namespaces)
1715 (insert " xmlns:" nstag "=\"")
1716 (let ((nsname (cdr (assoc nstag soap-well-known-xmlns))))
1717 (unless nsname
1718 (setq nsname (cdr (assoc nstag (soap-wsdl-alias-table wsdl)))))
1719 (insert nsname)
1720 (insert "\"\n")))
1721 (insert ">\n")
1722 (goto-char (point-max))
1723 (insert "</soap:Envelope>\n"))
1724
1725 (buffer-string)))
1726
1727 ;;;; invoking soap methods
1728
1729 (defcustom soap-debug nil
1730 "When t, enable some debugging facilities."
1731 :type 'boolean
1732 :group 'soap-client)
1733
1734 (defun soap-invoke (wsdl service operation-name &rest parameters)
1735 "Invoke a SOAP operation and return the result.
1736
1737 WSDL is used for encoding the request and decoding the response.
1738 It also contains information about the WEB server address that
1739 will service the request.
1740
1741 SERVICE is the SOAP service to invoke.
1742
1743 OPERATION-NAME is the operation to invoke.
1744
1745 PARAMETERS -- the remaining parameters are used as parameters for
1746 the SOAP request.
1747
1748 NOTE: The SOAP service provider should document the available
1749 operations and their parameters for the service. You can also
1750 use the `soap-inspect' function to browse the available
1751 operations in a WSDL document."
1752 (let ((port (catch 'found
1753 (dolist (p (soap-wsdl-ports wsdl))
1754 (when (equal service (soap-element-name p))
1755 (throw 'found p))))))
1756 (unless port
1757 (error "Unknown SOAP service: %s" service))
1758
1759 (let* ((binding (soap-port-binding port))
1760 (operation (gethash operation-name
1761 (soap-binding-operations binding))))
1762 (unless operation
1763 (error "No operation %s for SOAP service %s" operation-name service))
1764
1765 (let ((url-request-method "POST")
1766 (url-package-name "soap-client.el")
1767 (url-package-version "1.0")
1768 (url-http-version "1.0")
1769 (url-request-data
1770 ;; url-request-data expects a unibyte string already encoded...
1771 (encode-coding-string
1772 (soap-create-envelope operation parameters wsdl)
1773 'utf-8))
1774 (url-mime-charset-string "utf-8;q=1, iso-8859-1;q=0.5")
1775 (url-request-coding-system 'utf-8)
1776 (url-http-attempt-keepalives t)
1777 (url-request-extra-headers (list
1778 (cons "SOAPAction"
1779 (soap-bound-operation-soap-action
1780 operation))
1781 (cons "Content-Type"
1782 "text/xml; charset=utf-8"))))
1783 (let ((buffer (url-retrieve-synchronously
1784 (soap-port-service-url port))))
1785 (condition-case err
1786 (with-current-buffer buffer
1787 (declare (special url-http-response-status))
1788 (if (null url-http-response-status)
1789 (error "No HTTP response from server"))
1790 (if (and soap-debug (> url-http-response-status 299))
1791 ;; This is a warning because some SOAP errors come
1792 ;; back with a HTTP response 500 (internal server
1793 ;; error)
1794 (warn "Error in SOAP response: HTTP code %s"
1795 url-http-response-status))
1796 (let ((mime-part (mm-dissect-buffer t t)))
1797 (unless mime-part
1798 (error "Failed to decode response from server"))
1799 (unless (equal (car (mm-handle-type mime-part)) "text/xml")
1800 (error "Server response is not an XML document"))
1801 (with-temp-buffer
1802 (mm-insert-part mime-part)
1803 (let ((response (car (xml-parse-region
1804 (point-min) (point-max)))))
1805 (prog1
1806 (soap-parse-envelope response operation wsdl)
1807 (kill-buffer buffer)
1808 (mm-destroy-part mime-part))))))
1809 (soap-error
1810 ;; Propagate soap-errors -- they are error replies of the
1811 ;; SOAP protocol and don't indicate a communication
1812 ;; problem or a bug in this code.
1813 (signal (car err) (cdr err)))
1814 (error
1815 (when soap-debug
1816 (pop-to-buffer buffer))
1817 (error (error-message-string err)))))))))
1818
1819 (provide 'soap-client)
1820
1821 \f
1822 ;; Local Variables:
1823 ;; eval: (outline-minor-mode 1)
1824 ;; outline-regexp: ";;;;+"
1825 ;; End:
1826
1827 ;;; soap-client.el ends here