|
1 # -*- coding: utf-8 -*- |
|
2 #Copyright (C) 2009 Sandro Knauß <bugs@sandroknauss.de> |
|
3 |
|
4 #This program is free software; you can redistribute it and/or modify it under the terms |
|
5 #of the GNU General Public License as published by the Free Software Foundation; |
|
6 #either version 3 of the License, or any later version. |
|
7 #This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
|
8 #without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
9 #See the GNU General Public License for more details. |
|
10 |
|
11 #You should have received a copy of the GNU General Public License |
|
12 #along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
13 from twisted.web import soap, xmlrpc, resource, server |
|
14 from twisted.internet import reactor |
|
15 |
|
16 import logging |
|
17 |
|
18 |
|
19 from controller.viewinterface import Interface |
|
20 |
|
21 |
|
22 class TwistedInterface(Interface): |
|
23 |
|
24 def __init__(self): |
|
25 Interface.__init__(self) |
|
26 |
|
27 |
|
28 def listMethods(self): |
|
29 """Since we override lookupProcedure, its suggested to override |
|
30 listProcedures too. |
|
31 """ |
|
32 return self.listProcedures() |
|
33 |
|
34 |
|
35 def listProcedures(self): |
|
36 """Since we override lookupProcedure, its suggested to override |
|
37 listProcedures too. |
|
38 """ |
|
39 return ['listMethods','status','stop','sms','fax','mail','routes','defaultRoute','statistic'] |
|
40 |
|
41 |
|
42 class XMLRPCInterface(TwistedInterface,xmlrpc.XMLRPC): |
|
43 def __init__(self): |
|
44 xmlrpc.XMLRPC.__init__(self) |
|
45 TwistedInterface.__init__(self) |
|
46 |
|
47 def lookupProcedure(self, procedurePath): |
|
48 logging.debug("lookupProcedure('%s')"%procedurePath) |
|
49 if procedurePath not in self.listProcedures(): |
|
50 raise xmlrpc.NoSuchFunction(self.NOT_FOUND, |
|
51 "procedure %s not found" % procedurePath) |
|
52 try: |
|
53 return getattr(self,procedurePath) |
|
54 except KeyError: |
|
55 raise xmlrpc.NoSuchFunction(self.NOT_FOUND, |
|
56 "procedure %s not found" % procedurePath) |
|
57 |
|
58 class SOAPInterface(TwistedInterface,soap.SOAPPublisher): |
|
59 def __init__(self): |
|
60 soap.SOAPPublisher.__init__(self) |
|
61 TwistedInterface.__init__(self) |
|
62 |
|
63 def lookupFunction(self, functionName): |
|
64 """Lookup published SOAP function. |
|
65 |
|
66 Override in subclasses. Default behaviour - publish methods |
|
67 starting with soap_, if they have true attribute useKeywords |
|
68 they are expected to accept keywords. |
|
69 |
|
70 @return: tuple (callable, useKeywords), or (None, None) if not found. |
|
71 """ |
|
72 if functionName in self.listProcedures(): |
|
73 function = getattr(self, functionName, None) |
|
74 if function: |
|
75 return function, getattr(function, "useKeywords", False) |
|
76 return None |
|
77 else: |
|
78 return None |
|
79 |
|
80 def getResource(): |
|
81 root = resource.Resource() |
|
82 root.putChild('RPC2', XMLRPCInterface()) |
|
83 root.putChild('SOAP', SOAPInterface()) |
|
84 return root |
|
85 |
|
86 def main(): |
|
87 reactor.listenTCP(7080, server.Site(getResource())) |
|
88 reactor.run() |
|
89 |
|
90 if __name__ == '__main__': |
|
91 main() |