1 16 17 package test.saaj; 18 19 import junit.framework.TestCase; 20 import org.apache.axis.components.logger.LogFactory; 21 import org.apache.commons.logging.Log; 22 23 import javax.activation.DataHandler ; 24 import javax.activation.FileDataSource ; 25 import javax.xml.soap.AttachmentPart ; 26 import javax.xml.soap.MessageFactory ; 27 import javax.xml.soap.MimeHeaders ; 28 import javax.xml.soap.SOAPBody ; 29 import javax.xml.soap.SOAPElement ; 30 import javax.xml.soap.SOAPEnvelope ; 31 import javax.xml.soap.SOAPHeader ; 32 import javax.xml.soap.SOAPMessage ; 33 import javax.xml.soap.SOAPPart ; 34 import java.io.ByteArrayInputStream ; 35 import java.io.ByteArrayOutputStream ; 36 import java.io.File ; 37 import java.io.InputStream ; 38 import java.io.OutputStream ; 39 40 41 43 public class TestAttachmentSerialization extends TestCase { 44 static Log log = LogFactory.getLog(TestAttachmentSerialization.class.getName()); 45 46 public TestAttachmentSerialization(String name) { 47 super(name); 48 } 49 50 public static void main(String args[]) throws Exception { 51 TestAttachmentSerialization tester = new TestAttachmentSerialization("tester"); 52 tester.testAttachments(); 53 } 54 55 public void testAttachments() throws Exception { 56 try { 57 ByteArrayOutputStream bais = new ByteArrayOutputStream (); 58 int count1 = saveMsgWithAttachments(bais); 59 int count2 = loadMsgWithAttachments(new ByteArrayInputStream (bais.toByteArray())); 60 assertEquals(count1, count2); 61 } catch (Exception e) { 62 e.printStackTrace(); 63 throw new Exception ("Fault returned from test: " + e); 64 } 65 } 66 67 public static final String MIME_MULTIPART_RELATED = "multipart/related"; 68 public static final String MIME_APPLICATION_DIME = "application/dime"; 69 public static final String NS_PREFIX = "jaxmtst"; 70 public static final String NS_URI = "http://www.jcommerce.net/soap/jaxm/TestJaxm"; 71 72 public int saveMsgWithAttachments(OutputStream os) throws Exception { 73 MessageFactory mf = MessageFactory.newInstance(); 74 SOAPMessage msg = mf.createMessage(); 75 76 SOAPPart sp = msg.getSOAPPart(); 77 SOAPEnvelope envelope = sp.getEnvelope(); 78 SOAPHeader header = envelope.getHeader(); 79 SOAPBody body = envelope.getBody(); 80 81 SOAPElement el = header.addHeaderElement(envelope.createName("field4", NS_PREFIX, NS_URI)); 82 SOAPElement el2 = el.addChildElement("field4b", NS_PREFIX); 83 SOAPElement el3 = el2.addTextNode("field4value"); 84 85 el = body.addBodyElement(envelope.createName("bodyfield3", NS_PREFIX, NS_URI)); 86 el2 = el.addChildElement("bodyfield3a", NS_PREFIX); 87 el2.addTextNode("bodyvalue3a"); 88 el2 = el.addChildElement("bodyfield3b", NS_PREFIX); 89 el2.addTextNode("bodyvalue3b"); 90 el2 = el.addChildElement("datefield", NS_PREFIX); 91 92 AttachmentPart ap = msg.createAttachmentPart(); 93 ap.setContent("some attachment text...", "text/plain"); 94 msg.addAttachmentPart(ap); 95 96 String jpgfilename = "docs/images/axis.jpg"; 97 File myfile = new File (jpgfilename); 98 FileDataSource fds = new FileDataSource (myfile); 99 DataHandler dh = new DataHandler (fds); 100 AttachmentPart ap2 = msg.createAttachmentPart(dh); 101 ap2.setContentType("image/jpg"); 102 msg.addAttachmentPart(ap2); 103 104 if(msg.saveRequired()) { 106 msg.saveChanges(); 107 } 108 MimeHeaders headers = msg.getMimeHeaders(); 109 assertTrue(headers != null); 110 String [] contentType = headers.getHeader("Content-Type"); 111 assertTrue(contentType != null); 112 113 msg.writeTo(os); 114 os.flush(); 115 return msg.countAttachments(); 116 } 117 118 public int loadMsgWithAttachments(InputStream is) throws Exception { 119 MimeHeaders headers = new MimeHeaders (); 120 headers.setHeader("Content-Type", MIME_MULTIPART_RELATED); 121 MessageFactory mf = MessageFactory.newInstance(); 122 SOAPMessage msg = mf.createMessage(headers, is); 123 SOAPPart sp = msg.getSOAPPart(); 124 SOAPEnvelope envelope = sp.getEnvelope(); 125 assertTrue(sp != null); 126 assertTrue(envelope != null); 127 return msg.countAttachments(); 128 } 129 } 130 131 132 | Popular Tags |