Chapter 9. Sending Binary Data with SOAP MTOM
Abstract
SOAP Message Transmission Optimization Mechanism (MTOM) replaces SOAP with attachments as a mechanism for sending binary data as part of an XML message. Using MTOM with Apache CXF requires adding the correct schema types to a service’s contract and enabling the MTOM optimizations.
9.1. Overview of MTOM Copy linkLink copied to clipboard!
SOAP Message Transmission Optimization Mechanism (MTOM) specifies an optimized method for sending binary data as part of a SOAP message. Unlike SOAP with Attachments, MTOM requires the use of XML-binary Optimized Packaging (XOP) packages for transmitting binary data. Using MTOM to send binary data does not require you to fully define the MIME Multipart/Related message as part of the SOAP binding. It does, however, require that you do the following:
Annotate the data that you are going to send as an attachment.
You can annotate either your WSDL or the Java class that implements your data.
Enable the runtime’s MTOM support.
This can be done either programmatically or through configuration.
Develop a
DataHandler
for the data being passed as an attachment.NoteDeveloping
DataHandler
s is beyond the scope of this book.
9.2. Annotating Data Types to use MTOM Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
In WSDL, when defining a data type for passing along a block of binary data, such as an image file or a sound file, you define the element for the data to be of type xsd:base64Binary
. By default, any element of type xsd:base64Binary
results in the generation of a byte[]
which can be serialized using MTOM. However, the default behavior of the code generators does not take full advantage of the serialization.
In order to fully take advantage of MTOM you must add annotations to either your service’s WSDL document or the JAXB class that implements the binary data structure. Adding the annotations to the WSDL document forces the code generators to generate streaming data handlers for the binary data. Annotating the JAXB class involves specifying the proper content types and might also involve changing the type specification of the field containing the binary data.
WSDL first Copy linkLink copied to clipboard!
Example 9.1, “Message for MTOM” shows a WSDL document for a Web service that uses a message which contains one string field, one integer field, and a binary field. The binary field is intended to carry a large image file, so it is not appropriate to send it as part of a normal SOAP message.
Example 9.1. Message for MTOM
If you want to use MTOM to send the binary part of the message as an optimized attachment you must add the xmime:expectedContentTypes
attribute to the element containing the binary data. This attribute is defined in the http://www.w3.org/2005/05/xmlmime namespace and specifies the MIME types that the element is expected to contain. You can specify a comma separated list of MIME types. The setting of this attribute changes how the code generators create the JAXB class for the data. For most MIME types, the code generator creates a DataHandler. Some MIME types, such as those for images, have defined mappings.
The MIME types are maintained by the Internet Assigned Numbers Authority(IANA) and are described in detail in Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies and Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types.
For most uses you specify application/octet-stream
.
Example 9.2, “Binary Data for MTOM” shows how you can modify xRayType
from Example 9.1, “Message for MTOM” for using MTOM.
Example 9.2. Binary Data for MTOM
The generated JAXB class generated for xRayType
no longer contains a byte[]
. Instead the code generator sees the xmime:expectedContentTypes
attribute and generates a DataHandler for the imageData field.
You do not need to change the binding
element to use MTOM. The runtime makes the appropriate changes when the data is sent.
Java first Copy linkLink copied to clipboard!
If you are doing Java first development you can make your JAXB class MTOM ready by doing the following:
- Make sure the field holding the binary data is a DataHandler.
-
Add the
@XmlMimeType()
annotation to the field containing the data you want to stream as an MTOM attachment.
Example 9.3, “JAXB Class for MTOM” shows a JAXB class annotated for using MTOM.
Example 9.3. JAXB Class for MTOM
9.3. Enabling MTOM Copy linkLink copied to clipboard!
By default the Apache CXF runtime does not enable MTOM support. It sends all binary data as either part of the normal SOAP message or as an unoptimized attachment. You can activate MTOM support either programmatically or through the use of configuration.
9.3.1. Using JAX-WS APIs Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
Both service providers and consumers must have the MTOM optimizations enabled. The JAX-WS APIs offer different mechanisms for each type of endpoint.
Service provider Copy linkLink copied to clipboard!
If you published your service provider using the JAX-WS APIs you enable the runtime’s MTOM support as follows:
Access the
Endpoint
object for your published service.The easiest way to access the
Endpoint
object is when you publish the endpoint. For more information see Chapter 31, Publishing a Service.Get the SOAP binding from the
Endpoint
using itsgetBinding()
method, as shown in Example 9.4, “Getting the SOAP Binding from an Endpoint”.Example 9.4. Getting the SOAP Binding from an Endpoint
// Endpoint ep is declared previously SOAPBinding binding = (SOAPBinding)ep.getBinding();
// Endpoint ep is declared previously SOAPBinding binding = (SOAPBinding)ep.getBinding();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You must cast the returned binding object to a
SOAPBinding
object to access the MTOM property.Set the binding’s MTOM enabled property to
true
using the binding’ssetMTOMEnabled()
method, as shown in Example 9.5, “Setting a Service Provider’s MTOM Enabled Property”.Example 9.5. Setting a Service Provider’s MTOM Enabled Property
binding.setMTOMEnabled(true);
binding.setMTOMEnabled(true);
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Consumer Copy linkLink copied to clipboard!
To MTOM enable a JAX-WS consumer you must do the following:
Cast the consumer’s proxy to a
BindingProvider
object.For information on getting a consumer proxy see Chapter 25, Developing a Consumer Without a WSDL Contract or Chapter 28, Developing a Consumer From a WSDL Contract.
Get the SOAP binding from the
BindingProvider
using itsgetBinding()
method, as shown in Example 9.6, “Getting a SOAP Binding from aBindingProvider
”.Example 9.6. Getting a SOAP Binding from a
BindingProvider
// BindingProvider bp declared previously SOAPBinding binding = (SOAPBinding)bp.getBinding();
// BindingProvider bp declared previously SOAPBinding binding = (SOAPBinding)bp.getBinding();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the bindings MTOM enabled property to
true
using the binding’ssetMTOMEnabled()
method, as shown in Example 9.7, “Setting a Consumer’s MTOM Enabled Property”.Example 9.7. Setting a Consumer’s MTOM Enabled Property
binding.setMTOMEnabled(true);
binding.setMTOMEnabled(true);
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
9.3.2. Using configuration Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
If you publish your service using XML, such as when deploying to a container, you can enable your endpoint’s MTOM support in the endpoint’s configuration file. For more information on configuring endpoint’s see Part IV, “Configuring Web Service Endpoints”.
Procedure Copy linkLink copied to clipboard!
The MTOM property is set inside the jaxws:endpoint
element for your endpoint. To enable MTOM do the following:
-
Add a
jaxws:property
child element to the endpoint’sjaxws:endpoint
element. -
Add a
entry
child element to thejaxws:property
element. -
Set the
entry
element’skey
attribute tomtom-enabled
. -
Set the
entry
element’svalue
attribute totrue
.
Example Copy linkLink copied to clipboard!
Example 9.8, “Configuration for Enabling MTOM” shows an endpoint that is MTOM enabled.
Example 9.8. Configuration for Enabling MTOM