Download
web services n.
Skip this Video
Loading SlideShow in 5 Seconds..
Web Services PowerPoint Presentation
Download Presentation
Web Services

Web Services

208 Views Download Presentation
Download Presentation

Web Services

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Web Services Representation and Management of Data on the Web

  2. "Web services are loosely coupled software components delivered over Internet standard technologies."

  3. Return Stock Price Get Stock Price Get Stock Price Return Stock Price Example Scenario: Get Online Information Stock Broker Application Stock Exchange

  4. The Hobbit (5) Price: 20.95 Copies in Stock: 5 The Hobbit (5) Price: 20.95 Copies in Stock: 4 Buy The Hobbit (5) How Much? Buy it Book Store Buy The Hobbit (5) 20.95 The Hobbit (5) Price: 25.95 Copies in Stock: 1 The Hobbit (5) Price: 25.95 Copies in Stock: 0 Example Scenario – Online Trade Book Store The Hobbit (5) Price: 25.95 Copies in Stock: 1 The Hobbit (5) Price: 25.95 Copies in Stock: 0

  5. Example Scenario:Grid Computation Grid Computation Using seamlessly the combined resources of many computers that are connected to the Internet

  6. What is a Web Service? • Self-contained, modular Web application that can be published, located and invoked across the Web • A Web service can perform functions of varying complexities • Once deployed, other applications (and other Web services) can discover and invoke the deployed service

  7. Why is it Difficult to Use Ordinary Web Sites as Services? • Consider an application that should return the price of the book “The Hobbit” • How can your application find suitable online stores? • How can your application find the price of the book in a Web page? • How can your application fill forms, if needed?

  8. How can we find this URL? What is the price here?

  9. Calling Remote FunctionsCould Help • It would help if we could call functions, such as: • Amazon.getPrice(“The Hobbit") • Amazon.buyBook(“The Hobbit", myId) getPrice(…) The Internet

  10. Difficulties in UsingRemote Functions • How can the calling application know in what language the functions are written? • How can the application know what functions are available and what are their signatures? • How can an application call a function that resides behind a firewall?

  11. The Solution • Use an agreed interface and a syntax that all applications are familiar with (e.g., XML) • For example, SOAP • Use HTTP to transfer data through port 80 • Use a standard for publishing methods, their signatures and their usage • For example, WSDL • Use standard directory structures for publishing available services • For example, UDDI

  12. Web Services that areAlready Available • Google search (http://www.google.com/apis) • Weather reports • Stock prices • Currency exchanges • Sending SMS messages, faxes • Prices of books in Barnes and Nobles • Dictionaries • etc.

  13. Implementing Web Services • Programmers are given tools that spare the need to directly write SOAP or WSDL documents • In Java: • JAX-RPC: part of SUN tools for publishing and deploying Web Services • AXIS: Apache’s tool for handling Web services in Java

  14. SOAPSimple Object-Access Protocol

  15. What is SOAP? • SOAP is a protocol for accessing Web Services • SOAP is XML based • Thus, SOAP provides interoperability • In SOAP, applications exchange information over HTTP • Thus, SOAP is not restricted by firewalls • SOAP allows to exchange structured and typed information on the Web • XSchema types are used to add types to XML • SOAP specification: http://www.w3.org/2000/xp/Group/

  16. POST /soap HTTP/1.0 SOAPAction: "" Content-Length: 520 <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <ns1:getRate soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:xmethods-CurrencyExchange"> <country1 xsi:type="xsd:string">Euro</country1> <country2 xsi:type="xsd:string">Israel</country2> </ns1:getRate> </soapenv:Body> </soapenv:Envelope> A request to http://services.xmethods.net:80/soap

  17. HTTP/1.0 200 OK Date: Sat, 07 May 2005 23:26:21 GMT Content-Length: 492 Content-Type: text/xml <?xml version='1.0' encoding='UTF-8'?> <soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' soap:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'> <soap:Body> <n:getRateResponse xmlns:n='urn:xmethods-CurrencyExchange'> <Result xsi:type='xsd:float'>5.5825</Result> </n:getRateResponse> </soap:Body> </soap:Envelope> The Response

  18. A SOAP Message • A SOAP message is an ordinary XML document containing the following elements: • Envelope – identifies the XML document as a SOAP message: required • Header – contains header information: optional • Body – contains call or response information: required • Fault – provides information about errors that occurred while processing the message: optional

  19. Name of the Interface Name of the Method Name of the Parameter SOAP Simplification (1) public interface Hello { public String sayHelloTo(String name); } • Consider the Java interface: • Suppose that a client wants to call the server'ssayHelloTomethod • Could send an XML message: <?xml version="1.0"?><Hello>    <sayHelloTo>        <name>Lisa</name>    </sayHelloTo></Hello>

  20. Name of the Interface Name of the Method + Response Returned Value SOAP Simplification (2) • The Server could respond with: <?xml version="1.0"?><Hello>    <sayHelloToResponse>       <message>Hello Lisa, How are you?</message>    </sayHelloToResponse></Hello>

  21. SOAP Intuition

  22. A SOAP envelope must be Associated with this name space Skeleton SOAP Message <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap:Header> ... ... </soap:Header> <soap:Body> ... ... <soap:Fault> ... ... </soap:Fault> </soap:Body> </soap:Envelope> Envelope Header Body Fault

  23. encodingStyle Attribute • “The SOAP encodingStyle attribute indicates the encoding rules used to serialize parts of a SOAP message” • Needed when sending data structures • This attribute may appear on any SOAP element, and it will apply to that element's content and all child elements • A SOAP message has no default encoding • Unencoded data may be used in SOAP messages • The SOAP default XMLSchema for SOAP encoding and data types is: http://www.w3.org/2002/12/soap-encoding • Other encoding rules may be used

  24. Envelope Actual Soap Request <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/1999/XMLSchema">    <SOAP-ENV:Header> </SOAP-ENV:Header>    <SOAP-ENV:Body>         <ns1:sayHelloTo  xmlns:ns1="Hello" SOAP-ENV:encodingStyle=" http://schemas.xmlsoap.org/soap/encoding/">           <name xsi:type="xsd:string">Lisa</name>         </ns1:sayHelloTo>    </SOAP-ENV:Body> </SOAP-ENV:Envelope>

  25. Name Spaces Actual Soap Request <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/1999/XMLSchema">    <SOAP-ENV:Header> </SOAP-ENV:Header>    <SOAP-ENV:Body>         <ns1:sayHelloTo  xmlns:ns1="Hello" SOAP-ENV:encodingStyle=" http://schemas.xmlsoap.org/soap/encoding/">           <name xsi:type="xsd:string">Lisa</name>         </ns1:sayHelloTo>    </SOAP-ENV:Body> </SOAP-ENV:Envelope>

  26. Header Body Actual Soap Request <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/1999/XMLSchema">    <SOAP-ENV:Header> </SOAP-ENV:Header>    <SOAP-ENV:Body>         <ns1:sayHelloTo  xmlns:ns1="Hello" SOAP-ENV:encodingStyle=" http://schemas.xmlsoap.org/soap/encoding/">           <name xsi:type="xsd:string">Lisa</name>         </ns1:sayHelloTo>    </SOAP-ENV:Body> </SOAP-ENV:Envelope>

  27. Interface Method Parameter Actual Soap Request <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"    xmlns:xsd="http://www.w3.org/1999/XMLSchema">    <SOAP-ENV:Header> </SOAP-ENV:Header>    <SOAP-ENV:Body>         <ns1:sayHelloTo  xmlns:ns1="Hello" SOAP-ENV:encodingStyle=" http://schemas.xmlsoap.org/soap/encoding/">           <name xsi:type="xsd:string">Lisa</name>         </ns1:sayHelloTo>    </SOAP-ENV:Body> </SOAP-ENV:Envelope>

  28. Envelope Actual Soap Response <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"    xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">    <SOAP-ENV:Body>          <ns1:sayHelloToResponse xmlns:ns1="Hello"                SOAP-ENV:encodingStyle=" http://schemas.xmlsoap.org/soap/encoding/">                 <return xsi:type="xsd:string"> Hello Lisa, How are you doing? </return>          </ns1:sayHelloToResponse>    </SOAP-ENV:Body> </SOAP-ENV:Envelope>

  29. Actual Soap Response <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"    xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">    <SOAP-ENV:Body>          <ns1:sayHelloToResponse xmlns:ns1="Hello"                SOAP-ENV:encodingStyle=" http://schemas.xmlsoap.org/soap/encoding/">                 <return xsi:type="xsd:string"> Hello Lisa, How are you doing? </return>          </ns1:sayHelloToResponse>    </SOAP-ENV:Body> </SOAP-ENV:Envelope> Body

  30. Method Interface Returned Value Actual Soap Response <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"    xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">    <SOAP-ENV:Body>          <ns1:sayHelloToResponse xmlns:ns1="Hello"                SOAP-ENV:encodingStyle=" http://schemas.xmlsoap.org/soap/encoding/">                 <return xsi:type="xsd:string"> Hello Lisa, How are you doing? </return>          </ns1:sayHelloToResponse>    </SOAP-ENV:Body> </SOAP-ENV:Envelope>

  31. SOAP Header Element • The SOAP Header element is optional • It contains application specific information (like authentication, payment, etc) about the SOAP message • If the Header element is present, it must be the first child element of the Envelope element • Attributes that the namespace defines: • Actor– used to address the Header element to a particular server (e.g., proxy) on the message path through the Internet • mustUnderstand– used to indicate whether a header entry is mandatory or optional for the recipient to process • encodingStyle– as explained before

  32. SOAP Header Element • Example: <SOAP-ENV:Header>     <t:Transaction xmlns:t="some-URI" SOAP-ENV:mustUnderstand="1">5</t:Transaction> </SOAP-ENV:Header> • 5 is the transaction ID of which this method is a part • In the above example, the SOAP-envelope attribute mustUnderstand is set to 1, which means that the server must either understand and honor the transaction request or must fail to process the message

  33. SOAP Response on Error • There can be many errors in processing a SOAP request • Error in Running Methods: For example, suppose that the "Hello Server" does not allow anyone to say hello on Tuesday • Error in Processing SOAP Headers: For example, a problem running the method as part of a transaction

  34. The Fault Element May Include the Following Sub-Elements • <faultcode> : A code for identifying the fault • <faultstring> : A human readable explanation of the fault • <faultactor> : Information about who caused the fault • <detail> : Holds application-specific error information related to the Body element of the SOAP request

  35. SOAP Fault Codes • VersionMismatch: Found an invalid namespace for the SOAP Envelope element • MustUnderstand: An immediate child element of the Header element, with the mustUnderstand attribute set to 1, was not understood • Client: The message was incorrectly formed or contained incorrect information • Server: There was a problem with the server so the message could not proceed

  36. SOAP Error Response for Method Error <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <SOAP-ENV:Fault> <faultcode>SOAP-ENV:Server</faultcode> <faultstring>Server Error</faultstring> <detail> <e:myfaultdetails xmlns:e="Hello"> <message> Sorry, I cannot say hello on Tuesday. </message> <errorcode>1001</errorcode> </e:myfaultdetails> </detail> </SOAP-ENV:Fault> </SOAP-ENV:Body></SOAP-ENV:Envelope>

  37. SOAP Error Response for Header Error <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <SOAP-ENV:Fault> <faultcode>SOAP-ENV:MustUnderstand</faultcode> <faultstring>SOAP Must Understand Error</faultstring> </SOAP-ENV:Fault> </SOAP-ENV:Body></SOAP-ENV:Envelope> No detail element may appear when there is an error in processing the Headers of a SOAP request

  38. Sending a Request • The SOAP request does 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 (usually HTTP, but could also be another protocol, e.g., SMTP)

  39. SOAP Request via HTTP POST http://www.Hello.com/HelloApplication HTTP/1.0 Content-Type: text/xml; charset=UTF-8 Content-Length: 587 SOAPAction: urn:helloApp <SOAP-ENV:Envelope … Note: There are 2 addresses (1) URL of a SOAP Server (2) URI of an application to run (this needn't correspond to an actual Internet address)

  40. SOAPAction Header • Used to indicate the intent of the SOAP HTTP request • The presence and content of the SOAPAction header field can be used by servers, such as firewalls, to appropriately filter SOAP request messages in HTTP • The header-field value of an empty string ("") means that the intent of the SOAP message is provided by the URL of the HTTP Request

  41. SOAP Response via HTTP HTTP/1.0 200 OK Content-Type: text/xml; charset=UTF-8 Content-Length: 615 <SOAP-ENV:Envelope …

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

  43. POST http://services.xmethods.net:80/soap HTTP/1.0 Content-Type: text/xml Content-Length: 485 SOAPAction: "" <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body> <ns1:getRate xmlns:ns1="urn:xmethods-CurrencyExchange" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <country1 xsi:type="xsd:string">United States</country1> <country2 xsi:type="xsd:string">Israel</country2> </ns1:getRate> </SOAP-ENV:Body></SOAP-ENV:Envelope>

  44. And Here is the Response HTTP/1.0 200 OK Content-Type: text/xml <?xml version='1.0' encoding='UTF-8'?> <soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance' xmlns:xsd='http://www.w3.org/1999/XMLSchema' xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' soap:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'> <soap:Body><n:getRateResponse xmlns:n='urn:xmethods-CurrencyExchange'> <Result xsi:type='xsd:float'>4.521</Result> </n:getRateResponse> </soap:Body></soap:Envelope>

  45. Example - Calling Google Spelling <?xml version="1.0" encoding="UTF-8" ?> <SOAP-ENV:Envelope xmlns:SOAP ENV=“http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsi=“http://www.w3.org/1999/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/1999/XMLSchema”> <SOAP-ENV:Body> <ns1:doSpellingSuggestion xmlns:ns1=“urn:GoogleSearch” SOAP- ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <key xsi:type="xsd:string">00000000000000000000000000</key> <phrase xsi:type="xsd:string">britney speers</phrase> </ns1:doSpellingSuggestion> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

  46. Example - Google Spelling Response <?xml version="1.0" encoding="UTF-8" ?> <SOAP-ENV:Envelope xmlns:SOAP ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body> <ns1:doSpellingSuggestionResponse xmlns:ns1="urn:GoogleSearch“ SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <return xsi:type="xsd:string">britney spears</return> </ns1:doSpellingSuggestionResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

  47. An alternative to SOAP: XML-RPC • XML-RPC is similar to SOAP but simpler • You just have to specify what is the method and what are the parameters • See http://www.xmlrpc.com for further details

  48. XML-RPC Request Example POST /MyRPC HTTP/1.0 Host: www.cs.huji.ac.il Content-Type: text/xml Content-length: 181 <?xml version="1.0"?> <methodCall> <methodName>Hello.sayHelloTo</methodName> <params> <param> <value><string>Lisa</string></value> </param> </params> </methodCall>

  49. XML-RPC Response Example HTTP/1.1 200 OK Connection: close Content-Length: 158 Content-Type: text/xml Date: Sun, 6 Jun 2004 10:33:08 GMT <?xml version="1.0"?> <methodResponse> <params> <param> <value><string>Hello Lisa</string></value> </param> </params> </methodResponse>