Vous êtes sur la page 1sur 10

Programming Web Services - Lec. 3.1 SOAP!

DarkSwitch 2010

Soap (Simple Object Access Protocol)


http://en.wikipedia.org/wiki/SOAP

• SOAP Is a protocol specification for exchanging structured information in the


implementation of Web Services in computer networks.
• SOAP can form the foundation layer of a web services protocol stack. Its basic structure
are:
• Envelope: which defines what is in the message and how to process it
• Header: a set of encoding rules for expressing instances of application-defined
datatypes
• Body: A convention for representing procedure calls and responses
• It uses internet application layer protocol as a transport protocol (SMTP and HTTP)
• Itʼs based on XML (Use the message format)

Sample SOAP Message

" Post /InStock HTTP/1.1


! Host: www.example.org
! Content-Type: application/soap+xml; charset=utf-8
! Content-Length: nnn

! <?xml version=”1.0” ?>


! <soap:Envelope
! xmlns:soap=”http://www.w3.org/2001/12/soap-envelope”
! soap:encodingStyle=”http://www.w3.org/2001/12/soap-encoding”>

! <soap:Body xmlns:m=”http://www.example.org/stock”>
! ! <m:GetStockPrice>
! ! ! <m:StockName>IBM</m:StockName>
! ! </m:GetStockPrice>
! </soap:Body>

! </soap:Envelope>

SOAP Advantages:
• It can use different transport protocols

SOAP Disadvantages:
• Itʼs slower than CORBA cause it uses XML format
• In some cases, when SOAP is transported by HTTP, if the firewalls want inspect the
contents of the package it must have some knowledge of the SOAP structure. So, this
operation could be very expensive for the Firewall.
• Not all languages offer appropriate support for it.

Soap (Simple Object Access Protocol) by W3schools


http://www.w3schools.com/soap/

1. SOAP introduction
Soap is a simple XML-based protocol to let applications exchange information over HTTP

V 1.1 Last modification: 27/05/2010! Page 1


Programming Web Services - Lec. 3.1 SOAP! DarkSwitch 2010

SOAP is a format for sending messages


SOAP is platform independent
SOAP is language independent

Todayʼs applications communicate using Remote Procedure Calls (RPC) between objects
like DCOM and CORBA, but HTTP was not designed for this. RPC represents a
compatibility and security problem; firewalls and proxy servers will normally block this kind
of traffic.

A better way to communicate between applications is over HTTP, because HTTP is


supported by all Internet browsers and servers. SOAP was created to accomplish this.

2. SOAP Syntax
A SOAP message is an ordinary XML document containing the following elements:
• Envelope: Identifies the XML document as a SOAP message
• Header: contains header information
• Body: Contains call and response information
• Fault: Contains errors and status information

Syntax rules:
• A SOAP message MUST be encoded using XML
• A SOAP message MUST use the SOAP Envelope namespace
• A SOAP message MUST use the SOAP encoding namespace
• A SOAP message must NOT contains a DTD reference
• A SOAP message must NOT contains XML Processing Instructions

3. SOAP envelope
The SOAP Envelope element is the root element of a SOAP message.
A SOAP message has no default encoding.

Example:
" <?xml version=”1.0”>
! <soap:Envelope
! xmlns:soap=”http://www.w3.org/2001/12/soap-envelope”
! soap:encodingStyle=”http://www.w3.org/2001/12/soap-encoding”>
! ! ....
! ! Message information goes here
! ! ...
! </soap:Envelope>

4. SOAP Header (Optional)


If the Header element is present, it must be the first child element of the Envelope
element.
All immediate child elements of the Header element must be namespace-qualified.
The attributes defined in the SOAP Header defines how a receiver should process the
SOAP message.
Example:

! <?xml version =”1.0”?>


! <soap:Envelope
! xmlns:soap=”http://www.w3.org/2001/12/soap-envelope”
V 1.1 Last modification: 27/05/2010! Page 2
Programming Web Services - Lec. 3.1 SOAP! DarkSwitch 2010

! soap:encodingStyle=”http://www.w3.org/2001/12/soap-encoding”>

! <soap:Header>
! ! <m:Trans xmlns:m=”http://www.w3scholls.com/transaction/”
! ! soap:mustUnderstand=”1”>234
! ! </m:Trans>
! </soap:Header>
! ....
! ....
! </soap:Envelope>

4.1 The mustUnderstand Attribute

Syntaxis:
soap:mustUnderstand=”0|1”

It indicates that the receiver processing the Header must or not recognize the element. If
the receiver does not recognize the element it will fail when processing the Header.

4.2 The actor (role) attribute

Syntaxis:
soap:actor =”URI”

A SOAP message may travel from a sender to a receiver by passing different endpoints
along the message path. However, not all parts of a SOAP message may be intended for
the ultimate endpoint, instead, it may be intended for one or more of the endpoints on the
message path.

The SOAP actor attribute is used to address the Header element to a specific endpoint.

4.3 The encodingStyle Attribute


Syntaxis:
soap:encodingStyle=”URI”

The encodingStyle attribute is used to define the data types used in the document.

5. SOAP Body
The SOAP Body element contains the actual SOAP message intended for the ultimate
endpoint of the message.

Example: Request

" <?xml version =”1.0”?>


! <soap:Envelope
! xmlns:soap=”http://www.w3.org/2001/12/soap-envelope”
! soap:encodingStyle=”http://ww.w3.org/2001/12/soap-encoding”>

! <soap:Body>
! ! <m:GetPrice xmlns:m=”http://www.w3schools.com/price”>

V 1.1 Last modification: 27/05/2010! Page 3


Programming Web Services - Lec. 3.1 SOAP! DarkSwitch 2010

! ! ! <m:Item>Apples</m:Item>
! ! </m:GetPrice>
! </soap:Body>
! </soap:Envelope>

This is a request for the price of the apples.


The response could be like this:

Example: Response

! <?xml version=”1.0”?>
! <soap:Envelope
! xmlns:soap=”http://www.w3.org/2001/12/soap-envelope”
! soap:encodingStyle=”http://www.w3.org/2001/12/soap-encoding”>

! <soap:Body>
! ! <m:GetPriceResponse xmlns:m=”http://www.w3schools.com/prices”>
! ! ! <m:Price>1.90</m:Price>
! ! </m:GetPriceResponse>
! </soap:Body>

! </soap:Envelope>

6. SOAP Fault
The SOAP Fault element hold errors and status information for a SOAP message.

7. SOAP HTTP Binding

HTTP + XML = SOAP


A SOAP request could be an HTTP POST or an HTTP GET request
The HTTP POST request specifies at least two HTTP headers: Content-Type and Content-
length

7.1 Content-Type

Syntaxis:
Content-Type: MIMEType; charset=character-encoding

The Content-Type header for a SOAP request and response defines the MIME type of the
message and the character encoding (optional) used for the XML body of the request or
response.

Example:

" POST /item HTTP/1.1


! Content-Type: application/soap+xml; charset=utf-8

7.2 Content-Length

Syntaxis:
Content-Length: bytes
V 1.1 Last modification: 27/05/2010! Page 4
Programming Web Services - Lec. 3.1 SOAP! DarkSwitch 2010

The Content-Length header for a SOAP request and response specifies the number of
bytes in the body of the request or response

Example:

! POST /item HTTP/1.1


! Content-Type: application/soap+xml; charset=utf-8
! Content-Length: 20

8. SOAP Example
In this example, a GetStockPrice request is sent to a server. The request has a
StockName parameter, and a Price parameter that will be returned in the response. The
namespace for the functions defined in “http://www.example.org/stock”

Example: (Request)

" POST /InStock HTTP/1.1


! Host: www.example.org
! Content-Type: application/soap+xml; charset=utf-8
! Content-length: nnn

" <?xml version=”1.0”?>


! <soap:Envelope
! xmlns:soap=”http://www.w3.org/2001/12/soap-envelope”
! soap:encodingStyle=”http://wws.w3.org/2001/12/soap-encoding”>

! <soap:Body xmlns:m=”http://www.example.org/stock”>
! ! <m:GetStockPrice>
! ! ! <m:StockName>IBM</m:StockName>
! ! <m:GetStockPrice>
! </soap:Body>

Example: (Response)

" HTTP/1.1 200 OK


! Content-Type: application/soap+xml; charset=utf-8
! Content-Length: nnn

! <?xml version=”1.0”?>
! <soap:Envelope
! xmlns:soap=”http://www.w3.org/2001/12/soap-envelope”
! soap:encodingStyle=”http://www.w3.org/2001/12/soap-encoding”>

! <soap:Body xmlns:m=”http://www.example.org/stock”>
! ! <m:GetStockPriceResponse>
! ! ! <m:Price>34.5</m:Price>
! ! </m:GetStockPriceResponse>
! </soap:Body>

Soap (Simple Object Access Protocol) by Matskin lecture 3


V 1.1 Last modification: 27/05/2010! Page 5
Programming Web Services - Lec. 3.1 SOAP! DarkSwitch 2010

http://www.ict.kth.se/courses/ID2208/2010/lectures/SOAP2010.pdf

1. Introduction
To achieve XML-based interaction, developers need to establish a standard transport and
data-exchange framework

Initially we have XML-RPC.


Advantage
• Simple and effective
Disadvantage
• Thereʼs no clean mechanism to pass XML documents themselves in an XML-RPC
request or response
• Thereʼs no solution that enables programmers to extend the request or response format
• XML-RPC is not fully aligned with the latest XML standardization

2. Definition of SOAP
SOAP provides:
• A mechanism for defining the unit of communication
• a processing model
• a mechanism for error handling
• an extensibility mechanism
• a flexible mechanism for data representation
• a convention for representing RPCs and responses as SOAP messages
• a binding mechanism for SOAP messages to HTTP
SOAP supports
• a Document centric approach for documents exchange

XML documents = SOAP messages

SOAP messages donʼt provide programming instructions - they rather specify which
operation to invoke

3. SOAP Elements

• env:Envelope is the root of the SOAP request


• env:Header contains auxiliary information. (optional)
• env:Body contains the main information in one or more SOAP blocks
• env:Fault is a special block that indicates protocol-level errors. If present, it must appear
in the body

3.1 SOAP Message (Header)


The header offers a flexible framework for specifying additional application level elements
(routing information, authentication, transaction management, ...)
It can has some pre-defined attributes:
• Actor/role
• MustUnderstand

As not endpoint user of a SOAP message, we can put everything in the header. The
important thing is that someone can understand it and we havenʼt change the original
message.

V 1.1 Last modification: 27/05/2010! Page 6


Programming Web Services - Lec. 3.1 SOAP! DarkSwitch 2010

Definition: SOAP node: is an application component that understands SOAP

3.1.1: SOAP Intermediaries

" Requestor -> Intermediary -> Intermediary -> Provider

The intermediary can be:


• Transparent: You donʼt ever know that it exists
• Explicit: You know it exists.
And be:
• Forwarding: Just forward the message, without change it
• active: Forward the message. The message could be modified

Every headers elements can have role attribute:


• next
• ultimateReceiver
• none

3.1.2 Soap Processing Model

1. Determinate the set of roles in which the node is to act


2. Identify all header block targeted at the node that are mandatory
3. If one or more identified mandatory header blocks are not understood then a fault with
fault code set to “mustUnderstood” is generated and processing is stopped
4. Process all mandatory headers targeted at the node (in case of ultimate receiver also
process the body)
5. Relay the message if it is intermediary.

3.2 SOAP message (Body)

Describes the purpose of SOAP message


SOAP Fault:
• Codes
• Sender
• Receiver
• VersionMismatch: Version of SOAP
• MustUnderstand
• Detail
• Subcodes
• Reason
• Node: The node which processed the message
• Role:

4. SOAP Fault
Codes:
• Seder: The problem was caused by incorrect or missing data from the sender
• Receiver: The problem because of something happens on receiver side.
• Version/Mismatch: Indicates the node does not recognize the version of SOAP used
• MustUnderstand: Indicates the node does not recognize a block flagged with
mustUnderstand.
• Subcodes: More fine-grain fault codes
• Reason: Contains huma-readable description
V 1.1 Last modification: 27/05/2010! Page 7
Programming Web Services - Lec. 3.1 SOAP! DarkSwitch 2010

• Node: Shows the node which processed the message at the time of fault.
• Role: Tells which role the faulting node was playing when the fault occurred.

5. Encoding data for SOAP

There is no standard SOAP mapping for any language (For example, JAVA).
The most basic rule is that values are always encoded in elements.
SOAP uses attributes exclusively to modify the default processing of an element.
For example:

<name>Board room </name>


not:
<item name=”Board room” />

5.1 Encoding Style


If an element has the “encodingStyle” attribute, then its encoding style is equal to the value
of that attribute
Otherwise, the encoding style is equal to the encoding style of the closest ancestor
element that has the “encodingStyle” attribute .... Unless there is not such ancestor, which
implies that the element has not specified encoding style.

5.2 Compound Types


Definition: A structure is a list of elements logically grouped together. Id = name
Definition: The array is a group of values identified by their ordinal position. Id = position

Example:

" <array soapenc: itemType=”xsd:String”


! soapenc: arraySize=”3”>
! ! <item>Board room</item>
! ! <item>Meeting room 1</item>
! ! <item>Meeting room 2</item>
! </array>

Example: Multidimensional Arrays

" <Array soapenc: itemType=”xsd:String”


! ! soapenc:arraySize=”2 2”>
! ! <item>NW</item>
! ! <item>NE</item>
! ! <item>SW</item>
! ! <item>SE</item>
! </Array>

You can encode some files (ex. picture) into binary in order to include it to SOAP

Example:

" <soapenv: Body>


" " ...
" " <Picture xsi:type=”xsi:base64binary” imageType=”jpg”>

V 1.1 Last modification: 27/05/2010! Page 8


Programming Web Services - Lec. 3.1 SOAP! DarkSwitch 2010

" " " aG93IG5vDyBicm73biBjb3cNCg==....


" " </Picture>
" " ....
" </soapenv:Body>

6. Messaging

6.1 Type of messaging (patterns):

" One Way: " " " Client -> Server

" Request-response:" " Client -> Server


" " " " " Server -> Client

" Notification" " " Server -> Client

" Notification-response:" Server -> Client


" " " " " Client -> Server

6.2 Features and Properties

How can we send the patterns of the messages?


1. Defines all the patterns
2. Use Features

Definition: Feature: Is a semantic that has a name and a specification

Features are expressed by:


" Binding: outside of SOAP. Means for performing functions below the SOAP
" " processing model
" Modules: Using SOAP processing model (headers). For example, secure modules,
" " correlation, authentication, ...
Definition: Properties: Pieces of states named with URI which affect the operation of
features. For example, for the security, the properties could be the username and the
password.

6.3 SOAP and HTTP

Normal SOAP exchange uses HTTP POST operation.

The feature defines a property which contains one of words:


• GET
• POST
• PUT
• DELETE
• or other that can be defined

The MIME media type of both HTTP requests and responses muse be application/soap
+xml

6.4 SOAP and SMTP (Simple Mail Transfer Protocol)

V 1.1 Last modification: 27/05/2010! Page 9


Programming Web Services - Lec. 3.1 SOAP! DarkSwitch 2010

Both sending and receiving of e-mail messages can be configured to requiere


authentication.

E-mail can support one-to-one and one-to-many participant configurations.

V 1.1 Last modification: 27/05/2010! Page 10

Vous aimerez peut-être aussi