The SOAP messaging protocol allows you to send MIME attachments via SOAP messages. WSDL provides a description of these attachments. JAX-RPC provides a mapping of the WSDL description of attachments into Java artifacts. This tip describes how to use those JAX-RPC mappings to send attachments in SOAP messages.
The WSDL description of attachments is, unfortunately, not particularly straightforward. Take a look at the WSDL in Listing 1, particularly the highlighted elements of the binding.
Listing 1. WSDL with attachments
<?xml version="1.0" encoding="utf-8"?>
<definitions
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
targetNamespace="urn:attachment.tip"
xmlns:tns="urn:attachment.tip">
<message name="empty"/>
<message name="imageMsg">
<part name="image" type="xsd:hexBinary"/>
</message>
<message name="octetMsg">
<part name="octet" type="xsd:hexBinary"/>
</message>
<portType name="AttachmentTip">
<operation name="sendImage">
<input message="tns:imageMsg"/>
<output message="tns:empty"/>
</operation>
<operation name="sendOctet">
<input message="tns:octetMsg"/>
<output message="tns:empty"/>
</operation>
</portType>
<binding name="AttachmentBinding" type="tns:AttachmentTip">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="sendImage">
<soap:operation soapAction=""/>
<input>
<mime:multipartRelated>
<mime:part>
<soap:body use="literal"/>
</mime:part>
<mime:part>
<mime:content part="image" type="image/jpeg"/>
</mime:part>
</mime:multipartRelated>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="sendOctet">
<soap:operation soapAction=""/>
<input>
<mime:multipartRelated>
<mime:part>
<soap:body use="literal"/>
</mime:part>
<mime:part>
<mime:content part="octet" type="application/octet-stream"/>
</mime:part>
</mime:multipartRelated>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="AttachmentService">
<port name="AttachmentTip" binding="tns:AttachmentBinding">
<soap:address
location="http://localhost:9080/attachment/services/AttachmentTip"/>
</port>
</service>
</definitions> |
The first thing to note is that there is no MIME information until you get all the way down to the binding. If you ignore the binding for a moment and only look at the WSDL's 'interface' (portType and messages), you'll see two operations - sendImage and sendOctet. Each of these has a hexBinary input. hexBinary maps to byte[]. Using only this information, you could reasonably guess that this portType would map to a Java language Service Endpoint Interface (SEI) which would have two methods, each of which would have a single byte[] parameter. This is a good guess, and for anything but MIME types, you'd be correct. But for MIME types you must inspect the binding to determine the real parameter types.
In the binding you'll see that the inputs for the operations are really MIME content types -- image/jpeg and application/octet-stream -- instead of hexBinary. JAX-RPC defines these to map to java.awt.Image and javax.activation.DataHandler, respectively.
The Service Endpoint Interface
Listing 2 contains a Java language SEI generated from the WSDL in Listing 1.
Listing 2. The AttachmentTip SEI
package tip.attachment;
import java.awt.Image;
import java.rmi.Remote;
import java.rmi.RemoteException;
import javax.activation.DataHandler;
public interface AttachmentTip extends Remote {
public void sendImage(Image image) throws RemoteException;
public void sendOctet(DataHandler octet) throws RemoteException;
} |
The WSDL's sendImage operation, which the binding says has a MIME content of image/jpeg, becomes a Java program method with a java.awt.Image parameter. Very straightforward. Nice and tidy. Table 1 lists all of the nice-and-tidy mappings of MIME types to Java types.
Table 1 - JAX-RPC mappings of MIME types to Java types
| MIME Type | Java Type |
| image/gif, image/jpeg | java.awt.Image |
| text/plain | java.lang.String |
| multipart/* | javax.mail.internet.MimeMultipart |
| text/xml, application/xml | javax.xml.transform.Source |
If you define a MIME type that is not in this table, as I did in the sendOctet operation, then a JAX-RPC implementation will map your type to javax.activation.DataHandler. The DataHandler type is defined in the Java Activation Framework (JAF -- see more details below, and also a link to the JAF pages in the Resources section).
From this point on, I assume that you have used your favorite JAX-RPC implementation to generate all the client-side mappings from the AttachmentTip WSDL. The client implementation will depend on these mappings, particularly the AttachmentTip SEI from Listing 2.
The client implementation of attachments must first acquire an implementation of the SEI from a Service. You can see how this is done in Listing 3 in the getTip method (a discussion of the ServiceFactory and Service is beyond the scope of this tip, see Resources for more information).
Once you have an SEI implementation, you can call its methods with your attachment (which for this example is in a file whose name you provide). In the case of sendImage, that attachment is a java.awt.Image. That's all there is to it. The JAX-RPC implementation knows that all images are sent as attachments and it constructs the SOAP message accordingly. The client programmer need not even be aware that attachments are involved. See the highlighted code in Listing 3 for the call to sendImage.
Listing 3. The AttachmentTip client implementation to call sendImage
package tip.attachment;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
public class AttachmentTipClient {
static AttachmentTip getTip() throws Exception {
QName serviceName = new QName(
"urn:attachment.tip",
"AttachmentService");
URL wsdlLocation = new URL(
"http://localhost:9080/attachment/services/AttachmentTip?wsdl");
ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(wsdlLocation, serviceName);
QName portName = new QName("", "AttachmentTip");
return (AttachmentTip) service.getPort(
portName,
AttachmentTip.class);
}
static void sendImage(AttachmentTip tip, String fileName)
throws RemoteException {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.createImage(fileName);
tip.sendImage(image);
}
public static void main(String[] args) {
try {
AttachmentTip tip = getTip();
sendImage(tip, args[0]);
}
catch (Throwable t) {
t.printStackTrace();
}
}
} |
For those few MIME types for which JAX-RPC has defined a mapping (see Table 1), life is fairly easy. For those MIME types for which there is no explicit JAX-RPC mapping, you're left with a javax.activation.DataHandler object. This class is part of the Java Activation Framework (JAF -- see Resources), so you have to know something about the JAF, but it's not too difficult. A DataHandler contains a javax.activation.DataSource which, in turn, contains input and output streams. Most Java programming types can be converted fairly easily to/from streams, so this is fairly straightforward. Moreover, the activation framework itself even helps a little. If your attachment data is in a file, as this one is, the activation framework provides the javax.activation.FileDataSource class, so you can bypass the stream step. Listing 4 is the code from Listing 3 with the addition of a new method to call sendOctet and pass it a DataHandler.
Listing 4. The complete AttachmentTip client implementation
package tip.attachment;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.rmi.RemoteException;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
public class AttachmentTipClient {
static AttachmentTip getTip() throws Exception {
QName serviceName = new QName(
"urn:attachment.tip",
"AttachmentService");
URL wsdlLocation = new URL(
"http://localhost:9080/attachment/services/AttachmentTip?wsdl");
ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(wsdlLocation, serviceName);
QName portName = new QName("", "AttachmentTip");
return (AttachmentTip) service.getPort(
portName,
AttachmentTip.class);
}
static void sendImage(AttachmentTip tip, String fileName)
throws RemoteException {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.createImage(fileName);
tip.sendImage(image);
}
static void sendOctet(AttachmentTip tip, String fileName)
throws RemoteException {
FileDataSource fds = new FileDataSource(fileName);
DataHandler dh = new DataHandler(fds);
tip.sendOctet(dh);
}
public static void main(String[] args) {
try {
AttachmentTip tip = getTip();
sendImage(tip, args[0]);
sendOctet(tip, args[0]);
}
catch (Throwable t) {
t.printStackTrace();
}
}
} |
Note that I used the same file as the data for both attachment operations, but as far as sendOctet is concerned, the contents of the file is merely an application/octet-stream. It doesn't know, or care, that it's really an image. application/octet-stream is the default content type for FileDataSource, which is why I used it. It did everything for me. If you aren't so lucky to have your data in a file, and you have to send it as something other than an application/octet-stream, then you have to create your own implementation of DataSource, but it's a simple interface, so it shouldn't take too much effort. (This is left as an exercise to the reader!)
As I've shown, sending attachments via JAX-RPC mappings is a fairly simple matter. The application programmer doesn't have to be aware of attachments at all. At worst, the programmer must be aware of DataHandlers, DataSources, and streams.
| Name | Size | Download method |
|---|---|---|
| ws-tip-soapjax.zip | 40.0 KB | HTTP |
Information about download methods
- Download this article's zip file, which contains a service ear file, client code, WSDL, and an input image file used in this article.
- For a more information on
ServiceFactoryandService, read Tip: Send and receive SOAP messages with JAX-RPC (developerWorks, September 2003). - Find out more about passing binary data to a Web service in this tip (developerWorks, February 2004).
- Read the Web Services Description Language (WSDL) 1.1, the specification of WSDL.
- Get the IBM WebSphere Application Server Technology for Developers, version 6.0, an early release of WebSphere Application Server supporting the latest J2EE, version 1.4.
- Java API for XML-Based RPC (JAX-RPC) Downloads & Specifications provides links to the JAX-RPC 1.1 specification itself, as well as javadocs, class files, and Sun's JAX-RPC reference implementation.
- Browse the WS-I (Web Services Interoperability) organization's Web pages.
- Browse the Java Activation Framework pages, where you can download the framework, its javadocs, and demos.
Russell Butek is one of the developers of the IBM WebSphere Web services engine. He is also an IBM representative on the JAX-RPC Java Specification Request (JSR) expert group. He was involved in the implementation of Apache's AXIS SOAP engine, driving AXIS 1.0 to comply with JAX-RPC 1.0. Previously, he was a developer of the IBM CORBA ORB and an IBM representative on a number of OMG task forces: the portable interceptor task force (of which he was chair), the core task force, and the interoperability task force.




