Faster Data Transport Means Faster Web Services with MTOM/XOP

Faster Data Transport Means Faster Web Services with MTOM/XOP

he technologies of Message Transmission Optimization Mechanism (MTOM) and XML-Binary Optimized Packaging (XOP) are concerned with the fundamental issue of binary data transmission, which is of great importance to web service performance. Since becoming W3C standards in 2005, XOP and MTOM have been quickly and widely adopted in next-generation SOAP engines. In hindsight, one has to wonder why they were not proposed earlier in the development of web services or as part of the original SOAP specification.

This article introduces MTOM and XOP, discusses the problems they address, and then uses an example to demonstrate how to work with them in JAX-WS 2.0.

Challenges That MTOM/XOP Addresses
The issue MTOM/XOP tackles is the coordinated transport of opaque binary data in conjunction with an XML document. The two traditional approaches for handling this issue are:

  1. Embedding such data in XML as text-encoded octets with type xs:base64Binary or xs:hexBinary
  2. Referring to the binary data with a URI of type xs:anyURI in the XML document, with the opaque data bundled alongside using a package format (e.g., SwA and WS-Attachment)

Unfortunately, these approaches suffer from performance and interoperability problems. The first one bloats the message size (with UTF-8 text encoding, base64 encoded data expands by a factor of 1.33x, and hexadecimal encoded data expands by a factor of 2x). It also carries the overhead of processing costs, especially when decoding back into binary. In the second approach, the data is external to the XML document and isn’t part of the message Infoset. As a result, layered technologies for processing and describing XML and SOAP need to provide one set of solutions for the XML component of their data and another set of solutions for the external components (e.g., DIME and multipart MIME). This not only complicates all the entities working with such messages, but also brings interoperability issues because no standard model governs how SOAP intermediaries process the referenced data; existing technologies such as XML signature and encryption weren’t really designed with such referenced data in mind.

For a detailed and well-written analysis of those challenges, refer to “XML, SOAP and Binary Data“, a white paper from BEA and Microsoft written by Adam Bosworth and Don Box, et al.

How XOP Works
XOP extracts binary data in an XML document and serializes them into packages located inside an extensible packaging format such as MIME Multipart/Related. The new XML document marks the locations of those binary data packages with special elements that contain URI identifying the corresponding location.

Unlike traditional approaches for handling binary data in XML documents, XOP does not require encoding binary data in base64. The data can take its original format as octet streams. An application parsing an XOP package may work directly with the octet stream, or if appropriate, compute the base64 binary character representation from this stream.

Listing 1 and Listing 2 are SOAP messages that contain the same content in transmission over HTTP, but Listing 2 shows the message as serialized using the XOP format into a MIME Multipart/Related package. Look at the …/book/coverImage and …/book/sample/content elements in Listing 1. Both contain base64-encoded streams. In Listing 2, those base64-encoded streams are replaced with xop:Include elements, each of which contains an attribute href pointing to a MIME part through a URI following the cid:URI schema. The MIME parts contain the actual binary data in octet streams.

The W3C recommendation, XML-binary Optimized Packaging, specifies the various constructs in an XOP package, such as the syntax and semantics of the xop:Include element and its href attribute. It also specifies how to create and interpret an XOP package using the original XML document as the reference. By the specification, XOP works only with XML elements of xs:base64Binary type.

How MTOM Addresses These Challenges
MTOM provides a concrete implementation for using XOP to optimize the transmission and/or wire format of SOAP messages. It describes how to serialize a SOAP envelope using the XOP format and MIME Multipart/Related packaging (note that MIME Multipart/Related is not required in XOP). Following the rules specified, MTOM serializes a SOAP message into a MIME Multipart/Related XOP package with one body part (the root) that contains an XML representation of the modified SOAP envelope and additional parts that contain the binary representation of each element of the xs:base64Binary type. With MTOM, deserialization of this transferred SOAP message follows the standard XOP approach.

MTOM further specifies a concrete implementation of this mechanism for SOAP HTTP binding. For example, it requires placing the MIME headers of the resulting MIME Multipart/Related XOP package in HTTP headers and the rest of the package into the HTTP body.

You can identify MTOM in a SOAP message transmission over HTTP by the following four attributes:

  1. The application/xop+xml media is present.
  2. The media type of the HTTP message is multipart/related.
  3. The media type of the root part of the MIME Multipart/Related package is application/xop+xml.
  4. The start-info parameter indicates a content type of application/soap+xml.

Note that the W3C MTOM specification considers only SOAP 1.2. On April 2006, IBM, Microsoft, SAP, and Oracle submitted SOAP 1.1 Binding for MTOM 1.0 to the W3C to cover SOAP 1.1. This document, while reflecting the difference between SOAP 1.1 and SOAP 1.2, keeps the essential features of MTOM untouched. Following this member submission, the above-mentioned start-info parameter of the root part of the MIME Multipart/Related package becomes text/xml instead of application/soap+xml in SOAP 1.1. (Listing 2 shows a SOAP 1.1 example.)

With this background information covered, the next section will illustrate how to work with MTOM/XOP in JAX-WS 2.0, which is part of Java SE 6.0, (and its reference implementation) using a sample web service.

A Simple JAX-WS 2.0 Web Service
The sample web service developed in this section simulates a book catalog management system. (Listing 3 shows the web service WSDL.) It defines two operations in the service endpoint interface CatalogPort: upload and retrieve, which allow client applications to register (and update) book information and retrieve the sample chapter stored in the system, respectively. Listing 4 presents the schema file, Book.xsd, which is referred to in the WSDL. Pay particular attention to two previously mentioned elements, /book/coverImage and /book/sample/content.

To implement the sample web service with JAX-WS 2.0, you first need to use the apt tool to generate the port type interface CatalogPortType.java (see Listing 5), the domain objects in the com.company.mtom.catalog.book package, and the service class Catalog.java, which the clients will use to obtain the CatalogPort.

In the endpoint implementation class CatalogImpl.java, the upload() method stores the following:

  1. Basic information about the book and the sample chapter name in a properties file with the name of ${isbn}.properties under the book_info folder.
  2. The cover image in a file ${isbn}.jpg under the book_cover folder.
  3. The description (desc) string in a file ${isbn}.txt under the book_desc folder.
  4. The sample chapter content in a file ${isbn}.pdf under the folder book_sample.

The retrieve() method, on the other hand, obtains the sample chapter name from the properties file and the content from the book_sample directory, all based on an input parameter isbn, and returns a Chapter object to the client.

To get the web service up and running, all you have to do now is come up with the web service deployment descriptor (sun-jaxws.xml) and the web application deployment descriptor (web.xml), and then deploy the application war into a container (Tomcat, in this example). You can download the accompanying source code and look at these artifacts and the various Ant tasks for packaging, deployment, etc. For reference, here is the example’s sun-jaxws.xml descriptor:

     

That’s about it for the service. Listing 6 shows the main body of the sample web service client, in which the input book information is captured in a properties file named isbn, and the book cover image, sample chapter content, and description text are all kept in separate files (under ./etc). The private method getBook() creates a Book object to be used for invoking upload(), and compareSample() compares retrieved (through retrieve()) sample chapter names and content with information available in the local repository at this client side (./etc).

The example application comes with all the input files utilized by the client for three sample books. To look at the SOAP messages in transmission, you can install tcpmon as a proxy. Listing 1 shows a SOAP request message as captured by tcpmon while invoking the upload() method with information about one of the sample books. Listing 7 shows the response from the web service when the client calls the retrieve() method. In this listing, the sample chapter content is base64 encoded and embedded in the SOAP body.

Enabling the Example Application with MTOM
JAX-WS 2.0 supports MTOM for both SOAP 1.1 and SOAP 1.2 HTTP bindings. This section describes how to enable the example application with MTOM using the SOAP 1.1 binding. Working with SOAP 1.2 would require slight adjustments to the application and obvious changes to the instructions to follow.

With JAX-WS 2.0, you can enable MTOM on the client side, the service side, or both. To enable MTOM on the client side for the example application, all you need to do is uncomment the first two bold lines in Listing 6, the client application:

SOAPBinding binding = (SOAPBinding)((BindingProvider)port).getBinding();binding.setMTOMEnabled (true);

When you call the upload() method in this case (with the client MTOM enabled and the service not), the request SOAP message will look like Listing 2, indicating the client is sending optimized messages. Even though the service is not MTOM-enabled, the call succeeds. This shows that the service is able to respond to both optimized and ordinary messages.

When you invoke the retrieve() operation, the response SOAP message looks like Listing 7, indicating the service is sending ordinary messages even though the client is MTOM-enabled.

Two provisions to note are:

  1. The SOAPBinding interface also provides a method for checking if MTOM is enabled on the binding, javax.xml.ws.soap.SOAPBinding.isMTOMEnabled().
  2. JAX-WS 2.0 allows clients to interact with a web service through either a port proxy (as in this example) or the Dispatch API. MTOM can be enabled only for port proxy-based clients.

On the service side, you can enable MTOM in one of three ways:

  1. You can add one line to the proprietary web service deployment descriptor, sun-jaxws.xml, as follows:
         
  2. Use the same descriptor as in the following example:
         

    Notice the binding attribute of endpoint. To enable MTOM for SOAP 1.1 or SOAP 1.2 HTTP bindings, you may specify one of the following identifiers for the binding attribute, respectively:

    • http://schemas.xmlsoap.org/wsdl/soap/http?mtom=true
    • http://www.w3.org/2003/05/soap/bindings/HTTP/?mtom=true
  3. Use the @javax.xml.ws.BindingType annotation on the service endpoint implementation class, specifying a value of javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_MTOM_BINDING (or javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_MTOM_BINDING for SOAP 1.2). This approach is a variant of the second and is JAX-WS 2.0 standard compliant. The following example illustrates it:
    @WebService(endpointInterface = "com.company.mtom.catalog.service.CatalogPortType")@BindingType(value=SOAPBinding.SOAP11HTTP_MTOM_BINDING)public class CatalogImpl implements CatalogPortType {     ......}

    The predefined constant javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_MTOM_BINDING (.SOAP12HTTP_MTOM_BINDING) corresponds to the MTOM-enabled SOAP 1.1 (SOAP 1.2) HTTP binding identifier listed above.

When the service-side MTOM is enabled, if you disable MTOM at the client side you will find from tcpmon that the web service will send an optimized SOAP response when the client calls the retrieve() method (see Listing 8). Comparing this with Listing 7, the content of the …/chapter/content element in the SOAP body is replaced with an xop:Include element, which refers to the actual sample chapter content as a binary stream in a MIME part.

With MTOM enabled only on the service side, when the client invokes the upload() method it sends an ordinary SOAP request message, and the service is able to respond properly per the JAX-WS 2.0 specification.

If you have both the service side and the client side MTOM enabled, all communications are optimized when applicable.

Enabling MTOM for Large-Sized Binary Data Only
The reference implementation of JAX-WS 2.0 has a proprietary feature that allows the service and/or client to optimize only binary data that are larger than a configured threshold. With the example application, you can set the threshold on the client side by uncommenting the third bold line in Listing 6. The number is measured in bytes, and the following sample will instruct the client runtime to optimize only binary data with a size larger than 5 KB (Yes, the property is named MTOM_THRESHOLOD_VALUE, obviously a typo not caught before the final release):

((BindingProvider) port).getRequestContext().put(JAXWSProperties.MTOM_THRESHOLOD_VALUE, 6120);

Binary data less than 5 KB will remain in line in the SOAP message and stay base64 encoded. The default threshold value of the JAX-WS 2.0 reference implementation is 1 KB.

On the service side, you can set the threshold programmatically by adding the following lines in the endpoint implementation class CatalogImpl.java:

@Resourceprivate WebServiceContext wsContext;...wsContext.getMessageContext().put(com.sun.xml.ws.developer.JAXWSProperties.MTOM_THRESHOLOD_VALUE, 30720);...

The following code snippet shows how you can set the threshold for the retrieve() method using this approach:

public class CatalogImpl implements CatalogPortType {     @Resource     WebServiceContext wsContext;     public Chapter retrieve(String isbn) {          wsContext.getMessageContext().put(com.sun.xml.ws.developer.JAXWSProperties.MTOM_THRESHOLOD_VALUE, 30720);          ......     }     public boolean upload(Book bookInfo) {          ......     }     ......}

You may also specify the service side threshold value in the web service deployment descriptor, sun-jaxws.xml, as follows:

     

While the threshold value set in the deployment descriptor applies to all web methods exposed in the corresponding endpoint, using the programmatic approach allows different web methods to have different threshold values.

MTOM Features in JAX-WS 2.1.1 Release
The next version of JAX-WS (2.1.1) standardizes the feature of setting thresholds to enable MTOM only for large-sized data. It also will feature two new annotations (@javax.xml.ws.Feature and @javax.xml.ws.FeatureParameter) for easily configuring MTOM support.

devx-admin

devx-admin

Share the Post:
Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of constructing residential and commercial buildings.

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the