Web services programming tips and tricks: SOAP attachments with JAX-RPC

来源:百度文库 编辑:神马文学网 时间:2024/05/02 11:27:21

The SOAP messaging protocol allows you to send MIME attachments viaSOAP messages. WSDL provides a description of these attachments.JAX-RPC provides a mapping of the WSDL description of attachments intoJava artifacts. This tip describes how to use those JAX-RPC mappings tosend attachments in SOAP messages.

The WSDL

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
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

WS-I and attachments

As the paragraphs to the left describe, MIME types cannot be described by WSDL's 'interface'. The WS-I organization (see Resources)is attempting to repair this shortcoming, at least partially, byintroducing a new XML type for all MIME types: wsi:swaRef. But at thetime of this writing, this type is still in the works, and nostandardized Java language binding exists for it.

Thefirst thing to note is that there is no MIME information until you getall the way down to the binding. If you ignore the binding for a momentand only look at the WSDL's 'interface' (portType and messages), you'llsee 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 thisportType 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 todetermine 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).


The client implementation

Fromthis point on, I assume that you have used your favorite JAX-RPCimplementation to generate all the client-side mappings from theAttachmentTip WSDL. The client implementation will depend on thesemappings, particularly the AttachmentTip SEI from Listing 2.

Theclient implementation of attachments must first acquire animplementation of the SEI from a Service. You can see how this is donein 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).

Call the sendImage method

Onceyou have an SEI implementation, you can call its methods with yourattachment (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 allimages are sent as attachments and it constructs the SOAP messageaccordingly. The client programmer need not even be aware thatattachments 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();            }            }            }

Call the sendOctet method

A caveat about mapping non-specified MIME types

JAX-RPC does not require an implementation to support MIME types which do not appear in Table 1. Implementations may not map unspecified MIME types to DataHandler. For instance, WebSphere Web services does not support this mapping until version 6.0 of the Application Server.

Forthose few MIME types for which JAX-RPC has defined a mapping (see Table1), life is fairly easy. For those MIME types for which there is noexplicit 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.DataSourcewhich, in turn, contains input and output streams. Most Javaprogramming types can be converted fairly easily to/from streams, sothis is fairly straightforward. Moreover, the activation frameworkitself even helps a little. If your attachment data is in a file, asthis 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 solucky to have your data in a file, and you have to send it as somethingother 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!)