1 22 package org.jboss.test.webservice.attachment; 23 24 import java.awt.Image ; 25 import java.awt.Toolkit ; 26 import java.io.File ; 27 import java.net.URL ; 28 import java.rmi.RemoteException ; 29 30 import javax.activation.DataHandler ; 31 import javax.activation.DataSource ; 32 import javax.activation.FileDataSource ; 33 import javax.mail.internet.MimeBodyPart ; 34 import javax.mail.internet.MimeMultipart ; 35 import javax.xml.namespace.QName ; 36 import javax.xml.rpc.Call ; 37 import javax.xml.rpc.ParameterMode ; 38 import javax.xml.rpc.Service ; 39 import javax.xml.rpc.ServiceFactory ; 40 41 import junit.framework.Test; 42 43 import org.jboss.test.webservice.WebserviceTestBase; 44 45 53 public class AttachmentDIITestCase extends WebserviceTestBase 54 { 55 private static final String NS_URI = "http://org.jboss.webservice/attachment"; 56 private static final QName SERVICE_NAME = new QName (NS_URI, "Attachment"); 57 private static final QName PORT_NAME = new QName (NS_URI, "AttachmentPort"); 58 59 private static final QName XSD_STRING = new QName ("http://www.w3.org/2001/XMLSchema", "string"); 60 private static final QName AXIS_MIME_IMAGE = new QName ("http://xml.apache.org/xml-soap", "Image"); 61 private static final QName AXIS_MIME_PLAINTEXT = new QName ("http://xml.apache.org/xml-soap", "PlainText"); 62 private static final QName AXIS_MIME_MULTIPART = new QName ("http://xml.apache.org/xml-soap", "Multipart"); 63 private static final QName AXIS_MIME_SOURCE = new QName ("http://xml.apache.org/xml-soap", "Source"); 64 65 private static final String JBOSS_MIME_NS = "http://www.jboss.org/jbossws/attachment/mimetype"; 66 private static final QName JBOSS_MIME_IMAGE_JPEG = new QName (JBOSS_MIME_NS, "image_jpeg"); 67 private static final QName JBOSS_MIME_IMAGE_GIF = new QName (JBOSS_MIME_NS, "image_gif"); 68 private static final QName JBOSS_MIME_MULTIPART_MIXED = new QName (JBOSS_MIME_NS, "multipart_mixed"); 69 private static final QName JBOSS_MIME_TEXT_PLAIN = new QName (JBOSS_MIME_NS, "text_plain"); 70 private static final QName JBOSS_MIME_TEXT_XML = new QName (JBOSS_MIME_NS, "text_xml"); 71 private static final QName JBOSS_MIME_APPLICATION_XML = new QName (JBOSS_MIME_NS, "application_xml"); 72 73 private final QName MIME_IMAGE_JPEG; 74 private final QName MIME_IMAGE_GIF; 75 private final QName MIME_MULTIPART_MIXED; 76 private final QName MIME_TEXT_PLAIN; 77 private final QName MIME_TEXT_XML; 78 private final QName MIME_APPLICATION_XML; 79 80 public AttachmentDIITestCase(String name) 81 { 82 super(name); 83 84 if (isWS4EEAvailable()) 85 { 86 MIME_IMAGE_GIF = AXIS_MIME_IMAGE; 87 MIME_IMAGE_JPEG = AXIS_MIME_IMAGE; 88 MIME_MULTIPART_MIXED = AXIS_MIME_MULTIPART; 89 MIME_TEXT_PLAIN = AXIS_MIME_PLAINTEXT; 90 MIME_TEXT_XML = AXIS_MIME_SOURCE; 91 MIME_APPLICATION_XML = AXIS_MIME_SOURCE; 92 } 93 else 94 { 95 MIME_IMAGE_GIF = JBOSS_MIME_IMAGE_GIF; 96 MIME_IMAGE_JPEG = JBOSS_MIME_IMAGE_JPEG; 97 MIME_MULTIPART_MIXED = JBOSS_MIME_MULTIPART_MIXED; 98 MIME_TEXT_PLAIN = JBOSS_MIME_TEXT_PLAIN; 99 MIME_TEXT_XML = JBOSS_MIME_TEXT_XML; 100 MIME_APPLICATION_XML = JBOSS_MIME_APPLICATION_XML; 101 } 102 } 103 104 105 public static Test suite() throws Exception 106 { 107 return getDeploySetup(AttachmentDIITestCase.class, "ws4ee-attachment.war"); 108 } 109 110 112 public void testSendMimeImageGIF() throws Exception 113 { 114 String rpcMethodName = "sendMimeImageGIF"; 115 Call call = setupMimeMessage(rpcMethodName, "image/gif"); 116 117 URL url = new File ("resources/webservice/attachment/attach.gif").toURL(); 118 119 Image image = null; 123 try 124 { 125 image = Toolkit.getDefaultToolkit().createImage(url); 126 } 127 catch (Throwable th) 128 { 129 log.warn("Cannot create Image: " + th); 130 } 131 132 if (image != null) 133 { 134 sendAndValidateMimeMessage(call, new DataHandler (url)); 135 } 136 } 137 138 140 public void testSendMimeImageJPEG() throws Exception 141 { 142 String rpcMethodName = "sendMimeImageJPEG"; 143 Call call = setupMimeMessage(rpcMethodName, "image/jpeg"); 144 145 URL url = new File ("resources/webservice/attachment/attach.jpeg").toURL(); 146 147 Image image = null; 151 try 152 { 153 image = Toolkit.getDefaultToolkit().createImage(url); 154 } 155 catch (Throwable th) 156 { 157 log.warn("Cannot create Image: " + th); 158 } 159 160 if (image != null) 161 { 162 sendAndValidateMimeMessage(call, new DataHandler (url)); 163 } 164 } 165 166 168 public void testSendMimeTextPlain() throws Exception 169 { 170 String rpcMethodName = "sendMimeTextPlain"; 171 Call call = setupMimeMessage(rpcMethodName, "text/plain"); 172 173 URL url = new File ("resources/webservice/attachment/attach.txt").toURL(); 174 sendAndValidateMimeMessage(call, new DataHandler (url)); 175 } 176 177 179 public void testSendMimeMultipart() throws Exception 180 { 181 String rpcMethodName = "sendMimeMultipart"; 182 Call call = setupMimeMessage(rpcMethodName, "multipart/mixed"); 183 184 URL url = new File ("resources/webservice/attachment/attach.txt").toURL(); 185 MimeMultipart mimepart = new MimeMultipart ("mixed"); 186 MimeBodyPart bodyPart = new MimeBodyPart (); 187 bodyPart.setDataHandler(new DataHandler (url)); 188 String bpct = bodyPart.getContentType(); 189 bodyPart.setHeader("Content-Type", bpct); 190 mimepart.addBodyPart(bodyPart); 191 192 sendAndValidateMimeMessage(call, mimepart); 193 } 194 195 197 public void testSendMimeTextXML() throws Exception 198 { 199 String rpcMethodName = "sendMimeTextXML"; 200 Call call = setupMimeMessage(rpcMethodName, "text/xml"); 201 202 DataSource ds = new ExFileDataSource("resources/webservice/attachment/attach.xml", "text/xml"); 203 sendAndValidateMimeMessage(call, new DataHandler (ds)); 204 } 205 206 208 public void testSendMimeApplicationXML() throws Exception 209 { 210 String rpcMethodName = "sendMimeApplicationXML"; 211 Call call = setupMimeMessage(rpcMethodName, "application/xml"); 212 213 DataSource ds = new ExFileDataSource("resources/webservice/attachment/attach.xml", "application/xml"); 214 sendAndValidateMimeMessage(call, new DataHandler (ds)); 215 } 216 217 219 private Call setupMimeMessage(String rpcMethodName, String contentType) 220 throws Exception 221 { 222 ServiceFactory factory = ServiceFactory.newInstance(); 223 Service service = factory.createService(SERVICE_NAME); 224 225 Call call = service.createCall(); 226 call.setOperationName(new QName (NS_URI, rpcMethodName)); 227 call.addParameter("message", XSD_STRING, ParameterMode.IN); 228 229 if (contentType.equals("image/gif")) 230 call.addParameter("mimepart", MIME_IMAGE_GIF, ParameterMode.IN); 231 else if (contentType.equals("image/jpeg")) 232 call.addParameter("mimepart", MIME_IMAGE_JPEG, ParameterMode.IN); 233 else if (contentType.equals("text/plain")) 234 call.addParameter("mimepart", MIME_TEXT_PLAIN, ParameterMode.IN); 235 else if (contentType.startsWith("multipart/")) 236 call.addParameter("mimepart", MIME_MULTIPART_MIXED, ParameterMode.IN); 237 else if (contentType.equals("text/xml")) 238 call.addParameter("mimepart", MIME_TEXT_XML, ParameterMode.IN); 239 else if (contentType.equals("application/xml")) 240 call.addParameter("mimepart", MIME_APPLICATION_XML, ParameterMode.IN); 241 242 call.setReturnType(XSD_STRING); 243 244 call.setTargetEndpointAddress("http://" + getServerHost() + ":8080/ws4ee-attachment"); 245 246 return call; 247 } 248 249 251 private void sendAndValidateMimeMessage(Call call, Object mimepart) 252 throws RemoteException 253 { 254 String message = "Some text message"; 255 String value = (String )call.invoke(new Object []{message, mimepart}); 256 257 assertEquals("[pass]", value); 258 } 259 260 static class ExFileDataSource extends FileDataSource 261 { 262 private String contentType; 263 264 public ExFileDataSource(String source, String contentType) 265 { 266 super(source); 267 this.contentType = contentType; 268 } 269 270 public String getContentType() 271 { 272 return contentType; 273 } 274 } 275 } 276 | Popular Tags |