Home > Articles > Programming

  • Print
  • + Share This

SOAP

SOAP is the new standard for network communication between software services. It is a general-purpose technology for sending messages between endpoints, and may be used for RPC or straightforward document transfer. SOAP messages are represented using XML and can be sent over any transport layer. HTTP is the most common transport layer, with implementations also available for Simple Mail Transport Protocol (SMTP), Java Messaging Service (JMS), and IBM MQSeries (see Figure 1.9).

Figure 1.9FIGURE 1.9 SOAP messages are XML documents, usually sent over HTTP

The easiest way to publish a software component as a web service is to use a SOAP container which accepts incoming requests and dispatches them to published components, automatically translating between SOAP and the component's native language interface. SOAP containers are available for most programming languages, including Java, C++, Perl, and C# (see Figure 1.10).

Figure 1.10Figure 1.10 A SOAP container converts XML messages into native calls

Once a component has been published as a web service, any SOAP-enabled client that knows the network address of the service and the messages that it understands can send a SOAP request and get back a SOAP response. To get the address and message information, SOAP clients read a WSDL file that describes the web service. Fortunately, most SOAP containers will automatically generate WSDL for the web services that they host, so developers don't have to write WSDL manually unless they really want to. Once the WSDL file is read, the client can start sending SOAP messages to the web service (see Figure 1.11).

Figure 1.11FIGURE 1.11 A client needs WSDL before invoking the service

Publishing a Web Service

Before delving into the details of the SOAP protocol, I'll show you how easy it is to create and invoke a web service using a modern language like Java. The main thing to note is that no knowledge of SOAP or WSDL is necessary to immediately become a productive web services developer.

The following example shows the steps that are necessary to publish an object as a web service and then invoke it from a SOAP client. Although most examples in this book are written in Java, it is important to note that SOAP is language neutral and can support any combination of languages on the client and server. Some examples of Java programs talking to C# programs using SOAP are presented in the .NET chapter.

The object in this example is a simple stock trading service that defines a single method for buying stock. The buy() method returns the cost of purchasing a specified quantity of a particular stock. Here is the source code for the ITrader interface.

wsbook\src\book\soap\ITrader.java

package book.soap;

/**
 * An interface for buying stock.
 */
public interface ITrader
  {
  /**
  * @param quantity The number of shares to purchase.
  * @param symbol The ticker symbol of the company.
  * @throws TradeException If the symbol is not recognized.
  * @return The cost of the purchase.
  */
float buy( int quantity, String symbol ) throws TradeException;
}

The Trader class is a simple implementation of ITrader that uses hard-coded stock prices and throws an exception if it doesn't recognize a particular ticker symbol.

wsbook\src\book\soap\Trader.java

package book.soap;

 /**
  * Simple implementation of ITrader.
  */
public class Trader implements ITrader
  {
  public float buy( int quantity, String symbol )
    throws TradeException
   {
    if( symbol.equals( "IBM" ) )
      return 117.4F * quantity;
    else if( symbol.equals( "MSFT" ) )
      return 68.1F * quantity;
    else
      throw new TradeException( "symbol " + symbol + " not
recognized" );
    }
  }

Notice that neither the interface nor the source code for the trader service contains any code related to SOAP or web services. Most SOAP containers are able to publish unmodified software components, which is good because domain objects should not be coupled to details of distributed computing.

Each SOAP container has different Application Programming Interfaces (APIs) for starting up an in-process HTTP server and for publishing objects as web services. Here is the way that you would start an HTTP server on http://localhost:8003/soap and export an instance of Trader using GLUE, the web services platform included with this book. GLUE is described in more detail in the next chapter.

wsbook\src\book\soap\TraderServer.java

package book.soap;

import electric.registry.Registry;
import electric.server.http.HTTP;

public class TraderServer
  {
  public static void main( String[] args )
    throws Exception
    {
    // start a web server on port 8003, accept messages via /soap
    HTTP.startup( "http://localhost:8003/soap" );

    // publish an instance of Trader
    Registry.publish( "trader", new Trader() );
    }
  } 

Binding to a Web Service

Once an object is published as a web service, a SOAP client can bind to it and invoke it. For example, here's what a SOAP client written using GLUE looks like. Fortunately, from a Java developer's viewpoint, a web service can be invoked as if it were a local object, with all the details of SOAP and WSDL hidden by the underlying infrastructure. Microsoft .NET provides a similar mechanism for C# and Visual Basic developers.

wsbook\src\book\soap\TraderClient.java

package book.soap;

import electric.registry.Registry;

public class TraderClient
  {
  public static void main( String[] args ) throws Exception
   {
   // the URL of the web service WSDL file
   String url = "http://localhost:8003/soap/trader.wsdl";

   // read the WSDL file and bind to its associated web service

   ITrader trader = (ITrader) Registry.bind( url, ITrader.class );
   // invoke the web service as if it was a local object
   float ibmCost = trader.buy( 54, "IBM" );
   System.out.println( "IBM cost is " + ibmCost );
   float tmeCost = trader.buy( 32, "TME" );
   System.out.println( "TME cost is " + tmeCost );
   }
  }

The binding process returns a proxy that implements a Java interface whose methods mirror those of the remote service. A message sent to the proxy is automatically converted into a SOAP request, delivered across the network, and the SOAP response is converted back into a regular Java result.

Figure 1.12FIGURE 1.12 A client proxy hides the communication details from the application

When the TraderClient is executed, SOAP messages fly back and forth between the client and server, translated automatically between XML and native calls by the SOAP container. The first method succeeds and returns a value, whereas the second method throws an exception because the symbol TME is not recognized.

Here is the server output:

> java book.soap.TraderServer
GLUE 1.2 (c) 2001 The Mind Electric
startup server on http://199.174.20.117:8003/soap

Here is the client output:

> java book.soap.TraderClient
IBM cost is 6339.6
Exception in thread "main" book.soap.TradeException: symbol TME not
recognized

> _

This example hopefully has convinced you that web services programs can be written without any detailed knowledge of SOAP or WSDL. Now let's examine the SOAP messages in detail.

Anatomy of a SOAP Request

Here's what the SOAP request looks like when the example client sends a buy() message, with the method and arguments highlighted for clarity.

POST /soap/trader HTTP/1.1
Host: 199.174.18.220:8004
Content-Type: text/xml
User-Agent: GLUE/1.0
Connection: Keep-Alive
SOAPAction: "buy"
Content-Length: 525

<?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:buy xmlns:n='http://tempuri.org/book.soap.Trader'>
       <quantity xsi:type='xsd:int'>54</quantity>
       <symbol xsi:type='xsd:string'>IBM</symbol>
    </n:buy>
  </soap:Body>
</soap:Envelope>

Even without an explanation of the SOAP format, you can probably figure out what most of it means. Contrast this with the CORBA and DCOM protocols, which are binary, not self-describing, and tough to trace. I know this firsthand, having written a CORBA ORB in a previous lifetime.

The first part of the SOAP request is a standard HTTP header that indicates that the request is an HTTP POST operation whose Universal Resource Identifier (URI) is /soap/trader. The Content-Type field shows that the HTTP payload is XML, and the SOAPAction field tells the remote host that the content is a SOAP message. SOAPAction is often set to the name of the method to invoke so that the host web server or firewall can perform some high-level message filtering.

The second part of the SOAP request is an XML document that consists of three main portions:

Envelope

The envelope defines the various XML namespaces that are used by the rest of the SOAP message, and typically include xmlns:soap (SOAP envelope namespace), xmlns:xsi (XML Schema for instances), xmlns:xsd (XML Schema for data types) and xmlns:soapenc (SOAP encoding namespace). More information about these namespaces is presented later in this book.

Header

The header is an optional element for carrying auxiliary information for authentication, transactions, routing, and payments. Any element in a SOAP processing chain can add or delete items from the header; elements can also choose to ignore items if they are unknown. If a header is present, it must be the first child of the envelope. Because our example is simple and does not invoke routers, the header is absent.

Body

The body is the main payload of the message. When SOAP is used to perform an RPC call, the body contains a single element that contains the method name and arguments. The namespace of the method name is specified by the web service, and in this case is equal to http://tempuri.org/ followed by the type of the target web service. The type of each argument can be optionally supplied using the xsi:type attribute; in this example, the first argument is flagged as an xsd:int, and the second argument as an xsd:string. If a header is present, the body must be its immediate sibling; otherwise it must be the first child of the envelope.


A SOAP request is typically accepted by a servlet, CGI or standalone daemon running on the remote web server. In this example, the GLUE SOAP container started a servlet running on localhost:8003/soap. When the servlet gets a request, it checks that the request has a SOAPAction field, and if it does, forwards it to the SOAP container. The container uses the POST URI to look up the target web service, parses the XML payload, and then invokes the method on the component.

Anatomy of a SOAP Response

The result of the invocation is translated by the SOAP container into a SOAP response and returned back to the sender within the HTTP reply. Here's the SOAP response from the buy() message sent to the Trader service, with the result name and value highlighted for clarity.

HTTP/1.1 200 OK
Date: Sat, 19 May 2001 06:58:38 GMT
Content-Type: text/xml
Server: GLUE/1.0
Content-Length: 489

  <?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:buyResponse xmlns:n='http://tempuri.org/book.soap.Trader'>
        <Result xsi:type='xsd:float'>6339.6</Result>
      </n:buyResponse>
    </soap:Body>
</soap:Envelope>

The XML document is structured just like the request except that the body contains the encoded method result. By convention, the name of the result is equal to the name of the method followed by "Response", and the namespace of the result is the same as the namespace of the original method.

SOAP Exceptions

If an exception occurs at any time during the processing of a message, a SOAP fault is generated and encoded in a manner similar to a regular SOAP response. Here is the SOAP response that is returned when our example client attempts to buy stock for a ticker symbol that is not recognized.

HTTP/1.1 500 Internal Server Error
Content-Type: text/xml
Content-Length: 244

<soap:Fault>
  <faultcode>soap:Server</faultcode>
  <faultstring>symbol TME not recognized</faultstring>
  <detail>
     <stacktrace>
       book.soap.TradeException: symbol TME not recognized
         at book.soap.Trader.buy(Trader.java:16)
         at java.lang.reflect.Method.invoke(Native Method)
     </stacktrace>
  </detail>
</soap:Fault>

The standard HTTP reply header indicates an exception by using status code 500. The XML payload contains an envelope and body just like a regular response, except that the content of the body is a soap:Fault structure whose fields are defined as follows:

faultcode

A code that indicates the type of the fault. The valid values are soap:Client (incorrectly formed message), soap:Server (delivery problem), soap:VersionMismatch (invalid namespace for Envelope element) and soap:MustUnderstand (error processing header content).

Faultstring

A human readable description of the fault.

Faultactor

An optional field that indicates the URL of the source of the fault.

detail

An application-specific XML document that contains detailed information about the fault.


Some SOAP implementations add an additional element to encode information about remote exceptions such as their type, data, and stack trace so that they can be rethrown automatically on the client.

Performance

Now that you've seen how SOAP messages are passed back and forth using HTTP and XML, it is time to contemplate performance issues.

CORBA and DCOM use binary encoding for arguments and return values. In addition, they assume that both the sender and the receiver have full knowledge of the message context and do not encode any meta-information such as the names or types of the arguments. This approach results in good performance, but makes it hard for intermediaries to process messages. And since each system uses a different binary encoding, it's hard to build systems that interoperate.

Because SOAP uses XML to encode messages, it's very easy to process messages at every step of the invocation process. In addition, the ease of debugging SOAP messages is leading to a quick convergence of the various SOAP implementations, which is important because large-scale interoperability is what SOAP is all about.

On the surface, it seems that an XML-based scheme would be intrinsically slower than that of a binary-based model, but it's not as straightforward as that.

First, when SOAP is used for sending messages across the Internet, the time to encode/decode the messages at each endpoint is tiny compared with the time to transfer bytes between endpoints, so using XML in this case is not significant.

Second, when SOAP is used to send messages between endpoints in a closed environment, such as between departments within the same company, it's likely that the endpoints will be running the same implementation of SOAP. In this case, there are opportunities for optimizations that are unique to that particular implementation. For example, a SOAP client could add an HTTP header tag to a SOAP request that indicates that it supports a particular optimization. If the SOAP server also supports that optimization, it could return an HTTP header tag in the first SOAP response that tells the client that it's okay to use that optimization in subsequent communications. At that point, both the client and the server could start using the optimization.

The fastest SOAP implementations typically get at least 500 messages/second on a 600MHz desktop PC when the client and the server are in different programs in the same machine, and around 300 messages/second on a fast local area network (LAN).

Other SOAP Features

The example in this section was very simple and demonstrated only a subset of SOAP functionality. Additional features, many of which are covered later in this book, include:

  • Arrays, objects, and other complex data structures may be sent across the network in a platform and language neutral way.

  • SOAP headers support security, transactions, and routing.

  • Custom encoding types may be defined.

  • SOAP supports request-response, one-way, solicit-response, and notification operations.

Now that you've seen what SOAP messaging looks like, it's time to look at WSDL.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020