Simple Object Access Protocol – SOAP: Methods, Concepts and Examples

31 July, 2008 at 20:01 1 comment

SOAP imageSOAP (Simple Object Access Protocol) is a protocol for exchanging XML-based messages over computer networks, normally using HTTP/HTTPS. SOAP forms the foundation layer of the web services protocol stack providing a basic messaging framework upon which abstract layers can be built.

There are several different types of messaging patterns in SOAP, but by far the most common is the Remote Procedure Call (RPC) pattern, in which one network node (the client) sends a request message to another node (the server) and the server immediately sends a response message to the client. SOAP is the successor of XML-RPC, though it borrows its transport and interaction neutrality and the envelope/header/body from elsewhere, probably from WDDX.

What is Service Oriented Architecture? SOA

What is SOAP?

  • SOAP stands for Simple Object Access Protocol
  • SOAP is a communication protocol
  • SOAP is for communication between applications
  • SOAP is a format for sending messages
  • SOAP is designed to communicate via Internet
  • SOAP is platform independent
  • SOAP is language independent
  • SOAP is based on XML
  • SOAP is simple and extensible
  • SOAP allows you to get around firewalls
  • SOAP will be developed as a W3C standard

Why SOAP?

It is important for application development to allow Internet communication between programs.

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.

SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages.

Skeleton SOAP Message

<?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:Header>
  ...
  ...
</soap:Header>
<soap:Body>
  ...
  ...
  <soap:Fault>
    ...
    ...
  </soap:Fault>
</soap:Body>
</soap:Envelope>



Click here for a larger image.

SOAP examples

Request example

POST /examples HTTP/1.0 User-Agent: Radio UserLand/7.0 (MacOS) Host: localhost:81 Content-Type: text/xml Content-length: 474 SOAPAction: "/examples"
<?xml version="1.0"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
   <SOAP-ENV:Body>
  	<m:getStateName xmlns:m="http://soapware.org/">
            <param1 xsi:type="xsd:int">41</param1>
        </m:getStateName>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Simple types

Type value Type Example
xsd:int or xsd:integer 32-bit signed integer -12
xsd:boolean a boolean value, true or false true
xsd:string string of characters hello world
xsd:float or xsd:double signed floating point number -12.214
xsd:timeInstant date/time 2001-03-27T00:00:01-08:00
xsd:base64 base64-encoded binary eW91IGNhbid0IHJlYWQgdGhpcyE=

Struct example

<param>
       <lowerBound xsi:type="xsd:int">18</lowerBound>
       <upperBound xsi:type="xsd:int">139</upperBound>
   </param>

Array example

<param SOAP-ENC:arrayType="xsd:ur-type[4]">
      <item xsi:type="xsd:int">12</item>
      <item xsi:type="xsd:string">Egypt</item>
      <item xsi:type="xsd:boolean">false</item>
      <item xsi:type="xsd:int">-31</item>
   </param>

Response example

HTTP/1.1 200 OK Connection: close Content-Length: 499 Content-Type: text/xml Date: Wed, 28 Mar 2001 05:05:04 GMT Server: UserLand Frontier/7.0-MacOS
<?xml version="1.0"?>
 <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
    <SOAP-ENV:Body>
         <getStateNameResponse>
             <Result xsi:type="xsd:string">South Dakota</Result>
         </getStateNameResponse>
    </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

Fault example

HTTP/1.1 500 Server Error Connection: close Content-Length: 511 Content-Type: text/xml Date: Wed, 28 Mar 2001 05:06:32 GMT Server: UserLand Frontier/7.0-MacOS
<?xml version="1.0"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
        <faultcode>SOAP-ENV:Client</faultcode>
        <faultstring>Can’t call “getStateName” because there are too many parameters. </faultstring>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Other example

SOAP request and response example

More

SAAJ

JavaScript/SOAP

Advanced Web Services Hacking

SOAP vs REST

REST is an approach, SOAP is a protocol. It is more appropriate to compare SOAP and POX. This said a comparison of these payload protocols is not very enlightening. You need to focus on the big architectural questions not the wire format. Key questions are a) whether there is a requirement for broad system-2-system level interoperability. b) Whether there are requirements for enterprise-level QOS. (more)

SAAJ: SOAP with Attachments API for Java

SOAP with Attachments API for Java (SAAJ) is used mainly for the SOAP messaging that goes on behind the scenes in JAX-WS handlers and JAXR implementations. Secondarily, it is an API that developers can use when they choose to write SOAP messaging applications directly rather than use JAX-WS. The SAAJ API allows you to do XML messaging from the Java platform: By simply making method calls using the SAAJ API, you can read and write SOAP-based XML messages, and you can optionally send and receive such messages over the Internet (some implementations may not support sending and receiving). This chapter will help you learn how to use the SAAJ API.

The SAAJ API conforms to the Simple Object Access Protocol (SOAP) 1.1 and 1.2 specifications and the SOAP with Attachments specification. The SAAJ 1.3 specification defines the javax.xml.soap package, which contains the API for creating and populating a SOAP message. This package has all the API necessary for sending request-response messages. (Request-response messages are explained in SOAPConnection Objects.) (more)

Request Example

The class Request puts together the code fragments used in the section SAAJ Tutorial and adds what is needed to make it a complete example of a client sending a request-response message. In addition to putting all the code together, it adds import statements, a main method, and a try/catch block with exception handling.

import javax.xml.soap.*;
import javax.xml.namespace.QName;
import java.util.Iterator;
import java.net.URL;

public class Request {
    public static void main(String[] args)    {
        try {
            SOAPConnectionFactory soapConnectionFactory =
                SOAPConnectionFactory.newInstance();
            SOAPConnection connection =
                soapConnectionFactory.createConnection();

            MessageFactory factory = MessageFactory.newInstance();
            SOAPMessage message = factory.createMessage();

            SOAPHeader header = message.getSOAPHeader();
            SOAPBody body = message.getSOAPBody();
            header.detachNode();

            QName bodyName = new QName("http://wombat.ztrade.com",
                "GetLastTradePrice", "m");
            SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

            QName name = new QName("symbol");
            SOAPElement symbol = bodyElement.addChildElement(name);
            symbol.addTextNode("SUNW");

            URL endpoint = new URL("http://wombat.ztrade.com/quotes");
            SOAPMessage response = connection.call(message, endpoint);

            connection.close();

            SOAPBody soapBody = response.getSOAPBody();

            Iterator iterator = soapBody.getChildElements(bodyName);
            bodyElement = (SOAPBodyElement)iterator.next();
            String lastPrice = bodyElement.getValue();

            System.out.print("The last price for SUNW is ");
            System.out.println(lastPrice);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

For the Request class to be runnable, the second argument supplied to the call method would have to be a valid existing URI, and this is not true in this case. (more)

SOAP and PHP

A PHP SOAP Extension can be used to provide and consume Web services. In other words, this PHP extension can be used by PHP developers to write their own Web Services, as well as to write clients to make use of the existing Web services.
The SOAP extension that comes with PHP 5 is an attempt to implement the Web services stack support for PHP. Unlike most other efforts to support Web services for PHP, the SOAP extension is written in C. Hence it has the advantage of speed, over the other extensions.

The SOAP extension supports the following specifications. If not whole, at least the required subset for basic Web services.

  • SOAP 1.1
  • SOAP 1.2
  • WSDL 1.1

The SOAP extension mainly focuses on RPC style Web services. However, you can also use document literal style WSDL files with the WSDL mode of services and clients.

This extension uses GNOME XML library for XML processing.

Create a SOAP server

The first thing we need to do is to create the SOAP server. This is the script that will fetch the data from the database and then deliver it to the Client. One wonderful thing about the NuSOAP library is that this same Server script will also create a WSDL document for us.

The first step is to create a function that will fetch the data we want. Create this function just as you would any other. It is just straight up PHP. The one trick is to name the function something sensible, as this will be the name that is used when the Client contacts the Server.

<?php
function getStockQuote($symbol) {

mysql_connect('server','user','pass');
mysql_select_db('test');
$query = "SELECT stock_price FROM stockprices "
. "WHERE stock_symbol = '$symbol'";
$result = mysql_query($query);

$row = mysql_fetch_assoc($result);
return
$row['stock_price'];
}
?>

Now, it is time to turn this function into a Web Service. Basically, all we have to do is include the NuSOAP library, instantiate the soap_server class and then register the function with the server. Let’s go through it step by step, after which I will present the completed script. (more)

Others examples you can find here

SOAP Testing

Latest SOAP Testing Related Papers

(more)

[wikipedia.org]
[http://www.w3schools.com]
[http://www.xmlrpc.com]
[http://java.sun.com]
[http://www.theserverside.com]
[http://www.codewalkers.com]
[http://wso2.org]
[http://www.soaptesting.com/]
[http://www.developer.com]

Bookmark and Share

Advertisements

Entry filed under: JAAS, SOAP, Web Services, WSDL. Tags: , , , .

REST (Representational State Transfer) and RESTful web services: Methods, Concepts and Examples Zend Framework – ZF: Concepts, Methods and Examples

1 Comment Add your own

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


IT Passion’s Store

Archives

Communities

Get the Source
OSGi supporter
JUG Milano

Upcoming Events



....

Blog Stats

  • 348,048 hits

My PageRank

What's My Google PageRank?

%d bloggers like this: