1 ;;;; soap-client.el -- Access SOAP web services from Emacs
3 ;; Copyright (C) 2009-2015 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/>.
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 ;; Ideally, the service you want to access will have some documentation about
36 ;; the operations it supports. If it does not, you can try using
37 ;; `soap-inspect' to browse the WSDL document and see the available operations
38 ;; and their parameters.
43 (eval-when-compile (require 'cl
))
52 (defsubst soap-warning
(message &rest args
)
53 "Display a warning MESSAGE with ARGS, using the 'soap-client warning type."
54 (display-warning 'soap-client
(apply #'format-message message args
)
57 (defgroup soap-client nil
58 "Access SOAP web services from Emacs."
62 ;;;; Support for parsing XML documents with namespaces
64 ;; XML documents with namespaces are difficult to parse because the names of
65 ;; the nodes depend on what "xmlns" aliases have been defined in the document.
66 ;; To work with such documents, we introduce a translation layer between a
67 ;; "well known" namespace tag and the local namespace tag in the document
70 (defconst soap-well-known-xmlns
71 '(("apachesoap" .
"http://xml.apache.org/xml-soap")
72 ("soapenc" .
"http://schemas.xmlsoap.org/soap/encoding/")
73 ("wsdl" .
"http://schemas.xmlsoap.org/wsdl/")
74 ("wsdlsoap" .
"http://schemas.xmlsoap.org/wsdl/soap/")
75 ("xsd" .
"http://www.w3.org/2001/XMLSchema")
76 ("xsi" .
"http://www.w3.org/2001/XMLSchema-instance")
77 ("soap" .
"http://schemas.xmlsoap.org/soap/envelope/")
78 ("soap12" .
"http://schemas.xmlsoap.org/wsdl/soap12/")
79 ("http" .
"http://schemas.xmlsoap.org/wsdl/http/")
80 ("mime" .
"http://schemas.xmlsoap.org/wsdl/mime/"))
81 "A list of well known xml namespaces and their aliases.")
83 (defvar soap-local-xmlns nil
84 "A list of local namespace aliases.
85 This is a dynamically bound variable, controlled by
86 `soap-with-local-xmlns'.")
88 (defvar soap-default-xmlns nil
89 "The default XML namespaces.
90 Names in this namespace will be unqualified. This is a
91 dynamically bound variable, controlled by
92 `soap-with-local-xmlns'")
94 (defvar soap-target-xmlns nil
95 "The target XML namespace.
96 New XSD elements will be defined in this namespace, unless they
97 are fully qualified for a different namespace. This is a
98 dynamically bound variable, controlled by
99 `soap-with-local-xmlns'")
101 (defun soap-wk2l (well-known-name)
102 "Return local variant of WELL-KNOWN-NAME.
103 This is done by looking up the namespace in the
104 `soap-well-known-xmlns' table and resolving the namespace to
105 the local name based on the current local translation table
106 `soap-local-xmlns'. See also `soap-with-local-xmlns'."
107 (let ((wk-name-1 (if (symbolp well-known-name
)
108 (symbol-name well-known-name
)
111 ((string-match "^\\(.*\\):\\(.*\\)$" wk-name-1
)
112 (let ((ns (match-string 1 wk-name-1
))
113 (name (match-string 2 wk-name-1
)))
114 (let ((namespace (cdr (assoc ns soap-well-known-xmlns
))))
115 (cond ((equal namespace soap-default-xmlns
)
116 ;; Name is unqualified in the default namespace
117 (if (symbolp well-known-name
)
121 (let* ((local-ns (car (rassoc namespace soap-local-xmlns
)))
122 (local-name (concat local-ns
":" name
)))
123 (if (symbolp well-known-name
)
126 (t well-known-name
))))
128 (defun soap-l2wk (local-name)
129 "Convert LOCAL-NAME into a well known name.
130 The namespace of LOCAL-NAME is looked up in the
131 `soap-well-known-xmlns' table and a well known namespace tag is
134 nil is returned if there is no well-known namespace for the
135 namespace of LOCAL-NAME."
136 (let ((l-name-1 (if (symbolp local-name
)
137 (symbol-name local-name
)
141 ((string-match "^\\(.*\\):\\(.*\\)$" l-name-1
)
142 (setq name
(match-string 2 l-name-1
))
143 (let ((ns (match-string 1 l-name-1
)))
144 (setq namespace
(cdr (assoc ns soap-local-xmlns
)))
146 (error "Soap-l2wk(%s): no namespace for alias %s" local-name ns
))))
149 (setq namespace soap-default-xmlns
)))
152 (let ((well-known-ns (car (rassoc namespace soap-well-known-xmlns
))))
154 (let ((well-known-name (concat well-known-ns
":" name
)))
155 (if (symbol-name local-name
)
156 (intern well-known-name
)
159 ;; (soap-warning "soap-l2wk(%s): namespace %s has no well-known tag"
160 ;; local-name namespace)
162 ;; if no namespace is defined, just return the unqualified name
166 (defun soap-l2fq (local-name &optional use-tns
)
167 "Convert LOCAL-NAME into a fully qualified name.
168 A fully qualified name is a cons of the namespace name and the
169 name of the element itself. For example \"xsd:string\" is
170 converted to \(\"http://www.w3.org/2001/XMLSchema\" . \"string\"\).
172 The USE-TNS argument specifies what to do when LOCAL-NAME has no
173 namespace tag. If USE-TNS is non-nil, the `soap-target-xmlns'
174 will be used as the element's namespace, otherwise
175 `soap-default-xmlns' will be used.
177 This is needed because different parts of a WSDL document can use
178 different namespace aliases for the same element."
179 (let ((local-name-1 (if (symbolp local-name
)
180 (symbol-name local-name
)
182 (cond ((string-match "^\\(.*\\):\\(.*\\)$" local-name-1
)
183 (let ((ns (match-string 1 local-name-1
))
184 (name (match-string 2 local-name-1
)))
185 (let ((namespace (cdr (assoc ns soap-local-xmlns
))))
187 (cons namespace name
)
188 (error "Soap-l2fq(%s): unknown alias %s" local-name ns
)))))
195 (defun soap-extract-xmlns (node &optional xmlns-table
)
196 "Return a namespace alias table for NODE by extending XMLNS-TABLE."
197 (let (xmlns default-ns target-ns
)
198 (dolist (a (xml-node-attributes node
))
199 (let ((name (symbol-name (car a
)))
201 (cond ((string= name
"targetNamespace")
202 (setq target-ns value
))
203 ((string= name
"xmlns")
204 (setq default-ns value
))
205 ((string-match "^xmlns:\\(.*\\)$" name
)
206 (push (cons (match-string 1 name
) value
) xmlns
)))))
208 (let ((tns (assoc "tns" xmlns
)))
209 (cond ((and tns target-ns
)
210 ;; If a tns alias is defined for this node, it must match
211 ;; the target namespace.
212 (unless (equal target-ns
(cdr tns
))
214 "soap-extract-xmlns(%s): tns alias and targetNamespace mismatch"
215 (xml-node-name node
))))
216 ((and tns
(not target-ns
))
217 (setq target-ns
(cdr tns
)))
218 ((and (not tns
) target-ns
)
219 ;; a tns alias was not defined in this node. See if the node has
220 ;; a "targetNamespace" attribute and add an alias to this. Note
221 ;; that we might override an existing tns alias in XMLNS-TABLE,
222 ;; but that is intended.
223 (push (cons "tns" target-ns
) xmlns
))))
225 (list default-ns target-ns
(append xmlns xmlns-table
))))
227 (defmacro soap-with-local-xmlns
(node &rest body
)
228 "Install a local alias table from NODE and execute BODY."
229 (declare (debug (form &rest form
)) (indent 1))
230 (let ((xmlns (make-symbol "xmlns")))
231 `(let ((,xmlns
(soap-extract-xmlns ,node soap-local-xmlns
)))
232 (let ((soap-default-xmlns (or (nth 0 ,xmlns
) soap-default-xmlns
))
233 (soap-target-xmlns (or (nth 1 ,xmlns
) soap-target-xmlns
))
234 (soap-local-xmlns (nth 2 ,xmlns
)))
237 (defun soap-get-target-namespace (node)
238 "Return the target namespace of NODE.
239 This is the namespace in which new elements will be defined."
240 (or (xml-get-attribute-or-nil node
'targetNamespace
)
241 (cdr (assoc "tns" soap-local-xmlns
))
244 (defun soap-xml-get-children1 (node child-name
)
245 "Return the children of NODE named CHILD-NAME.
246 This is the same as `xml-get-children', but CHILD-NAME can have
249 (dolist (c (xml-node-children node
))
251 (soap-with-local-xmlns c
252 ;; We use `ignore-errors' here because we want to silently
253 ;; skip nodes for which we cannot convert them to a
255 (eq (ignore-errors (soap-l2wk (xml-node-name c
)))
260 (defun soap-xml-get-attribute-or-nil1 (node attribute
)
261 "Return the NODE's ATTRIBUTE, or nil if it does not exist.
262 This is the same as `xml-get-attribute-or-nil', but ATTRIBUTE can
263 be tagged with a namespace tag."
265 (soap-with-local-xmlns node
266 (dolist (a (xml-node-attributes node
))
267 ;; We use `ignore-errors' here because we want to silently skip
268 ;; attributes for which we cannot convert them to a well-known name.
269 (when (eq (ignore-errors (soap-l2wk (car a
))) attribute
)
270 (throw 'found
(cdr a
)))))))
275 ;; An element in an XML namespace, "things" stored in soap-xml-namespaces will
276 ;; be derived from this object.
278 (defstruct soap-element
280 ;; The "well-known" namespace tag for the element. For example, while
281 ;; parsing XML documents, we can have different tags for the XMLSchema
282 ;; namespace, but internally all our XMLSchema elements will have the "xsd"
286 (defun soap-element-fq-name (element)
287 "Return a fully qualified name for ELEMENT.
288 A fq name is the concatenation of the namespace tag and the
290 (concat (soap-element-namespace-tag element
)
291 ":" (soap-element-name element
)))
293 ;; a namespace link stores an alias for an object in once namespace to a
294 ;; "target" object possibly in a different namespace
296 (defstruct (soap-namespace-link (:include soap-element
))
299 ;; A namespace is a collection of soap-element objects under a name (the name
300 ;; of the namespace).
302 (defstruct soap-namespace
303 (name nil
:read-only t
) ; e.g "http://xml.apache.org/xml-soap"
304 (elements (make-hash-table :test
'equal
) :read-only t
))
306 (defun soap-namespace-put (element ns
)
307 "Store ELEMENT in NS.
308 Multiple elements with the same name can be stored in a
309 namespace. When retrieving the element you can specify a
310 discriminant predicate to `soap-namespace-get'"
311 (let ((name (soap-element-name element
)))
312 (push element
(gethash name
(soap-namespace-elements ns
)))))
314 (defun soap-namespace-put-link (name target ns
&optional replace
)
315 "Store a link from NAME to TARGET in NS.
316 An error will be signaled if an element by the same name is
317 already present in NS, unless REPLACE is non nil.
319 TARGET can be either a SOAP-ELEMENT or a string denoting an
320 element name into another namespace.
322 If NAME is nil, an element with the same name as TARGET will be
323 added to the namespace."
325 (unless (and name
(not (equal name
"")))
326 ;; if name is nil, use TARGET as a name...
327 (cond ((soap-element-p target
)
328 (setq name
(soap-element-name target
)))
329 ((consp target
) ; a fq name: (namespace . name)
330 (setq name
(cdr target
)))
332 (cond ((string-match "^\\(.*\\):\\(.*\\)$" target
)
333 (setq name
(match-string 2 target
)))
335 (setq name target
))))))
337 ;; by now, name should be valid
338 (assert (and name
(not (equal name
"")))
340 "Cannot determine name for namespace link")
341 (push (make-soap-namespace-link :name name
:target target
)
342 (gethash name
(soap-namespace-elements ns
))))
344 (defun soap-namespace-get (name ns
&optional discriminant-predicate
)
345 "Retrieve an element with NAME from the namespace NS.
346 If multiple elements with the same name exist,
347 DISCRIMINANT-PREDICATE is used to pick one of them. This allows
348 storing elements of different types (like a message type and a
349 binding) but the same name."
350 (assert (stringp name
))
351 (let ((elements (gethash name
(soap-namespace-elements ns
))))
352 (cond (discriminant-predicate
355 (when (funcall discriminant-predicate e
)
357 ((= (length elements
) 1) (car elements
))
358 ((> (length elements
) 1)
360 "Soap-namespace-get(%s): multiple elements, discriminant needed"
367 ;;;;; WSDL document elements
369 (defstruct (soap-basic-type (:include soap-element
))
370 kind
; a symbol of: string, dateTime, long, int
373 (defstruct (soap-simple-type (:include soap-basic-type
))
376 (defstruct soap-sequence-element
377 name type nillable? multiple?
)
379 (defstruct (soap-sequence-type (:include soap-element
))
380 parent
; OPTIONAL WSDL-TYPE name
381 elements
; LIST of SOAP-SEQUENCE-ELEMENT
384 (defstruct (soap-array-type (:include soap-element
))
385 element-type
; WSDL-TYPE of the array elements
388 (defstruct (soap-message (:include soap-element
))
389 parts
; ALIST of NAME => WSDL-TYPE name
392 (defstruct (soap-operation (:include soap-element
))
394 input
; (NAME . MESSAGE)
395 output
; (NAME . MESSAGE)
396 faults
) ; a list of (NAME . MESSAGE)
398 (defstruct (soap-port-type (:include soap-element
))
399 operations
) ; a namespace of operations
401 ;; A bound operation is an operation which has a soap action and a use
402 ;; method attached -- these are attached as part of a binding and we
403 ;; can have different bindings for the same operations.
404 (defstruct soap-bound-operation
405 operation
; SOAP-OPERATION
406 soap-action
; value for SOAPAction HTTP header
407 use
; 'literal or 'encoded, see
408 ; http://www.w3.org/TR/wsdl#_soap:body
411 (defstruct (soap-binding (:include soap-element
))
413 (operations (make-hash-table :test
'equal
) :readonly t
))
415 (defstruct (soap-port (:include soap-element
))
419 (defun soap-default-xsd-types ()
420 "Return a namespace containing some of the XMLSchema types."
421 (let ((ns (make-soap-namespace :name
"http://www.w3.org/2001/XMLSchema")))
422 (dolist (type '("string" "dateTime" "boolean"
423 "long" "int" "integer" "unsignedInt" "byte" "float" "double"
424 "base64Binary" "anyType" "anyURI" "Array" "byte[]"))
426 (make-soap-basic-type :name type
:kind
(intern type
))
430 (defun soap-default-soapenc-types ()
431 "Return a namespace containing some of the SOAPEnc types."
432 (let ((ns (make-soap-namespace
433 :name
"http://schemas.xmlsoap.org/soap/encoding/")))
434 (dolist (type '("string" "dateTime" "boolean"
435 "long" "int" "integer" "unsignedInt" "byte" "float" "double"
436 "base64Binary" "anyType" "anyURI" "Array" "byte[]"))
438 (make-soap-basic-type :name type
:kind
(intern type
))
442 (defun soap-type-p (element)
443 "Return t if ELEMENT is a SOAP data type (basic or complex)."
444 (or (soap-basic-type-p element
)
445 (soap-sequence-type-p element
)
446 (soap-array-type-p element
)))
449 ;;;;; The WSDL document
451 ;; The WSDL data structure used for encoding/decoding SOAP messages
453 origin
; file or URL from which this wsdl was loaded
454 ports
; a list of SOAP-PORT instances
455 alias-table
; a list of namespace aliases
456 namespaces
; a list of namespaces
459 (defun soap-wsdl-add-alias (alias name wsdl
)
460 "Add a namespace ALIAS for NAME to the WSDL document."
461 (push (cons alias name
) (soap-wsdl-alias-table wsdl
)))
463 (defun soap-wsdl-find-namespace (name wsdl
)
464 "Find a namespace by NAME in the WSDL document."
466 (dolist (ns (soap-wsdl-namespaces wsdl
))
467 (when (equal name
(soap-namespace-name ns
))
468 (throw 'found ns
)))))
470 (defun soap-wsdl-add-namespace (ns wsdl
)
471 "Add the namespace NS to the WSDL document.
472 If a namespace by this name already exists in WSDL, individual
473 elements will be added to it."
474 (let ((existing (soap-wsdl-find-namespace (soap-namespace-name ns
) wsdl
)))
476 ;; Add elements from NS to EXISTING, replacing existing values.
477 (maphash (lambda (key value
)
479 (soap-namespace-put v existing
)))
480 (soap-namespace-elements ns
))
481 (push ns
(soap-wsdl-namespaces wsdl
)))))
483 (defun soap-wsdl-get (name wsdl
&optional predicate use-local-alias-table
)
484 "Retrieve element NAME from the WSDL document.
486 PREDICATE is used to differentiate between elements when NAME
487 refers to multiple elements. A typical value for this would be a
488 structure predicate for the type of element you want to retrieve.
489 For example, to retrieve a message named \"foo\" when other
490 elements named \"foo\" exist in the WSDL you could use:
492 (soap-wsdl-get \"foo\" WSDL 'soap-message-p)
494 If USE-LOCAL-ALIAS-TABLE is not nil, `soap-local-xmlns' will be
495 used to resolve the namespace alias."
496 (let ((alias-table (soap-wsdl-alias-table wsdl
))
497 namespace element-name element
)
500 (setq name
(symbol-name name
)))
502 (when use-local-alias-table
503 (setq alias-table
(append soap-local-xmlns alias-table
)))
505 (cond ((consp name
) ; a fully qualified name, as returned by `soap-l2fq'
506 (setq element-name
(cdr name
))
507 (when (symbolp element-name
)
508 (setq element-name
(symbol-name element-name
)))
509 (setq namespace
(soap-wsdl-find-namespace (car name
) wsdl
))
511 (error "Soap-wsdl-get(%s): unknown namespace: %s" name namespace
)))
513 ((string-match "^\\(.*\\):\\(.*\\)$" name
)
514 (setq element-name
(match-string 2 name
))
516 (let* ((ns-alias (match-string 1 name
))
517 (ns-name (cdr (assoc ns-alias alias-table
))))
519 (error "Soap-wsdl-get(%s): cannot find namespace alias %s"
522 (setq namespace
(soap-wsdl-find-namespace ns-name wsdl
))
525 "Soap-wsdl-get(%s): unknown namespace %s, referenced by alias %s"
526 name ns-name ns-alias
))))
528 (error "Soap-wsdl-get(%s): bad name" name
)))
530 (setq element
(soap-namespace-get
531 element-name namespace
534 (or (funcall 'soap-namespace-link-p e
)
535 (funcall predicate e
)))
539 (error "Soap-wsdl-get(%s): cannot find element" name
))
541 (if (soap-namespace-link-p element
)
542 ;; NOTE: don't use the local alias table here
543 (soap-wsdl-get (soap-namespace-link-target element
) wsdl predicate
)
546 ;;;;; Resolving references for wsdl types
548 ;; See `soap-wsdl-resolve-references', which is the main entry point for
549 ;; resolving references
551 (defun soap-resolve-references-for-element (element wsdl
)
552 "Resolve references in ELEMENT using the WSDL document.
553 This is a generic function which invokes a specific function
554 depending on the element type.
556 If ELEMENT has no resolver function, it is silently ignored.
558 All references are resolved in-place, that is the ELEMENT is
560 (let ((resolver (get (aref element
0) 'soap-resolve-references
)))
562 (funcall resolver element wsdl
))))
564 (defun soap-resolve-references-for-simple-type (type wsdl
)
565 "Resolve the base type for the simple TYPE using the WSDL
567 (let ((kind (soap-basic-type-kind type
)))
568 (unless (symbolp kind
)
569 (let ((basic-type (soap-wsdl-get kind wsdl
'soap-basic-type-p
)))
570 (setf (soap-basic-type-kind type
)
571 (soap-basic-type-kind basic-type
))))))
573 (defun soap-resolve-references-for-sequence-type (type wsdl
)
574 "Resolve references for a sequence TYPE using WSDL document.
575 See also `soap-resolve-references-for-element' and
576 `soap-wsdl-resolve-references'"
577 (let ((parent (soap-sequence-type-parent type
)))
578 (when (or (consp parent
) (stringp parent
))
579 (setf (soap-sequence-type-parent type
)
582 ;; Prevent self references, see Bug#9
583 (lambda (e) (and (not (eq e type
)) (soap-type-p e
)))))))
584 (dolist (element (soap-sequence-type-elements type
))
585 (let ((element-type (soap-sequence-element-type element
)))
586 (cond ((or (consp element-type
) (stringp element-type
))
587 (setf (soap-sequence-element-type element
)
590 ;; Prevent self references, see Bug#9
591 (lambda (e) (and (not (eq e type
)) (soap-type-p e
))))))
592 ((soap-element-p element-type
)
593 ;; since the element already has a child element, it
594 ;; could be an inline structure. we must resolve
595 ;; references in it, because it might not be reached by
596 ;; scanning the wsdl names.
597 (soap-resolve-references-for-element element-type wsdl
))))))
599 (defun soap-resolve-references-for-array-type (type wsdl
)
600 "Resolve references for an array TYPE using WSDL.
601 See also `soap-resolve-references-for-element' and
602 `soap-wsdl-resolve-references'"
603 (let ((element-type (soap-array-type-element-type type
)))
604 (when (or (consp element-type
) (stringp element-type
))
605 (setf (soap-array-type-element-type type
)
608 ;; Prevent self references, see Bug#9
609 (lambda (e) (and (not (eq e type
)) (soap-type-p e
))))))))
611 (defun soap-resolve-references-for-message (message wsdl
)
612 "Resolve references for a MESSAGE type using the WSDL document.
613 See also `soap-resolve-references-for-element' and
614 `soap-wsdl-resolve-references'"
615 (let (resolved-parts)
616 (dolist (part (soap-message-parts message
))
617 (let ((name (car part
))
620 (setq name
(intern name
)))
621 (when (or (consp type
) (stringp type
))
622 (setq type
(soap-wsdl-get type wsdl
'soap-type-p
)))
623 (push (cons name type
) resolved-parts
)))
624 (setf (soap-message-parts message
) (nreverse resolved-parts
))))
626 (defun soap-resolve-references-for-operation (operation wsdl
)
627 "Resolve references for an OPERATION type using the WSDL document.
628 See also `soap-resolve-references-for-element' and
629 `soap-wsdl-resolve-references'"
630 (let ((input (soap-operation-input operation
))
632 (let ((name (car input
))
633 (message (cdr input
)))
634 ;; Name this part if it was not named
635 (when (or (null name
) (equal name
""))
636 (setq name
(format "in%d" (incf counter
))))
637 (when (or (consp message
) (stringp message
))
638 (setf (soap-operation-input operation
)
640 (soap-wsdl-get message wsdl
'soap-message-p
))))))
642 (let ((output (soap-operation-output operation
))
644 (let ((name (car output
))
645 (message (cdr output
)))
646 (when (or (null name
) (equal name
""))
647 (setq name
(format "out%d" (incf counter
))))
648 (when (or (consp message
) (stringp message
))
649 (setf (soap-operation-output operation
)
651 (soap-wsdl-get message wsdl
'soap-message-p
))))))
653 (let ((resolved-faults nil
)
655 (dolist (fault (soap-operation-faults operation
))
656 (let ((name (car fault
))
657 (message (cdr fault
)))
658 (when (or (null name
) (equal name
""))
659 (setq name
(format "fault%d" (incf counter
))))
660 (if (or (consp message
) (stringp message
))
661 (push (cons (intern name
)
662 (soap-wsdl-get message wsdl
'soap-message-p
))
664 (push fault resolved-faults
))))
665 (setf (soap-operation-faults operation
) resolved-faults
))
667 (when (= (length (soap-operation-parameter-order operation
)) 0)
668 (setf (soap-operation-parameter-order operation
)
669 (mapcar 'car
(soap-message-parts
670 (cdr (soap-operation-input operation
))))))
672 (setf (soap-operation-parameter-order operation
)
677 (soap-operation-parameter-order operation
))))
679 (defun soap-resolve-references-for-binding (binding wsdl
)
680 "Resolve references for a BINDING type using the WSDL document.
681 See also `soap-resolve-references-for-element' and
682 `soap-wsdl-resolve-references'"
683 (when (or (consp (soap-binding-port-type binding
))
684 (stringp (soap-binding-port-type binding
)))
685 (setf (soap-binding-port-type binding
)
686 (soap-wsdl-get (soap-binding-port-type binding
)
687 wsdl
'soap-port-type-p
)))
689 (let ((port-ops (soap-port-type-operations (soap-binding-port-type binding
))))
690 (maphash (lambda (k v
)
691 (setf (soap-bound-operation-operation v
)
692 (soap-namespace-get k port-ops
'soap-operation-p
)))
693 (soap-binding-operations binding
))))
695 (defun soap-resolve-references-for-port (port wsdl
)
696 "Resolve references for a PORT type using the WSDL document.
697 See also `soap-resolve-references-for-element' and
698 `soap-wsdl-resolve-references'"
699 (when (or (consp (soap-port-binding port
))
700 (stringp (soap-port-binding port
)))
701 (setf (soap-port-binding port
)
702 (soap-wsdl-get (soap-port-binding port
) wsdl
'soap-binding-p
))))
704 ;; Install resolvers for our types
706 (put (aref (make-soap-simple-type) 0) 'soap-resolve-references
707 'soap-resolve-references-for-simple-type
)
708 (put (aref (make-soap-sequence-type) 0) 'soap-resolve-references
709 'soap-resolve-references-for-sequence-type
)
710 (put (aref (make-soap-array-type) 0) 'soap-resolve-references
711 'soap-resolve-references-for-array-type
)
712 (put (aref (make-soap-message) 0) 'soap-resolve-references
713 'soap-resolve-references-for-message
)
714 (put (aref (make-soap-operation) 0) 'soap-resolve-references
715 'soap-resolve-references-for-operation
)
716 (put (aref (make-soap-binding) 0) 'soap-resolve-references
717 'soap-resolve-references-for-binding
)
718 (put (aref (make-soap-port) 0) 'soap-resolve-references
719 'soap-resolve-references-for-port
))
721 (defun soap-wsdl-resolve-references (wsdl)
722 "Resolve all references inside the WSDL structure.
724 When the WSDL elements are created from the XML document, they
725 refer to each other by name. For example, the ELEMENT-TYPE slot
726 of an SOAP-ARRAY-TYPE will contain the name of the element and
727 the user would have to call `soap-wsdl-get' to obtain the actual
730 After the entire document is loaded, we resolve all these
731 references to the actual elements they refer to so that at
732 runtime, we don't have to call `soap-wsdl-get' each time we
733 traverse an element tree."
736 (alias-table (soap-wsdl-alias-table wsdl
)))
737 (dolist (ns (soap-wsdl-namespaces wsdl
))
738 (let ((nstag (car-safe (rassoc (soap-namespace-name ns
) alias-table
))))
740 ;; If this namespace does not have an alias, create one for it.
743 (setq nstag
(format "ns%d" (incf nstag-id
)))
744 (unless (assoc nstag alias-table
)
745 (soap-wsdl-add-alias nstag
(soap-namespace-name ns
) wsdl
)
748 (maphash (lambda (name element
)
749 (cond ((soap-element-p element
) ; skip links
751 (soap-resolve-references-for-element element wsdl
)
752 (setf (soap-element-namespace-tag element
) nstag
))
755 (when (soap-element-p e
)
757 (soap-resolve-references-for-element e wsdl
)
758 (setf (soap-element-namespace-tag e
) nstag
))))))
759 (soap-namespace-elements ns
)))))
762 ;;;;; Loading WSDL from XML documents
764 (defun soap-load-wsdl-from-url (url)
765 "Load a WSDL document from URL and return it.
766 The returned WSDL document needs to be used for `soap-invoke'
768 (let ((url-request-method "GET")
769 (url-package-name "soap-client.el")
770 (url-package-version "1.0")
771 (url-mime-charset-string "utf-8;q=1, iso-8859-1;q=0.5")
772 (url-request-coding-system 'utf-8
)
773 (url-http-attempt-keepalives nil
))
774 (let ((buffer (url-retrieve-synchronously url
)))
775 (with-current-buffer buffer
776 (declare (special url-http-response-status
))
777 (if (> url-http-response-status
299)
778 (error "Error retrieving WSDL: %s" url-http-response-status
))
779 (let ((mime-part (mm-dissect-buffer t t
)))
781 (error "Failed to decode response from server"))
782 (unless (equal (car (mm-handle-type mime-part
)) "text/xml")
783 (error "Server response is not an XML document"))
785 (mm-insert-part mime-part
)
786 (let ((wsdl-xml (car (xml-parse-region (point-min) (point-max)))))
788 (let ((wsdl (soap-parse-wsdl wsdl-xml
)))
789 (setf (soap-wsdl-origin wsdl
) url
)
791 (kill-buffer buffer
)))))))))
793 (defun soap-load-wsdl (file)
794 "Load a WSDL document from FILE and return it."
796 (insert-file-contents file
)
797 (let ((xml (car (xml-parse-region (point-min) (point-max)))))
798 (let ((wsdl (soap-parse-wsdl xml
)))
799 (setf (soap-wsdl-origin wsdl
) file
)
802 (defun soap-parse-wsdl (node)
803 "Construct a WSDL structure from NODE, which is an XML document."
804 (soap-with-local-xmlns node
806 (assert (eq (soap-l2wk (xml-node-name node
)) 'wsdl
:definitions
)
808 "soap-parse-wsdl: expecting wsdl:definitions node, got %s"
809 (soap-l2wk (xml-node-name node
)))
811 (let ((wsdl (make-soap-wsdl)))
813 ;; Add the local alias table to the wsdl document -- it will be used for
814 ;; all types in this document even after we finish parsing it.
815 (setf (soap-wsdl-alias-table wsdl
) soap-local-xmlns
)
817 ;; Add the XSD types to the wsdl document
818 (let ((ns (soap-default-xsd-types)))
819 (soap-wsdl-add-namespace ns wsdl
)
820 (soap-wsdl-add-alias "xsd" (soap-namespace-name ns
) wsdl
))
822 ;; Add the soapenc types to the wsdl document
823 (let ((ns (soap-default-soapenc-types)))
824 (soap-wsdl-add-namespace ns wsdl
)
825 (soap-wsdl-add-alias "soapenc" (soap-namespace-name ns
) wsdl
))
827 ;; Find all the 'xsd:schema nodes which are children of wsdl:types nodes
828 ;; and build our type-library
830 (let ((types (car (soap-xml-get-children1 node
'wsdl
:types
))))
831 (dolist (node (xml-node-children types
))
832 ;; We cannot use (xml-get-children node (soap-wk2l 'xsd:schema))
833 ;; because each node can install its own alias type so the schema
834 ;; nodes might have a different prefix.
836 (soap-with-local-xmlns node
837 (when (eq (soap-l2wk (xml-node-name node
)) 'xsd
:schema
)
838 (soap-wsdl-add-namespace (soap-parse-schema node
) wsdl
))))))
840 (let ((ns (make-soap-namespace :name
(soap-get-target-namespace node
))))
841 (dolist (node (soap-xml-get-children1 node
'wsdl
:message
))
842 (soap-namespace-put (soap-parse-message node
) ns
))
844 (dolist (node (soap-xml-get-children1 node
'wsdl
:portType
))
845 (let ((port-type (soap-parse-port-type node
)))
846 (soap-namespace-put port-type ns
)
847 (soap-wsdl-add-namespace
848 (soap-port-type-operations port-type
) wsdl
)))
850 (dolist (node (soap-xml-get-children1 node
'wsdl
:binding
))
851 (soap-namespace-put (soap-parse-binding node
) ns
))
853 (dolist (node (soap-xml-get-children1 node
'wsdl
:service
))
854 (dolist (node (soap-xml-get-children1 node
'wsdl
:port
))
855 (let ((name (xml-get-attribute node
'name
))
856 (binding (xml-get-attribute node
'binding
))
857 (url (let ((n (car (soap-xml-get-children1
858 node
'wsdlsoap
:address
))))
859 (xml-get-attribute n
'location
))))
860 (let ((port (make-soap-port
861 :name name
:binding
(soap-l2fq binding
'tns
)
863 (soap-namespace-put port ns
)
864 (push port
(soap-wsdl-ports wsdl
))))))
866 (soap-wsdl-add-namespace ns wsdl
))
868 (soap-wsdl-resolve-references wsdl
)
872 (defun soap-parse-schema (node)
873 "Parse a schema NODE.
874 Return a SOAP-NAMESPACE containing the elements."
875 (soap-with-local-xmlns node
876 (assert (eq (soap-l2wk (xml-node-name node
)) 'xsd
:schema
)
878 "soap-parse-schema: expecting an xsd:schema node, got %s"
879 (soap-l2wk (xml-node-name node
)))
880 (let ((ns (make-soap-namespace :name
(soap-get-target-namespace node
))))
881 ;; NOTE: we only extract the complexTypes from the schema, we wouldn't
882 ;; know how to handle basic types beyond the built in ones anyway.
883 (dolist (node (soap-xml-get-children1 node
'xsd
:simpleType
))
884 (soap-namespace-put (soap-parse-simple-type node
) ns
))
886 (dolist (node (soap-xml-get-children1 node
'xsd
:complexType
))
887 (soap-namespace-put (soap-parse-complex-type node
) ns
))
889 (dolist (node (soap-xml-get-children1 node
'xsd
:element
))
890 (soap-namespace-put (soap-parse-schema-element node
) ns
))
894 (defun soap-parse-simple-type (node)
895 "Parse NODE and construct a simple type from it."
896 (assert (eq (soap-l2wk (xml-node-name node
)) 'xsd
:simpleType
)
898 "soap-parse-complex-type: expecting xsd:simpleType node, got %s"
899 (soap-l2wk (xml-node-name node
)))
900 (let ((name (xml-get-attribute-or-nil node
'name
))
903 (restriction (car-safe
904 (soap-xml-get-children1 node
'xsd
:restriction
))))
906 (error "simpleType %s has no base type" name
))
908 (setq type
(xml-get-attribute-or-nil restriction
'base
))
909 (dolist (e (soap-xml-get-children1 restriction
'xsd
:enumeration
))
910 (push (xml-get-attribute e
'value
) enumeration
))
912 (make-soap-simple-type :name name
:kind type
:enumeration enumeration
)))
914 (defun soap-parse-schema-element (node)
915 "Parse NODE and construct a schema element from it."
916 (assert (eq (soap-l2wk (xml-node-name node
)) 'xsd
:element
)
918 "soap-parse-schema-element: expecting xsd:element node, got %s"
919 (soap-l2wk (xml-node-name node
)))
920 (let ((name (xml-get-attribute-or-nil node
'name
))
922 ;; A schema element that contains an inline complex type --
923 ;; construct the actual complex type for it.
924 (let ((type-node (soap-xml-get-children1 node
'xsd
:complexType
)))
925 (when (> (length type-node
) 0)
926 (assert (= (length type-node
) 1)) ; only one complex type
927 ; definition per element
928 (setq type
(soap-parse-complex-type (car type-node
)))))
929 (setf (soap-element-name type
) name
)
932 (defun soap-parse-complex-type (node)
933 "Parse NODE and construct a complex type from it."
934 (assert (eq (soap-l2wk (xml-node-name node
)) 'xsd
:complexType
)
936 "soap-parse-complex-type: expecting xsd:complexType node, got %s"
937 (soap-l2wk (xml-node-name node
)))
938 (let ((name (xml-get-attribute-or-nil node
'name
))
939 ;; Use a dummy type for the complex type, it will be replaced
940 ;; with the real type below, except when the complex type node
942 (type (make-soap-sequence-type :elements nil
)))
943 (dolist (c (xml-node-children node
))
944 (when (consp c
) ; skip string nodes, which are whitespace
945 (let ((node-name (soap-l2wk (xml-node-name c
))))
947 ;; The difference between xsd:all and xsd:sequence is that fields
948 ;; in xsd:all are not ordered and they can occur only once. We
949 ;; don't care about that difference in soap-client.el
950 ((or (eq node-name
'xsd
:sequence
)
951 (eq node-name
'xsd
:all
))
952 (setq type
(soap-parse-complex-type-sequence c
)))
953 ((eq node-name
'xsd
:complexContent
)
954 (setq type
(soap-parse-complex-type-complex-content c
)))
955 ((eq node-name
'xsd
:attribute
)
956 ;; The name of this node comes from an attribute tag
957 (let ((n (xml-get-attribute-or-nil c
'name
)))
960 (error "Unknown node type %s" node-name
))))))
961 (setf (soap-element-name type
) name
)
964 (defun soap-parse-sequence (node)
965 "Parse NODE and a list of sequence elements that it defines.
966 NODE is assumed to be an xsd:sequence node. In that case, each
967 of its children is assumed to be a sequence element. Each
968 sequence element is parsed constructing the corresponding type.
969 A list of these types is returned."
970 (assert (let ((n (soap-l2wk (xml-node-name node
))))
971 (memq n
'(xsd:sequence xsd
:all
)))
973 "soap-parse-sequence: expecting xsd:sequence or xsd:all node, got %s"
974 (soap-l2wk (xml-node-name node
)))
976 (dolist (e (soap-xml-get-children1 node
'xsd
:element
))
977 (let ((name (xml-get-attribute-or-nil e
'name
))
978 (type (xml-get-attribute-or-nil e
'type
))
979 (nillable?
(or (equal (xml-get-attribute-or-nil e
'nillable
) "true")
980 (let ((e (xml-get-attribute-or-nil e
'minOccurs
)))
981 (and e
(equal e
"0")))))
982 (multiple?
(let ((e (xml-get-attribute-or-nil e
'maxOccurs
)))
983 (and e
(not (equal e
"1"))))))
985 (setq type
(soap-l2fq type
'tns
))
987 ;; The node does not have a type, maybe it has a complexType
989 (let ((type-node (soap-xml-get-children1 e
'xsd
:complexType
)))
990 (when (> (length type-node
) 0)
991 (assert (= (length type-node
) 1)
993 "only one complex type definition per element supported")
994 (setq type
(soap-parse-complex-type (car type-node
))))))
996 (push (make-soap-sequence-element
997 :name
(intern name
) :type type
:nillable? nillable?
998 :multiple? multiple?
)
1000 (nreverse elements
)))
1002 (defun soap-parse-complex-type-sequence (node)
1003 "Parse NODE as a sequence type."
1004 (let ((elements (soap-parse-sequence node
)))
1005 (make-soap-sequence-type :elements elements
)))
1007 (defun soap-parse-complex-type-complex-content (node)
1008 "Parse NODE as a xsd:complexContent node.
1009 A sequence or an array type is returned depending on the actual
1011 (assert (eq (soap-l2wk (xml-node-name node
)) 'xsd
:complexContent
)
1013 "soap-parse-complex-type-complex-content: expecting xsd:complexContent node, got %s"
1014 (soap-l2wk (xml-node-name node
)))
1015 (let (array? parent elements
)
1016 (let ((extension (car-safe (soap-xml-get-children1 node
'xsd
:extension
)))
1017 (restriction (car-safe
1018 (soap-xml-get-children1 node
'xsd
:restriction
))))
1019 ;; a complex content node is either an extension or a restriction
1021 (setq parent
(xml-get-attribute-or-nil extension
'base
))
1022 (setq elements
(soap-parse-sequence
1023 (car (soap-xml-get-children1
1024 extension
'xsd
:sequence
)))))
1026 (let ((base (xml-get-attribute-or-nil restriction
'base
)))
1027 (assert (equal base
(soap-wk2l "soapenc:Array"))
1029 "restrictions supported only for soapenc:Array types, this is a %s"
1032 (let ((attribute (car (soap-xml-get-children1
1033 restriction
'xsd
:attribute
))))
1034 (let ((array-type (soap-xml-get-attribute-or-nil1
1035 attribute
'wsdl
:arrayType
)))
1036 (when (string-match "^\\(.*\\)\\[\\]$" array-type
)
1037 (setq parent
(match-string 1 array-type
))))))
1040 (error "Unknown complex type"))))
1043 (setq parent
(soap-l2fq parent
'tns
)))
1046 (make-soap-array-type :element-type parent
)
1047 (make-soap-sequence-type :parent parent
:elements elements
))))
1049 (defun soap-parse-message (node)
1050 "Parse NODE as a wsdl:message and return the corresponding type."
1051 (assert (eq (soap-l2wk (xml-node-name node
)) 'wsdl
:message
)
1053 "soap-parse-message: expecting wsdl:message node, got %s"
1054 (soap-l2wk (xml-node-name node
)))
1055 (let ((name (xml-get-attribute-or-nil node
'name
))
1057 (dolist (p (soap-xml-get-children1 node
'wsdl
:part
))
1058 (let ((name (xml-get-attribute-or-nil p
'name
))
1059 (type (xml-get-attribute-or-nil p
'type
))
1060 (element (xml-get-attribute-or-nil p
'element
)))
1063 (setq type
(soap-l2fq type
'tns
)))
1066 (setq element
(soap-l2fq element
'tns
)))
1068 (push (cons name
(or type element
)) parts
)))
1069 (make-soap-message :name name
:parts
(nreverse parts
))))
1071 (defun soap-parse-port-type (node)
1072 "Parse NODE as a wsdl:portType and return the corresponding port."
1073 (assert (eq (soap-l2wk (xml-node-name node
)) 'wsdl
:portType
)
1075 "soap-parse-port-type: expecting wsdl:portType node got %s"
1076 (soap-l2wk (xml-node-name node
)))
1077 (let ((ns (make-soap-namespace
1078 :name
(concat "urn:" (xml-get-attribute node
'name
)))))
1079 (dolist (node (soap-xml-get-children1 node
'wsdl
:operation
))
1080 (let ((o (soap-parse-operation node
)))
1082 (let ((other-operation (soap-namespace-get
1083 (soap-element-name o
) ns
'soap-operation-p
)))
1085 ;; Unfortunately, the Confluence WSDL defines two operations
1086 ;; named "search" which differ only in parameter names...
1087 (soap-warning "Discarding duplicate operation: %s"
1088 (soap-element-name o
))
1091 (soap-namespace-put o ns
)
1093 ;; link all messages from this namespace, as this namespace
1094 ;; will be used for decoding the response.
1095 (destructuring-bind (name . message
) (soap-operation-input o
)
1096 (soap-namespace-put-link name message ns
))
1098 (destructuring-bind (name . message
) (soap-operation-output o
)
1099 (soap-namespace-put-link name message ns
))
1101 (dolist (fault (soap-operation-faults o
))
1102 (destructuring-bind (name . message
) fault
1103 (soap-namespace-put-link name message ns
'replace
)))
1107 (make-soap-port-type :name
(xml-get-attribute node
'name
)
1110 (defun soap-parse-operation (node)
1111 "Parse NODE as a wsdl:operation and return the corresponding type."
1112 (assert (eq (soap-l2wk (xml-node-name node
)) 'wsdl
:operation
)
1114 "soap-parse-operation: expecting wsdl:operation node, got %s"
1115 (soap-l2wk (xml-node-name node
)))
1116 (let ((name (xml-get-attribute node
'name
))
1117 (parameter-order (split-string
1118 (xml-get-attribute node
'parameterOrder
)))
1119 input output faults
)
1120 (dolist (n (xml-node-children node
))
1121 (when (consp n
) ; skip string nodes which are whitespace
1122 (let ((node-name (soap-l2wk (xml-node-name n
))))
1124 ((eq node-name
'wsdl
:input
)
1125 (let ((message (xml-get-attribute n
'message
))
1126 (name (xml-get-attribute n
'name
)))
1127 (setq input
(cons name
(soap-l2fq message
'tns
)))))
1128 ((eq node-name
'wsdl
:output
)
1129 (let ((message (xml-get-attribute n
'message
))
1130 (name (xml-get-attribute n
'name
)))
1131 (setq output
(cons name
(soap-l2fq message
'tns
)))))
1132 ((eq node-name
'wsdl
:fault
)
1133 (let ((message (xml-get-attribute n
'message
))
1134 (name (xml-get-attribute n
'name
)))
1135 (push (cons name
(soap-l2fq message
'tns
)) faults
)))))))
1136 (make-soap-operation
1138 :parameter-order parameter-order
1141 :faults
(nreverse faults
))))
1143 (defun soap-parse-binding (node)
1144 "Parse NODE as a wsdl:binding and return the corresponding type."
1145 (assert (eq (soap-l2wk (xml-node-name node
)) 'wsdl
:binding
)
1147 "soap-parse-binding: expecting wsdl:binding node, got %s"
1148 (soap-l2wk (xml-node-name node
)))
1149 (let ((name (xml-get-attribute node
'name
))
1150 (type (xml-get-attribute node
'type
)))
1151 (let ((binding (make-soap-binding :name name
1152 :port-type
(soap-l2fq type
'tns
))))
1153 (dolist (wo (soap-xml-get-children1 node
'wsdl
:operation
))
1154 (let ((name (xml-get-attribute wo
'name
))
1157 (dolist (so (soap-xml-get-children1 wo
'wsdlsoap
:operation
))
1158 (setq soap-action
(xml-get-attribute-or-nil so
'soapAction
)))
1160 ;; Search a wsdlsoap:body node and find a "use" tag. The
1161 ;; same use tag is assumed to be present for both input and
1162 ;; output types (although the WDSL spec allows separate
1163 ;; "use"-s for each of them...
1165 (dolist (i (soap-xml-get-children1 wo
'wsdl
:input
))
1166 (dolist (b (soap-xml-get-children1 i
'wsdlsoap
:body
))
1168 (xml-get-attribute-or-nil b
'use
)))))
1171 (dolist (i (soap-xml-get-children1 wo
'wsdl
:output
))
1172 (dolist (b (soap-xml-get-children1 i
'wsdlsoap
:body
))
1174 (xml-get-attribute-or-nil b
'use
))))))
1176 (puthash name
(make-soap-bound-operation :operation name
1177 :soap-action soap-action
1178 :use
(and use
(intern use
)))
1179 (soap-binding-operations binding
))))
1182 ;;;; SOAP type decoding
1184 (defvar soap-multi-refs nil
1185 "The list of multi-ref nodes in the current SOAP response.
1186 This is a dynamically bound variable used during decoding the
1189 (defvar soap-decoded-multi-refs nil
1190 "List of decoded multi-ref nodes in the current SOAP response.
1191 This is a dynamically bound variable used during decoding the
1194 (defvar soap-current-wsdl nil
1195 "The current WSDL document used when decoding the SOAP response.
1196 This is a dynamically bound variable.")
1198 (defun soap-decode-type (type node
)
1199 "Use TYPE (an xsd type) to decode the contents of NODE.
1201 NODE is an XML node, representing some SOAP encoded value or a
1202 reference to another XML node (a multiRef). This function will
1203 resolve the multiRef reference, if any, than call a TYPE specific
1204 decode function to perform the actual decoding."
1205 (let ((href (xml-get-attribute-or-nil node
'href
)))
1208 ;; NODE is actually a HREF, find the target and decode that.
1209 ;; Check first if we already decoded this multiref.
1211 (let ((decoded (cdr (assoc href soap-decoded-multi-refs
))))
1213 (throw 'done decoded
)))
1215 (string-match "^#\\(.*\\)$" href
) ; TODO: check that it matched
1217 (let ((id (match-string 1 href
)))
1218 (dolist (mr soap-multi-refs
)
1219 (let ((mrid (xml-get-attribute mr
'id
)))
1220 (when (equal id mrid
)
1221 ;; recurse here, in case there are multiple HREF's
1222 (let ((decoded (soap-decode-type type mr
)))
1223 (push (cons href decoded
) soap-decoded-multi-refs
)
1224 (throw 'done decoded
)))))
1225 (error "Cannot find href %s" href
))))
1227 (soap-with-local-xmlns node
1228 (if (equal (soap-xml-get-attribute-or-nil1 node
'xsi
:nil
) "true")
1230 (let ((decoder (get (aref type
0) 'soap-decoder
)))
1231 (assert decoder nil
"no soap-decoder for %s type"
1233 (funcall decoder type node
))))))))
1235 (defun soap-decode-any-type (node)
1236 "Decode NODE using type information inside it."
1237 ;; If the NODE has type information, we use that...
1238 (let ((type (soap-xml-get-attribute-or-nil1 node
'xsi
:type
)))
1240 (let ((wtype (soap-wsdl-get type soap-current-wsdl
'soap-type-p
)))
1242 (soap-decode-type wtype node
)
1243 ;; The node has type info encoded in it, but we don't know how
1245 (error "Soap-decode-any-type: node has unknown type: %s" type
)))
1247 ;; No type info in the node...
1249 (let ((contents (xml-node-children node
)))
1250 (if (and (= (length contents
) 1) (stringp (car contents
)))
1251 ;; contents is just a string
1254 ;; we assume the NODE is a sequence with every element a
1257 (dolist (element contents
)
1258 (let ((key (xml-node-name element
))
1259 (value (soap-decode-any-type element
)))
1260 (push (cons key value
) result
)))
1261 (nreverse result
)))))))
1263 (defun soap-decode-array (node)
1264 "Decode NODE as an Array using type information inside it."
1265 (let ((type (soap-xml-get-attribute-or-nil1 node
'soapenc
:arrayType
))
1267 (contents (xml-node-children node
))
1270 ;; Type is in the format "someType[NUM]" where NUM is the number of
1271 ;; elements in the array. We discard the [NUM] part.
1272 (setq type
(replace-regexp-in-string "\\[[0-9]+\\]\\'" "" type
))
1273 (setq wtype
(soap-wsdl-get type soap-current-wsdl
'soap-type-p
))
1275 ;; The node has type info encoded in it, but we don't know how to
1277 (error "Soap-decode-array: node has unknown type: %s" type
)))
1278 (dolist (e contents
)
1281 (soap-decode-type wtype e
)
1282 (soap-decode-any-type e
))
1286 (defun soap-decode-basic-type (type node
)
1287 "Use TYPE to decode the contents of NODE.
1288 TYPE is a `soap-basic-type' struct, and NODE is an XML document.
1289 A LISP value is returned based on the contents of NODE and the
1290 type-info stored in TYPE."
1291 (let ((contents (xml-node-children node
))
1292 (type-kind (soap-basic-type-kind type
)))
1297 ((string anyURI
) (car contents
))
1298 (dateTime (car contents
)) ; TODO: convert to a date time
1299 ((long int integer unsignedInt byte float double
) (string-to-number (car contents
)))
1300 (boolean (string= (downcase (car contents
)) "true"))
1301 (base64Binary (base64-decode-string (car contents
)))
1302 (anyType (soap-decode-any-type node
))
1303 (Array (soap-decode-array node
))))))
1305 (defun soap-decode-sequence-type (type node
)
1306 "Use TYPE to decode the contents of NODE.
1307 TYPE is assumed to be a sequence type and an ALIST with the
1308 contents of the NODE is returned."
1310 (parent (soap-sequence-type-parent type
)))
1312 (setq result
(nreverse (soap-decode-type parent node
))))
1313 (dolist (element (soap-sequence-type-elements type
))
1314 (let ((instance-count 0)
1315 (e-name (soap-sequence-element-name element
))
1316 (e-type (soap-sequence-element-type element
)))
1317 (dolist (node (xml-get-children node e-name
))
1318 (incf instance-count
)
1319 (push (cons e-name
(soap-decode-type e-type node
)) result
))
1320 ;; Do some sanity checking
1321 (cond ((and (= instance-count
0)
1322 (not (soap-sequence-element-nillable? element
)))
1323 (soap-warning "While decoding %s: missing non-nillable slot %s"
1324 (soap-element-name type
) e-name
))
1325 ((and (> instance-count
1)
1326 (not (soap-sequence-element-multiple? element
)))
1327 (soap-warning "While decoding %s: multiple slots named %s"
1328 (soap-element-name type
) e-name
)))))
1331 (defun soap-decode-array-type (type node
)
1332 "Use TYPE to decode the contents of NODE.
1333 TYPE is assumed to be an array type. Arrays are decoded as lists.
1334 This is because it is easier to work with list results in LISP."
1336 (element-type (soap-array-type-element-type type
)))
1337 (dolist (node (xml-node-children node
))
1339 (push (soap-decode-type element-type node
) result
)))
1343 (put (aref (make-soap-basic-type) 0)
1344 'soap-decoder
'soap-decode-basic-type
)
1345 ;; just use the basic type decoder for the simple type -- we accept any
1346 ;; value and don't do any validation on it.
1347 (put (aref (make-soap-simple-type) 0)
1348 'soap-decoder
'soap-decode-basic-type
)
1349 (put (aref (make-soap-sequence-type) 0)
1350 'soap-decoder
'soap-decode-sequence-type
)
1351 (put (aref (make-soap-array-type) 0)
1352 'soap-decoder
'soap-decode-array-type
))
1354 ;;;; Soap Envelope parsing
1356 (define-error 'soap-error
"SOAP error")
1358 (defun soap-parse-envelope (node operation wsdl
)
1359 "Parse the SOAP envelope in NODE and return the response.
1360 OPERATION is the WSDL operation for which we expect the response,
1361 WSDL is used to decode the NODE"
1362 (soap-with-local-xmlns node
1363 (assert (eq (soap-l2wk (xml-node-name node
)) 'soap
:Envelope
)
1365 "soap-parse-envelope: expecting soap:Envelope node, got %s"
1366 (soap-l2wk (xml-node-name node
)))
1367 (let ((body (car (soap-xml-get-children1 node
'soap
:Body
))))
1369 (let ((fault (car (soap-xml-get-children1 body
'soap
:Fault
))))
1371 (let ((fault-code (let ((n (car (xml-get-children
1372 fault
'faultcode
))))
1373 (car-safe (xml-node-children n
))))
1374 (fault-string (let ((n (car (xml-get-children
1375 fault
'faultstring
))))
1376 (car-safe (xml-node-children n
))))
1377 (detail (xml-get-children fault
'detail
)))
1379 (signal 'soap-error
(list fault-code fault-string detail
))))))
1381 ;; First (non string) element of the body is the root node of he
1383 (let ((response (if (eq (soap-bound-operation-use operation
) 'literal
)
1384 ;; For 'literal uses, the response is the actual body
1386 ;; ...otherwise the first non string element
1387 ;; of the body is the response
1389 (dolist (n (xml-node-children body
))
1391 (throw 'found n
)))))))
1392 (soap-parse-response response operation wsdl body
)))))
1394 (defun soap-parse-response (response-node operation wsdl soap-body
)
1395 "Parse RESPONSE-NODE and return the result as a LISP value.
1396 OPERATION is the WSDL operation for which we expect the response,
1397 WSDL is used to decode the NODE.
1399 SOAP-BODY is the body of the SOAP envelope (of which
1400 RESPONSE-NODE is a sub-node). It is used in case RESPONSE-NODE
1401 reference multiRef parts which are external to RESPONSE-NODE."
1402 (let* ((soap-current-wsdl wsdl
)
1403 (op (soap-bound-operation-operation operation
))
1404 (use (soap-bound-operation-use operation
))
1405 (message (cdr (soap-operation-output op
))))
1407 (soap-with-local-xmlns response-node
1409 (when (eq use
'encoded
)
1410 (let* ((received-message-name (soap-l2fq (xml-node-name response-node
)))
1411 (received-message (soap-wsdl-get
1412 received-message-name wsdl
'soap-message-p
)))
1413 (unless (eq received-message message
)
1414 (error "Unexpected message: got %s, expecting %s"
1415 received-message-name
1416 (soap-element-name message
)))))
1418 (let ((decoded-parts nil
)
1419 (soap-multi-refs (xml-get-children soap-body
'multiRef
))
1420 (soap-decoded-multi-refs nil
))
1422 (dolist (part (soap-message-parts message
))
1423 (let ((tag (car part
))
1430 (car (xml-get-children response-node tag
)))
1434 (let* ((ns-aliases (soap-wsdl-alias-table wsdl
))
1435 (ns-name (cdr (assoc
1436 (soap-element-namespace-tag type
)
1438 (fqname (cons ns-name
(soap-element-name type
))))
1439 (dolist (c (xml-node-children response-node
))
1441 (soap-with-local-xmlns c
1442 (when (equal (soap-l2fq (xml-node-name c
))
1444 (throw 'found c
))))))))))
1447 (error "Soap-parse-response(%s): cannot find message part %s"
1448 (soap-element-name op
) tag
))
1449 (push (soap-decode-type type node
) decoded-parts
)))
1453 ;;;; SOAP type encoding
1455 (defvar soap-encoded-namespaces nil
1456 "A list of namespace tags used during encoding a message.
1457 This list is populated by `soap-encode-value' and used by
1458 `soap-create-envelope' to add aliases for these namespace to the
1461 This variable is dynamically bound in `soap-create-envelope'.")
1463 (defun soap-encode-value (xml-tag value type
)
1464 "Encode inside an XML-TAG the VALUE using TYPE.
1465 The resulting XML data is inserted in the current buffer
1468 TYPE is one of the soap-*-type structures which defines how VALUE
1469 is to be encoded. This is a generic function which finds an
1470 encoder function based on TYPE and calls that encoder to do the
1472 (let ((encoder (get (aref type
0) 'soap-encoder
)))
1473 (assert encoder nil
"no soap-encoder for %s type" (aref type
0))
1474 ;; XML-TAG can be a string or a symbol, but we pass only string's to the
1476 (when (symbolp xml-tag
)
1477 (setq xml-tag
(symbol-name xml-tag
)))
1478 (funcall encoder xml-tag value type
))
1479 (add-to-list 'soap-encoded-namespaces
(soap-element-namespace-tag type
)))
1481 (defun soap-encode-basic-type (xml-tag value type
)
1482 "Encode inside XML-TAG the LISP VALUE according to TYPE.
1483 Do not call this function directly, use `soap-encode-value'
1485 (let ((xsi-type (soap-element-fq-name type
))
1486 (basic-type (soap-basic-type-kind type
)))
1488 ;; try to classify the type based on the value type and use that type when
1490 (when (eq basic-type
'anyType
)
1491 (cond ((stringp value
)
1492 (setq xsi-type
"xsd:string" basic-type
'string
))
1494 (setq xsi-type
"xsd:int" basic-type
'int
))
1495 ((memq value
'(t nil
))
1496 (setq xsi-type
"xsd:boolean" basic-type
'boolean
))
1499 "Soap-encode-basic-type(%s, %s, %s): cannot classify anyType value"
1500 xml-tag value xsi-type
))))
1502 (insert "<" xml-tag
" xsi:type=\"" xsi-type
"\"")
1504 ;; We have some ambiguity here, as a nil value represents "false" when the
1505 ;; type is boolean, we will never have a "nil" boolean type...
1507 (if (or value
(eq basic-type
'boolean
))
1512 (unless (stringp value
)
1513 (error "Soap-encode-basic-type(%s, %s, %s): not a string value"
1514 xml-tag value xsi-type
))
1515 (insert (url-insert-entities-in-string value
)))
1518 (cond ((and (consp value
) ; is there a time-value-p ?
1519 (>= (length value
) 2)
1520 (numberp (nth 0 value
))
1521 (numberp (nth 1 value
)))
1522 ;; Value is a (current-time) style value, convert
1524 (insert (format-time-string "%Y-%m-%dT%H:%M:%S" value
)))
1526 (insert (url-insert-entities-in-string value
)))
1529 "Soap-encode-basic-type(%s, %s, %s): not a dateTime value"
1530 xml-tag value xsi-type
))))
1533 (unless (memq value
'(t nil
))
1534 (error "Soap-encode-basic-type(%s, %s, %s): not a boolean value"
1535 xml-tag value xsi-type
))
1536 (insert (if value
"true" "false")))
1538 ((long int integer byte unsignedInt
)
1539 (unless (integerp value
)
1540 (error "Soap-encode-basic-type(%s, %s, %s): not an integer value"
1541 xml-tag value xsi-type
))
1542 (when (and (eq basic-type
'unsignedInt
) (< value
0))
1543 (error "Soap-encode-basic-type(%s, %s, %s): not a positive integer"
1544 xml-tag value xsi-type
))
1545 (insert (number-to-string value
)))
1548 (unless (numberp value
)
1549 (error "Soap-encode-basic-type(%s, %s, %s): not a number"
1550 xml-tag value xsi-type
))
1551 (insert (number-to-string value
)))
1554 (unless (stringp value
)
1555 (error "Soap-encode-basic-type(%s, %s, %s): not a string value"
1556 xml-tag value xsi-type
))
1557 (insert (base64-encode-string value
)))
1561 "Soap-encode-basic-type(%s, %s, %s): don't know how to encode"
1562 xml-tag value xsi-type
))))
1564 (insert " xsi:nil=\"true\">"))
1565 (insert "</" xml-tag
">\n")))
1567 (defun soap-encode-simple-type (xml-tag value type
)
1568 "Encode inside XML-TAG the LISP VALUE according to TYPE."
1570 ;; Validate VALUE against the simple type's enumeration, than just encode it
1571 ;; using `soap-encode-basic-type'
1573 (let ((enumeration (soap-simple-type-enumeration type
)))
1574 (unless (and (> (length enumeration
) 1)
1575 (member value enumeration
))
1576 (error "soap-encode-simple-type(%s, %s, %s): bad value, should be one of %s"
1577 xml-tag value
(soap-element-fq-name type
) enumeration
)))
1579 (soap-encode-basic-type xml-tag value type
))
1581 (defun soap-encode-sequence-type (xml-tag value type
)
1582 "Encode inside XML-TAG the LISP VALUE according to TYPE.
1583 Do not call this function directly, use `soap-encode-value'
1585 (let ((xsi-type (soap-element-fq-name type
)))
1586 (insert "<" xml-tag
" xsi:type=\"" xsi-type
"\"")
1590 (let ((parents (list type
))
1591 (parent (soap-sequence-type-parent type
)))
1594 (push parent parents
)
1595 (setq parent
(soap-sequence-type-parent parent
)))
1597 (dolist (type parents
)
1598 (dolist (element (soap-sequence-type-elements type
))
1599 (let ((instance-count 0)
1600 (e-name (soap-sequence-element-name element
))
1601 (e-type (soap-sequence-element-type element
)))
1603 (when (equal (car v
) e-name
)
1604 (incf instance-count
)
1605 (soap-encode-value e-name
(cdr v
) e-type
)))
1607 ;; Do some sanity checking
1608 (cond ((and (= instance-count
0)
1609 (not (soap-sequence-element-nillable? element
)))
1611 "While encoding %s: missing non-nillable slot %s"
1612 (soap-element-name type
) e-name
))
1613 ((and (> instance-count
1)
1614 (not (soap-sequence-element-multiple? element
)))
1616 "While encoding %s: multiple slots named %s"
1617 (soap-element-name type
) e-name
))))))))
1618 (insert " xsi:nil=\"true\">"))
1619 (insert "</" xml-tag
">\n")))
1621 (defun soap-encode-array-type (xml-tag value type
)
1622 "Encode inside XML-TAG the LISP VALUE according to TYPE.
1623 Do not call this function directly, use `soap-encode-value'
1625 (unless (vectorp value
)
1626 (error "Soap-encode: %s(%s) expects a vector, got: %s"
1627 xml-tag
(soap-element-fq-name type
) value
))
1628 (let* ((element-type (soap-array-type-element-type type
))
1629 (array-type (concat (soap-element-fq-name element-type
)
1630 "[" (format "%s" (length value
)) "]")))
1632 " soapenc:arrayType=\"" array-type
"\" "
1633 " xsi:type=\"soapenc:Array\">\n")
1634 (loop for i below
(length value
)
1635 do
(soap-encode-value xml-tag
(aref value i
) element-type
))
1636 (insert "</" xml-tag
">\n")))
1639 (put (aref (make-soap-basic-type) 0)
1640 'soap-encoder
'soap-encode-basic-type
)
1641 (put (aref (make-soap-simple-type) 0)
1642 'soap-encoder
'soap-encode-simple-type
)
1643 (put (aref (make-soap-sequence-type) 0)
1644 'soap-encoder
'soap-encode-sequence-type
)
1645 (put (aref (make-soap-array-type) 0)
1646 'soap-encoder
'soap-encode-array-type
))
1648 (defun soap-encode-body (operation parameters wsdl
)
1649 "Create the body of a SOAP request for OPERATION in the current buffer.
1650 PARAMETERS is a list of parameters supplied to the OPERATION.
1652 The OPERATION and PARAMETERS are encoded according to the WSDL
1654 (let* ((op (soap-bound-operation-operation operation
))
1655 (use (soap-bound-operation-use operation
))
1656 (message (cdr (soap-operation-input op
)))
1657 (parameter-order (soap-operation-parameter-order op
)))
1659 (unless (= (length parameter-order
) (length parameters
))
1660 (error "Wrong number of parameters for %s: expected %d, got %s"
1661 (soap-element-name op
)
1662 (length parameter-order
)
1663 (length parameters
)))
1665 (insert "<soap:Body>\n")
1666 (when (eq use
'encoded
)
1667 (add-to-list 'soap-encoded-namespaces
(soap-element-namespace-tag op
))
1668 (insert "<" (soap-element-fq-name op
) ">\n"))
1670 (let ((param-table (loop for formal in parameter-order
1671 for value in parameters
1672 collect
(cons formal value
))))
1673 (dolist (part (soap-message-parts message
))
1674 (let* ((param-name (car part
))
1676 (tag-name (if (eq use
'encoded
)
1678 (soap-element-name type
)))
1679 (value (cdr (assoc param-name param-table
)))
1680 (start-pos (point)))
1681 (soap-encode-value tag-name value type
)
1682 (when (eq use
'literal
)
1683 ;; hack: add the xmlns attribute to the tag, the only way
1684 ;; ASP.NET web services recognize the namespace of the
1685 ;; element itself...
1687 (goto-char start-pos
)
1688 (when (re-search-forward " ")
1689 (let* ((ns (soap-element-namespace-tag type
))
1690 (namespace (cdr (assoc ns
1691 (soap-wsdl-alias-table wsdl
)))))
1693 (insert "xmlns=\"" namespace
"\" ")))))))))
1695 (when (eq use
'encoded
)
1696 (insert "</" (soap-element-fq-name op
) ">\n"))
1697 (insert "</soap:Body>\n")))
1699 (defun soap-create-envelope (operation parameters wsdl
)
1700 "Create a SOAP request envelope for OPERATION using PARAMETERS.
1701 WSDL is the wsdl document used to encode the PARAMETERS."
1703 (let ((soap-encoded-namespaces '("xsi" "soap" "soapenc"))
1704 (use (soap-bound-operation-use operation
)))
1706 ;; Create the request body
1707 (soap-encode-body operation parameters wsdl
)
1709 ;; Put the envelope around the body
1710 (goto-char (point-min))
1711 (insert "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<soap:Envelope\n")
1712 (when (eq use
'encoded
)
1713 (insert " soapenc:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"\n"))
1714 (dolist (nstag soap-encoded-namespaces
)
1715 (insert " xmlns:" nstag
"=\"")
1716 (let ((nsname (cdr (assoc nstag soap-well-known-xmlns
))))
1718 (setq nsname
(cdr (assoc nstag
(soap-wsdl-alias-table wsdl
)))))
1722 (goto-char (point-max))
1723 (insert "</soap:Envelope>\n"))
1727 ;;;; invoking soap methods
1729 (defcustom soap-debug nil
1730 "When t, enable some debugging facilities."
1732 :group
'soap-client
)
1734 (defun soap-invoke (wsdl service operation-name
&rest parameters
)
1735 "Invoke a SOAP operation and return the result.
1737 WSDL is used for encoding the request and decoding the response.
1738 It also contains information about the WEB server address that
1739 will service the request.
1741 SERVICE is the SOAP service to invoke.
1743 OPERATION-NAME is the operation to invoke.
1745 PARAMETERS -- the remaining parameters are used as parameters for
1748 NOTE: The SOAP service provider should document the available
1749 operations and their parameters for the service. You can also
1750 use the `soap-inspect' function to browse the available
1751 operations in a WSDL document."
1752 (let ((port (catch 'found
1753 (dolist (p (soap-wsdl-ports wsdl
))
1754 (when (equal service
(soap-element-name p
))
1755 (throw 'found p
))))))
1757 (error "Unknown SOAP service: %s" service
))
1759 (let* ((binding (soap-port-binding port
))
1760 (operation (gethash operation-name
1761 (soap-binding-operations binding
))))
1763 (error "No operation %s for SOAP service %s" operation-name service
))
1765 (let ((url-request-method "POST")
1766 (url-package-name "soap-client.el")
1767 (url-package-version "1.0")
1768 (url-http-version "1.0")
1770 ;; url-request-data expects a unibyte string already encoded...
1771 (encode-coding-string
1772 (soap-create-envelope operation parameters wsdl
)
1774 (url-mime-charset-string "utf-8;q=1, iso-8859-1;q=0.5")
1775 (url-request-coding-system 'utf-8
)
1776 (url-http-attempt-keepalives t
)
1777 (url-request-extra-headers (list
1779 (soap-bound-operation-soap-action
1781 (cons "Content-Type"
1782 "text/xml; charset=utf-8"))))
1783 (let ((buffer (url-retrieve-synchronously
1784 (soap-port-service-url port
))))
1786 (with-current-buffer buffer
1787 (declare (special url-http-response-status
))
1788 (if (null url-http-response-status
)
1789 (error "No HTTP response from server"))
1790 (if (and soap-debug
(> url-http-response-status
299))
1791 ;; This is a warning because some SOAP errors come
1792 ;; back with a HTTP response 500 (internal server
1794 (warn "Error in SOAP response: HTTP code %s"
1795 url-http-response-status
))
1796 (let ((mime-part (mm-dissect-buffer t t
)))
1798 (error "Failed to decode response from server"))
1799 (unless (equal (car (mm-handle-type mime-part
)) "text/xml")
1800 (error "Server response is not an XML document"))
1802 (mm-insert-part mime-part
)
1803 (let ((response (car (xml-parse-region
1804 (point-min) (point-max)))))
1806 (soap-parse-envelope response operation wsdl
)
1807 (kill-buffer buffer
)
1808 (mm-destroy-part mime-part
))))))
1810 ;; Propagate soap-errors -- they are error replies of the
1811 ;; SOAP protocol and don't indicate a communication
1812 ;; problem or a bug in this code.
1813 (signal (car err
) (cdr err
)))
1816 (pop-to-buffer buffer
))
1817 (error (error-message-string err
)))))))))
1819 (provide 'soap-client
)
1823 ;; eval: (outline-minor-mode 1)
1824 ;; outline-regexp: ";;;;+"
1827 ;;; soap-client.el ends here