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