Web Services and SOAP - PowerPoint PPT Presentation

About This Presentation
Title:

Web Services and SOAP

Description:

Prices of books in Barnes and Nobles. Dictionaries. etc. 10. SOAP Simplification (1) ... Instead, use Java methods that create request and analyze result ... – PowerPoint PPT presentation

Number of Views:130
Avg rating:3.0/5.0
Slides: 84
Provided by: csHu
Category:
Tags: soap | and | barnes | nobles | services | web

less

Transcript and Presenter's Notes

Title: Web Services and SOAP


1
Web Services and SOAP
  • Representation and Management of Data on the Web

2
Example Scenario
Book Store
Harry Potter (5) Price 25.95 Copies in Stock 1
Harry Potter (5) Price 25.95 Copies in Stock 0
3
What is a Web Service?
  • A Web Service is simply a service available via
    the Web
  • Service can be implemented as Java application,
    C application, Javascript code, etc.
  • Usually, web service means a service that can be
    accessed programmatically

4
Difficulties in Using a Web Site As a Web Service
5
More Difficulties (Filling out a Form)
Forms span multiple pages
How do we find this URL?
6
What Would We Like to Do?
  • Call functions such as
  • Amazon.getPrice("Harry Potter")
  • Amazon.buyBook("Harry Potter", myId)
  • The language that our program uses shouldn't
    depend on the language that Amazon uses
  • Use a standard underlying protocol (HTTP, FTP,
    SNMP)

7
Solution SOAP
  • SOAP stands for "Simple Object Access Protocol"
  • Used for "Remote Procedure Calls", similar to
  • IIOP (for Corba), ORPC (for DCOM), RMI (for Java)
  • Difference SOAP is text-based (actually XML),
    not binary. Firewall Friendly
  • Difference Language independent, can call a
    program in any language
  • Difference Uses standard port, since uses
    standard protocols

8
SOAP RPC versus DOC
  • SOAP is simply a standard for sending messages
    (think of it as an envelope)
  • You can send two types of messages using SOAP
  • RPC Remote Procedure Call, a request to call a
    method
  • DOC A document (this is used for more complex
    client - server communication)
  • We will only discuss RPC today

9
What Web Services are Available Already?
  • Google search
  • Weather reports
  • Stock prices
  • Currency exchanges
  • Sending SMS messages, Faxes
  • Prices of books in Barnes and Nobles
  • Dictionaries
  • etc.

10
SOAP Simplification (1)
  • Consider the Java interface
  • public interface Hello
  • public String sayHelloTo(String name)
  • Suppose that a client wants to call the server's
    sayHelloTo method. Could send an XML message
  • lt?xml version"1.0"?gtltHellogt    ltsayHelloTogt   
         ltnamegtJohnlt/namegt    lt/sayHelloTogtlt/Hellogt

11
SOAP Simplification (2)
  • The Server could respond with
  • lt?xml version"1.0"?gtltHellogt    ltsayHelloToRespo
    nsegt       ltmessagegtHello John, How are
    you?lt/messagegt    lt/sayHelloToResponsegtlt/Hellogt

12
SOAP Intuition
13
Actual Soap Request
  • ltSOAP-ENVEnvelope
  • xmlnsSOAP-ENV"http//schemas.xmlsoap.org/soap
    /envelope/"
  • xmlnsxsi"http//www.w3.org/1999/XMLSchema-ins
    tance"
  •    xmlnsxsd"http//www.w3.org/1999/XMLSchema"gt 
       ltSOAP-ENVHeadergt lt/SOAP-ENVHeadergt    ltSOAP-
    ENVBodygt         ltns1sayHelloTo
     xmlnsns1"Hello"
  • SOAP-ENVencodingStyle"
  • http//schemas.xmlsoap.org/s
    oap/encoding/"gt           ltname
    xsitype"xsdstring"gtJohnlt/namegt        
    lt/ns1sayHelloTogt    lt/SOAP-ENVBodygt
  • lt/SOAP-ENVEnvelopegt

14
Actual Soap Response
  • ltSOAP-ENVEnvelope
  • xmlnsSOAP-ENV"http//schemas.xmlsoap.org/soap
    /envelope/"
  •    xmlnsxsi"http//www.w3.org/1999/XMLSchema-ins
    tance"
  • xmlnsxsd"http//www.w3.org/1999/XMLSchema"gt 
       ltSOAP-ENVBodygt          ltns1sayHelloToRespon
    se xmlnsns1"Hello"                SOAP-ENVenco
    dingStyle"
  • http//schemas.xmlsoap.org
    /soap/encoding/"gt                 ltreturn
    xsitype"xsdstring"gt
  • Hello John, How are you doing?
  • lt/returngt          lt/ns1sayHelloToResponsegt 
       lt/SOAP-ENVBodygt
  • lt/SOAP-ENVEnvelopegt

15
SOAP Header Section
  • The SOAP Header can contain information that
    describes the SOAP request. Example
  • ltSOAP-ENVHeadergt     lttTransaction
    xmlnst"some-URI"
  • SOAP-ENVmustUnderstand"1"gt5    
    lt/tTransactiongtlt/SOAP-ENVHeadergt
  • 5 is the transaction ID of which this method is a
    part
  • SOAP envelope's mustUnderstand attribute is set
    to 1, which means that the server must either
    understand and honor the transaction request or
    must fail to process the message

16
SOAP Response on Error
  • There can be many errors in processing a SOAP
    request
  • Error in Running Method e.g., Suppose that the
    "Hello Server" does not allow anyone to say hello
    on Tuesday
  • Error in Processing SOAP Headers e.g., Problem
    running method as part of a transaction

17
Soap Error Response for Method Error
  • ltSOAP-ENVEnvelope xmlnsSOAP-ENV"http//schemas
    .xmlsoap.org/soap/envelope/"gt  
    ltSOAP-ENVBodygt       ltSOAP-ENVFaultgt          
    ltfaultcodegtSOAP-ENVServerlt/faultcodegt          
    ltfaultstringgtServer Errorlt/faultstringgt         
      ltdetailgt               ltemyfaultdetails
    xmlnse"Hello"gt                
    ltmessagegt                   Sorry, I cannot say
    hello on Tuesday.                
    lt/messagegt                 lterrorcodegt1001lt/error
    codegt               lt/emyfaultdetailsgt         
      lt/detailgt       lt/SOAP-ENVFaultgt  
    lt/SOAP-ENVBodygtlt/SOAP-ENVEnvelopegt

18
Soap Error Response for Header Error
  • ltSOAP-ENVEnvelope xmlnsSOAP-ENV"http//schemas
    .xmlsoap.org/soap/envelope/"gt  
    ltSOAP-ENVBodygt       ltSOAP-ENVFaultgt          
    ltfaultcodegtSOAP-ENVMustUnderstandlt/faultcodegt  
             ltfaultstringgtSOAP Must Understand
    Errorlt/faultstringgt       lt/SOAP-ENVFaultgt  
    lt/SOAP-ENVBodygtlt/SOAP-ENVEnvelopegt

No detail element may appear when there is an
error in processing the Headers of a SOAP request.
19
Sending a Request
  • The SOAP request did not contain the address to
    which it should be sent
  • Q Where do we put the URL of the Web Service?
  • A It depends on the Protocol used to send the
    request (today, consider HTTP)

20
SOAP Request via HTTP
  • POST http//www.Hello.com/HelloApplication
    HTTP/1.0
  • Content-Type text/xml
  • Content-Length 587
  • SOAPAction urnhelloApp
  • ltSOAP-ENVEnvelope

Note There are 2 addresses (1) URL of SOAP
Server (2) URI of application to run (this
needn't correspond to an actual address)
21
SOAP Response via HTTP
  • HTTP/1.0 200 OK
  • Content-Type text/xml
  • Content-Length 615
  • ltSOAP-ENVEnvelope

22
Example Currency Rate
  • There are many web services available that you
    can use
  • See http//www.xmethods.com/ for a list
  • Look at ones marked "RPC" (Remote Procedure
    Call), especially
  • To get Currency exchange, for example, you can do
    "telnet wwwproxy.cs.huji.ac.il 8080" and then
    send the following request

23
  • POST http//services.xmethods.net80/soap
    HTTP/1.0
  • Content-Type text/xml
  • Content-Length 485
  • SOAPAction ""
  • ltSOAP-ENVEnvelope xmlnsSOAP-ENV"http//schemas.
    xmlsoap.org/soap/envelope/" xmlnsxsi"http//www.
    w3.org/1999/XMLSchema-instance"
    xmlnsxsd"http//www.w3.org/1999/XMLSchema"gt
  • ltSOAP-ENVBodygt
  • ltns1getRate xmlnsns1"urnxmethods-CurrencyExcha
    nge" SOAP-ENVencodingStyle"http//schemas.xmlsoa
    p.org/soap/encoding/"gt
  • ltcountry1 xsitype"xsdstring"gtUnited
    Stateslt/country1gt
  • ltcountry2 xsitype"xsdstring"gtIsraellt/country2gt
  • lt/ns1getRategt
  • lt/SOAP-ENVBodygtlt/SOAP-ENVEnvelopegt

24
Here is Yesterday's Response
  • HTTP/1.0 200 OK
  • Content-Type text/xml
  • lt?xml version'1.0' encoding'UTF-8'?gt
  • ltsoapEnvelope xmlnssoap'http//schemas.xmlsoap.
    org/soap/envelope/' xmlnsxsi'http//www.w3.org/1
    999/XMLSchema-instance' xmlnsxsd'http//www.w3.o
    rg/1999/XMLSchema' xmlnssoapenc'http//schemas.x
    mlsoap.org/soap/encoding/' soapencodingStyle'htt
    p//schemas.xmlsoap.org/soap/encoding/'gt
  • ltsoapBodygtltngetRateResponse
  • xmlnsn'urnxmethods-CurrencyExchange'gt
  • ltResult xsitype'xsdfloat'gt4.521lt/Resultgt
  • lt/ngetRateResponsegt
  • lt/soapBodygtlt/soapEnvelopegt

25
Programming SOAP
26
The Main Players
  • There are 3 components that take part in a SOAP
    application
  • Client Application A program/Servlet/etc. that
    sends a SOAP request. Wants to use a service.
  • SOAP Processor A program that can receive SOAP
    requests and act accordingly (e.g., call an
    method of the Application Server)
  • Application Server A program that supplies the
    Web service

27
What do we have to Program?
  • We won't directly read or write SOAP messages
  • Instead, use Java methods that create request and
    analyze result
  • Use a SOAP processor that is actually a Servlet
    (you will download it)
  • Code the Client Application and the Application
    server

28
Technical Details - Application Server
  • Your application server does not need anything
    special
  • In fact, your application server does not have to
    "know" that it is being used as a Web Service!!
  • However, you will need to put the application
    server somewhere in the classpath of Tomcat so
    that the SOAP Processor can find it and run it.
    More details on this soon...

29
Technical Details - Client Application
  • Your SOAP client will use special packages to
    generate a SOAP request
  • Need the following packages in your CLASSPATH to
    compile
  • soap.jar
  • mail.jar
  • activation.jar
  • Note All files mentioned here that you need are
    linked to from Exercise 5

30
Technical Details - SOAP Processor
  • Your Tomcat web server needs a web application
    that is a SOAP Processor
  • Put soap.war in your lttomcat_homegt/webapps
    directory
  • To actually run the SOAP Processor, it needs the
    soap.jar, mail.jar, activation.jar files in its
    classpath
  • Easiest way to get the files in its classpath
    Add them to the directory lttomcat_homegt/lib

31
Creating the Application Server
  • package hello
  • public class HelloServer
  • public String sayHelloTo(String name)
  •             return "Hello " name
  • ", How are you doing?"              
  • Note No SOAP specific code here!!
  • Note Put application in a package. Create a jar
    file from the package and put the package in
    lttomcat_homegt/lib, so that it will be in Tomcat's
    classpath

32
Deploying the Web Service
  • The SOAP Processor must be told about your
    application. This is called "deploying"
  • Deploying is a two-step process
  • Create a deployment descriptor
  • Call the java command that deploys the web
    application

33
Deployment Descriptor
  • ltisdservice
  • xmlnsisd"http//xml.apache.org/xml-soap/depl
    oyment"
  • id"urnhelloApp"gt
  • ltisdprovider type"java"
  • scope"application"
  • methods"sayHelloTo"gt
  • ltisdjava class"hello.HelloServer"/gt
  • lt/isdprovidergt
  • ltisdfaultListenergt
  • org.apache.soap.server.DOMFaultListener
  • lt/isdfaultListenergt
  • lt/isdservicegt

34
Deployment Descriptor
  • ltisdservice
  • xmlnsisd"http//xml.apache.org/xml-soap/depl
    oyment"
  • id"urnhelloApp"gt
  • ltisdprovider type"java"
  • scope"application"
  • methods"sayHelloTo"gt
  • ltisdjava class"hello.HelloServer"/gt
  • lt/isdprovidergt
  • ltisdfaultListenergt
  • org.apache.soap.server.DOMFaultListener
  • lt/isdfaultListenergt
  • lt/isdservicegt

35
Deployment Descriptor
  • ltisdservice
  • xmlnsisd"http//xml.apache.org/xml-soap/depl
    oyment"
  • id"urnhelloApp"gt
  • ltisdprovider type"java"
  • scope"application"
  • methods"sayHelloTo"gt
  • ltisdjava class"hello.HelloServer"/gt
  • lt/isdprovidergt
  • ltisdfaultListenergt
  • org.apache.soap.server.DOMFaultListener
  • lt/isdfaultListenergt
  • lt/isdservicegt

36
Deployment Descriptor
  • ltisdservice
  • xmlnsisd"http//xml.apache.org/xml-soap/depl
    oyment"
  • id"urnhelloApp"gt
  • ltisdprovider type"java"
  • scope"application"
  • methods"sayHelloTo"gt
  • ltisdjava class"hello.HelloServer"/gt
  • lt/isdprovidergt
  • ltisdfaultListenergt
  • org.apache.soap.server.DOMFaultListener
  • lt/isdfaultListenergt
  • lt/isdservicegt

37
Deployment Descriptor
  • ltisdservice
  • xmlnsisd"http//xml.apache.org/xml-soap/depl
    oyment"
  • id"urnhelloApp"gt
  • ltisdprovider type"java"
  • scope"application"
  • methods"sayHelloTo"gt
  • ltisdjava class"hello.HelloServer"/gt
  • lt/isdprovidergt
  • ltisdfaultListenergt
  • org.apache.soap.server.DOMFaultListener
  • lt/isdfaultListenergt
  • lt/isdservicegt

38
Scope of Web Service
  • page The service instance is available until a
    response is sent back or the request is forwarded
    to another page
  • request The service instance is available for
    the duration of the request, regardless of
    forwarding
  • session The service instance is available for
    the entire session

39
Scope of Web Service (cont.)
  • application The same service instance is used to
    serve all invocations
  • Which of these scope values require us to think
    about synchronizing access to data members and
    methods?

40
Completing the Deployment
  • Save the deployment descriptor in a file, e.g.,
    HelloDescriptor.xml
  • Run the command
  • java org.apache.soap.server.ServiceManagerClient
    http//lthostgtltportgt/soap/servlet/rpcrouter
    deploy HelloDescriptor.xml
  • where lthostgt and ltportgt are those of Tomcat
  • Note that Tomcat must be running for this to work

41
Checking for Deployed Services
  • You can get a list of all deployed web services
    using the command
  • java org.apache.soap.server.ServiceManagerClient
    http//lthostgtltportgt/soap/servlet/rpcrouter list
  • In this case you should see urnhelloApp

42
Undeploying a Service
  • You can undeploy a web service, so that it is no
    longer recognized by the SOAP Processor using the
    command
  • java org.apache.soap.server.ServiceManagerClient
    http//lthostgtltportgt/soap/servlet/rpcrouter
    undeploy urnhelloApp
  • Note that the last argument is the URI of the web
    service to be removed

43
What must the client do?
  • Create the SOAP-RPC call
  • Set up any type mappings for custom parameters
  • Set the URI of the SOAP service to use
  • Specify the method to invoke
  • Specify the encoding to use
  • Add any parameters to the call
  • Connect to the SOAP service
  • Receive and interpret a response

44
Creating the Client Application
  • import java.net.URL
  • import java.util.Vector 
  • import org.apache.soap.
  • import org.apache.soap.rpc.                     
           
  • public class Client
  • public static void main(String args) throws
    Exception       
  •    URL url new URL("http//localhost8080"
  • "/soap/servlet/rpcrouter")
       // Build the call.   Call call new
    Call()   call.setTargetObjectURI("urnhelloApp")
       call.setMethodName("sayHelloTo")   call.set
    EncodingStyleURI(Constants.NS_URI_SOAP_ENC)

45
Creating the Client Application
  • import java.net.URL
  • import java.util.Vector 
  • import org.apache.soap.
  • import org.apache.soap.rpc.                     
           
  • public class Client
  • public static void main(String args) throws
    Exception       
  •    URL url new URL("http//localhost8080"
  • "/soap/servlet/rpcrouter")
       // Build the call.   Call call new
    Call()   call.setTargetObjectURI("urnhelloApp")
       call.setMethodName("sayHelloTo")   call.set
    EncodingStyleURI(Constants.NS_URI_SOAP_ENC)

46
Creating the Client Application
  • import java.net.URL
  • import java.util.Vector 
  • import org.apache.soap.
  • import org.apache.soap.rpc.
  • public class Client
  • public static void main(String args) throws
    Exception       
  •    URL url new URL("http//localhost8080"
  • "/soap/servlet/rpcrouter")
       // Build the call.   Call call new
    Call()   call.setTargetObjectURI("urnhelloApp")
       call.setMethodName("sayHelloTo")   call.set
    EncodingStyleURI(Constants.NS_URI_SOAP_ENC)

47
  •        Vector params new Vector()        
  •    params.addElement(new Parameter("name",
    String.class,
  • args0, null))
  •        call.setParams(params)   // Invoke the
    call.   Response resp null            try
            resp call.invoke(url, "")
  •      catch( SOAPException e )
  • System.err.println("Caught SOAPException ("
  • e.getFaultCode() ") "
  • e.getMessage())       System.exit(-
    1)           

48
  •        Vector params new Vector()        
  •    params.addElement(new Parameter("name",
    String.class,
  • args0, null))
  •        call.setParams(params)   // Invoke the
    call.   Response resp null            try
            resp call.invoke(url, "")
  •      catch( SOAPException e )
  • System.err.println("Caught SOAPException ("
  • e.getFaultCode() ") "
  • e.getMessage())       System.exit(-
    1)           

49
  •        // Check the response.   if(
    !resp.generatedFault() )
  • Parameter ret resp.getReturnValue()
  • Object value ret.getValue()           
  • System.out.println(value)
  • else
  • Fault fault resp.getFault()            
  • System.err.println("Generated fault ")
  • System.out.println ("  Fault Code   "
  • fault.getFaultCode())  
  •             System.out.println ("  Fault String
    "
  • fault.getFaultString())          

50
Note on Parameters
  • It must be possible to "serialize" the parameters
    that the method invoked receives and returns.
    Why?
  • The following have default serialization/deseriali
    zation
  • primitive types int, long, double, etc.
  • primitive Objects Integer, Long, Double, String,
    etc.
  • complex Objects Vector, Enumeration, Hashtable,
    arrays
  • easy to use JavaBeans (not discussed here)

51
Another Example
  • Web Service to compute addition and subtraction
    of numbers.
  • Web Service will be implemented in Javascript
  • Client Application will be a Servlet

52
Creating the Server
  • When the application server is a script, the
    script is actually put in the deployment
    descriptor
  • Need the jar files bsf.jar and js.jar
  • Put them in your lttomcat_homegt/lib directory

53
Deployment Descriptor CalcDescriptor.xml
  • ltisdservice
  • xmlnsisd"http//xml.apache.org/xml-soap/depl
    oyment"
  • id"urncalcServer"gt
  • ltisdprovider type"script"
  • scope"application"
  • methods"add subtract"gt
  • ltisdscript language"javascript"gt
  • function add(p1, p2) return p1p2
  • function subtract(p1, p2) return p1-p2
  • lt/isdscriptgt
  • lt/isdprovidergt
  • lt/isdservicegt

54
Deployment Descriptor CalcDescriptor.xml
  • ltisdservice
  • xmlnsisd"http//xml.apache.org/xml-soap/depl
    oyment"
  • id"urncalcServer"gt
  • ltisdprovider type"script"
  • scope"application"
  • methods"add subtract"gt
  • ltisdscript language"javascript"gt
  • function add(p1, p2) return p1p2
  • function subtract(p1, p2) return p1-p2
  • lt/isdscriptgt
  • lt/isdprovidergt
  • lt/isdservicegt

Don't forget to deploy using java
org.apache.soap.server.ServiceManagerClient
http//lthostgtltportgt/soap/servlet/rpcrouter
deploy CalcDescriptor.xml
55
Creating the Client Form
  • Start with an HTML form that gets two numbers and
    an operation (basic code below)

ltform method"get" action"servlet/CalcServlet"gt N
umber 1 ltinput type"text" name"p1"gtltbrgt Number
2 ltinput type"text" name"p2"gtltbrgt Operation
ltinput type"radio" name"operation"
value"add"gt Add ltinput type"radio"
name"operation" value"subtract"gt
Subtract ltinput type"submit"gt lt/formgt
Here it is
56
Creating CalcServlet
  • import java.io. import java.net. import
    java.util.
  • import org.apache.soap. import
    org.apache.soap.rpc.
  • import javax.servlet. import javax.servlet.http.
  • public class CalcServlet extends HttpServlet
  • public void doGet(HttpServletRequest req,
    HttpServletResponse res)
  • throws ServletException, IOException
  • res.setContentType("text/html")
  • PrintWriter out res.getWriter()
  • int p1 Integer.parseInt(req.getParameter("p1"))
  • int p2 Integer.parseInt(req.getParameter("p2"))
  • String operation req.getParameter("operation")

57
  • out.println("ltHTMLgtltBODYgtUsing javascript to
    Compute "
  • p1 " " (operation.equals("add")? ""
    "-")
  • " " p2 "ltbrgt")
  • try
  • out.println("Result is" compute(new
    Integer(p1),
  • new Integer(p2),
  • operation))
  • catch (Exception e)
  • e.printStackTrace(new java.io.PrintWriter(out
    ))
  • out.println("lt/BODYgtlt/HTMLgt")
  • out.close()

58
  • private String compute(Integer arg1, Integer
    arg2,
  • String operation) throws Exception
  • URL url new URL ("http//localhost8080/soap
    /servlet/rpcrouter")
  • // Build the call.
  • Call call new Call()
  • call.setTargetObjectURI("urncalcServer")
  • call.setMethodName(operation)
  • call.setEncodingStyleURI(Constants.NS_URI_SOAP
    _ENC)
  • Vector params new Vector()
  • params.addElement(new Parameter("p1",
    Integer.class, p1, null))
  • params.addElement(new Parameter("p2",Integer.c
    lass, p2, null))
  • call.setParams (params)
  • Response resp call.invoke(url, "" )

59
  • // Check the response.
  • if ( resp.generatedFault() )
  • Fault fault resp.getFault ()
  • System.out.println("The call failed ")
  • System.out.println("Fault Code "
    fault.getFaultCode())
  • System.out.println("Fault String "
    fault.getFaultString())
  • return null
  • else
  • Parameter result resp.getReturnValue()
  • return result.getValue().toString()

60
Implementing a SOAP Processor
61
How Could a SOAP Processor be Implemented?
  • No, we won't be implementing a SOAP Processor in
    this course ?
  • However, it could be implemented using reflection
  • Reflection is a Java mechanism for discovering
    the class/methods/fields of an object

62
Simple SOAP Implementation
  • In SOAP, the details about the RPC are in an XML
    message
  • In our Simple SOAP Implementation example,
    details will be in parameters of HTTP request
  • We will allow user to invoke any method of any
    class, provided that the method only has String
    arguments
  • All this is to give us an idea of how a SOAP
    processor works

63
Getting Invocation Details from Client (HTML Form)
  • ltform name"info" method"get" action"servlet/SOA
    PImitation"gt
  • Class Name ltinput type"text" name"class"gtltbrgt
  • Method Name ltinput type"text"
    name"method"gtltbrgt
  • ltinput type"button" value"Set Number of
    Arguments" onClick"setArguments()"gt
  • ltinput type"hidden" name"paramNum" value"0"gt
  • ltp id "argumentP"gtlt/pgt
  • ltinput type"submit"gt
  • lt/formgt

64
Getting Invocation Details from Client (HTML Form)
  • function setArguments()
  • num prompt("Enter the number of arguments to
    the method", 0)
  • p document.getElementById("argumentP")
  • str ""
  • for (i 0 i lt num i)
  • str "Argument " i " "
  • "ltinput type'text' name'param"
    i "'gtltbrgt"
  • p.innerHTMLstr
  • info.paramNum.valuenum

Here it is
65
Our Simple SOAP Processor
  • import java.io. import javax.servlet. import
    javax.servlet.http.
  • import java.lang.reflect.
  • public class SoapImitation extends HttpServlet
  • public void doGet(HttpServletRequest req,
  • HttpServletResponse res)
  • throws ServletException, IOException
  • res.setContentType("text/html")
  • PrintWriter out res.getWriter()
  • try
  • Class objClass Class.forName(req.getParamet
    er("class"))
  • int numParams Integer.parseInt(req.getParame
    ter("paramNum"))

66
  • Class paramClasses new ClassnumParams
  • Object paramObjs new ObjectnumParams
  • for (int i 0 i lt numParams i)
  • paramClassesi Class.forName("java.lang.Strin
    g")
  • paramObjsi req.getParameter("param" i)
  • Method method
  • objClass.getMethod(req.getParameter("method"),
  • paramClasses)
  • Object result method.invoke(objClass.newIns
    tance(), paramObjs)
  • out.println("ltHTMLgtltBODYgtlth1gtMethod
    Resultlt/h1gt"
  • result "lt/BODYgtlt/HTMLgt")
  • catch (Exception e)
  • out.println("ltHTMLgtltBODYgtError!lt/BODYgtlt/HTMLgt
    ")

67
(No Transcript)
68
(No Transcript)
69
Scoping
  • What did the scoping of the Web Service correspnd
    to (page?, request?, session?, application?)
  • How would the implementation differ if the
    scoping was different?

70
UDDI - Universal Description, Discovery and
Integration Service
71
A Telephone Book
  • How can you find a web service?
  • How can you register your web service so others
    will find it?
  • UDDI is a standard for describing and finding web
    services
  • Think of UDDI as a telephone book

72
"Types" of Pages
  • White Pages
  • Basic contact information, business name,
    address, etc.
  • Allow others to find you based on your
    identification
  • Yellow Pages
  • Describe web services by category
  • Allow others to find you by category (e.g., car
    sales business
  • Green Pages
  • Technical information about supported methods of
    web service

73
UDDI Business Registry (UBR), Public Cloud
  • Nodes contain all UDDI information
  • Nodes are synchronized, so they retain the same
    data
  • You can query any node
  • You can add UDDI to a node, and it will be
    replicated to all others

74
Interacting with the UDDI
  • UDDI is itself a web service!!!
  • Interaction is via SOAP messages
  • The JAXR package defines a standard way to
    interact with registries (can work with other
    types of registries too, e.g., ebXML)
  • Two types of interaction
  • Inquiry Does not need authentification
  • Publish Needs authentification
  • Here is a web interface for a UBR node

75
WSDL - Web Services Description Language
76
Describing a Web Service
  • SOAP is just one standard to access a web
    service, there are many others (XML-RPC, WDDX)
  • Need a standard way to describe a Web Service
  • the methods available
  • their parameters
  • etc.
  • WSDL is a standard for describing web services
    using XML, i.e., a language for green pages

77
Recall Currency Exchange Example
  • POST http//services.xmethods.net80/soap
    HTTP/1.0
  • Content-Type text/xml
  • Content-Length 485
  • SOAPAction ""
  • ltSOAP-ENVEnvelope xmlnsSOAP-ENV"http//schemas.
    xmlsoap.org/soap/envelope/" xmlnsxsi"http//www.
    w3.org/1999/XMLSchema-instance"
    xmlnsxsd"http//www.w3.org/1999/XMLSchema"gt
  • ltSOAP-ENVBodygt
  • ltns1getRate xmlnsns1"urnxmethods-CurrencyExcha
    nge" SOAP-ENVencodingStyle"http//schemas.xmlsoa
    p.org/soap/encoding/"gt
  • ltcountry1 xsitype"xsdstring"gtUnited
    Stateslt/country1gt
  • ltcountry2 xsitype"xsdstring"gtIsraellt/country2gt
  • lt/ns1getRategtlt/SOAP-ENVBodygtlt/SOAP-ENVEnvelopegt

78
CurrencyExchange's WSDL
  • Here is the WSDL for this service
  • Note that it has to describe
  • URL
  • URI
  • Method Name
  • Method Namespace
  • Parameter Names
  • Parameter Types
  • Encoding of Parameters

79
lt?xml version"1.0"?gt ltdefinitions
name"CurrencyExchangeService" targetNamespace"ht
tp//www.xmethods.net/sd/CurrencyExchangeService.w
sdl" xmlnstns"http//www.xmethods.net/sd/Currenc
yExchangeService.wsdl" xmlnsxsd"http//www.w3.or
g/2001/XMLSchema" xmlnssoap"http//schemas.xmlso
ap.org/wsdl/soap/" xmlns"http//schemas.xmlsoap.o
rg/wsdl/"gt ltmessage name"getRateRequest"gt ltpar
t name"country1" type"xsdstring"/gt ltpart
name"country2" type"xsdstring"/gt lt/messagegt lt
message name"getRateResponse"gt ltpart
name"Result" type"xsdfloat"/gt lt/messagegt
80
ltportType name"CurrencyExchangePortType"gt
ltoperation name"getRate"gt ltinput
message"tnsgetRateRequest" /gt ltoutput
message"tnsgetRateResponse" /gt
lt/operationgt lt/portTypegt ltbinding
name"CurrencyExchangeBinding"
type"tnsCurrencyExchangePortType"gt
ltsoapbinding style"rpc"
transport"http//schemas.xmlsoap.org/soap/http"/gt
ltoperation name"getRate"gt ltsoapoperation
soapAction""/gt ltinput gt ltsoapbody
use"encoded" namespace"urnxmethods-CurrencyExc
hange" encodingStyle"http//schemas.xmlsoap.org/
soap/encoding/"/gt lt/inputgt
81
ltoutput gt ltsoapbody use"encoded"
namespace"urnxmethods-CurrencyExchange"
encodingStyle"http//schemas.xmlsoap.org/soap/en
coding/"/gt lt/outputgt lt/operationgt lt/bindinggt lt
service name"CurrencyExchangeService"gt
ltdocumentationgtReturns the exchange rate
lt/documentationgt ltport name"CurrencyExchangePort
" binding"tnsCurrencyExchangeBinding"gt ltsoap
address location"http//services.xmethods.n
et80/soap"/gt lt/portgt lt/servicegtlt/definitionsgt
82
Web Services Pitfalls
83
Using Web Services is not as Simple as it Looks
  • It is not practical to automatically find web
    services for your needs (UBR contains a lot of
    junk)
  • There is no built-in mechanism for payment for
    use of a web service
  • There is no built-in security control
  • When a web service changes (e.g., adds a
    parameter to its method), the program using it
    breaks
Write a Comment
User Comments (0)
About PowerShow.com