Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Web services software repository, Part 3

Return to article

import sys, string, httplib, base64, mimetools
import wsdllib
from Ft.Lib import pDomlette
from xml import xpath

SERVER_ADDR = '127.0.0.1'
SERVER_PORT = 8080

WEB_SERVICE_NAME = "Software Repository"

BODY_TEMPLATE = """<SOAP-ENV:Envelope
     xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
     xmlns:s="http://spam.com/softrepo"
     SOAP-ENV:encodingStyle="http://spam.com/softrepo/encoding"
   >
     <SOAP-ENV:Body>
       <s:Add>
         <s:Title><![CDATA[%(title)s]]></s:Title>
         <s:Creator><![CDATA[%(creator)s]]></s:Creator>
         <s:Home><![CDATA[%(home)s]]></s:Home>
         <s:Version><![CDATA[%(version)s]]></s:Version>
         <s:Description><![CDATA[%(description)s]]></s:Description>
       </s:Add>
     </SOAP-ENV:Body>
   </SOAP-ENV:Envelope>"""


def _GetInputChoice(title,choices):
    print "Please enter a selection for '%s'" % title

    while 1:
        ctr = 0
        names = choices.keys()
        names.sort()
        for name in names:
            print "  %d.  %s" % (ctr,name)
            ctr = ctr + 1

        print

        resp = raw_input("Selection: ")
        error = 0
        try:
            resp = int(resp)
        except:
            error = 1
        else:
            if resp < 0 or resp >= ctr:
                error = 1
        if error:
            print "Invalid Entry, please try again"
        else:
            break
    print
    return names[resp]

def CreateSOAPMessage(wsdl):

    #Get the service name
    serviceName = _GetInputChoice("Service",wsdl.services)
    service = wsdl.services[serviceName]

    #Get the port name
    portName = _GetInputChoice("Service Port",service.ports)
    port = service.ports[portName]

    #Get the corresponding binding
    bindingName = port.binding
    if bindingName[:4] == 'tns:':
        bindingName = bindingName[4:]
    binding = wsdl.bindings[bindingName]

    portTypeName = binding.type
    if portTypeName[:4] == 'tns:':
        portTypeName = portTypeName[4:]
    portType = wsdl.portTypes[portTypeName]

    #Get the operation name
    operationName = _GetInputChoice("Operation",portType.operations)
    operation = portType.operations[operationName]

    messageName = operation.input.message
    if messageName[:4] == 'tns:':
        messageName = messageName[4:]
    message = wsdl.messages[messageName]


    #Finally, ask the user for the data about the parts
    actualParts = {}
    for name,part in message.parts.items():
        actualParts[name] = raw_input("Please enter value for '%s': "%name)

    body = BODY_TEMPLATE % actualParts

    return body

def main():

    #Request the WSDL
    requestor = httplib.HTTP(SERVER_ADDR, SERVER_PORT)
    requestor.putrequest('WSDL', '/')
    requestor.putheader('Host', SERVER_ADDR)
    requestor.putheader('Content-Type', 'text/plain; charset="iso-8859-1"')
    requestor.putheader('name', WEB_SERVICE_NAME)
    requestor.endheaders()
    requestor.send("")
    (status_code, message, reply_headers) = requestor.getreply()
    reply_body = requestor.getfile().read()

    if status_code != 200:
        raise Exception("Expected status code 200, actual %d: %s" % (status_code,message)) 

    #now that we have gotten the WSDL, parse it in
    wsdl = wsdllib.ReadFromString(reply_body)

    request_body = CreateSOAPMessage(wsdl)

    #Now make the SOAP request
    blen = len(request_body)
    requestor = httplib.HTTP(SERVER_ADDR, SERVER_PORT)
    requestor.putrequest('POST', '/softrepo/soap-handler')
    requestor.putheader('Host', SERVER_ADDR)
    requestor.putheader('Content-Type', 'text/plain; charset="iso-8859-1"')
    requestor.putheader('Content-Length', str(blen))
    requestor.putheader('SOAPAction', "")
    requestor.endheaders()
    requestor.send(request_body)
    (status_code, message, reply_headers) = requestor.getreply()
    reply_body = requestor.getfile().read()
    if status_code != 200:
        raise Exception("Expected status code 200, actual %d: %s" % (status_code,message)) 
    reader = pDomlette.PyExpatReader()
    dom = reader.fromString(reply_body)
    con = xpath.Context.Context(dom,processorNss = {'ftsoap':'http://spam.com/softrepo'})

    doc = xpath.Evaluate("//ftsoap:Document",context=con)[0]

    uri = doc.getAttributeNS('','uri')

    print "New Document (uri = %s)" % uri
    print base64.decodestring(doc.firstChild.data)
                        


if __name__ == '__main__':
    main()

Return to article