The Python Web services developer, Part 3: Web services software repository, Part 2

A Web service for content updating

Return to article

Listing 5: A custom SOAP handler in 4Suite Server
import base64
from xml import xpath
from xml.dom import implementation, ext
from FtServer.Protocols.Http import SoapHandler

SOFTREPO_SOAP_NS = 'http://spam.com/softrepo'
DOCDEF = 'dublin_core'
URI_STEM = 'softrepo/incoming'
TEMPLATE = """<rdf:RDF
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:dc="http://purl.org/dc/elements/1.1"
 xmlns="http://namespaces.4suite.org/www/software-map"
>
 <Software rdf:ID="%s">
 <dc:Title>%s</dc:Title>
 <dc:Creator>%s</dc:Creator>
 <Home rdf:resource="%s"/>
 <CurrentVersion>%s</CurrentVersion>
 <dc:Description>%s</dc:Description>
 </Software>
</rdf:RDF>
"""

def AddEntry(repo, reqbody, respbody):
 context = xpath.Context.Context(reqbody,
 processorNss={'s': SOFTREPO_SOAP_NS})
 title = xpath.Evaluate('string(s:Title)', context=context)
 creator = xpath.Evaluate('string(s:Creator)', context=context)
 home = xpath.Evaluate('string(s:Home)', context=context)
 version = xpath.Evaluate('string(s:Version)', context=context)
 desc = xpath.Evaluate('string(s:Description)', context=context)

 source = TEMPLATE%(title, title, creator, home, version, desc)
 uri = '%s/%s-%s.xml'%(URI_STEM, title, version)

 res = repo.createDocument(DOCDEF, uri, source)
 imt = res.getImt()
 result = respbody.ownerDocument.createElementNS(SOFTREPO_SOAP_NS, 'ftsoap:Document')
 result.setAttributeNS('', 'uri', res.getUri())
 result.setAttributeNS('', 'imt', res.getImt())
 result.appendChild(respbody.ownerDocument.createTextNode(base64.encodestring(source)))
 respbody.appendChild(result)
 return 1


class SoftRepoSoapHandler(SoapHandler.SoapHandler):
 NS_TO_HANDLER_MAPPING = {
 SOFTREPO_SOAP_NS: {'Add': AddEntry,
 },
 }


def Register(props):
 handler = SoftRepoSoapHandler()
 return [(handler.handle, 'POST')]

Return to article