Docstoc

Type

Document Sample
Type Powered By Docstoc
					Web Service

  Jianguo Lu




               1
Motivation
• Build a web site about Windsor
    – Include a weather report from cnn.com;
    – Include daily stocks report for companies in
      Windsor;
    – Include maps from google map;
    – Include currency exchange from xe.com
    – …
• In the old days, we extract information from
  various sites using screen/html scraping
    – It is unreliable
    – Difficult to extract the data and maintain the
      web site
• The solution: web service
    – Each web site provide a programmable
      interface

                                                       2
What is web service
• Web Service
    – A vague term that refers to distributed or virtual applications or
      processes that use the Internet to link activities or software components.
• Another example application (vertical integration)
    – A travel Web site that takes a reservation from a customer, and then
      sends a message to a hotel application, accessed via the Web, to
      determine if a room is available, books it, and tells the customer he or
      she has a reservation is an example of a Web Services application.
• Challenges
    –   Find the services
    –   Publish the services
    –   Invoke the services
    –   Describe the services
    –   …..



                                                                             3
Service Oriented Architecture


                    Discovery
                    agency


                                 publish
          find


                      interact
        Requester                Provider




                                            4
A concrete SOA


     UDDI
    Registry


          Finds
          Service                        WSDL

                                                      Describes
                                                      Service
     Service                            Web Service
                         SOAP
    Consumer                             Provider

                    Communicates with
                     XML Messages

                                                          5
Web service definitions
• A web service is an application accessible to other applications over
  the internet
• “Self-contained, modular business applications that have open,
  internet-oriented, standard based interfaces”—UDDI Consortium
• “a software application identified by a URI, whose interfaces and
  bindings are capable of being defined, described, and discovered as
  XML artifacts. A web service supports direct interactions with other
  software agents using XML-based messages exchanged via
  Internet-based protocols.” --W3C
• “a standardized way of integrating Web-based applications using the
  XML, SOAP, WSDL, and UDDI open standards over an Internet
  protocol backbone. XML is used to tag the data, SOAP is used to
  transfer the data, WSDL is used for describing the service available,
  and UDDI is used for listing what services are available”--Webopedia

More concrete
                                                                   6
Web Service definition
• “encapsulated, loosely coupled, contracted software objects offered
  via standard protocols” --ZapThink Research
   – Encapsulated
       • Web Service implementation is invisible to entities outside the service
       • Exposes an interface but hides details
   – Loosely Coupled
       • Service and consumer software can be redesigned independently
       • Messages are decoupled from the data transport, and are easy to integrate
         with other platforms and open standards technology.
       • Changing the implementation of one component does not require changing
         the rest of the services, which makes configuration and deployment easier to
         manage.
   – Self-Describing and Adapting.
   – Distributed and Location-Independent.
   – Open Standards-Based.



                                                                                   7
Why Web Service
• “Just as the web revolutionized how users talk to applications, XML
  transforms way applications talk to each other”—Bill Gates
• “Web services will be bigger than Java or XML”. – Rod Smith, VP of
  Emerging Technology, IBM
• “Web services are expected to revolutionize our life in much the
  same way as the internet has during the past decade or so.”—
  Gartner
• “By 2004, 40% of the financial services transactions and 35% of
  online government services will be web service-based.” –Gartner


• There are web services for Google, Amazon, Fedex, EBay … …

• It is a buzzword. Is it another hype?



                                                                   8
Three Laws of Computing
• Moore's Law
   – Computing power doubles every 18 months
• Gilder's Law
   – Network bandwidth capacity doubles every 12
     months
• Metcalfe's Law (Net Effect)
   – Value of network is proportional to the square
     of the number of users of the system (n2).
• Web services greatly increase network
  value




                                                      9
HTTP form


 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
    Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html> <head>
    <title>Simple Form</title>
    <meta http-equiv="Content-Type"
                 content="text/html; charset=iso-8859-1">
    </head>
 <body>
    <form action="http://localhost:1234/" method="post"
           enctype="multipart/form-data" name="simpleForm" >
        <p>Text Field
            <input type="text" name="textfield"></p>
        <p>File to upload
            <input type="file" name="file"></p> <p>
            <input type="submit" name="Submit" value="Submit">
    </p>
    </form>

 </body> </html>
                                                                 10
http request
  POST / HTTP/1.1
  Host: cascadetg.com
  User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5) Gecko/20031007
       Firebird/0.7
  Accept: text/xml,application/xml,application/xhtml+xml,text/ html;q=0.9,text/plain;q=0.8,video/x-
       mng,image/png,image/ jpeg,image/gif;q=0.2,*/*;q=0.1
  Accept-Language: en-us,en;q=0.5
  Accept-Encoding: gzip,deflate
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  Keep-Alive: 300
  Connection: keep-alive
  Cookie: CP=null*
  Content-Type: multipart/form-data; boundary=---------------------------41184676334
  Content-Length: 438
  -----------------------------41184676334
  Content-Disposition: form-data; name="textfield"
  Test
  -----------------------------41184676334
  Content-Disposition: form-data; name="file"; filename="tiny.txt"
  Content-Type: text/plain

  A small text file. Nothing to see here. Move along.
  -----------------------------41184676334
  Content-Disposition: form-data; name="Submit"

  Submit                                                                                         11
  -----------------------------41184676334--
From HTTP to RPC
• Remote procedure calls, or RPC, have been around for a
  long time. The concept is simple: let's say you have a
  function:
   bankAccount.adjust(Money amount);

• The method is easy to understand and work with as part
  of a program compiled and running on a single machine.
• A RPC makes a method call on another machine.
   // Local code
   Money adjustment = new Money(1.50);

   // Remote code
   myBankAccount.adjust(adjustment);

                                                     12
Web service distributed computing

Concept            CORBA                Java             Web service

Interface          IDL                  Java interface   WSDL
Description
RPC support        ORBs                 RMI              SOAP
Service registry   CORBA naming         JNDI             UDDI
                   service
Messaging          CORBA                JMS              ?
support            Event/Notification
                   service
Transaction        CORBA transaction    JTS, EJB         ?
support            service
Security support   CORBA security       ?                ?
                   service




                                                                       13
History of Interface Definition Languages (IDLs)
• IDL has a long history in distributed computing
   –   DCE (Distributed Computing Environment)
   –   CORBA IDL, OMG (Object Management Group)
   –   COM IDL, Microsoft
   –   WSDL
• Traditional IDLs
   – Specifying what to call: the operation names, their signatures,
     exceptions. This is the job of IDL.
   – Agreeing on how to make an invocation: the mechanism of
     naming, activation, data encoding. This is what distributed
     standards such as CORBA or COM do.
• WSDL needs to specify the both: the operation provided
  by the service, and the mechanism to access the service.

                                                                   14
Web Service Description Language
• WSDL defines
   – What the service is: the operations the service provides, including
     the signature of the operation –- what
   – Access specification: details of the data format and protocol
     necessary to access the service’s operation–- how
   – Location of the service: details of the network address, such as a
     URL –- where




                                                                   15
                                                         –What
WSDL functionality view                         Legend: –how
                                                         –where
 Interface
                               Supports
         Port Type                               Operation

    Formats & Protocols                         Input & Output


             Binding          How to encode
                                                  Message


   Implements

                                 Provides
              Port                                 Service
                               Endpoints
                                                                 16
                     In WSDL 2.0, portType is changed to interface.
WSDL document structure view
    WSDL specification


       abstract part
               types

              messages

               operations

              port types


       concrete part
               bindings

             services and
                 ports

                               17
<?xml version="1.0"?>
<definitions name="Procurement"
   targetNamespace="http://example.com/procurement/definitions"
   xmlns:tns="http://example.com/procurement/definitions"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns="http://schemas.xmlsoap.org/wsdl/" >
                                                                             abstract
   <message name="OrderMsg">                                                 part
      <part name="productName" type="xs:string"/>
      <part name="quantity" type="xs:integer"/>                              messages
   </message>


   <portType name="procurementPortType">
      <operation name="orderGoods">
         <input message = "OrderMsg"/>
                                                                             operation and
      </operation>                                                           port type
   </portType>



    <binding name="ProcurementSoapBinding" type="tns:procurementPortType">
        <soap:binding style="document"                                       concrete
               transport="http://schemas.xmlsoap.org/soap/http"/>            part
        <operation name="orderGoods">
           <soap:operation soapAction="http://example.com/orderGoods"/>
           <input>
                <soap:body use="literal"/>                                   binding
           </input>
           <output>
                <soap:body use="literal"/>
           </output>
        </operation>
    </binding>

    <service name="ProcurementService">
        <port name="ProcurementPort" binding="tns:ProcurementSoapBinding">   port and
           <soap:address location="http://example.com/procurement"/>
        </port>
                                                                             service
    </service>                                                                               18
</definitions>
<definitions … name="BNQuoteService">
<message name="getPriceRequest"> <part name="isbn" type="xsd:string"/> </message>
<message name="getPriceResponse">          <part name="return" type="xsd:float"/> </message>
<portType name="BNQuotePortType">
<operation name="getPrice">
    <input name="getPrice" message="tns:getPriceRequest"/>
    <output name="getPriceResponse" message="tns:getPriceResponse"/>
</operation>
</portType>

<binding name="BNQuoteBinding" type="tns:BNQuotePortType">
     <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
     <operation name="getPrice"><soap:operation/>
     <input>            <soap:body use="encoded"
     encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:xmethods-
     BNPriceCheck"/>
     </input>
     <output>
      <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
     namespace="urn:xmethods-BNPriceCheck"/>
     </output>
     </operation>
</binding>
<service name="BNQuoteService">
     <documentation>Returns price of a book at BN.com given an ISBN number</documentation>
     <port name="BNQuotePort" binding="tns:BNQuoteBinding">
             <soap:address location="http://services.xmethods.net:80/soap/servlet/rpcrouter"/>
     </port>
</service>
</definitions>                                                                                   19
Visual representation




                        20
  WSDL structure—interconnection view
         WSDL document




                                                                                                   Abstract description of the service
                   Types (type information for the document, e.g., XML Schema)


          Message 1       Message 2        Message 3           Message 4               Message 5



                   Operation 1                   Operation 2                Operation 3



                                         Interface (abstract service)




                                                                                                   Concrete description
                      binding 1          binding 2       binding 3        binding 4




                                                                                                      of the service
                     endpoint 1          endpoint 2     endpoint 3        endpoint 4

                                          Service (the interface in all
                                        its available implementations)
                                                                                                   21
By Gustavo Alonso and Cesare Pautasso
A WSDL example
<?xml version="1.0"?>
<definitions name="PriceCheck"
    targetNamespace="http://www.skatestown.com/services/PriceCheck"
     xmlns:pc="http://www.skatestown.com/services/PriceCheck"
     xmlns:avail="http://www.skatestown.com/ns/availability"
     xmlns:wsi="http://ws-i.org/schemas/conformanceClaim/"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
     xmlns="http://schemas.xmlsoap.org/wsdl/">
<types> …</types>
<message> …</message>
<message> … </message>
<portType> … </portType>
<binding> … </binding>
<service> … </service>
</definitions>

                 This example is from Steve Graham et al: Building Web Services   22
                 with Java
PortType
<portType name="PriceCheckPortType">
   <operation name="checkPrice">
     <input message="pc:PriceCheckRequest"/>
     <output message="pc:PriceCheckResponse"/>
   </operation>
 </portType>

•   <portType> defines the interface of web service. Just as Java Interface
    declaration;
     – Will change the name to <interface> in WSDL 2.0.
•   It consists of a sequence of operation declarations.
•   WSDL can have zero or more <portType>s. Typically just one.
•   It has a name attribute, must be unique.
     – The binding will refer the portType by its name



                                                                              23
Operation
• Operation defines a method signature;
   – Name, input, output, and fault
• Input and output elements are associated with
  messages;
• Different combinations of input/output define different
  operations types.

  <operation name="checkPrice">
    <input message="pc:PriceCheckRequest"/>
    <output message="pc:PriceCheckResponse"/>
  </operation>



                                                            24
Operation Types
•   The request-response type is the most common operation type, but WSDL
    defines four types:
     – Request-response: The operation can receive a request and will return a response
         <operation name="checkPrice">
               <input message="pc:PriceCheckRequest"/>
               <output message="pc:PriceCheckResponse"/>
         </operation>
     – One-way: The operation can receive a message but will not return a response.
        <operation name=“cancellation”>
            <input message=“tns:orderCancellation”/>
        </operation>
     – Notification:The operation can send a message but will not wait for a response
        <operation name=“notification”>
            <output message=“tns:promotionNotification”/>
        </operation>
     – Solicit-response:The operation can send a request and will wait for a response
        <operation name=“cancellation”>
             <output message=“tns:pushThis”/>
             <input message=“tns:reponseToPush”/>
        </operation>

•   Different types are defined by decided by the order/occurrences of input and output.



                                                                                     25
Messages
<message name="PriceCheckRequest">
   <part name="sku" element="avail:sku"/>
</message>

    <message name="PriceCheckResponse">
      <part name="result" element="avail:StockAvailability"/>
    </message>

•     Describe the abstract form of input, output, or fault.
•     A WSDL file can have zero or more messages.
•     Each message has a name, which is unique within the document.
•     Each message has a collection of <part> elements.




                                                                      26
Part
   <part name="sku" element="avail:sku"/>
   <part name="result" element="avail:StockAvailability"/>
• A <part> element can be compared to a parameter in a
  method.
• A part element has two properties: one is name, the other
  is its kind.
• Kind can be a type or an element
   – Element refers to an element defined in XML Schema
   – Type refers to a simpleType or a complexType in XSD
• In corresponding sku definition is:
   <xsd:element name="sku" type="xsd:string" />



                                                             27
Types in WSDL
<types>
   <xsd:schema
        targetNamespace="http://www.skatestown.com/ns/availability" >

       <xsd:element name="sku" type="xsd:string" />

       <xsd:complexType name="availabilityType">
         <xsd:sequence>
           <xsd:element ref="avail:sku"/>
           <xsd:element name="price" type="xsd:double"/>
           <xsd:element name="quantityAvailable" type="xsd:integer"/>
         </xsd:sequence>
       </xsd:complexType>

        <xsd:element name="StockAvailability"
                type="avail:availabilityType" />
      </xsd:schema>
    </types>

•     The default type system is XML Schema;
        –   Theoretically you can use any type system, such as Java types.
•     To be used in<part> element;
•     We can also import XML Schemas

                                                                             28
SOAP request message
<soapenv:Envelope
  xmlns:soapenv=“http://schema.xmlsoap.org/soap/evelope/”
  xmlns:xsd=“http://www.w3.org/2001/XMLSchema”
  xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”>
  <soapenv:Body>
      <sku xmlns=“http://www.skatestown.com/ns/availability”>123</sku>
  </soapenv:Body>
</soapenv:Envelope>
                                                        Envelope
•   Relevant part of the WSDL file:                             Header #0
<xsd:element name="sku" type="xsd:string" />
                                                                Header #1
<message name="PriceCheckRequest">
   <part name="sku" element="avail:sku"/>
</message>                                                         Body
<operation name="checkPrice">                                    Message
  <input message="pc:PriceCheckRequest"/>                        Payload
  <output message="pc:PriceCheckResponse"/>
</operation>                                                                29
SOAP response message
<soapenv:Envelope                     • Relevant part of the WSDL file:
 xmlns:soapenv= …                     <xsd:complexType
 xmlns:xsd=…                              name="availabilityType">
                                       <xsd:sequence>
 xmlns:xsi=… >                           <xsd:element ref="avail:sku"/>
 <soapenv:Body>                          <xsd:element name="price"
                                          type="xsd:double"/>
   <StockAvailability xmlns= … >          <xsd:element name="quantityAvailable"
     <sku> 123 </sku>                     type="xsd:integer"/>
     <price xmlns=“”>100.00</price>    </xsd:sequence>
                                      </xsd:complexType>
     <quantityAvailable xmlns=“”>
         12                           <message name="PriceCheckResponse">
     </quantityAvailable>               <part name="result"
                                          element="avail:StockAvailability"/>
   </StockAvailability>               </message>
 </soapenv:Body>
</soapenv:Envelope>                   <operation name="checkPrice">
                                        <input message="pc:PriceCheckRequest"/>
                                        <output message="pc:PriceCheckResponse"/>
                                      </operation>




                                                                                30
Binding
<binding name="PriceCheckSOAPBinding" type="pc:PriceCheckPortType">
   <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
   <operation name="checkPrice">
      <soap:operation soapAction =
                     "http://www.skatestown.com/services/PriceCheck/checkPrice" />
         <input> <soap:body use="literal" />
         </input>
         <output> <soap:body use="literal" />
         </output>
   </operation>
 </binding>
• Name of the binding should be unique;
• Link to the portType is achieved by the portType name
    – This explains why portType name should be unique.
• Typically, there is only one <binding> element;
• Defines:
    –   Invocation style
    –   SOAPAction
    –   Input message appearance
    –   Output message appearance
                                                                                 31
Invocation Style
  <soap:binding style="document"
      transport="http://schemas.xmlsoap.org/soap/http" />
• All operations in this portType use SOAP messages;
• The style attribute indicates that operations will follow a
  document-centric approach;
   – The body of the SOAP message is a straight XML document;
   – This is in contrast to RPC style;




                                                                32
RPC style and document style
•   When using Document style, you can structure the contents of the SOAP Body any
    way you like.
•   When using RPC style, the contents of the SOAP Body must conform to a structure
    that indicates the method name and contains a set of parameters. It looks like this:
     <env:Body>
        <m:methodName xmlns:m="someURI">
              <m:m1>...</m:m1> <m:m2>...</m:m2> ...
        </m:methodName>
     </env:Body>

•   Document style:
     <env:Body>
          <m:purchaseOrder xmlns:m="someURI"> ... </m:purchaseOrder>
     </env:Body>
•   RPC style:
     <env:Body>
         <m:placeOrder xmlns:m="someURI">
             <m:purchaseOrder> ... </m:purchaseOrder>
         </m:placeOrder>
     </env:Body>




                                                                                     33
Service
<service name="PriceCheck">
  <port name="Pricecheck" binding="pc:PriceCheckSOAPBinding">
   <documentation>
     <wsi:Claim
       conformsTo="http://ws-i.org/profiles/basic/1.0" />
   </documentation>
   <soap:address location =
                  "http://www.skatestown.com/services/PriceCheck"/>
  </port>
 </service>
• Contains a set of <port> elements;
• <port> combines the interface binding with a network address
  specified by a URI;
• A web service can be available in different web addresses;



                                                                      34
Implications of WSDL
• Interaction mode:
   – Not only expose the operations to invoke, but also a service
     behave like a client. Blurring the distinction between a client and
     server.
• Does not presume a particular form of communication,
  protocol, data encoding.
   – You can use HTTP, or SMTP, or FTP, …
   – You can use SOAP, or others




                                                                     35
WSDL 1.1 vs WSDL 2.0
               WSDL 2.0                                  WSDL 1.1
•   Endpoints                              •   Ports
•   Interfaces                             •   PortTypes
     – Support for Interface inheritance

•   Removed operation overloading          •   Supported operation overloading


•   Messages defined through Types         •   Messages composed of Parts
•   Operations nested inside               •   6 Top level elements: Messages,
    Interfaces                                 Operations, PortTypes, Bindings,
•   Endpoints nested inside Bindings           Ports and Services.


•   9 Message Exchange Patterns            •   4 Transmission Primitives
                                                – (One-way, Request-Response,
                                                  Solicit-Response, Notification)


•   New: Features and Properties                                            36
Tools
• WSDL parser: WSDL4J
• Apache Axis
   –   Invoking SOAP web service;
   –   Translate WSDL to Java, and vice versa;
   –   Mapping Java into XML Schema, and vice versa;
   –   Host web service;
   –   API for manipulating SOAP;




                                                       37
Mapping between Java and WSDL in JAX-RPC
• A simple example
             <message name="fooRequest">
                         <part name="para1" type="xs:string"/>
             </message>
             <message name="fooResponse">
                         <part name="para2" type="xs:float"/>
             </message>
             <portType name="FooBar">
                         <operation name="foo">
                                      <input message="y:fooRequest"/>
                                      <output message="y:fooResponse"/>
                         </operation>
             </portType>




  public interface FooBar extends java.rmi.Remote{
    public float foo(java.lang.String para1)
                      throws java.rmi.RemoteException;
  }


                                                                          38
• Multiple input parameters

          <message name="fooRequest">
                      <part name="param1" type="xs:string"/>
                      <part name="param2" type="xs:int"/>
                      <part name="param3" type="xs:boolean"/>
          </message>
          <message name="fooResponse">
                      <part name="para2" type="xs:float"/>
          </message>
          <portType name="FooBar">
                      <operation name="foo">
                                   <input message="y:fooRequest"/>
                                   <output message="y:fooResponse"/>
                      </operation>
          </portType>


  public interface FooBar extends java.rmi.Remote{
    public float foo(String param1, int param2, boolean param3)
                     throws java.rmi.RemoteException;
  }


                                                                       39
• Multiple output parameters
                <message name="fooRequest">
                            <part name="param1" type="xs:string"/>
                </message>
                <message name="fooResponse">
                            <part name="param2" type="xs:int"/>
                            <part name="param3" type="xs:boolean"/>
                            <part name="param4" type="xs:float"/>
                </message>
                <portType name="FooBar">
                            <operation name="foo">
                                         <input message="y:fooRequest"/>
                                         <output message="y:fooResponse"/>
                            </operation>
                </portType>



  public interface FooBar extends java.rmi.Remote{
    public void foo(String param1,
                     javax.xml.rpc.holders.IntHolder param2,
                      javax.xml.rpc.holders.BooleanHolder param3,
                      javax.xml.rpc.holders.FloatHolder param4,
            ) throws java.rmi.RemoteException;
  }                                                                          40
Mapping XML Schema to Java
   public class Address {
     private String street;
     private String city;
     public String getCity(){return city; }
     public void setCity(String c){city=c; }
     ……
   }

   <complexType name="Address">
      <sequence>
        <element name="city" nillable="true" type="xsd:string"/>
        <element name="street" nillable="true" type="xsd:string"/>
      </sequence>
   </complexType>

• Class has to have correct getters and setters;
• In axis, you can use WSDL2Java and Java2WSDL to do the mapping.
                                                                     41

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:4/1/2013
language:Unknown
pages:41