blob: 27466c9f418df455d27fe19c8783e381ff053392 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<link type="text/css" rel="stylesheet" href="/resources/site.css">
<script src='/resources/space.js'></script>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta name="keywords" content="business integration, EAI, SOA, Service Oriented Architecture, web services, SOAP, JBI, JMS, WSDL, XML, EDI, Electronic Data Interchange, standards support, integration standards, application integration, middleware, software, solutions, services, CXF, open source">
<meta name="description" content="Apache CXF, Services Framework - MTOM">
<link type="text/css" rel="stylesheet" href="/resources/highlighter/styles/shCoreCXF.css">
<link type="text/css" rel="stylesheet" href="/resources/highlighter/styles/shThemeCXF.css">
<script src='/resources/highlighter/scripts/shCore.js'></script>
<script src='/resources/highlighter/scripts/shBrushXml.js'></script>
<script src='/resources/highlighter/scripts/shBrushJava.js'></script>
<script>
SyntaxHighlighter.defaults['toolbar'] = false;
SyntaxHighlighter.all();
</script>
<title>
Apache CXF -- MTOM
</title>
</head>
<body onload="init()">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td id="cell-0-0" colspan="2">&nbsp;</td>
<td id="cell-0-1">&nbsp;</td>
<td id="cell-0-2" colspan="2">&nbsp;</td>
</tr>
<tr>
<td id="cell-1-0">&nbsp;</td>
<td id="cell-1-1">&nbsp;</td>
<td id="cell-1-2">
<!-- Banner -->
<div class="banner" id="banner"><div><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td align="left" colspan="1" nowrap>
<a shape="rect" href="http://cxf.apache.org/" title="Apache CXF"><span style="font-weight: bold; font-size: 170%; color: white">Apache CXF</span></a>
</td><td align="right" colspan="1" nowrap>
<a shape="rect" href="http://www.apache.org/" title="The Apache Sofware Foundation"><img border="0" alt="ASF Logo" src="http://cxf.apache.org/images/asf-logo.png"></a>
</td></tr></table></div></div>
<!-- Banner -->
<div id="top-menu">
<table border="0" cellpadding="1" cellspacing="0" width="100%">
<tr>
<td>
<div align="left">
<!-- Breadcrumbs -->
<a href="index.html">Index</a>&nbsp;&gt;&nbsp;<a href="wsdl-bindings.html">WSDL Bindings</a>&nbsp;&gt;&nbsp;<a href="mtom.html">MTOM</a>
<!-- Breadcrumbs -->
</div>
</td>
<td>
<div align="right">
<!-- Quicklinks -->
<div id="quicklinks"><p><a shape="rect" href="http://cxf.apache.org/download.html">Download</a> | <a shape="rect" href="http://cxf.apache.org/docs/index.html">Documentation</a></p></div>
<!-- Quicklinks -->
</div>
</td>
</tr>
</table>
</div>
</td>
<td id="cell-1-3">&nbsp;</td>
<td id="cell-1-4">&nbsp;</td>
</tr>
<tr>
<td id="cell-2-0" colspan="2">&nbsp;</td>
<td id="cell-2-1">
<table>
<tr valign="top">
<td height="100%">
<div id="wrapper-menu-page-right">
<div id="wrapper-menu-page-top">
<div id="wrapper-menu-page-bottom">
<div id="menu-page">
<!-- NavigationBar -->
<div id="navigation"><ul class="alternate"><li><a shape="rect" href="overview.html">Overview</a></li><li><a shape="rect" href="how-tos.html">How-Tos</a></li><li><a shape="rect" href="frontends.html">Frontends</a></li><li><a shape="rect" href="databindings.html">DataBindings</a></li><li><a shape="rect" href="transports.html">Transports</a></li><li><a shape="rect" href="configuration.html">Configuration</a></li><li><a shape="rect" href="debugging-and-logging.html">Debugging and Logging</a></li><li><a shape="rect" href="tools.html">Tools</a></li><li><a shape="rect" href="restful-services.html">RESTful Services</a></li><li><a shape="rect" href="wsdl-bindings.html">WSDL Bindings</a></li><li><a shape="rect" href="service-routing.html">Service Routing</a></li><li><a shape="rect" href="dynamic-languages.html">Dynamic Languages</a></li><li><a shape="rect" href="ws-support.html">WS-* Support</a></li><li><a shape="rect" href="advanced-integration.html">Advanced Integration</a></li><li><a shape="rect" href="deployment.html">Deployment</a></li><li><a shape="rect" href="schemas-and-namespaces.html">Use of Schemas and Namespaces</a></li></ul><hr><ul class="alternate"><li><p>Search</p></li></ul><form enctype="application/x-www-form-urlencoded" method="get" id="cse-search-box" action="http://www.google.com/cse">
<div>
<input type="hidden" name="cx" value="002890367768291051730:o99qiwa09y4">
<input type="hidden" name="ie" value="UTF-8">
<input type="text" name="q" size="21">
<input type="submit" name="sa" value="Search">
</div>
</form>
<script type="text/javascript" src="http://www.google.com/cse/brand?form=cse-search-box&amp;lang=en"></script><hr><ul class="alternate"><li><a shape="rect" href="http://cxf.apache.org/javadoc/latest/">API 3.2.x (Javadoc)</a></li><li><a shape="rect" href="http://cxf.apache.org/javadoc/latest-3.1.x/">API 3.1.x (Javadoc)</a></li><li><a shape="rect" href="http://cxf.apache.org/">CXF Website</a></li></ul><p>&#160;</p><p><a shape="rect" class="external-link" href="http://www.apache.org/events/current-event.html"><span class="confluence-embedded-file-wrapper"><img class="confluence-embedded-image confluence-external-resource" src="http://www.apache.org/events/current-event-125x125.png" data-image-src="http://www.apache.org/events/current-event-125x125.png"></span></a></p></div>
<!-- NavigationBar -->
</div>
</div>
</div>
</div>
</td>
<td height="100%">
<!-- Content -->
<div class="wiki-content">
<div id="ConfluenceContent"><h1 id="MTOM-UsingMTOM">Using MTOM</h1><p>SOAP Message Transmission Optimization Mechanism (<a shape="rect" class="external-link" href="http://www.w3.org/TR/soap12-mtom/" rel="nofollow">MTOM</a>) 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:</p><ol><li>Annotate the data that you are going to send as an attachment.<br clear="none" class="atl-forced-newline"> You can annotate either your WSDL or the Java class that implements your data.</li><li>Enable the runtime's MTOM support.<br clear="none" class="atl-forced-newline"> This can be done either programatically or through configuration.</li><li>Develop a <code>DataHandler</code> for the data being passed as an attachment.</li></ol><h2 id="MTOM-AnnotatingDataTypestouseMTOM">Annotating Data Types to use MTOM</h2><h3 id="MTOM-Overview">Overview</h3><p>When defining a data type for passing along a block of binary data, such as an image file or a sound file, in WSDL you define the element for the data to be of type <code>xsd:base64Binary</code>. By default, any element of type <code>xsd:base64Binary</code> results in the generation of a <code>byte[]</code> which can be serialized using MTOM. However, the default behavior of the code generators does not take full advantage of the serialization.</p><p>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 may also involve changing the type specification of the field containing the binary data.</p><h3 id="MTOM-WSDLfirst">WSDL first</h3><p>The following example 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 for sending along as part of a normal SOAP message.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Message for MTOM</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;definitions name="XrayStorage"
targetNamespace="http://mediStor.org/x-rays"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://mediStor.org/x-rays"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:xsd1="http://mediStor.org/types/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
&lt;types&gt;
&lt;schema targetNamespace="http://mediStor.org/types/"
xmlns="http://www.w3.org/2001/XMLSchema"&gt;
&lt;complexType name="xRayType"&gt;
&lt;sequence&gt;
&lt;element name="patientName" type="xsd:string" /&gt;
&lt;element name="patientNumber" type="xsd:int" /&gt;
&lt;element name="imageData" type="xsd:base64Binary" /&gt;
&lt;/sequence&gt;
&lt;/complexType&gt;
&lt;element name="xRay" type="xsd1:xRayType" /&gt;
&lt;/schema&gt;
&lt;/types&gt;
&lt;message name="storRequest"&gt;
&lt;part name="record" element="xsd1:xRay"/&gt;
&lt;/message&gt;
&lt;message name="storResponse"&gt;
&lt;part name="success" type="xsd:boolean"/&gt;
&lt;/message&gt;
&lt;portType name="xRayStorage"&gt;
&lt;operation name="store"&gt;
&lt;input message="tns:storRequest" name="storRequest"/&gt;
&lt;output message="tns:storResponse" name="storResponse"/&gt;
&lt;/operation&gt;
&lt;/portType&gt;
&lt;binding name="xRayStorageSOAPBinding" type="tns:xRayStorage"&gt;
&lt;soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/&gt;
&lt;operation name="store"&gt;
&lt;soap12:operation soapAction="" style="document"/&gt;
&lt;input name="storRequest"&gt;
&lt;soap12:body use="literal"/&gt;
&lt;/input&gt;
&lt;output name="storResponse"&gt;
&lt;soap12:body use="literal"/&gt;
&lt;/output&gt;
&lt;/operation&gt;
&lt;/binding&gt;
...
&lt;/definitions&gt;
</pre>
</div></div><p>If you wanted to use MTOM to send the binary part of the message as an optimized attachment you would need to add the <code>xmime:expectedContentTypes</code> attribute to the element containing the binary data. This attribute is defined in the <code><a shape="rect" class="external-link" href="http://www.w3.org/2005/05/xmlmime" rel="nofollow">http://www.w3.org/2005/05/xmlmime</a></code> 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 will change how the code generators create the JAXB class for the data. For most MIME types, the code generator will create a <code>DataHandler</code>. Some MIME types, such as those for images, have defined mappings.</p><div class="confluence-information-macro confluence-information-macro-note"><span class="aui-icon aui-icon-small aui-iconfont-warning confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>The MIME types are maintained by the Internet Assigned Numbers Authority (IANA) and described in detail in <strong>Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</strong> (<a shape="rect" class="external-link" href="ftp://ftp.isi.edu/in-notes/rfc2045.txt" rel="nofollow">ftp://ftp.isi.edu/in-notes/rfc2045.txt</a>) and <strong>Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types</strong>(<a shape="rect" class="external-link" href="ftp://ftp.isi.edu/in-notes/rfc2046.txt" rel="nofollow">ftp://ftp.isi.edu/in-notes/rfc2046.txt</a>)</p></div></div><div class="confluence-information-macro confluence-information-macro-tip"><span class="aui-icon aui-icon-small aui-iconfont-approve confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>For most uses you would specify <code>application/octet-stream</code>.</p></div></div><p>The following example shows how you would modify <code>xRayType</code> to use MTOM.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Binary Data for MTOM</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">...
&lt;types&gt;
&lt;schema targetNamespace="http://mediStor.org/types/"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:xmime="http://www.w3.org/2005/05/xmlmime"&gt;
&lt;complexType name="xRayType"&gt;
&lt;sequence&gt;
&lt;element name="patientName" type="xsd:string" /&gt;
&lt;element name="patientNumber" type="xsd:int" /&gt;
&lt;element name="imageData" type="xsd:base64Binary"
xmime:expectedContentTypes="application/octet-stream"/&gt;
&lt;/sequence&gt;
&lt;/complexType&gt;
&lt;element name="xRay" type="xsd1:xRayType" /&gt;
&lt;/schema&gt;
&lt;/types&gt;
...
</pre>
</div></div><p>The generated JAXB class generated for <code>xRayType</code> will no longer contain a <code>byte[]</code>. Instead the code generator will see the <code>xmime:expectedContentTypes</code> attribute and generate a <code>DataHandler</code> for the <code>imageData</code> field.</p><div class="confluence-information-macro confluence-information-macro-note"><span class="aui-icon aui-icon-small aui-iconfont-warning confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>You do not need to change the binding element to use MTOM. The runtime will make the appropriate changes when the data is sent.</p></div></div><h3 id="MTOM-Javafirst">Java first</h3><p>If you are doing Java first development you can make your JAXB class MTOM ready by doing the following:</p><ol><li>Make sure the field holding the binary data is a <code>DataHandler</code>.</li><li>Add the <code>@XmlMimeType()</code> annotation to the field containing the data you want to be streamed as an MTOM attachment.</li></ol><p>The following example shows a JAXB class annotated for using MTOM.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>JAXB Class for MTOM</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">@XmlType
public class XRayType {
protected String patientName;
protected int patientNumber;
@XmlMimeType("application/octet-stream")
protected DataHandler imageData;
...
}
</pre>
</div></div><h2 id="MTOM-EnablingMTOM">Enabling MTOM</h2><p>By default the runtime does not enable MTOM support. It will send all binary data as either part of the normal SOAP message or as an unoptimized attachment. You can activate MTOM support either programatically or through the use of configuration.</p><h3 id="MTOM-UsingJAX-WSAPIs">Using JAX-WS APIs</h3><p>Both clients and servers need to have the MTOM optimizations enabled. The JAX-WS APIs offer different mechanisms for each type of endpoint.</p><h4 id="MTOM-server">server</h4><p>If you published your server using the JAX-WS APIs you enable the runtime's MTOM support as follows:</p><ol><li>Get access to the <code>Endpoint</code> object for your published service.<br clear="none" class="atl-forced-newline"> The easiest way to get the <code>Endpoint</code> object is when you publish the endpoint.</li><li><p>Get the SOAP binding from the <code>Endpoint</code> using its <code>getBinding()</code> method as shown below.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Getting the SOAP Binding from an Endpoint</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">// Endpoint ep is declared previously
SOAPBinding binding = (SOAPBinding)ep.getBinding();
</pre>
</div></div><p>You must cast the returned binding object to a <code>SOAPBinding</code> object in order to access the MTOM property.</p></li><li><p>Set the bindings MTOM enabled property to true using the binding's setMTOMEnabled() method as shown below.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Setting a Service Provider's MTOM Enabled Property</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">binding.setMTOMEnabled(true);
</pre>
</div></div></li></ol><h4 id="MTOM-Client">Client</h4><p>To MTOM enable a JAX-WS client you do the following:</p><ol><li>Cast the client's proxy to a <code>BindingProvider</code> object.</li><li><p>Get the SOAP binding from the <code>BindingProvider</code> using its <code>getBinding()</code> method as shown below.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Getting a SOAP Binding from a BindingProvider</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">// BindingProvider bp declared previously
SOAPBinding binding = (SOAPBinding)bp.getBinding();
</pre>
</div></div></li><li><p>Set the bindings MTOM enabled property to <code>true</code> using the binding's <code>setMTOMEnabled()</code> method as shown below.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Setting a Consumer's MTOM Enabled Property</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">binding.setMTOMEnabled(true);
</pre>
</div></div></li></ol><h3 id="MTOM-Usingconfiguration">Using configuration</h3><h4 id="MTOM-Overview.1">Overview</h4><p>If you publish your service using XML, such as when deploying into a container, you can enable your endpoint's MTOM support in the endpoint's configuration file.</p><h4 id="MTOM-ConfigFileProcedure"><span class="confluence-anchor-link" id="MTOM-ConfigFile"></span>Procedure</h4><p>The MTOM property is set inside the <code>jaxws:endpoint</code> element for your endpoint. To enable MTOM do the following:</p><ol><li>Add a <code>jaxws:property</code> child element to the endpoint's <code>jaxws:endpoint</code> element.</li><li>Add a <code>entry</code> child element to the <code>jaxws:property</code> element.</li><li>Set the <code>entry</code> element's <code>key</code> attribute to <code>mtom-enabled</code>.</li><li>Set the <code>entry</code> element's <code>value</code> attribute to <code>true</code>.</li></ol><h4 id="MTOM-Example">Example</h4><p>The following example shows an endpoint that is MTOM enabled.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Configuration for Enabling MTOM</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default">&lt;beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"&gt;
&lt;jaxws:endpoint id="xRayStorage"
implementor="demo.spring.xRayStorImpl"
address="http://localhost/xRayStorage"&gt;
&lt;jaxws:properties&gt;
&lt;entry key="mtom-enabled" value="true"/&gt;
&lt;/jaxws:properties&gt;
&lt;/jaxws:endpoint&gt;
&lt;/beans&gt;
</pre>
</div></div></div>
</div>
<!-- Content -->
</td>
</tr>
</table>
</td>
<td id="cell-2-2" colspan="2">&nbsp;</td>
</tr>
<tr>
<td id="cell-3-0">&nbsp;</td>
<td id="cell-3-1">&nbsp;</td>
<td id="cell-3-2">
<div id="footer">
<!-- Footer -->
<div id="site-footer">
<a href="http://cxf.apache.org/privacy-policy.html">Privacy Policy</a> -
(<a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=60618">edit page</a>)
(<a href="https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=60618&amp;showComments=true&amp;showCommentArea=true#addcomment">add comment</a>)<br>
Apache CXF, CXF, Apache, the Apache feather logo are trademarks of The Apache Software Foundation.<br>
All other marks mentioned may be trademarks or registered trademarks of their respective owners.
</div>
<!-- Footer -->
</div>
</td>
<td id="cell-3-3">&nbsp;</td>
<td id="cell-3-4">&nbsp;</td>
</tr>
<tr>
<td id="cell-4-0" colspan="2">&nbsp;</td>
<td id="cell-4-1">&nbsp;</td>
<td id="cell-4-2" colspan="2">&nbsp;</td>
</tr>
</table>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-4458903-1");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>