Merge changes from emacs-23 branch
[emacs.git] / lisp / net / soap-client.el
blob42c698876cdfd40dcbf200d832fd75e0f0b3a244
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)))))
733 wsdl)
735 ;;;;; Loading WSDL from XML documents
737 (defun soap-load-wsdl-from-url (url)
738 "Load a WSDL document from URL and return it.
739 The returned WSDL document needs to be used for `soap-invoke'
740 calls."
741 (let ((url-request-method "GET")
742 (url-package-name "soap-client.el")
743 (url-package-version "1.0")
744 (url-mime-charset-string "utf-8;q=1, iso-8859-1;q=0.5")
745 (url-request-coding-system 'utf-8)
746 (url-http-attempt-keepalives nil))
747 (let ((buffer (url-retrieve-synchronously url)))
748 (with-current-buffer buffer
749 (declare (special url-http-response-status))
750 (if (> url-http-response-status 299)
751 (error "Error retrieving WSDL: %s" url-http-response-status))
752 (let ((mime-part (mm-dissect-buffer t t)))
753 (unless mime-part
754 (error "Failed to decode response from server"))
755 (unless (equal (car (mm-handle-type mime-part)) "text/xml")
756 (error "Server response is not an XML document"))
757 (with-temp-buffer
758 (mm-insert-part mime-part)
759 (let ((wsdl-xml (car (xml-parse-region (point-min) (point-max)))))
760 (prog1
761 (let ((wsdl (soap-parse-wsdl wsdl-xml)))
762 (setf (soap-wsdl-origin wsdl) url)
763 wsdl)
764 (kill-buffer buffer)))))))))
766 (defun soap-load-wsdl (file)
767 "Load a WSDL document from FILE and return it."
768 (with-temp-buffer
769 (insert-file-contents file)
770 (let ((xml (car (xml-parse-region (point-min) (point-max)))))
771 (let ((wsdl (soap-parse-wsdl xml)))
772 (setf (soap-wsdl-origin wsdl) file)
773 wsdl))))
775 (defun soap-parse-wsdl (node)
776 "Construct a WSDL structure from NODE, which is an XML document."
777 (soap-with-local-xmlns node
779 (assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:definitions)
781 "soap-parse-wsdl: expecting wsdl:definitions node, got %s"
782 (soap-l2wk (xml-node-name node)))
784 (let ((wsdl (make-soap-wsdl)))
786 ;; Add the local alias table to the wsdl document -- it will be used for
787 ;; all types in this document even after we finish parsing it.
788 (setf (soap-wsdl-alias-table wsdl) soap-local-xmlns)
790 ;; Add the XSD types to the wsdl document
791 (let ((ns (soap-default-xsd-types)))
792 (soap-wsdl-add-namespace ns wsdl)
793 (soap-wsdl-add-alias "xsd" (soap-namespace-name ns) wsdl))
795 ;; Add the soapenc types to the wsdl document
796 (let ((ns (soap-default-soapenc-types)))
797 (soap-wsdl-add-namespace ns wsdl)
798 (soap-wsdl-add-alias "soapenc" (soap-namespace-name ns) wsdl))
800 ;; Find all the 'xsd:schema nodes which are children of wsdl:types nodes
801 ;; and build our type-library
803 (let ((types (car (soap-xml-get-children1 node 'wsdl:types))))
804 (dolist (node (xml-node-children types))
805 ;; We cannot use (xml-get-children node (soap-wk2l 'xsd:schema))
806 ;; because each node can install its own alias type so the schema
807 ;; nodes might have a different prefix.
808 (when (consp node)
809 (soap-with-local-xmlns node
810 (when (eq (soap-l2wk (xml-node-name node)) 'xsd:schema)
811 (soap-wsdl-add-namespace (soap-parse-schema node) wsdl))))))
813 (let ((ns (make-soap-namespace :name (soap-get-target-namespace node))))
814 (dolist (node (soap-xml-get-children1 node 'wsdl:message))
815 (soap-namespace-put (soap-parse-message node) ns))
817 (dolist (node (soap-xml-get-children1 node 'wsdl:portType))
818 (let ((port-type (soap-parse-port-type node)))
819 (soap-namespace-put port-type ns)
820 (soap-wsdl-add-namespace
821 (soap-port-type-operations port-type) wsdl)))
823 (dolist (node (soap-xml-get-children1 node 'wsdl:binding))
824 (soap-namespace-put (soap-parse-binding node) ns))
826 (dolist (node (soap-xml-get-children1 node 'wsdl:service))
827 (dolist (node (soap-xml-get-children1 node 'wsdl:port))
828 (let ((name (xml-get-attribute node 'name))
829 (binding (xml-get-attribute node 'binding))
830 (url (let ((n (car (soap-xml-get-children1
831 node 'wsdlsoap:address))))
832 (xml-get-attribute n 'location))))
833 (let ((port (make-soap-port
834 :name name :binding (soap-l2fq binding 'tns)
835 :service-url url)))
836 (soap-namespace-put port ns)
837 (push port (soap-wsdl-ports wsdl))))))
839 (soap-wsdl-add-namespace ns wsdl))
841 (soap-wsdl-resolve-references wsdl)
843 wsdl)))
845 (defun soap-parse-schema (node)
846 "Parse a schema NODE.
847 Return a SOAP-NAMESPACE containing the elements."
848 (soap-with-local-xmlns node
849 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:schema)
851 "soap-parse-schema: expecting an xsd:schema node, got %s"
852 (soap-l2wk (xml-node-name node)))
853 (let ((ns (make-soap-namespace :name (soap-get-target-namespace node))))
854 ;; NOTE: we only extract the complexTypes from the schema, we wouldn't
855 ;; know how to handle basic types beyond the built in ones anyway.
856 (dolist (node (soap-xml-get-children1 node 'xsd:complexType))
857 (soap-namespace-put (soap-parse-complex-type node) ns))
859 (dolist (node (soap-xml-get-children1 node 'xsd:element))
860 (soap-namespace-put (soap-parse-schema-element node) ns))
862 ns)))
864 (defun soap-parse-schema-element (node)
865 "Parse NODE and construct a schema element from it."
866 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:element)
868 "soap-parse-schema-element: expecting xsd:element node, got %s"
869 (soap-l2wk (xml-node-name node)))
870 (let ((name (xml-get-attribute-or-nil node 'name))
871 type)
872 ;; A schema element that contains an inline complex type --
873 ;; construct the actual complex type for it.
874 (let ((type-node (soap-xml-get-children1 node 'xsd:complexType)))
875 (when (> (length type-node) 0)
876 (assert (= (length type-node) 1)) ; only one complex type
877 ; definition per element
878 (setq type (soap-parse-complex-type (car type-node)))))
879 (setf (soap-element-name type) name)
880 type))
882 (defun soap-parse-complex-type (node)
883 "Parse NODE and construct a complex type from it."
884 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:complexType)
886 "soap-parse-complex-type: expecting xsd:complexType node, got %s"
887 (soap-l2wk (xml-node-name node)))
888 (let ((name (xml-get-attribute-or-nil node 'name))
889 ;; Use a dummy type for the complex type, it will be replaced
890 ;; with the real type below, except when the complex type node
891 ;; is empty...
892 (type (make-soap-sequence-type :elements nil)))
893 (dolist (c (xml-node-children node))
894 (when (consp c) ; skip string nodes, which are whitespace
895 (let ((node-name (soap-l2wk (xml-node-name c))))
896 (cond
897 ;; The difference between xsd:all and xsd:sequence is that fields
898 ;; in xsd:all are not ordered and they can occur only once. We
899 ;; don't care about that difference in soap-client.el
900 ((or (eq node-name 'xsd:sequence)
901 (eq node-name 'xsd:all))
902 (setq type (soap-parse-complex-type-sequence c)))
903 ((eq node-name 'xsd:complexContent)
904 (setq type (soap-parse-complex-type-complex-content c)))
905 ((eq node-name 'xsd:attribute)
906 ;; The name of this node comes from an attribute tag
907 (let ((n (xml-get-attribute-or-nil c 'name)))
908 (setq name n)))
910 (error "Unknown node type %s" node-name))))))
911 (setf (soap-element-name type) name)
912 type))
914 (defun soap-parse-sequence (node)
915 "Parse NODE and a list of sequence elements that it defines.
916 NODE is assumed to be an xsd:sequence node. In that case, each
917 of its children is assumed to be a sequence element. Each
918 sequence element is parsed constructing the corresponding type.
919 A list of these types is returned."
920 (assert (let ((n (soap-l2wk (xml-node-name node))))
921 (memq n '(xsd:sequence xsd:all)))
923 "soap-parse-sequence: expecting xsd:sequence or xsd:all node, got %s"
924 (soap-l2wk (xml-node-name node)))
925 (let (elements)
926 (dolist (e (soap-xml-get-children1 node 'xsd:element))
927 (let ((name (xml-get-attribute-or-nil e 'name))
928 (type (xml-get-attribute-or-nil e 'type))
929 (nillable? (or (equal (xml-get-attribute-or-nil e 'nillable) "true")
930 (let ((e (xml-get-attribute-or-nil e 'minOccurs)))
931 (and e (equal e "0")))))
932 (multiple? (let ((e (xml-get-attribute-or-nil e 'maxOccurs)))
933 (and e (not (equal e "1"))))))
934 (if type
935 (setq type (soap-l2fq type 'tns))
937 ;; The node does not have a type, maybe it has a complexType
938 ;; defined inline...
939 (let ((type-node (soap-xml-get-children1 e 'xsd:complexType)))
940 (when (> (length type-node) 0)
941 (assert (= (length type-node) 1)
943 "only one complex type definition per element supported")
944 (setq type (soap-parse-complex-type (car type-node))))))
946 (push (make-soap-sequence-element
947 :name (intern name) :type type :nillable? nillable?
948 :multiple? multiple?)
949 elements)))
950 (nreverse elements)))
952 (defun soap-parse-complex-type-sequence (node)
953 "Parse NODE as a sequence type."
954 (let ((elements (soap-parse-sequence node)))
955 (make-soap-sequence-type :elements elements)))
957 (defun soap-parse-complex-type-complex-content (node)
958 "Parse NODE as a xsd:complexContent node.
959 A sequence or an array type is returned depending on the actual
960 contents."
961 (assert (eq (soap-l2wk (xml-node-name node)) 'xsd:complexContent)
963 "soap-parse-complex-type-complex-content: expecting xsd:complexContent node, got %s"
964 (soap-l2wk (xml-node-name node)))
965 (let (array? parent elements)
966 (let ((extension (car-safe (soap-xml-get-children1 node 'xsd:extension)))
967 (restriction (car-safe
968 (soap-xml-get-children1 node 'xsd:restriction))))
969 ;; a complex content node is either an extension or a restriction
970 (cond (extension
971 (setq parent (xml-get-attribute-or-nil extension 'base))
972 (setq elements (soap-parse-sequence
973 (car (soap-xml-get-children1
974 extension 'xsd:sequence)))))
975 (restriction
976 (let ((base (xml-get-attribute-or-nil restriction 'base)))
977 (assert (equal base "soapenc:Array")
979 "restrictions supported only for soapenc:Array types, this is a %s"
980 base))
981 (setq array? t)
982 (let ((attribute (car (soap-xml-get-children1
983 restriction 'xsd:attribute))))
984 (let ((array-type (soap-xml-get-attribute-or-nil1
985 attribute 'wsdl:arrayType)))
986 (when (string-match "^\\(.*\\)\\[\\]$" array-type)
987 (setq parent (match-string 1 array-type))))))
990 (error "Unknown complex type"))))
992 (if parent
993 (setq parent (soap-l2fq parent 'tns)))
995 (if array?
996 (make-soap-array-type :element-type parent)
997 (make-soap-sequence-type :parent parent :elements elements))))
999 (defun soap-parse-message (node)
1000 "Parse NODE as a wsdl:message and return the corresponding type."
1001 (assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:message)
1003 "soap-parse-message: expecting wsdl:message node, got %s"
1004 (soap-l2wk (xml-node-name node)))
1005 (let ((name (xml-get-attribute-or-nil node 'name))
1006 parts)
1007 (dolist (p (soap-xml-get-children1 node 'wsdl:part))
1008 (let ((name (xml-get-attribute-or-nil p 'name))
1009 (type (xml-get-attribute-or-nil p 'type))
1010 (element (xml-get-attribute-or-nil p 'element)))
1012 (when type
1013 (setq type (soap-l2fq type 'tns)))
1015 (when element
1016 (setq element (soap-l2fq element 'tns)))
1018 (push (cons name (or type element)) parts)))
1019 (make-soap-message :name name :parts (nreverse parts))))
1021 (defun soap-parse-port-type (node)
1022 "Parse NODE as a wsdl:portType and return the corresponding port."
1023 (assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:portType)
1025 "soap-parse-port-type: expecting wsdl:portType node got %s"
1026 (soap-l2wk (xml-node-name node)))
1027 (let ((ns (make-soap-namespace
1028 :name (concat "urn:" (xml-get-attribute node 'name)))))
1029 (dolist (node (soap-xml-get-children1 node 'wsdl:operation))
1030 (let ((o (soap-parse-operation node)))
1032 (let ((other-operation (soap-namespace-get
1033 (soap-element-name o) ns 'soap-operation-p)))
1034 (if other-operation
1035 ;; Unfortunately, the Confluence WSDL defines two operations
1036 ;; named "search" which differ only in parameter names...
1037 (soap-warning "Discarding duplicate operation: %s"
1038 (soap-element-name o))
1040 (progn
1041 (soap-namespace-put o ns)
1043 ;; link all messages from this namespace, as this namespace
1044 ;; will be used for decoding the response.
1045 (destructuring-bind (name . message) (soap-operation-input o)
1046 (soap-namespace-put-link name message ns))
1048 (destructuring-bind (name . message) (soap-operation-output o)
1049 (soap-namespace-put-link name message ns))
1051 (dolist (fault (soap-operation-faults o))
1052 (destructuring-bind (name . message) fault
1053 (soap-namespace-put-link name message ns 'replace)))
1055 )))))
1057 (make-soap-port-type :name (xml-get-attribute node 'name)
1058 :operations ns)))
1060 (defun soap-parse-operation (node)
1061 "Parse NODE as a wsdl:operation and return the corresponding type."
1062 (assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:operation)
1064 "soap-parse-operation: expecting wsdl:operation node, got %s"
1065 (soap-l2wk (xml-node-name node)))
1066 (let ((name (xml-get-attribute node 'name))
1067 (parameter-order (split-string
1068 (xml-get-attribute node 'parameterOrder)))
1069 input output faults)
1070 (dolist (n (xml-node-children node))
1071 (when (consp n) ; skip string nodes which are whitespace
1072 (let ((node-name (soap-l2wk (xml-node-name n))))
1073 (cond
1074 ((eq node-name 'wsdl:input)
1075 (let ((message (xml-get-attribute n 'message))
1076 (name (xml-get-attribute n 'name)))
1077 (setq input (cons name (soap-l2fq message 'tns)))))
1078 ((eq node-name 'wsdl:output)
1079 (let ((message (xml-get-attribute n 'message))
1080 (name (xml-get-attribute n 'name)))
1081 (setq output (cons name (soap-l2fq message 'tns)))))
1082 ((eq node-name 'wsdl:fault)
1083 (let ((message (xml-get-attribute n 'message))
1084 (name (xml-get-attribute n 'name)))
1085 (push (cons name (soap-l2fq message 'tns)) faults)))))))
1086 (make-soap-operation
1087 :name name
1088 :parameter-order parameter-order
1089 :input input
1090 :output output
1091 :faults (nreverse faults))))
1093 (defun soap-parse-binding (node)
1094 "Parse NODE as a wsdl:binding and return the corresponding type."
1095 (assert (eq (soap-l2wk (xml-node-name node)) 'wsdl:binding)
1097 "soap-parse-binding: expecting wsdl:binding node, got %s"
1098 (soap-l2wk (xml-node-name node)))
1099 (let ((name (xml-get-attribute node 'name))
1100 (type (xml-get-attribute node 'type)))
1101 (let ((binding (make-soap-binding :name name
1102 :port-type (soap-l2fq type 'tns))))
1103 (dolist (wo (soap-xml-get-children1 node 'wsdl:operation))
1104 (let ((name (xml-get-attribute wo 'name))
1105 soap-action
1106 use)
1107 (dolist (so (soap-xml-get-children1 wo 'wsdlsoap:operation))
1108 (setq soap-action (xml-get-attribute-or-nil so 'soapAction)))
1110 ;; Search a wsdlsoap:body node and find a "use" tag. The
1111 ;; same use tag is assumed to be present for both input and
1112 ;; output types (although the WDSL spec allows separate
1113 ;; "use"-s for each of them...
1115 (dolist (i (soap-xml-get-children1 wo 'wsdl:input))
1116 (dolist (b (soap-xml-get-children1 i 'wsdlsoap:body))
1117 (setq use (or use
1118 (xml-get-attribute-or-nil b 'use)))))
1120 (unless use
1121 (dolist (i (soap-xml-get-children1 wo 'wsdl:output))
1122 (dolist (b (soap-xml-get-children1 i 'wsdlsoap:body))
1123 (setq use (or use
1124 (xml-get-attribute-or-nil b 'use))))))
1126 (puthash name (make-soap-bound-operation :operation name
1127 :soap-action soap-action
1128 :use (and use (intern use)))
1129 (soap-binding-operations binding))))
1130 binding)))
1132 ;;;; SOAP type decoding
1134 (defvar soap-multi-refs nil
1135 "The list of multi-ref nodes in the current SOAP response.
1136 This is a dynamically bound variable used during decoding the
1137 SOAP response.")
1139 (defvar soap-decoded-multi-refs nil
1140 "List of decoded multi-ref nodes in the current SOAP response.
1141 This is a dynamically bound variable used during decoding the
1142 SOAP response.")
1144 (defvar soap-current-wsdl nil
1145 "The current WSDL document used when decoding the SOAP response.
1146 This is a dynamically bound variable.")
1148 (defun soap-decode-type (type node)
1149 "Use TYPE (an xsd type) to decode the contents of NODE.
1151 NODE is an XML node, representing some SOAP encoded value or a
1152 reference to another XML node (a multiRef). This function will
1153 resolve the multiRef reference, if any, than call a TYPE specific
1154 decode function to perform the actual decoding."
1155 (let ((href (xml-get-attribute-or-nil node 'href)))
1156 (cond (href
1157 (catch 'done
1158 ;; NODE is actually a HREF, find the target and decode that.
1159 ;; Check first if we already decoded this multiref.
1161 (let ((decoded (cdr (assoc href soap-decoded-multi-refs))))
1162 (when decoded
1163 (throw 'done decoded)))
1165 (string-match "^#\\(.*\\)$" href) ; TODO: check that it matched
1167 (let ((id (match-string 1 href)))
1168 (dolist (mr soap-multi-refs)
1169 (let ((mrid (xml-get-attribute mr 'id)))
1170 (when (equal id mrid)
1171 ;; recurse here, in case there are multiple HREF's
1172 (let ((decoded (soap-decode-type type mr)))
1173 (push (cons href decoded) soap-decoded-multi-refs)
1174 (throw 'done decoded)))))
1175 (error "Cannot find href %s" href))))
1177 (soap-with-local-xmlns node
1178 (if (equal (soap-xml-get-attribute-or-nil1 node 'xsi:nil) "true")
1180 (let ((decoder (get (aref type 0) 'soap-decoder)))
1181 (assert decoder nil "no soap-decoder for %s type"
1182 (aref type 0))
1183 (funcall decoder type node))))))))
1185 (defun soap-decode-any-type (node)
1186 "Decode NODE using type information inside it."
1187 ;; If the NODE has type information, we use that...
1188 (let ((type (soap-xml-get-attribute-or-nil1 node 'xsi:type)))
1189 (if type
1190 (let ((wtype (soap-wsdl-get type soap-current-wsdl 'soap-type-p)))
1191 (if wtype
1192 (soap-decode-type wtype node)
1193 ;; The node has type info encoded in it, but we don't know how
1194 ;; to decode it...
1195 (error "Soap-decode-any-type: node has unknown type: %s" type)))
1197 ;; No type info in the node...
1199 (let ((contents (xml-node-children node)))
1200 (if (and (= (length contents) 1) (stringp (car contents)))
1201 ;; contents is just a string
1202 (car contents)
1204 ;; we assume the NODE is a sequence with every element a
1205 ;; structure name
1206 (let (result)
1207 (dolist (element contents)
1208 (let ((key (xml-node-name element))
1209 (value (soap-decode-any-type element)))
1210 (push (cons key value) result)))
1211 (nreverse result)))))))
1213 (defun soap-decode-array (node)
1214 "Decode NODE as an Array using type information inside it."
1215 (let ((type (soap-xml-get-attribute-or-nil1 node 'soapenc:arrayType))
1216 (wtype nil)
1217 (contents (xml-node-children node))
1218 result)
1219 (when type
1220 ;; Type is in the format "someType[NUM]" where NUM is the number of
1221 ;; elements in the array. We discard the [NUM] part.
1222 (setq type (replace-regexp-in-string "\\[[0-9]+\\]\\'" "" type))
1223 (setq wtype (soap-wsdl-get type soap-current-wsdl 'soap-type-p))
1224 (unless wtype
1225 ;; The node has type info encoded in it, but we don't know how to
1226 ;; decode it...
1227 (error "Soap-decode-array: node has unknown type: %s" type)))
1228 (dolist (e contents)
1229 (when (consp e)
1230 (push (if wtype
1231 (soap-decode-type wtype e)
1232 (soap-decode-any-type e))
1233 result)))
1234 (nreverse result)))
1236 (defun soap-decode-basic-type (type node)
1237 "Use TYPE to decode the contents of NODE.
1238 TYPE is a `soap-basic-type' struct, and NODE is an XML document.
1239 A LISP value is returned based on the contents of NODE and the
1240 type-info stored in TYPE."
1241 (let ((contents (xml-node-children node))
1242 (type-kind (soap-basic-type-kind type)))
1244 (if (null contents)
1246 (ecase type-kind
1247 (string (car contents))
1248 (dateTime (car contents)) ; TODO: convert to a date time
1249 ((long int float) (string-to-number (car contents)))
1250 (boolean (string= (downcase (car contents)) "true"))
1251 (base64Binary (base64-decode-string (car contents)))
1252 (anyType (soap-decode-any-type node))
1253 (Array (soap-decode-array node))))))
1255 (defun soap-decode-sequence-type (type node)
1256 "Use TYPE to decode the contents of NODE.
1257 TYPE is assumed to be a sequence type and an ALIST with the
1258 contents of the NODE is returned."
1259 (let ((result nil)
1260 (parent (soap-sequence-type-parent type)))
1261 (when parent
1262 (setq result (nreverse (soap-decode-type parent node))))
1263 (dolist (element (soap-sequence-type-elements type))
1264 (let ((instance-count 0)
1265 (e-name (soap-sequence-element-name element))
1266 (e-type (soap-sequence-element-type element)))
1267 (dolist (node (xml-get-children node e-name))
1268 (incf instance-count)
1269 (push (cons e-name (soap-decode-type e-type node)) result))
1270 ;; Do some sanity checking
1271 (cond ((and (= instance-count 0)
1272 (not (soap-sequence-element-nillable? element)))
1273 (soap-warning "While decoding %s: missing non-nillable slot %s"
1274 (soap-element-name type) e-name))
1275 ((and (> instance-count 1)
1276 (not (soap-sequence-element-multiple? element)))
1277 (soap-warning "While decoding %s: multiple slots named %s"
1278 (soap-element-name type) e-name)))))
1279 (nreverse result)))
1281 (defun soap-decode-array-type (type node)
1282 "Use TYPE to decode the contents of NODE.
1283 TYPE is assumed to be an array type. Arrays are decoded as lists.
1284 This is because it is easier to work with list results in LISP."
1285 (let ((result nil)
1286 (element-type (soap-array-type-element-type type)))
1287 (dolist (node (xml-node-children node))
1288 (when (consp node)
1289 (push (soap-decode-type element-type node) result)))
1290 (nreverse result)))
1292 (progn
1293 (put (aref (make-soap-basic-type) 0)
1294 'soap-decoder 'soap-decode-basic-type)
1295 (put (aref (make-soap-sequence-type) 0)
1296 'soap-decoder 'soap-decode-sequence-type)
1297 (put (aref (make-soap-array-type) 0)
1298 'soap-decoder 'soap-decode-array-type))
1300 ;;;; Soap Envelope parsing
1302 (put 'soap-error
1303 'error-conditions
1304 '(error soap-error))
1305 (put 'soap-error 'error-message "SOAP error")
1307 (defun soap-parse-envelope (node operation wsdl)
1308 "Parse the SOAP envelope in NODE and return the response.
1309 OPERATION is the WSDL operation for which we expect the response,
1310 WSDL is used to decode the NODE"
1311 (soap-with-local-xmlns node
1312 (assert (eq (soap-l2wk (xml-node-name node)) 'soap:Envelope)
1314 "soap-parse-envelope: expecting soap:Envelope node, got %s"
1315 (soap-l2wk (xml-node-name node)))
1316 (let ((body (car (soap-xml-get-children1 node 'soap:Body))))
1318 (let ((fault (car (soap-xml-get-children1 body 'soap:Fault))))
1319 (when fault
1320 (let ((fault-code (let ((n (car (xml-get-children
1321 fault 'faultcode))))
1322 (car-safe (xml-node-children n))))
1323 (fault-string (let ((n (car (xml-get-children
1324 fault 'faultstring))))
1325 (car-safe (xml-node-children n)))))
1326 (while t
1327 (signal 'soap-error (list fault-code fault-string))))))
1329 ;; First (non string) element of the body is the root node of he
1330 ;; response
1331 (let ((response (if (eq (soap-bound-operation-use operation) 'literal)
1332 ;; For 'literal uses, the response is the actual body
1333 body
1334 ;; ...otherwise the first non string element
1335 ;; of the body is the response
1336 (catch 'found
1337 (dolist (n (xml-node-children body))
1338 (when (consp n)
1339 (throw 'found n)))))))
1340 (soap-parse-response response operation wsdl body)))))
1342 (defun soap-parse-response (response-node operation wsdl soap-body)
1343 "Parse RESPONSE-NODE and return the result as a LISP value.
1344 OPERATION is the WSDL operation for which we expect the response,
1345 WSDL is used to decode the NODE.
1347 SOAP-BODY is the body of the SOAP envelope (of which
1348 RESPONSE-NODE is a sub-node). It is used in case RESPONSE-NODE
1349 reference multiRef parts which are external to RESPONSE-NODE."
1350 (let* ((soap-current-wsdl wsdl)
1351 (op (soap-bound-operation-operation operation))
1352 (use (soap-bound-operation-use operation))
1353 (message (cdr (soap-operation-output op))))
1355 (soap-with-local-xmlns response-node
1357 (when (eq use 'encoded)
1358 (let* ((received-message-name (soap-l2fq (xml-node-name response-node)))
1359 (received-message (soap-wsdl-get
1360 received-message-name wsdl 'soap-message-p)))
1361 (unless (eq received-message message)
1362 (error "Unexpected message: got %s, expecting %s"
1363 received-message-name
1364 (soap-element-name message)))))
1366 (let ((decoded-parts nil)
1367 (soap-multi-refs (xml-get-children soap-body 'multiRef))
1368 (soap-decoded-multi-refs nil))
1370 (dolist (part (soap-message-parts message))
1371 (let ((tag (car part))
1372 (type (cdr part))
1373 node)
1375 (setq node
1376 (cond
1377 ((eq use 'encoded)
1378 (car (xml-get-children response-node tag)))
1380 ((eq use 'literal)
1381 (catch 'found
1382 (let* ((ns-aliases (soap-wsdl-alias-table wsdl))
1383 (ns-name (cdr (assoc
1384 (soap-element-namespace-tag type)
1385 ns-aliases)))
1386 (fqname (cons ns-name (soap-element-name type))))
1387 (dolist (c (xml-node-children response-node))
1388 (when (consp c)
1389 (soap-with-local-xmlns c
1390 (when (equal (soap-l2fq (xml-node-name c))
1391 fqname)
1392 (throw 'found c))))))))))
1394 (unless node
1395 (error "Soap-parse-response(%s): cannot find message part %s"
1396 (soap-element-name op) tag))
1397 (push (soap-decode-type type node) decoded-parts)))
1399 decoded-parts))))
1401 ;;;; SOAP type encoding
1403 (defvar soap-encoded-namespaces nil
1404 "A list of namespace tags used during encoding a message.
1405 This list is populated by `soap-encode-value' and used by
1406 `soap-create-envelope' to add aliases for these namespace to the
1407 XML request.
1409 This variable is dynamically bound in `soap-create-envelope'.")
1411 (defun soap-encode-value (xml-tag value type)
1412 "Encode inside an XML-TAG the VALUE using TYPE.
1413 The resulting XML data is inserted in the current buffer
1414 at (point)/
1416 TYPE is one of the soap-*-type structures which defines how VALUE
1417 is to be encoded. This is a generic function which finds an
1418 encoder function based on TYPE and calls that encoder to do the
1419 work."
1420 (let ((encoder (get (aref type 0) 'soap-encoder)))
1421 (assert encoder nil "no soap-encoder for %s type" (aref type 0))
1422 ;; XML-TAG can be a string or a symbol, but we pass only string's to the
1423 ;; encoders
1424 (when (symbolp xml-tag)
1425 (setq xml-tag (symbol-name xml-tag)))
1426 (funcall encoder xml-tag value type))
1427 (add-to-list 'soap-encoded-namespaces (soap-element-namespace-tag type)))
1429 (defun soap-encode-basic-type (xml-tag value type)
1430 "Encode inside XML-TAG the LISP VALUE according to TYPE.
1431 Do not call this function directly, use `soap-encode-value'
1432 instead."
1433 (let ((xsi-type (soap-element-fq-name type))
1434 (basic-type (soap-basic-type-kind type)))
1436 ;; try to classify the type based on the value type and use that type when
1437 ;; encoding
1438 (when (eq basic-type 'anyType)
1439 (cond ((stringp value)
1440 (setq xsi-type "xsd:string" basic-type 'string))
1441 ((integerp value)
1442 (setq xsi-type "xsd:int" basic-type 'int))
1443 ((memq value '(t nil))
1444 (setq xsi-type "xsd:boolean" basic-type 'boolean))
1446 (error
1447 "Soap-encode-basic-type(%s, %s, %s): cannot classify anyType value"
1448 xml-tag value xsi-type))))
1450 (insert "<" xml-tag " xsi:type=\"" xsi-type "\"")
1452 ;; We have some ambiguity here, as a nil value represents "false" when the
1453 ;; type is boolean, we will never have a "nil" boolean type...
1455 (if (or value (eq basic-type 'boolean))
1456 (progn
1457 (insert ">")
1458 (case basic-type
1459 (string
1460 (unless (stringp value)
1461 (error "Soap-encode-basic-type(%s, %s, %s): not a string value"
1462 xml-tag value xsi-type))
1463 (insert (url-insert-entities-in-string value)))
1465 (dateTime
1466 (cond ((and (consp value) ; is there a time-value-p ?
1467 (>= (length value) 2)
1468 (numberp (nth 0 value))
1469 (numberp (nth 1 value)))
1470 ;; Value is a (current-time) style value, convert
1471 ;; to a string
1472 (insert (format-time-string "%Y-%m-%dT%H:%M:%S" value)))
1473 ((stringp value)
1474 (insert (url-insert-entities-in-string value)))
1476 (error
1477 "Soap-encode-basic-type(%s, %s, %s): not a dateTime value"
1478 xml-tag value xsi-type))))
1480 (boolean
1481 (unless (memq value '(t nil))
1482 (error "Soap-encode-basic-type(%s, %s, %s): not a boolean value"
1483 xml-tag value xsi-type))
1484 (insert (if value "true" "false")))
1486 ((long int)
1487 (unless (integerp value)
1488 (error "Soap-encode-basic-type(%s, %s, %s): not an integer value"
1489 xml-tag value xsi-type))
1490 (insert (number-to-string value)))
1492 (base64Binary
1493 (unless (stringp value)
1494 (error "Soap-encode-basic-type(%s, %s, %s): not a string value"
1495 xml-tag value xsi-type))
1496 (insert (base64-encode-string value)))
1498 (otherwise
1499 (error
1500 "Soap-encode-basic-type(%s, %s, %s): don't know how to encode"
1501 xml-tag value xsi-type))))
1503 (insert " xsi:nil=\"true\">"))
1504 (insert "</" xml-tag ">\n")))
1506 (defun soap-encode-sequence-type (xml-tag value type)
1507 "Encode inside XML-TAG the LISP VALUE according to TYPE.
1508 Do not call this function directly, use `soap-encode-value'
1509 instead."
1510 (let ((xsi-type (soap-element-fq-name type)))
1511 (insert "<" xml-tag " xsi:type=\"" xsi-type "\"")
1512 (if value
1513 (progn
1514 (insert ">\n")
1515 (let ((parents (list type))
1516 (parent (soap-sequence-type-parent type)))
1518 (while parent
1519 (push parent parents)
1520 (setq parent (soap-sequence-type-parent parent)))
1522 (dolist (type parents)
1523 (dolist (element (soap-sequence-type-elements type))
1524 (let ((instance-count 0)
1525 (e-name (soap-sequence-element-name element))
1526 (e-type (soap-sequence-element-type element)))
1527 (dolist (v value)
1528 (when (equal (car v) e-name)
1529 (incf instance-count)
1530 (soap-encode-value e-name (cdr v) e-type)))
1532 ;; Do some sanity checking
1533 (cond ((and (= instance-count 0)
1534 (not (soap-sequence-element-nillable? element)))
1535 (soap-warning
1536 "While encoding %s: missing non-nillable slot %s"
1537 (soap-element-name type) e-name))
1538 ((and (> instance-count 1)
1539 (not (soap-sequence-element-multiple? element)))
1540 (soap-warning
1541 "While encoding %s: multiple slots named %s"
1542 (soap-element-name type) e-name))))))))
1543 (insert " xsi:nil=\"true\">"))
1544 (insert "</" xml-tag ">\n")))
1546 (defun soap-encode-array-type (xml-tag value type)
1547 "Encode inside XML-TAG the LISP VALUE according to TYPE.
1548 Do not call this function directly, use `soap-encode-value'
1549 instead."
1550 (unless (vectorp value)
1551 (error "Soap-encode: %s(%s) expects a vector, got: %s"
1552 xml-tag (soap-element-fq-name type) value))
1553 (let* ((element-type (soap-array-type-element-type type))
1554 (array-type (concat (soap-element-fq-name element-type)
1555 "[" (format "%s" (length value)) "]")))
1556 (insert "<" xml-tag
1557 " soapenc:arrayType=\"" array-type "\" "
1558 " xsi:type=\"soapenc:Array\">\n")
1559 (loop for i below (length value)
1560 do (soap-encode-value xml-tag (aref value i) element-type))
1561 (insert "</" xml-tag ">\n")))
1563 (progn
1564 (put (aref (make-soap-basic-type) 0)
1565 'soap-encoder 'soap-encode-basic-type)
1566 (put (aref (make-soap-sequence-type) 0)
1567 'soap-encoder 'soap-encode-sequence-type)
1568 (put (aref (make-soap-array-type) 0)
1569 'soap-encoder 'soap-encode-array-type))
1571 (defun soap-encode-body (operation parameters wsdl)
1572 "Create the body of a SOAP request for OPERATION in the current buffer.
1573 PARAMETERS is a list of parameters supplied to the OPERATION.
1575 The OPERATION and PARAMETERS are encoded according to the WSDL
1576 document."
1577 (let* ((op (soap-bound-operation-operation operation))
1578 (use (soap-bound-operation-use operation))
1579 (message (cdr (soap-operation-input op)))
1580 (parameter-order (soap-operation-parameter-order op)))
1582 (unless (= (length parameter-order) (length parameters))
1583 (error "Wrong number of parameters for %s: expected %d, got %s"
1584 (soap-element-name op)
1585 (length parameter-order)
1586 (length parameters)))
1588 (insert "<soap:Body>\n")
1589 (when (eq use 'encoded)
1590 (add-to-list 'soap-encoded-namespaces (soap-element-namespace-tag op))
1591 (insert "<" (soap-element-fq-name op) ">\n"))
1593 (let ((param-table (loop for formal in parameter-order
1594 for value in parameters
1595 collect (cons formal value))))
1596 (dolist (part (soap-message-parts message))
1597 (let* ((param-name (car part))
1598 (type (cdr part))
1599 (tag-name (if (eq use 'encoded)
1600 param-name
1601 (soap-element-name type)))
1602 (value (cdr (assoc param-name param-table)))
1603 (start-pos (point)))
1604 (soap-encode-value tag-name value type)
1605 (when (eq use 'literal)
1606 ;; hack: add the xmlns attribute to the tag, the only way
1607 ;; ASP.NET web services recognize the namespace of the
1608 ;; element itself...
1609 (save-excursion
1610 (goto-char start-pos)
1611 (when (re-search-forward " ")
1612 (let* ((ns (soap-element-namespace-tag type))
1613 (namespace (cdr (assoc ns
1614 (soap-wsdl-alias-table wsdl)))))
1615 (when namespace
1616 (insert "xmlns=\"" namespace "\" ")))))))))
1618 (when (eq use 'encoded)
1619 (insert "</" (soap-element-fq-name op) ">\n"))
1620 (insert "</soap:Body>\n")))
1622 (defun soap-create-envelope (operation parameters wsdl)
1623 "Create a SOAP request envelope for OPERATION using PARAMETERS.
1624 WSDL is the wsdl document used to encode the PARAMETERS."
1625 (with-temp-buffer
1626 (let ((soap-encoded-namespaces '("xsi" "soap" "soapenc"))
1627 (use (soap-bound-operation-use operation)))
1629 ;; Create the request body
1630 (soap-encode-body operation parameters wsdl)
1632 ;; Put the envelope around the body
1633 (goto-char (point-min))
1634 (insert "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<soap:Envelope\n")
1635 (when (eq use 'encoded)
1636 (insert " soapenc:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\n"))
1637 (dolist (nstag soap-encoded-namespaces)
1638 (insert " xmlns:" nstag "=\"")
1639 (let ((nsname (cdr (assoc nstag soap-well-known-xmlns))))
1640 (unless nsname
1641 (setq nsname (cdr (assoc nstag (soap-wsdl-alias-table wsdl)))))
1642 (insert nsname)
1643 (insert "\"\n")))
1644 (insert ">\n")
1645 (goto-char (point-max))
1646 (insert "</soap:Envelope>\n"))
1648 (buffer-string)))
1650 ;;;; invoking soap methods
1652 (defcustom soap-debug nil
1653 "When t, enable some debugging facilities."
1654 :type 'boolean
1655 :group 'soap-client)
1657 (defun soap-invoke (wsdl service operation-name &rest parameters)
1658 "Invoke a SOAP operation and return the result.
1660 WSDL is used for encoding the request and decoding the response.
1661 It also contains information about the WEB server address that
1662 will service the request.
1664 SERVICE is the SOAP service to invoke.
1666 OPERATION-NAME is the operation to invoke.
1668 PARAMETERS -- the remaining parameters are used as parameters for
1669 the SOAP request.
1671 NOTE: The SOAP service provider should document the available
1672 operations and their parameters for the service. You can also
1673 use the `soap-inspect' function to browse the available
1674 operations in a WSDL document."
1675 (let ((port (catch 'found
1676 (dolist (p (soap-wsdl-ports wsdl))
1677 (when (equal service (soap-element-name p))
1678 (throw 'found p))))))
1679 (unless port
1680 (error "Unknown SOAP service: %s" service))
1682 (let* ((binding (soap-port-binding port))
1683 (operation (gethash operation-name
1684 (soap-binding-operations binding))))
1685 (unless operation
1686 (error "No operation %s for SOAP service %s" operation-name service))
1688 (let ((url-request-method "POST")
1689 (url-package-name "soap-client.el")
1690 (url-package-version "1.0")
1691 (url-http-version "1.0")
1692 (url-request-data (soap-create-envelope operation parameters wsdl))
1693 (url-mime-charset-string "utf-8;q=1, iso-8859-1;q=0.5")
1694 (url-request-coding-system 'utf-8)
1695 (url-http-attempt-keepalives t)
1696 (url-request-extra-headers (list
1697 (cons "SOAPAction"
1698 (soap-bound-operation-soap-action
1699 operation))
1700 (cons "Content-Type"
1701 "text/xml; charset=utf-8"))))
1702 (let ((buffer (url-retrieve-synchronously
1703 (soap-port-service-url port))))
1704 (condition-case err
1705 (with-current-buffer buffer
1706 (declare (special url-http-response-status))
1707 (if (null url-http-response-status)
1708 (error "No HTTP response from server"))
1709 (if (and soap-debug (> url-http-response-status 299))
1710 ;; This is a warning because some SOAP errors come
1711 ;; back with a HTTP response 500 (internal server
1712 ;; error)
1713 (warn "Error in SOAP response: HTTP code %s"
1714 url-http-response-status))
1715 (let ((mime-part (mm-dissect-buffer t t)))
1716 (unless mime-part
1717 (error "Failed to decode response from server"))
1718 (unless (equal (car (mm-handle-type mime-part)) "text/xml")
1719 (error "Server response is not an XML document"))
1720 (with-temp-buffer
1721 (mm-insert-part mime-part)
1722 (let ((response (car (xml-parse-region
1723 (point-min) (point-max)))))
1724 (prog1
1725 (soap-parse-envelope response operation wsdl)
1726 (kill-buffer buffer)
1727 (mm-destroy-part mime-part))))))
1728 (soap-error
1729 ;; Propagate soap-errors -- they are error replies of the
1730 ;; SOAP protocol and don't indicate a communication
1731 ;; problem or a bug in this code.
1732 (signal (car err) (cdr err)))
1733 (error
1734 (when soap-debug
1735 (pop-to-buffer buffer))
1736 (error (error-message-string err)))))))))
1738 (provide 'soap-client)
1741 ;;; Local Variables:
1742 ;;; eval: (outline-minor-mode 1)
1743 ;;; outline-regexp: ";;;;+"
1744 ;;; End:
1746 ;;; soap-client.el ends here