package org.jboss.test.webservice.attachment;
import junit.framework.Test;
import org.jboss.test.JBossTestCase;
import javax.activation.DataHandler;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
public class AttachmentSAAJTestCase extends JBossTestCase
{
private static final String NS_PREFIX = "ns1";
private static final String NS_URI = "http://org.jboss.webservice/attachment";
private static final String CID_MIMEPART = "cid:mimepart@jboss.org";
public AttachmentSAAJTestCase(String name)
{
super(name);
}
public void testSendMimeImageGIF() throws Exception
{
String rpcMethodName = "sendMimeImageGIF";
SOAPMessage msg = setupMimeMessage(rpcMethodName);
URL url = new File("resources/webservice/attachment/attach.gif").toURL();
Image image = null;
try
{
image = Toolkit.getDefaultToolkit().createImage(url);
}
catch (Throwable th)
{
log.warn("Cannot create Image: " + th);
}
if (image != null)
{
addAttachmentPart(msg, "image/gif", new DataHandler(url));
sendAndValidateMimeMessage(rpcMethodName, msg);
}
}
public void testSendMimeImageJPEG() throws Exception
{
String rpcMethodName = "sendMimeImageJPEG";
SOAPMessage msg = setupMimeMessage(rpcMethodName);
URL url = new File("resources/webservice/attachment/attach.jpeg").toURL();
Image image = null;
try
{
image = Toolkit.getDefaultToolkit().createImage(url);
}
catch (Throwable th)
{
log.warn("Cannot create Image: " + th);
}
if (image != null)
{
addAttachmentPart(msg, "image/jpeg", new DataHandler(url));
sendAndValidateMimeMessage(rpcMethodName, msg);
}
}
public void testSendMimeTextPlain() throws Exception
{
String rpcMethodName = "sendMimeTextPlain";
SOAPMessage msg = setupMimeMessage(rpcMethodName);
URL url = new File("resources/webservice/attachment/attach.txt").toURL();
addAttachmentPart(msg, "text/plain", new DataHandler(url));
sendAndValidateMimeMessage(rpcMethodName, msg);
}
public void testSendMimeMultipart() throws Exception
{
String rpcMethodName = "sendMimeMultipart";
SOAPMessage msg = setupMimeMessage(rpcMethodName);
URL url = new File("resources/webservice/attachment/attach.txt").toURL();
MimeMultipart multipart = new MimeMultipart("mixed");
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(url));
String bpct = bodyPart.getContentType();
bodyPart.setHeader("Content-Type", bpct);
multipart.addBodyPart(bodyPart);
multipart.writeTo(new FileOutputStream(rpcMethodName + "_Multipart.txt"));
String contentType = multipart.getContentType();
AttachmentPart ap = msg.createAttachmentPart(multipart, contentType);
ap.setContentId(CID_MIMEPART);
msg.addAttachmentPart(ap);
sendAndValidateMimeMessage(rpcMethodName, msg);
}
public void testSendMimeTextXML() throws Exception
{
String rpcMethodName = "sendMimeTextXML";
SOAPMessage msg = setupMimeMessage(rpcMethodName);
URL url = new File("resources/webservice/attachment/attach.xml").toURL();
addAttachmentPart(msg, "text/xml", new DataHandler(url));
sendAndValidateMimeMessage(rpcMethodName, msg);
}
public void testSendMimeApplicationXML() throws Exception
{
String rpcMethodName = "sendMimeApplicationXML";
SOAPMessage msg = setupMimeMessage(rpcMethodName);
URL url = new File("resources/webservice/attachment/attach.xml").toURL();
addAttachmentPart(msg, "application/xml", new DataHandler(url));
sendAndValidateMimeMessage(rpcMethodName, msg);
}
private SOAPMessage setupMimeMessage(String rpcMethodName)
throws Exception
{
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();
SOAPPart sp = msg.getSOAPPart();
SOAPEnvelope envelope = sp.getEnvelope();
SOAPBody bdy = envelope.getBody();
SOAPBodyElement sbe = bdy.addBodyElement(envelope.createName(rpcMethodName, NS_PREFIX, NS_URI));
sbe.addChildElement(envelope.createName("message")).addTextNode("Some text message");
sbe.addChildElement(envelope.createName("mimepart")).addAttribute(envelope.createName("href"), CID_MIMEPART);
return msg;
}
private void addAttachmentPart(SOAPMessage msg, String contentType, DataHandler dataHandler)
{
AttachmentPart ap = msg.createAttachmentPart(dataHandler);
ap.setContentType(contentType);
ap.setContentId(CID_MIMEPART);
msg.addAttachmentPart(ap);
}
private void sendAndValidateMimeMessage(String rpcMethodName, SOAPMessage msg)
throws SOAPException, MalformedURLException
{
SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
SOAPConnection con = conFactory.createConnection();
SOAPMessage resMessage = con.call(msg, new URL("http://" + getServerHost() + ":8080/ws4ee-attachment"));
SOAPBody soapBody = resMessage.getSOAPBody();
SOAPEnvelope soapEnvelope = (SOAPEnvelope)soapBody.getParentElement();
Name rpcName = soapEnvelope.createName(rpcMethodName + "Response", NS_PREFIX, NS_URI);
Iterator childElements = soapBody.getChildElements(rpcName);
assertTrue("Expexted child: " + rpcName, childElements.hasNext());
SOAPElement bodyChild = (SOAPElement)childElements.next();
Name resName = soapEnvelope.createName("response");
SOAPElement resElement = (SOAPElement)bodyChild.getChildElements(resName).next();
String value = resElement.getValue();
System.out.println(value);
assertEquals("[pass]", value);
}
public static Test suite() throws Exception
{
return getDeploySetup(AttachmentSAAJTestCase.class, "ws4ee-attachment.war");
}
}