The SOAP with Attachments API for Java (SAAJ) is a new Java API, which describes a standard way of dealing with SOAP messages from the Java language. In particular, SAAJ defines a SOAP message, which can carry data that conforms to the SOAP specification.
The Novell exteNd Messaging Platform's JMS supports a special
JMQSOAPMessage,
which can carry a SAAJ SOAPMessage
message as its payload. This makes it easy
to send SAAJ messages.
The example included here shows how to send a SOAP message with a GIF image
as its attachment. When the client receives the JMQSOAPMessage
it unwraps the contained SAAJ SOAPMessage
and displays the
attachment part in a small applet.
Below is the source code for the Client
class:
package saaj; import java.net.URL; import java.awt.Image; import java.awt.Toolkit; import java.awt.Graphics; import java.awt.Dimension; import java.util.Iterator; import java.io.InputStream; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.QueueReceiver; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.swing.JFrame; import javax.swing.JApplet; import javax.naming.InitialContext; import javax.activation.DataHandler; import javax.xml.soap.Name; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.MessageFactory; import javax.xml.soap.AttachmentPart; import com.sssw.jms.api.JMQSession; import com.sssw.jms.api.JMQSOAPMessage; /** This client sends a SAAJ SOAP message using the jBroker MQ JMQSOAPMessage wrapper. On receipt the GIF attachment part of the message is displayed. */ public class Client extends JApplet { public static void main(String[] args) throws Exception { | // bootstrap queue connection factory and queue | InitialContext ctx = new InitialContext(); | QueueConnectionFactory factory = | (QueueConnectionFactory) ctx.lookup("queue/connectionFactory"); | Queue queue = (Queue) ctx.lookup("queue/queue0"); | | // create and start a connection | QueueConnection conn = factory.createQueueConnection(); | conn.start(); | | // create sender and receiver | int mode = Session.AUTO_ACKNOWLEDGE; | QueueSession session = conn.createQueueSession(false, mode); | QueueSender sender = session.createSender(queue); | QueueReceiver receiver = session.createReceiver(queue); | | // create a SOAP message wrapper | JMQSession jses = (JMQSession) session; | JMQSOAPMessage m1 = jses.createSOAPMessage(); | | // create and send the SOAP message | SOAPMessage s1 = getMessage(); | m1.setMessage(s1); | sender.send(m1); | | // receive the SOAP message | JMQSOAPMessage m2 = (JMQSOAPMessage) receiver.receive(); | SOAPMessage s2 = m2.getMessage(); | conn.close(); | | // display attachment part | Iterator iter = s1.getAttachments(); | AttachmentPart part = (AttachmentPart) iter.next(); | DataHandler handler = part.getDataHandler(); | InputStream is = handler.getInputStream(); | byte[] data = new byte[is.available()]; | is.read(data); | Image logo = Toolkit.getDefaultToolkit().createImage(data); | JApplet applet = new Client(logo); | JFrame frame = new JFrame(); | frame.getContentPane().add(applet); | frame.setSize(new Dimension(245,135)); | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | frame.setVisible(true); } private final Image _logo; public Client(Image logo) { | _logo = logo; } public void paint(Graphics g) { | g.drawImage(_logo, 0, 0, this); } private static SOAPMessage getMessage() throws Exception { | // construct SOAP message to send | MessageFactory factory = MessageFactory.newInstance(); | SOAPMessage message = factory.createMessage(); | SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); | envelope.addNamespaceDeclaration("ns", "http://www.example.com"); | SOAPBody body = envelope.getBody(); | Name name = envelope.createName("sayHello", "ns", | "http://www.example.com"); | SOAPElement child = body.addChildElement(name); | | // add a gif | URL url = new URL("file:jbrokerlogo.gif"); | DataHandler handler = new DataHandler(url); | AttachmentPart part = message.createAttachmentPart(handler); | message.addAttachmentPart(part); | | message.saveChanges(); | | return message; } }
In order to run this example, you need an SAAJ provider such as the one included
with the Novell exteNd Web Services SDK. You can download this from the Novell
web site. Ensure that the
wssdk.jar
and activation.jar
files are in the CLASSPATH
of both the client application and the JMS server.
Copyright © 2003, 2004 Novell, Inc. All rights reserved. Copyright © 2001, 2002, 2003 SilverStream Software, LLC. All rights reserved.