SCDJWS 2. Soap


Published on

It's cover the SOAP's part about the SCDJWS certification.
Refers to:
1. SCDJWS 5.0 Study Guide by Mikalai Zaikin
2. SCDJWS 5 Study Notes by Ivan A Krizsan

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

SCDJWS 2. Soap

  1. 1. <ul>SOAP </ul><ul>Francesco Ierna </ul>
  2. 2. <ul>SOAP </ul><ul><li>Enconding types used in SOAP messages : </li><ul><li>The SOAP encoding style is based on a simple type system that is a generalization of the common features found in type systems in programming languages, databases and semi structured data
  3. 3. SOAP is a protocol that allows the exchange of structured data specified by an XML schema between peers in a decentralized, distributed environment </li></ul><li>There are following SOAP encoding types: </li><ul><li>Simple types. Adopts all the types found including : </li><ul><ul><li>Boolean , byte , short , int , long , float , double , java.lang.String , java.math.BigDecimal, java.util.GregorianCalendar , j ava.util.Date , byte [] </li></ul><li>String , Enumarations, Arrays of bytes </li></ul></ul></ul><ul><ul><li>Compound types. The members of a Compound Value are encoded as accessor elements </li></ul></ul><ul><ul><ul><li>Arrays </li><ul><li>itemType is the the type of the array ( String , int , XML complex type).
  4. 4. arraySize - The array's dimensions are represented by each item in the list of sizes </li></ul><li>Structure </li></ul></ul></ul>
  5. 5. <ul>SOAP </ul>
  6. 6. <ul>Example Types </ul>Simple: <element name=&quot;age&quot; type=&quot;int&quot;/> <element name=&quot;height&quot; type=&quot;float&quot;/> <element name=&quot;displacement&quot; type=&quot;negativeInteger&quot;/> <element name=&quot;color&quot;> -------> <simpleType base=&quot;xsd:string&quot;> <enumeration value=&quot;Green&quot;/> <enumeration value=&quot;Blue&quot;/> </simpleType> </element> <age>45</age> <height>5.9</height> <displacement>-450</displacement> <color>Blue</color> Compound: <xsd:element name=&quot;book&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;author&quot; type=&quot;xsd:string&quot; /> <xsd:element name=&quot;title&quot; type=&quot;xsd:string&quot; /> ---------> <xsd:element name=&quot;intro&quot; type=&quot;xsd:string&quot; /> </xsd:sequence> </xsd:complexType> </xsd:element> <book> <author>Mikalai Zaikin</author> <title>SCDJWS 5.0 Study Guide</title> <intro>This is a certification guide</intro> </book>
  7. 7. <ul>Example Types </ul>Arrays (SOAP 1.2): <xs:attribute name=&quot;arraySize&quot; type=&quot;tns:arraySize&quot; /> <xs:attribute name=&quot;itemType&quot; type=&quot;xs:QName&quot; /> <xs:attributeGroup name=&quot;arrayAttributes&quot;> ---------> <xs:attribute ref=&quot;tns:arraySize&quot; /> <xs:attribute ref=&quot;tns:itemType&quot; /> </xs:attributeGroup> <numbers enc:itemType=&quot;xs:int&quot; enc:arraySize=&quot;2&quot;> <number>3</number> <number>4</number> </numbers>
  8. 8. <ul>Table mapping </ul>XML Schema type SOAP Java type xsd:string -> java.lang.String xsd:integer -> java.math.BigInteger xsd:int -> int xsd:long -> long xsd:short -> short xsd:decimal -> java.math.BigDecimal xsd:float -> float xsd:double -> double xsd:boolean -> boolean xsd:byte -> byte xsd:QName -> QName xsd:dateTime -> XMLGregorianCalendar xsd:base64Binary -> byte[] xsd:hexBinary -> byte[] xsd:unsignedInt -> long xsd:unsignedShort -> int xsd:unsignedByte -> short xsd:time -> XMLGregorianCalendar xsd:date -> XMLGregorianCalendar xsd:g -> XMLGregorianCalendar
  9. 9. <ul>SOAP Processing Model </ul><ul><li>Processing Model : </li><ul><li>Allow to send messages from a sender to a receiver through zero or many intermediates.
  10. 10. Specifies how a SOAP reiceiver processes a SOAP message.
  11. 11. Each subsequent depends on the response to the previous message. </li></ul><li>Nodes : </li><ul><li>Inital sender, ultimate receiver(ultimateReceiver) or intermediary(next) . None -> must not act in this rôle.
  12. 12. Processed by a URL
  13. 13. Roles  : </li><ul><li>One o more roles
  14. 14. Processed by a URI known as the SOAP role name
  15. 15. Invariant during the proccessing of an individual SOAP message </li></ul></ul></ul>
  16. 16. <ul>Processing SOAP messages </ul><ul><li>Steps : </li></ul><ul><ul><li>Determine the set of roles in which the node is to act. </li></ul></ul><ul><ul><ul><li>Contents of the enveloppe, may be inspected in making such determination. </li></ul></ul></ul><ul><ul><li>Identify all headers block are mandatory
  17. 17. If one or more of the SOAP header blocks in the preceding step are not understood by the node then generate a single SOAP fault with the value of Code set to env:mustUnderstand. </li></ul></ul><ul><ul><ul><li>Fault  : max 1 </li><ul><li>No processing body </li></ul></ul></ul></ul><ul><ul><li>If Ultimate Receiver : if we have the last SOAP node we process the SOAP body. </li></ul></ul><ul><ul><li>In the case of a SOAP intermediary , and where the SOAP message exchange pattern and results of processing require that the SOAP message be sent further along the SOAP message path , relay the message as described in section </li></ul></ul><ul><li>Versioning Model : </li><ul><li>The version of a SOAP message is identified by the XML expanded name of the child element information item of the document information item. A SOAP Version 1.2 message has a child element information item of the document information item with a [local name] of enveloppe and a [namespace name] of &quot;; . </li></ul></ul>
  18. 18. <ul>Processing SOAP messages </ul>
  19. 19. <ul>SOAP Message </ul><ul><li>< Envelope > is mandatory  : </li></ul><ul><ul><li>Namespace -> &quot;; and URL </li></ul></ul><ul><ul><li><Header>   is optional. Information passed through the SOAP Messages that is not included in the body. </li></ul></ul><ul><ul><ul><li>Many header blocks.
  20. 20. First child of the enveloppe.
  21. 21. Must be namespaces qualified. </li></ul></ul></ul><ul><ul><ul><li>Roles  : by URI Example </li><ul><li>SOAP 1.2. Three types(none, next, ultimateReceiver(Default)) :
  22. 22. none
  23. 23. nect
  24. 24. ultimateReceiver </li></ul></ul></ul></ul><ul><ul><li><Body> is mandatory. It contains the payload of the message that is the information being transferred from the initial sender to the ultimate receiver. </li></ul></ul><ul><ul><ul><li>After header blocks, if it's present
  25. 25. Should namespaces qualified
  26. 26. May contain XML well-formed or be empty
  27. 27. Contains or data or fault </li></ul></ul></ul>
  28. 28. <ul>Envelopes roles </ul><ul><li>A receiver of an envelope that does not belong to the namespace (the SOAP 1.1 namespace) must generate a fault.
  29. 29. Must not contain a Document Type Declaration
  30. 30. Must not contain Processing Instructions
  31. 31. Should not contain the namespace declaration xmlns:xml=&quot; &quot;
  32. 32. Must not contain soap:encodingStyle attributes on any elements belonging to the same namespace as the SOAP <Envelope> element
  33. 33. Any soap:mustUnderstand attribute appearing in a SOAP Envelope is only allowed to have t he value “0” or “1”.
  34. 34. SOAP elements must not have attributes belonging to the same namespace asthe SOAP <Envelope> element ( ): <Envelope>, <Header>, <Body> </li></ul>
  35. 35. <ul>Envelopes Model </ul><ul><li>A receiver of a SOAP message must process SOAP Envelopes so that it at least appears as if all mandatory header blocks
  36. 36. A receiver of a SOAP message that contains a mandatory header block targeted at the receiver, using the soap:role attribute in the header block, must generate a “soap:MustUnderstand” fault if the receiver does not understand the header block
  37. 37. When a fault is generated, the receiver should not perform any further processing of the SOAP Envelope, except for any processing required to roll back the effects of the processing of the SOAP Envelope up to the point of the fault.
  38. 38. If the normal processing of a SOAP Envelope would have resulted in a SOAP response being sent, a receiver must transmit a fault in place of the response if a fault is generated. </li></ul>
  39. 39. <ul>Envelopes Fault </ul><ul><li>A When a SOAP message is a fault, the SOAP <Fault> element must not have any other child elements than: <faultcode>, <faultactor>, <faultstring>, <detail>
  40. 40. The above child elements of a SOAP <Fault> element must not be namespace qualified .
  41. 41. A receiver of a fault must be able to handle faults in which the <faultstring> element has a xml:lang attribute.
  42. 42. SOAP Envelope contains a <faultcode> element , its contents should be: </li><ul><li>One of the fault codes defined in SOAP 1.1 , supplying any additional information in the </li><ul><li><detail> element. Using these fault codes is preferred. </li></ul><li>A qualified name whose namespace is controlled by the fault's specifying authority </li></ul></ul>
  43. 43. <ul>Envelopes Http </ul><ul><li>SOAP messages must be sent using either HTTP/1.0 or HTTP/1.1, where HTTP/1.1 is preferred.
  44. 44. A HTTP request message must be sent using the HTTP POST method.
  45. 45. SOAPAction HTTP header field in a HTTP request message must be a quoted string.
  46. 46. The 2xx HTTP status codes must be used to indicate the successful outcome of a HTTP request. </li><ul><li>“ 200 OK“ or “202 Accepted” should be used to indicate the successful outcome of a HTTP request which response does not contain a SOAP message </li></ul><li>The 4xx HTTP status codes must be used when there is a problem with the format of a request. </li><ul><li>400 Bad Request” should be used when a HTTP request is malformed.
  47. 47. 405 Method not Allowed” should be used when the method of a HTTP request message is not POST.
  48. 48. “ 415 Unsupported Media Type” should be used when the value of the Content-Type HTTP header field is not permitted by its WSDL description </li></ul><li>500 Internal Server Error” must be used when the HTTP response contains a SOAP fault. </li></ul>
  49. 49. <ul>Example SOAP Messages </ul>EXAMPLE 1: NO ROLES <env:Envelope xmlns:env=&quot;;> <env:Header> <pns:qualityOfService xmlns:pns=&quot; &quot;> <pns:timestamp>2004-02-25T01:00:00-00:00</pns:timestamp> <pns:persist>true</pns:persist> </pns:appHeaderBlock> </env:Header> <env:Body> <bmns:businessPO xmlns:env=&quot;;> <bmns:quantity>100</bmns:quantity> </bmns:businessPO> </env:Body> </env:Envelope> EXAMPLE 2: NEXT <env:Envelope xmlns:env=&quot;;> <env:Header> <pns:qualityOfService xmlns:pns=&quot; &quot; env:role=&quot;; > <pns:timestamp>2004-02-25T01:00:00-00:00</pns:timestamp> <pns:persist>true</pns:persist> </pns:appHeaderBlock> </env:Header> <env:Body> <bmns:businessPO xmlns:env=&quot;;> <bmns:quantity>100</bmns:quantity> </bmns:businessPO> </env:Body> </env:Envelope>
  50. 50. <ul>SOAP Extensibility Model </ul><ul><li>Extensibility Model : </li><ul><li>Simple messaging framework.
  51. 51. Extensibility can be used to add capabilities found in richer messaging enveroiment
  52. 52. Two mechanism through which features can be expressed </li><ul><li>SOAP Processing Model </li><ul><li>Beahavior of a single SOAP node with respect to the processing of an individual message </li></ul><li>SOAP Protocol Binding Framework </li></ul></ul></ul><ul><ul><ul><ul><li>Binding : The formal set of rules for carrying a SOAP message within or on top of another protocol (underlying protocol) for the purpose of exchange is called a binding </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Mediates the act of sending and receiving SOAP messages by a SOAP node via an underlying protocol </li></ul></ul></ul></ul><ul><ul><ul><ul><li>The SOAP Protocol Binding Framework provides general rules for the specification of protocol bindings; the framework also describes the relationship between bindings and SOAP nodes that implement those bindings </li></ul></ul></ul></ul>
  53. 53. <ul>SOAP Fault Message </ul><ul><li>Soap Fault : used to carry error and/or status information within SOAP message. </li><ul><li>If present it must appear as a body entry and must appear not more than once within a body element.
  54. 54. Possible reasons for faults may be improper message formatting, version mismatches, trouble processing a header and application-specific errors
  55. 55. A SOAP fault is generated by the node, be it an intermediary or the ultimate receiver
  56. 56. If the messaging exchange pattern One-Way is used, then the SOAP fault must not be transmitted to the immediate sender. The SOAP fault may be stored somewhere
  57. 57. If the messaging exchange pattern Request-Response is used, then the SOAP fault must be transmitted to the immediate sender, that is, the SOAP node immediately preceding the current SOAP node in the messaging chain </li><ul><li>The node receiving the SOAP fault may, in turn, take some action and also, optionally, transmit the SOAP fault further up the SOAP messaging path to its previous node, if any </li></ul></ul></ul>
  58. 58. <ul>SOAP Fault Message </ul><ul><li>Soap Fault : used to carry error and/or status information within SOAP message. </li><ul><li>If present it must appear as a body entry and must appear not more than once within a body element.
  59. 59. Two or more child element items in its property in order as follow : </li><ul><li>Code : mandatory  has two elements: </li></ul></ul></ul><ul><ul><ul><ul><li>Value (mandatory). Enum fault codes ( env:faultCodeEnum) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Subcode has two elements : </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Value (mandatory). Element information is xs:QName </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Subcode (optional). </li></ul></ul></ul></ul></ul><ul><ul><ul><li>Reason : mandatory provides a human-readable explanation of the fault </li><ul><li>Text : One or more elements </li><ul><li>Mandatory attributes information item </li></ul></ul><li>Node  : optional . Provides information about which SOAP node on the SOAP message path caused the fault to happen. URI idetifies the SOAP nodes that generated the Fault message. </li><ul><li>Next : must include who is that generated the fault message.
  60. 60. UltimateReceiver : may include info </li></ul><li>Rôle  : optional . identifies the role the node was operating in at the point the fault occurred. </li><ul><li>information item is xs:anyURI </li></ul><li>Detail  : optional . is intended for carrying application specific error information. </li><ul><li>Character : any number of childs </li></ul></ul></ul></ul>
  61. 61. <ul>Example Fault </ul>Example Code: <xs:element name=&quot;Code&quot; type=&quot;tns:faultcode&quot;/> ... <xs:complexType name=&quot;faultcode&quot;> <xs:sequence> <xs:element name=&quot;Value&quot; type=&quot;tns:faultcodeEnum&quot;/> <xs:element name=&quot;Subcode&quot; type=&quot;tns:subcode&quot; minOccurs=&quot;0&quot;/> </xs:sequence> </xs:complexType> Example Reason: <xs:element name=&quot;Reason&quot; type=&quot;tns:faultreason&quot;/> ... <xs:complexType name=&quot;faultreason&quot;> <xs:sequence> <xs:element name=&quot;Text&quot; type=&quot;tns:reasontext&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;unbounded&quot;/> </xs:sequence> </xs:complexType> Example Node: <xs:element name=&quot;Node&quot; type=&quot;xs:anyURI&quot; minOccurs=&quot;0&quot;/> Example Role: <xs:element name=&quot;Role&quot; type=&quot;xs:anyURI&quot; minOccurs=&quot;0&quot;/> Example Detail: <xs:element name=&quot;Detail&quot; type=&quot;tns:detail&quot; minOccurs=&quot;0&quot;/> ... <xs:complexType name=&quot;detail&quot;> <xs:sequence> <xs:any namespace=&quot;##any&quot; processContents=&quot;lax&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;/> </xs:sequence> <xs:anyAttribute namespace=&quot;##other&quot; processContents=&quot;lax&quot;/> </xs:complexType>
  62. 62. <ul>SAAJ </ul><ul><li>SAAJ : SOAP 1.1 with attachments. </li><ul><li>It provides a standard way to send XML documents over the Internet from the Java platform
  63. 63. Standalone client that sends the message directly to a webservices that is implemented for req/resp messaging is : </li><ul><li>Syncroninus
  64. 64. SOAPMessage is sent over a SOAPConnection object via the method </li><ul><li>XML document that is a SOAPMessage (envelope, header, body)
  65. 65. Body  : </li><ul><li>XML document : standard SOAP message </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Not XML Document (jpg, mp3, mpg) and then you must use attachment part within the SOAP message. </li></ul></ul></ul></ul></ul><ul><ul><li>Processing creation and sending SOAP message with attachment : </li></ul></ul><ul><ul><ul><li>Creating SOAP connection
  66. 66. Creationg SOAP message
  67. 67. Populating the message
  68. 68. Sending the message
  69. 69. Retrieving the reply </li></ul></ul></ul>
  70. 70. <ul>SAAJ Steps </ul>ConnectionFactory SOAP: SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance(); Connection SOAP from ConnectionFactory: SOAPConnection connection = factory.createConnection(); MessageFactory: MessageFactory messageFactory = MessageFactory.newInstance(); Message from MessageFactory: SOAPMessage message = messageFactory.createMessage(); MessageSOAP: SOAPPart soapPart = message.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody body = envelope.getBody(); Name bodyName = envelope.createName(&quot;text&quot;, &quot;i&quot;, &quot;;); SOAPBodyElement bodyElement = body.addBodyElement(bodyName); bodyElement.addTextNode(&quot;some-xml-text&quot;); ====== <SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;;> <SOAP-ENV:Body> <i:text xmlns:i=&quot;;> some-xml-text </i:text> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
  71. 71. <ul>SAAJ Steps </ul>
  72. 72. <ul>SAAJ Steps </ul>SOAP Message: SOAPPart soapPart = message.getSOAPPart(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbFactory.newDocumentBuilder(); Document document = builder.parse(&quot;file:///;); DOMSource domSource = new DOMSource(document); soapPart.setContent(domSource); SOAP Message Body: SOAPBodyElement docElement = body.addDocument(document); Attachment Parts: URL url = new URL(&quot;;); DataHandler handler = new DataHandler(url); AttachmentPart attachPart = message.createAttachmentPart(handler); message.addAttachmentPart(attachPart); AttachmentPart attachPart = message.createAttachmentPart( &quot;content-string&quot;, &quot;text/plain&quot;); message.addAttachmentPart(attachPart); Send Message: SOAPMessage response =, endpoint);
  73. 73. <ul>SAAJ Diagram </ul>
  74. 74. <ul>Handlers </ul><ul><li>Handlers  : Handlers are message interceptors that can be easily plugged in to the JAX-WS runtime to do additional processing of the inbound and outbound messages </li><ul><li>Two types : </li></ul></ul><ul><ul><ul><li>logical handlers : are protocol-agnostic and cannot change any protocol-specific parts (like headers) of a message </li><ul><li>are commonly used if the processing does not needaccess to SOAP headers, for validation of the payload, and with Representational State Transfer (&quot;REST&quot;) style web services
  75. 75. Extend </li></ul><li>Protocol handlers : are specific to a protocol and may access or change the protocol specific aspects of a message. </li></ul></ul><li>SOAP handlers are generally used to process SOAP-specific information , such as SOAP headers
  76. 76. Handlers are invoked with a message context , which provides methods that the handler uses to access and modify inbound or outbound messages.
  77. 77. extend
  78. 78. Methods SOAPLoggingHandler : </li><ul><li>handleMessage() is called for normal processing of inbound and outbound messages.
  79. 79. handleFault() is called when a message contains a protocol fault.
  80. 80. close() is called after the completion of message processing by all handlers for each web service invocation, </li></ul></ul>
  81. 81. <ul>Handlers </ul>
  82. 82. <ul>Handlers - Message-Context </ul>