summaryrefslogtreecommitdiff
path: root/suds/bindings/binding.py
blob: 02beb9bbd487df45998cbff01c9bf37cf515aa70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# This program is free software; you can redistribute it and/or modify
# it under the terms of the (LGPL) GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the 
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library Lesser General Public License for more details at
# ( http://www.gnu.org/licenses/lgpl.html ).
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# written by: Jeff Ortel ( jortel@redhat.com )

"""
Provides classes for (WS) SOAP bindings.
"""

from logging import getLogger
from suds import *
from suds.sax import Namespace
from suds.sax.parser import Parser
from suds.sax.element import Element
from suds.sudsobject import Factory, Object
from suds.bindings.marshaller import Marshaller
from suds.bindings.unmarshaller import Unmarshaller
from suds.xsd.query import Query

log = getLogger(__name__)

encns = ('SOAP-ENC', 'http://schemas.xmlsoap.org/soap/encoding/')
envns = ('SOAP-ENV', 'http://schemas.xmlsoap.org/soap/envelope/')


class Binding:
    """
    The soap binding class used to process outgoing and imcoming
    soap messages per the WSDL port binding.
    @ivar wsdl: The wsdl.
    @type wsdl: L{suds.wsdl.Definitions}
    @ivar schema: The collective schema contained within the wsdl.
    @type schema: L{xsd.schema.Schema}
    @ivar faults: The faults flag used to indicate whether a web fault should raise
        an exception or that all results are returned in a tuple (http-code, result).
    @type faults: boolean
    @ivar parser: A sax parser.
    @type parser: L{suds.sax.parser.Parser}
    @ivar unmarshaller: An unmarshaller used to generate an L{Object}
        representation of received soap messages.
    @type unmarshaller: L{Unmarshaller}
    @ivar marshaller: A marshaller used to generate soap messages from
        python L{Object}s.
    @type marshaller: L{Marshaller}
    @ivar encoded: The I{usr=literal} vs I{use=encoded} flag defines with version
        of the I{marshaller} and I{unmarshaller} should be used to encode/decode
        soap messages.
    """

    def __init__(self, wsdl):
        self.wsdl = wsdl
        self.schema = wsdl.schema
        self.faults = True
        self.parser = Parser()
        self.unmarshaller = Unmarshaller(self.schema)
        self.marshaller = Marshaller(self.schema)
        self.encoded = False
        
    def use_literal(self):
        """
        Set the input message encoding to I{literal} by setting the
        L{self.encoded} flag = True.
        @return: self
        @rtype: L{Binding}
        """
        self.encoded = False
        return self
    
    def use_encoded(self):
        """
        Set the input message encoding to I{encoded} by setting the 
        L{self.encoded} flag = False.
        @return: self
        @rtype: L{Binding}
        """
        self.encoded = True
        return self

    def get_message(self, method, args, soapheaders):
        """
        Get the soap message for the specified method, args and soapheaders.
        This is the entry point for creating the outbound soap message.
        @param method: The method being invoked.
        @type method: I{service.Method}
        @param args: A I{list} of method arguments (parameters).
        @type args: list
        @param soapheaders: A list of objects to be encoded as soap-headers.
        @type soapheaders: list
        @return: The soap message.
        @rtype: str
        """
        content = self.bodycontent(method, args)
        body = self.body(content)
        header = self.header(soapheaders)
        env = self.envelope(body, header)
        env.promotePrefixes()
        return env
    
    def get_reply(self, method, reply):
        """
        Process the I{reply} for the specified I{method} by sax parsing the I{reply}
        and then unmarshalling into python object(s).
        @param method: The name of the invoked method.
        @type method: str
        @param reply: The reply XML received after invoking the specified method.
        @type reply: str
        @return: The unmarshalled reply.  The returned value is an L{Object} for a
            I{list} depending on whether the service returns a single object or a 
            collection.
        @rtype: tuple ( L{Element}, L{Object} )
        """
        replyroot = self.parser.parse(string=reply)
        soapenv = replyroot.getChild('Envelope')
        soapbody = soapenv.getChild('Body')
        nodes = soapbody[0].children
        rtypes = self.returned_types(method)
        if len(rtypes) == 1 and rtypes[0].unbounded():
            result = self.reply_list(rtypes[0], nodes)
            return (replyroot, result)
        if len(nodes) > 1:
            result = self.reply_composite(rtypes, nodes)
            return (replyroot, result)
        if len(nodes) == 1:
            unmarshaller = self.unmarshaller.typed
            resolved = rtypes[0].resolve(nobuiltin=True)
            result = unmarshaller.process(nodes[0], resolved)
            return (replyroot, result)
        return (replyroot, None)
    
    def reply_list(self, rt, nodes):
        """
        Construct a I{list} reply.  This mehod is called when it has been detected
        that the reply is a list.
        @param rt: The return I{type}.
        @type rt: L{suds.xsd.sxbase.SchemaObject}
        @param nodes: A collection of XML nodes.
        @type nodes: [L{Element},...]
        @return: A list of I{unmarshalled} objects.
        @rtype: [L{Object},...]
        """
        result = []
        resolved = rt.resolve(nobuiltin=True)
        unmarshaller = self.unmarshaller.typed
        for node in nodes:
            sobject = unmarshaller.process(node, resolved)
            result.append(sobject)
        return result
    
    def reply_composite(self, rtypes, nodes):
        """
        Construct a I{composite} reply.  This method is called when it has been
        detected that the reply is a composite (object).
        @param rtypes: A list of legal return I{types}.
        @type rtypes: [L{suds.xsd.sxbase.SchemaObject},...]
        @param nodes: A collection of XML nodes.
        @type nodes: [L{Element},...]
        @return: The I{unmarshalled} composite object.
        @rtype: L{Object},...
        """
        dictionary = {}
        for rt in rtypes:
            dictionary[rt.name] = rt
        unmarshaller = self.unmarshaller.typed
        composite = Factory.object()
        for node in nodes:
            tag = node.name
            rt = dictionary.get(tag, None)
            if rt is None:
                raise Exception('tag (%s), not-found' % tag)
            resolved = rt.resolve(nobuiltin=True)
            sobject = unmarshaller.process(node, resolved)
            if rt.unbounded():
                value = getattr(composite, tag, None)
                if value is None:
                    value = []
                    setattr(composite, tag, value)
                value.append(sobject)
            else:
                setattr(composite, tag, sobject)
        return composite
    
    def get_fault(self, reply):
        """
        Extract the fault from the specified soap reply.  If L{self.faults} is True, an
        exception is raised.  Otherwise, the I{unmarshalled} fault L{Object} is
        returned.  This method is called when the server raises a I{web fault}.
        @param reply: A soap reply message.
        @type reply: str
        @return: A fault object.
        @rtype: tuple ( L{Element}, L{Object} )
        """
        faultroot = self.parser.parse(string=reply)
        soapenv = faultroot.getChild('Envelope')
        soapbody = soapenv.getChild('Body')
        fault = soapbody.getChild('Fault')
        unmarshaller = self.unmarshaller.basic
        p = unmarshaller.process(fault)
        if self.faults:
            raise WebFault(p, faultroot)
        return (faultroot, p.detail)
    
    def param(self, method, pdef, object):
        """
        Builds a parameter for the specified I{method} using the parameter
        definition (pdef) and the specified value (object).
        @param method: A method name.
        @type method: str
        @param pdef: A parameter definition.
        @type pdef: tuple: (I{name}, L{xsd.sxbase.SchemaObject})
        @param object: The parameter value.
        @type object: any
        @return: The parameter fragment.
        @rtype: L{Element}
        """
        if self.encoded:
            marshaller = self.marshaller.encoded
        else:
            marshaller = self.marshaller.literal
        if isinstance(object, (Object, dict)):
            return marshaller.process(object, pdef[1], pdef[0])
        if isinstance(object, (list, tuple)):
            tags = []
            for item in object:
                tags.append(self.param(method, pdef, item))
            return tags
        return marshaller.process(object, pdef[1], pdef[0])
            
    def envelope(self, body, header):
        """
        Build the B{<Envelope/>} for an soap outbound message.
        @param body: The soap message B{body}.
        @type body: L{Element}
        @param header: The soap message B{header}.
        @type header: L{Element}
        @return: The soap envelope containing the body and header.
        @rtype: L{Element}
        """
        env = Element('Envelope', ns=envns)
        env.addPrefix(encns[0], encns[1])
        env.addPrefix(Namespace.xsins[0], Namespace.xsins[1])
        env.append(header)
        env.append(body)
        return env
    
    def header(self, headers):
        """
        Build the B{<Header/>} for an soap outbound message.
        @param headers: A collection of header objects.
        @type headers: [L{Object},..]
        @return: The soap header fragment.
        @rtype: L{Element}
        """
        hdr = Element('Header', ns=envns)
        if not isinstance(headers, (list,tuple)):
            headers = (headers,)
        if self.encoded:
            marshaller = self.marshaller.encoded
        else:
            marshaller = self.marshaller.literal
        for h in headers:
            tag = h.__class__.__name__
            if isinstance(h, Object):
                value = h
                type = h.__metadata__.__type__
                node = marshaller.process(value, type, tag)
                hdr.append(node)
            else:
                log.error('soapheader (%s) must be Object', tag)
        return hdr
    
    def body(self, method):
        """
        Build the B{<Body/>} for an soap outbound message.
        @param method: The name of the method.
        @return: the soap body fragment.
        @rtype: L{Element}
        """
        ns = self.wsdl.tns
        body = Element('Body', ns=envns)
        body.append(method)
        return body
    
    def part_types(self, method, input=True):
        """
        Get a list of I{parameter definitions} (pdef) defined for the specified method.
        Each I{pdef} is a tuple (I{name}, L{xsd.sxbase.SchemaObject})
        @param method: A service method.
        @type method: I{service.Method}
        @param input: Defines input/output message.
        @type input: boolean
        @return:  A list of parameter definitions
        @rtype: [I{pdef},]
        """
        result = []
        if input:
            parts = method.message.input.parts
        else:
            parts = method.message.output.parts
        for p in parts:
            qref = p.xsref()
            query = Query(qref)
            pt = query.execute(self.schema)
            if pt is None:
                raise TypeNotFound(qref)
            if input:
                result.append((p.name, pt))
            else:
                result.append(pt)
        return result
    
    def returned_types(self, method):
        """
        Get the L{xsd.sxbase.SchemaObject} returned by the I{method}.
        @param method: A service method.
        @type method: I{service.Method}
        @return: The name of the type return by the method.
        @rtype: [I{rtype},..]
        """
        result = []
        for rt in self.part_types(method, False):
            result.append(rt)
        return result