Home > Articles > Programming > Java

Using SOAP with J2EE

This chapter is from the book

This chapter is from the book

XML namespaces play an important role in SOAP messages. A SOAP message may include several different XML elements in the Header and Body elements, and to avoid name collisions each of these elements should be identified by a unique namespace. For example, a SOAP message that contains the purchaseOrder element as well as message-id and XML digital-signature header blocks would include no fewer than six different namespaces, as shown in bold in Listing 4-5.

Listing 4-5 Using XML Namespaces in a SOAP Message

<?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>
   ...
   </sec:Signature>
   </soap:Header>
   <soap:Body sec:id="Body">
   <po:purchaseOrder orderDate="2003-09-22"
   xmlns:po="http://www.Monson-Haefel.com/jwsbook/PO"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   ...
   </po:purchaseOrder>
   </soap:Body>
   </soap:Envelope>
   

The use of XML namespaces is what makes SOAP such a flexible and extensible protocol. An XML namespace fully qualifies an element or attribute name, as you learned in Section 2.2. Because their use was discussed in detail there, the basic mechanics of XML namespaces aren't covered here.

Of the six namespaces declared in Listing 4-5, the first, declared in the Envelope element, defines the namespace of the standard SOAP elements—Envelope, Header, and Body—as shown in bold in the following snippet from Listing 4-5.

<?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:Envelope>
   

This namespace determines the version of SOAP used (1.1 at this point). SOAP messages must declare the namespace of the Envelope element to be the standard SOAP 1.1 envelope namespace, "http://schemas.xmlsoap.org/soap/envelope/". If a SOAP application receives a message based on some other namespace, it must generate a fault. This rule ensures that all conforming messages are using exactly the same namespace and XML schema, and therefore the same processing rules.BP

The second, third, and fourth namespaces declared in the Envelope element are associated with XML elements in the header blocks:

<?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:Signature>
   </sec:Signature>
   </soap:Header>
   <soap:Body>
   <!-- Application-specific data goes here -->
   </soap:Body>
   </soap:Envelope>
   

Each header block in the Header element should have its own namespace. This is particularly important because namespaces help SOAP applications identify header blocks and process them separately. A variety of “standard” header blocks that address topics such as security, transactions, and other qualities of service are in development by several organizations, including W3C, OASIS, IETF, Microsoft, BEA, and IBM. All of the proposed standards define their own namespaces and XML schemas, as well as processing requirements—but none of these “standard” header blocks is addressed by J2EE 1.4 Web Services yet.

In Listing 4-5, two other namespaces are declared in the immediate child of the Body element, as shown in the following snippet. The first namespace declaration belongs to the Purchase Order Markup Language defined by Monson-Haefel Books (see Part I: XML).

<?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>
    <!-- Header blocks go here -->
  </soap:Header>
  <soap:Body sec:id="Body">
    <po:purchaseOrder orderDate="2003-09-22"
   xmlns:po="http://www.Monson-Haefel.com/jwsbook/PO"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   ...
   </po:purchaseOrder>
   </soap:Body>
   </soap:Envelope>
   

All of the local elements of a SOAP message must be namespace-qualified (prefixed with the SOAP 1.1 namespace), because the XML schema for SOAP 1.1 specifies the elementFormDefault attribute as "qualified". In addition, the Basic Profile 1.0 requires that all the application-specific elements contained by the Body element must be qualified.BP Unqualified elements in a SOAP Body element create too much ambiguity as to the meaning and proper structure of elements. (See Section 3.1.6 for details about qualified and unqualified local elements.)

The xsi:schemaLocation attribute (the attribute that provides the URL of the schema) may be declared for validation, but in most cases the SOAP stack will have handled this matter at design time, so that explicit declaration of the xsi :schemaLocation in a SOAP message is not necessary.

A SOAP stack is a library of code designed to process and transmit SOAP messages. For example, Apache Axis, J2EE 1.4, Perl::Lite, and Microsoft .NET all have their own SOAP stacks, their own libraries of code for processing SOAP messages.

Some SOAP stacks make extensive use of the XML schema-instance namespace to indicate the data types of elements (for example, xsi:type = "xsd:float"). Other SOAP stacks do not, though, which causes problems when the receiver expects elements to be typed but the sender doesn't type them. According to the BP, the xsi:type attribute must be used only to indicate that a derived XML type is being used in place of its base type—for example, a USAddress in place of an Address.BP

As you learned in Section 2.2, the real power of XML namespaces goes beyond simply avoiding name collisions, to proper versioning and processing. Using fully qualified names for the SOAP and application-specific data tells the SOAP receiver how to process the message, and which XML schemas to apply in order to validate its contents. Differences in a particular version of a header block, for example, can affect how a receiver processes messages, so identifying the header-block version by its namespace enables a receiver to switch processing models, or to reject messages if it doesn't support the specified version. Similarly, properly identifying the types of XML elements contained in the Body element enables a SOAP receiver either to process those elements using the appropriate code modules or possibly to reject the message if it doesn't support the specified namespace.

The term “code module” is used to express an aspect of computer code that performs some function. A code module may be a separate code library, a service, or simply a branch of logic within a larger set of code.

For example, if a new algorithm is used to generate the message-id header block, then the namespace of the message-id header could change to reflect the use of the new algorithm. The SOAP message in Listing 4-6 contains a message-id header block with a new namespace, which indicates that it's different from the message-id header block used in previous examples.

Listing 4-6 Changing the Namespace of a 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:mi2="http://www.Monson-Haefel.com/jwsbook/message-id_version2/">
   <soap:Header>
   <mi2:message-id>1-203950-3485-30503453098</mi2:message-id>
   <sec:Signature>
   ...
   </sec:Signature>
   </soap:Header>
   <soap:Body>
   <!-- Application-specific data goes here -->
   <soap:Body>
   </soap:Envelope>
   

Namespaces enable a SOAP receiver to handle different versions of a SOAP message, without impairing backward compatibility or requiring different Web service endpoints for each version of a particular SOAP message.

As you can see from the previous examples, a SOAP message may contain many different namespaces, which makes SOAP messaging very modular. This modularity enables different parts of a SOAP message to be processed independently of other parts and to evolve separately. The version of the SOAP Envelope or header blocks may change over time, while the structure of the application-specific contents in the Body element remains the same. Similarly, the application-specific contents may change while the version of the SOAP message and the header blocks do not.

The modularity of SOAP messaging permits the code that processes the SOAP messages to be modular as well. The code that processes the element Envelope is independent of the code that processes the header blocks, which is independent of the code that processes application-specific data in the SOAP Body element. Modularity enables you to use different code libraries to process different parts of a SOAP message. Figure 4-4 shows the structure of a SOAP message and the code modules used to process each of its parts. The code modules in gray boxes are associated with namespaces used in this SOAP message. The code modules in white boxes represent alternatives; they are associated with different namespaces, used to process alternative versions of the SOAP message.

04fig04.gifFigure 4-4. Using the Appropriate Code Modules with SOAP Namespaces

In all the examples so far, the namespaces of the header blocks have been declared in the Envelope element. Doing so is not required; we could just as easily declare those namespaces in the Header element or the header blocks. As you learned in Section 2.2, namespaces are always locally scoped and can be declared at any level as long as the elements in question are within that scope (the element the namespace is declared in, and its subelements). For example, we could declare the header-block namespaces in the Header element, as shown in the boldface code lines in Listing 4-7.

Listing 4-7 Declaring XML Namespaces in a Header Element

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" >
  <soap:Header
   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" >
   <mi:message-id>11d1def534:b1c5fa:f3bfb4dcd7:-8000</mi:message-id>
   <sec:Signature>
   ...
   </sec:Signature>
   </soap:Header>
   <soap:Body>
   <!-- Application-specific data goes here -->
   </soap:Body>
   </soap:Envelope>
   

We could also declare each namespace in its own header block as in Listing 4-8.

Listing 4-8 Declaring XML Namespaces in Header Blocks

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

Although application-specific elements in the Body element must be qualified by prefixes, there is no such requirement for the elements contained within a Header element. Local elements of header blocks may be qualified or unqualified.

The way to declare namespaces is really a matter of style. As long as you adhere to the conventions and limitations of namespace declarations as they're described in the W3C Namespaces in XML recommendation,3 you can use any style you wish. Table 4-1 shows the namespace prefixes used in this book and in the WS-I Basic Profile 1.0.

Table 4-1. Namespace Prefixes

Prefix

Namespace

soap

"http://schemas.xmlsoap.org/soap/envelope/"

xsi

"http://www.w3.org/2001/XMLSchema-instance"

xsd

"http://www.w3.org/2001/XMLSchema"

soapenc

"http://schemas.xmlsoap.org/soap/encoding/"

wsdl

"http://schemas.xmlsoap.org/wsdl/"

soapbind

"http://schemas.xmlsoap.org/wsdl/soap/"

wsi

"http://ws-i.org/schemas/conformanceClaim/"

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