Home > Articles > Programming > Java

Using SOAP with J2EE

This chapter is from the book

This chapter is from the book

SOAP was originally an acronym for Simple Object Access Protocol. (Now it's just a name.) SOAP 1.1 is the standard messaging protocol used by J2EE Web Services, and is the de facto standard for Web services in general. SOAP's primary application is Application-to-Application (A2A) communication. Specifically, it's used in Business-to-Business (B2B) and Enterprise Application Integration (EAI), which are two sides of the same coin: Both focus on integrating software applications and sharing data. To be truly effective in B2B and EAI, a protocol must be platform-independent, flexible, and based on standard, ubiquitous technologies. Unlike earlier B2B and EAI technologies, such as CORBA and EDI, SOAP meets these requirements, enjoys widespread use, and has been endorsed by most enterprise software vendors and major standards organizations (W3C, WS-I, OASIS, etc.).

Despite all the hoopla, however, SOAP is just another XML markup language accompanied by rules that dictate its use. SOAP has a clear purpose: exchanging data over networks. Specifically, it concerns itself with encapsulating and encoding XML data and defining the rules for transmitting and receiving that data. In a nutshell, SOAP is a network application protocol.

A SOAP XML document instance, which is called a SOAP message,1 is usually carried as the payload of some other network protocol. For example, the most common way to exchange SOAP messages is via HTTP (HyperText Transfer Protocol), used by Web browsers to access HTML Web pages. The big difference is that you don't view SOAP messages with a browser as you do HTML. SOAP messages are exchanged between applications on a network and are not meant for human consumption. HTTP is just a convenient way of sending and receiving SOAP messages.

SOAP messages can also be carried by e-mail using SMTP (Simple Mail Transfer Protocol) and by other network protocols, such as FTP (File Transfer Protocol) and raw TCP/IP (Transmission Control Protocol/Internet Protocol). At this time, however, the WS-I Basic Profile 1.0 sanctions the use of SOAP only over HTTP. Figure 4-1 illustrates how SOAP can be carried by various protocols between software applications on a network.

04fig01.gifFigure 4-1. SOAP over HTTP, SMTP, and Raw TCP/IP

Web services can use One-Way messaging or Request/Response messaging. In the former, SOAP messages travel in only one direction, from a sender to a receiver. In the latter, a SOAP message travels from the sender to the receiver, which is expected to send a reply back to the sender. Figure 4-2 illustrates these two forms of messaging.

04fig02.gifFigure 4-2. One-Way versus Request/Response Messaging

SOAP defines how messages can be structured and processed by software in a way that is independent of any programming language or platform, and thus facilitates interoperability between applications written in different programming languages and running on different operating systems. Of course, this is nothing new: CORBA IIOP and DCE RPC also focused on cross-platform interoperability. These legacy protocols were never embraced by the software industry as a whole, however, so they never became pervasive technologies. SOAP, on the other hand, has enjoyed unprecedented acceptance, and adoption by virtually all the players in distributed computing, including Microsoft, IBM, Sun Microsystems, BEA, HP, Oracle, and SAP, to name a few.

The tidal wave of support behind SOAP is interesting. One of the main reasons is probably its grounding in XML. The SOAP message format is defined by an XML schema, which exploits XML namespaces to make SOAP very extensible. Another advantage of SOAP is its explicit definition of an HTTP binding, a standard method for HTTP tunneling. HTTP tunneling is the process of hiding another protocol inside HTTP messages in order to pass through a firewall unimpeded. Firewalls will usually allow HTTP traffic through port 80, but will restrict or prohibit the use of other protocols and ports.

A port is a communication address on a computer that complements the Internet address. Each network application on a computer uses a different port to communicate. By convention, Web servers use port 80 for HTTP requests, but application servers can use any one of thousands of other ports.

The power that comes from XML's extensibility and the convenience of using the ubiquitous, firewall-immune HTTP protocol partly explain SOAP's success. It's difficult to justify SOAP's success purely on its technical merits, which are good but less than perfect. Another factor in SOAP's success is the stature of its patrons. SOAP is the brainchild of Dave Winner, Don Box, and Bob Atkinson. Microsoft and IBM supported it early, which sent a strong signal to everyone else in the industry: “If you want to compete in this arena, you better jump aboard SOAP.” The event that secured industry-wide support for SOAP was its publication by the World Wide Web Consortium as a Note2 in May of 2000, making it the de facto standard protocol for A2A messaging. Overnight, SOAP became the darling of distributed computing and started the biggest technology shift since the introduction of Java in 1995 and XML in 1998. SOAP is the cornerstone of what most people think of as Web services today, and will be for a long time to come.

Recently, the W3C has defined a successor to SOAP 1.1. SOAP 1.2 does a decent job of tightening up the SOAP processing rules and makes a number of changes that will improve interoperability. SOAP 1.2 is very new and has not yet been widely adopted, however, so it's not included in the WS-I Basic Profile 1.0. This exclusion is bound to end when the BP is updated, but for now J2EE 1.4 Web Services, which adheres to the WS-I Basic Profile 1.0, does not support the use of SOAP 1.2.

4.1 The Basic Structure of SOAP

As you now know, a SOAP message is a kind of XML document. SOAP has its own XML schema, namespaces, and processing rules. This section focuses on the structure of SOAP messages and the rules for creating and processing them.

A SOAP message is analogous to an envelope used in traditional postal service. Just as a paper envelope contains a letter, a SOAP message contains XML data. For example, a SOAP message could enclose a purchaseOrder element, as in Listing 4-1. Notice that XML namespaces are used to keep SOAP-specific elements separate from purchaseOrder elements—the SOAP elements are shown in bold.

Listing 4-1 A SOAP Message That Contains an Instance of Purchase Order Markup

<?xml version="1.0" encoding="UTF-8"?>
   <soap:Envelope
   xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" >
   <soap:Body>
   <po:purchaseOrder orderDate="2003-09-22"
   xmlns:po="http://www.Monson-Haefel.com/jwsbook/PO">
   <po:accountName>Amazon.com</po:accountName>
   <po:accountNumber>923</po:accountNumber>
   <po:address>
   <po:name>AMAZON.COM</po:name>
   <po:street>1850 Mercer Drive</po:street>
   <po:city>Lexington</po:city>
   <po:state>KY</po:state>
   <po:zip>40511</po:zip>
   </po:address>
   <po:book>
   <po:title>J2EE Web Services</po:title>
   <po:quantity>300</po:quantity>
   <po:wholesale-price>24.99</po:wholesale-price>
   </po:book>
   </po:purchaseOrder>
   </soap:Body>
   </soap:Envelope>
   

This message is an example of a SOAP message that contains an arbitrary XML element, the purchaseOrder element. In this case, the SOAP message will be One-Way; it will be sent from the initial sender to the ultimate receiver with no expectation of a reply. Monson-Haefel Books' retail customers will use this SOAP message to submit a purchase order, a request for a shipment of books. In this example, Amazon.com is ordering 300 copies of this book for sale on its Web site.

A SOAP message may have an XML declaration, which states the version of XML used and the encoding format, as shown in this snippet from Listing 4-1.

<?xml version="1.0" encoding="UTF-8"?>

If an xml declaration is used, the version of XML must be 1.0 and the encoding must be either UTF-8 or UTF-16. If encoding is absent, the assumption is that the SOAP message is based on XML 1.0 and UTF-8. An XML declaration isn't mandatory. Web services are required to accept messages with or without them.BP (Remember that I said I'd use a superscript BP to signal a BP-conformance rule.)

Every XML document must have a root element, and in SOAP it's the Envelope element. Envelope may contain an optional Header element, and must contain a Body element. If you use a Header element, it must be the immediate child of the Envelope element, and precede the Body element. The Body element contains, in XML format, the actual application data being exchanged between applications. The Body element delimits the application-specific data. Listing 4-2 shows the structure of a SOAP message.

Listing 4-2 The Structure of a SOAP Message

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header>
   <!-- Header blocks go here -->
   </soap:Header>
   <soap:Body>
   <!-- Application data goes here -->
   </soap:Body>
   </soap:Envelope>
   

A SOAP message adheres to the SOAP 1.1 XML schema, which requires that elements and attributes be fully qualified (use prefixes or default namespaces). A SOAP message may have a single Body element preceded, optionally, by one Header element. The Envelope element cannot contain any other children.

Because SOAP doesn't limit the type of XML data carried in the SOAP Body, SOAP messages are extremely flexible; they can exchange a wide spectrum of data. For example, the application data could be an arbitrary XML element like a purchaseOrder, or an element that maps to the arguments of a procedure call.

The Header element contains information about the message, in the form of one or more distinct XML elements, each of which describes some aspect or quality of service associated with the message. Figure 4-3 illustrates the structure of a basic SOAP message.

04fig03.gifFigure 4-3. The Structure of a Basic SOAP Message

The Header element can contain XML elements that describe security credentials, transaction IDs, routing instructions, debugging information, payment tokens, or any other information about the message that is important in processing the data in the Body element.

For example, we may want to attach a unique identifier to every SOAP message, to be used for debugging and logging. Although unique identifiers are not an integral part of the SOAP protocol itself, we can easily add an identifier to the Header element as in Listing 4-3.

Listing 4-3 A SOAP Message with a Unique Identifier

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:mi="http://www.Monson-Haefel.com/jwsbook/message-id" >
  <soap:Header>
    <mi:message-id>11d1def534ea:b1c5fa:f3bfb4dcd7:-8000</mi:message-id>
  </soap:Header>
  <soap:Body>
    <!-- Application-specific data goes here -->
  </soap:Body>
</soap:Envelope>

The message-id element is called a header block, and is an arbitrary XML element identified by its own namespace. A header block can be of any size and can be very extensive. For example, the header for an XML digital signature, shown in bold in Listing 4-4, is relatively complicated.

Listing 4-4 A SOAP Message with an XML Digital-Signature Header Block

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:sec="http://schemas.xmlsoap.org/soap/security/2000-12"
 xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
 xmlns:mi="http://www.Monson-Haefel.com/jwsbook/message-id">
  <soap:Header>
    <mi:message-id>11d1def534ea:b1c5fa:f3bfb4dcd7:-8000</mi:message-id>
    <sec:Signature >
   <ds:Signature>
   <ds:SignedInfo>
   <ds:CanonicalizationMethod Algorithm=
   "http://www.w3.org/TR/2000/CR-xml-c14n-20001026"/>
   <ds:SignatureMethod Algorithm=
   "http://www.w3.org/2000/09/xmldsig#dsa-sha1"/>
   <ds:Reference URI="#Body">
   <ds:Transforms>
   <ds:Transform Algorithm=
   "http://www.w3.org/TR/2000/CR-xml-c14n-20001026"/>
   </ds:Transforms>
   <ds:DigestMethod Algorithm=
   "http://www.w3.org/2000/09/xmldsig#sha1"/>
   <ds:DigestValue>u29dj93nnfksu937w93u8sjd9=
   </ds:DigestValue>
   </ds:Reference>
   </ds:SignedInfo>
   <ds:SignatureValue>CFFOMFCtVLrklR…</ds:SignatureValue>
   </ds:Signature>
   </sec:Signature>
   </soap:Header>
   <soap:Body sec:id="Body">
   <!-- Application-specific data goes here -->
   </soap:Body>
   </soap:Envelope>
   

You can place any number of header blocks in the Header element. The example above contains both the message-id and XML digital signature header blocks, each of which would be processed by appropriate functions. Header blocks are discussed in more detail in Section 4.3.

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