| 1 |
""" |
| 2 |
SOAP rendering and related classes. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
from twisted.web import soap |
| 12 |
|
| 13 |
from gavo import base |
| 14 |
from gavo import svcs |
| 15 |
from gavo.web import grend |
| 16 |
from gavo.web import wsdl |
| 17 |
|
| 18 |
|
| 19 |
class SOAPProcessor(soap.SOAPPublisher): |
| 20 |
"""A helper to the SOAP renderer. |
| 21 |
|
| 22 |
It's actually a nevow resource ("page"), so whatever really has |
| 23 |
to do with SOAP (as opposed to returning WSDL) is done by this. |
| 24 |
""" |
| 25 |
def __init__(self, request, service, runAsyncFromArgs): |
| 26 |
self.request, self.service = request, service |
| 27 |
self.runAsyncFromArgs = runAsyncFromArgs |
| 28 |
soap.SOAPPublisher.__init__(self) |
| 29 |
|
| 30 |
def _gotResult(self, result, request, methodName): |
| 31 |
# We want SOAP docs that actually match what we advertise in the WSDL. |
| 32 |
# So, I override SOAPPublisher's haphazard SOAPpy-based formatter. |
| 33 |
if result is None: # Error has occurred. This callback shouldn't be |
| 34 |
# called at all, but for some reason it is, and I can't be bothered |
| 35 |
# now to figure out why. |
| 36 |
return "" |
| 37 |
response = wsdl.serializePrimaryTable(result, self.service) |
| 38 |
self._sendResponse(request, response) |
| 39 |
|
| 40 |
def _gotError(self, failure, request, methodName): |
| 41 |
failure.printTraceback() |
| 42 |
try: |
| 43 |
self._sendResponse(request, |
| 44 |
wsdl.formatFault(failure.value, self.service), status=500) |
| 45 |
except: |
| 46 |
base.ui.logError("Error while writing SOAP error:") |
| 47 |
|
| 48 |
def soap_useService(self, *args): |
| 49 |
try: |
| 50 |
return self.runAsyncFromArgs(self.request, args) |
| 51 |
except Exception as exc: |
| 52 |
base.ui.notifyError("SOAP service failed.") |
| 53 |
return self._writeError(exc) |
| 54 |
return "" |
| 55 |
|
| 56 |
def _writeError(self, exc, request): |
| 57 |
self._sendResponse(self.request, wsdl.formatFault(exc, self.service), |
| 58 |
status=500) |
| 59 |
|
| 60 |
|
| 61 |
class SOAPRenderer(grend.ServiceBasedPage): |
| 62 |
"""A renderer that receives and formats SOAP messages. |
| 63 |
|
| 64 |
This is for remote procedure calls. In particular, the renderer takes |
| 65 |
care that you can obtain a WSDL definition of the service by |
| 66 |
appending ?wsdl to the access URL. |
| 67 |
""" |
| 68 |
name="soap" |
| 69 |
preferredMethod = b"POST" |
| 70 |
urlUse = "full" |
| 71 |
|
| 72 |
@classmethod |
| 73 |
def makeAccessURL(cls, baseURL): |
| 74 |
return baseURL+"/soap/go" |
| 75 |
|
| 76 |
def runAsyncFromArgs(self, request, args): |
| 77 |
"""starts the service. |
| 78 |
|
| 79 |
This being called back from the SOAPProcessor, and args is the |
| 80 |
argument tuple as given from SOAP. |
| 81 |
""" |
| 82 |
inputPars = dict(list(zip( |
| 83 |
[f.name for f in self.service.getInputKeysFor(self)], |
| 84 |
args))) |
| 85 |
return self.runAsyncWithFormalData(inputPars, request) |
| 86 |
|
| 87 |
def render(self, request): |
| 88 |
"""returns the WSDL for service. |
| 89 |
|
| 90 |
This is only called when there's a ?wsdl arg in the request, |
| 91 |
otherwise getChild will return the SOAPProcessor. |
| 92 |
""" |
| 93 |
if not hasattr(self.service, "_generatedWSDL"): |
| 94 |
queryMeta = svcs.QueryMeta.fromRequest(request) |
| 95 |
self.service._generatedWSDL = wsdl.makeSOAPWSDLForService( |
| 96 |
self.service, queryMeta).render() |
| 97 |
request.setHeader("content-type", "text/xml") |
| 98 |
return self.service._generatedWSDL |
| 99 |
|
| 100 |
def getChild(self, name, request): |
| 101 |
if request.uri.endswith(b"?wsdl"): # XXX TODO: use parsed headers here |
| 102 |
return self |
| 103 |
if request.method!=b'POST': |
| 104 |
# SOAP only makes sense when data is posted; with no data, |
| 105 |
# twisted generates ugly tracebacks, and we may as well do |
| 106 |
# something sensible, like... redirect to the service's info |
| 107 |
# page |
| 108 |
raise svcs.WebRedirect(self.service.getURL("info")) |
| 109 |
return SOAPProcessor(request, self.service, self.runAsyncFromArgs) |