Java Web Services [2/5]: Introduction to SOAP


Published on

Presentations for Java Web Services Course, September 2010

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Java Web Services [2/5]: Introduction to SOAP

  1. 1. Topic 2 Introduction to SOAPAssoc.Prof. Dr. Thanachart Numnonda August 2010
  2. 2. Agenda What is SOAP? SOAP Structure SOAP Communication / Encode 2
  3. 3. What is SOAP? 3
  4. 4. SOAP Definition [W3C] SOAP is a lightweight protocol intended for exchanging structured information in a decentralized, distributed environment SOAP uses XML technologies to define an extensible messaging framework providing a message construct that can be exchanged over a variety of underlying protocols The framework has been designed to be independent of any particular programming model and other implementation specific semantics 4
  5. 5. What is SOAP? Simple Object Access Protocol Wire protocol similar to – IIOP for CORBA – JRMP for RMI XML is used for data encoding – “text” based protocol vs. “binary” protocol Supports XML-based RPC (Remote Procedure Call) 5
  6. 6. Do I Need to know how SOAP works in detail as a Java Developer? Yes – Understanding it will help you to build better application – Example) Understanding how TCP/IP will help you build better TCP/IP application No – You will mostly likely use high-level API (JAX- WS, JAX-RPC) to build Web applications – How SOAP works is hidden from developers 6
  7. 7. Where is SOAP? SOAP 1.2 is W3C recommendation SOAP 1.2 Part 1 defines – SOAP envelope – Protocol binding framework SOAP 1.2 Part 2 defines SOAP 1.2 becomes a W3C recommendation in 2003. 7
  8. 8. SOAP Features Extensible Usable over a variety of underlying networking protocols Independent of programming models 8
  9. 9. SOAP Features : Extensible SOAP is simple by design SOAP lacks various distributed system features: – security – Routing – Transactions – etc. SOAP defines a communication framework that allows additional features to be added as layered extensions. 9
  10. 10. SOAP Features : Protocol Independent SOAP can be used over any protocol: – TCP – HTTP – SMTP – etc. SOAP provides a flexible framework for defining bindings to arbitrary protocols to maintain interoperability. SOAP provides an explicit binding for HTTP. 10
  11. 11. SOAP Features : Model-Independent Allows for any programming model not tied to RPC. Defines a model for processing individual, one-way messages, or combine multiple messages into an overall message exchange Allows for any number of message exchange patterns: request/response, solicit/response, notifications, peer-to-peer 11
  12. 12. SOAP Structure 12
  13. 13. SOAP Message Structure 13
  14. 14. SOAP Messaging The SOAP messaging framework defines a suite of XML elements for packaging arbitrary XML messages for transport between systems: – envelope – header – body – fault – etc. 14
  15. 15. SOAP Messaging : Example<soapenv:Envelope <soapenv:Envelope xmlns:soapenv="" xmlns:soapenv="" xmlns:web="http://www.webserviceX.NET/"> xmlns:web="http://www.webserviceX.NET/"> <soapenv:Header/> <soapenv:Header/> <soapenv:Body> <soapenv:Body> <web:GetQuote> <web:GetQuote> <!--Optional:--> <!--Optional:--> <web:symbol>goog</web:symbol> <web:symbol>goog</web:symbol> </web:GetQuote> </web:GetQuote> </soapenv:Body> </soapenv:Body></soapenv:Envelope>;; </soapenv:Envelope> 15
  16. 16. SOAP Namespaces All XML elements belong to the following namespaces: SOAP 1.1 - SOAP 1.2 - 16
  17. 17. SOAP Message Envelope Embedded Information – Namespaces – Encoding information Header – Optional – Can be handled by intermediaries Body – Mandatory – Handled only by ultimate receiver 17
  18. 18. SOAP Envelope : Embedded Information  Envelop is always the root element of a SOAP message:  The namespace is specified in the envelope for: – defining the envelope elements – controlling the SOAP version<soap:Envelope <soap:Envelopexmlns:soap=" /"> xmlns:soap=" /"><soap:Header>...</soap:Header> <soap:Header>...</soap:Header><soap:Body>...</soap:Body> <soap:Body>...</soap:Body></soap:Envelope> </soap:Envelope> 18
  19. 19. SOAP Header Provides a mechanism for extending SOAP messages in a decentralized and modular way Allows to pass control information to the receiving SOAP server. Used for extension – Context – Authentication – Transaction – Management – Many other higher level semantics 19
  20. 20. SOAP Header : Example<soap:Envelope xmlns:soap=“”> <soap:Envelope xmlns:soap=“”><soap:Header> <soap:Header> <t:transaction xmlns:t=“”> <t:transaction xmlns:t=“”> <t:loginTime>10:20:00</t:loginTime> <t:loginTime>10:20:00</t:loginTime> <t:logoutTime>10:21:00</t:logoutTime> <t:logoutTime>10:21:00</t:logoutTime> </t:transaction> </t:transaction></soap:Header> </soap:Header>... ...</soap:Envelope> </soap:Envelope> 20
  21. 21. SOAP Header : Attributes SOAP 1.2 provides mechanisms to specify who should deal with headers and what to do with them. For this purpose it includes attributes: – role – MustUnderstand – relay Also it is possible to define: – encodingStyle SOAP 1.1 has actor attribute instead of role, with the same semantic. 21
  22. 22. Mandatory/Optional Headers Headers may be mandatory or optional. If a header is mandatory: – the receiver must process the header – if the receiver is unable to process the header, it must fail mustUnderstand attribute indicates if a header is mandatory or optional. 22
  23. 23. SOAP Body The SOAP Body element represents a mechanism for exchanging information intended for the ultimate recipient of the message. Body represents the message payload – a generic container that includes any number of elements from any namespace. In the simplest case the body of a SOAP message includes: – message name – reference to a service instance – parameters with values and optional type references 23
  24. 24. SOAP Body (cont) Made of Body blocks (Body entries) Consumed by Ultimate SOAP receiver Carry end-to-end information  Applicationdata (XML document) (document style)  RPC method and parameters (rpc style)  SOAP fault 24
  25. 25. SOAP Body: Request Example  Request message to transfer funds between bank accounts:soap:Envelope xmlns:soap=“” soap:Envelope xmlns:soap=“” soap:Body soap:Body x:TransferFunds xmlns:x=“urn:examples-org:banking” x:TransferFunds xmlns:x=“urn:examples-org:banking” x:from983-23456/x:from x:from983-23456/x:from x:to672-24806/x:to x:to672-24806/x:to x:amount1000.00/x:amount x:amount1000.00/x:amount /x:TransferFunds /x:TransferFunds /soap:Body /soap:Body/soap:Envelope /soap:Envelope 25
  26. 26. SOAP Body: Response Examplesoap:Envelope xmlns:soap=“” soap:Envelope xmlns:soap=“” soap:Body soap:Body x:TransferFundsResponse xmlns:x=“urn:examples-org:banking” x:TransferFundsResponse xmlns:x=“urn:examples-org:banking” x:balances x:balances x:account x:account x:id983-23456/x:id x:id983-23456/x:id x:balance34.98/x:balance x:balance34.98/x:balance /x:account /x:account x:account x:account x:id672-24806/x:id x:id672-24806/x:id x:balance1267.14/x:balance x:balance1267.14/x:balance /x:account /x:account /x:balances /x:balances /x:TransferFundsResponse /x:TransferFundsResponse /soap:Body /soap:Body/soap:Envelope /soap:Envelope 26
  27. 27. SOAP Fault The Fault element is used to represent errors:  processing errors  errors understanding a mandatory header  all abnormal situations Faults are specified within the body of a SOAP message. 27
  28. 28. SOAP Fault: Examplesoap:Envelope xmlns:soap= soap:Envelope xmlns:soap= soap:Body soap:Body soap:Fault soap:Fault soap:Code soap:Code soap:Valuesoap:Sender/soap:Value soap:Valuesoap:Sender/soap:Value /soap:Code /soap:Code soap:ReasonInsufficient funds/soap:Reason soap:ReasonInsufficient funds/soap:Reason soap:Detail soap:Detail x:TransferError xmlns:x=urn:examplesorg:banking x:TransferError xmlns:x=urn:examplesorg:banking x:sourceAccount22-342439/x:sourceAccount x:sourceAccount22-342439/x:sourceAccount x:transferAmount100.00/x:transferAmount x:transferAmount100.00/x:transferAmount x:currentBalance89.23/x:currentBalance x:currentBalance89.23/x:currentBalance /x:TransferError /x:TransferError /soap:Detail /soap:Detail /soap:Fault /soap:Fault /soap:Body /soap:Body/soap:Envelope /soap:Envelope 28
  29. 29. SOAP Communication / Encode 29
  30. 30. SOAP Communication Styles SOAP enables two communication styles: Document-style – The message has no fixed structure, so the interacting applications must agree beforehand on this structure. RPC-style – Synchronous method invocation - pre-defined message structure. 30
  31. 31. RPC Style RPC-style is a synchronous invocation of an operation returning a result: One SOAP message encapsulates the request. – The body of the request message contains the actual call including the name of the procedure being invoked and the input parameters. Another SOAP message encapsulates the response. – The body of the response contains the result and output parameters. The two interacting applications agree upon the RPC method signature. 31
  32. 32. Document Style Also known as a message-oriented style: – a request is an XML document – an optional response is also an XML document Two interacting applications agree beforehands upon the structure of the documents exchanged, then use SOAP messages to transport them. Very flexible communication style that provides the best interoperability, using synchronous or asynchronous communication. 32
  33. 33. SOAP Document Style: Examplesoap:Envelope soap:Envelope xmlns:soap=“” xmlns:soap=“” xmlns:xsd=“” xmlns:xsd=“”soap:Body soap:Body orgNS:returnBalance orgNS:returnBalance xmlns:orgNS=“” xmlns:orgNS=“” soap:encodingStyle=“http://www/” soap:encodingStyle=“http://www/” orgNS:balance orgNS:type=“xsd:float”1235.95 orgNS:balance orgNS:type=“xsd:float”1235.95 /orgNS:balance /orgNS:balance /orgNS:returnBalance /orgNS:returnBalance/soap:Body /soap:Body/soap:Envelope /soap:Envelope 33
  34. 34. Data Model and Encoding In order to be able to send Java and others programming language objects inside SOAP envelopes, SOAP defines: SOAP Data Model - an abstract representation of the data structures such as the ones handled by Java or C# SOAP Encoding - a set or rules to map the data model into XML for sending the data inside SOAP envelopes 34
  35. 35. Data Model The SOAP data model represents data structures as connected graphs, where nodes represent values and edges represent labels. 35
  36. 36. Encoding SOAP encoding describes how the SOAP data model is written with XML. SOAP encoding is identified by the URI When serializing XML using encoding rules, processors should use the encodingStyle attribute to indicate the SOAP encoding in use. The encodingStyle attribute can appear in: – message headers – message bodies – Detail sub-element of Fault 36
  37. 37. Encoding Examplesoapenv:Envelope soapenv:Envelope xmlns:soapenv= xmlns:soapenv= xmlns:xsd= xmlns:xsd= xmlns:xsi= xmlns:xsi= soapenv:Body ns1:downloadFileResponse ns1:downloadFileResponse soapenv:encodingStyle= soapenv:encodingStyle= xmlns:ns1= xmlns:ns1= downloadFileReturn downloadFileReturn xmlns:soapenc= xmlns:soapenc= xsi:type=soapenc:base64“ xsi:type=soapenc:base64“ TW9..QogDQo= TW9..QogDQo= /downloadFileReturn /downloadFileReturn /ns1:downloadFileResponse /ns1:downloadFileResponse/soapenv:Body /soapenv:Body/soapenv:Envelope /soapenv:Envelope 37
  38. 38. SOAP Processing Model SOAP defines a processing model that outlines rules for processing a SOAP message as it travels from a SOAP sender to a SOAP receiver. The model allows for architectures with multiple intermediary nodes: 38
  39. 39. Resources Some contents are borrowed from the presentation slides of Sang Shin, Java™ Technology Evangelist, Sun Microsystems, Inc. Web Services and Java, Elsa Estevez, Tomasz Janowski and Gabriel Oteniya, UNU-IIST, Macau 39
  40. 40. Thank you 40