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