1 22 package org.jboss.test.webservice.attachment; 23 24 import javax.activation.DataHandler ; 25 import javax.mail.BodyPart ; 26 import javax.mail.internet.MimeMultipart ; 27 import javax.xml.rpc.ServiceException ; 28 import javax.xml.rpc.handler.soap.SOAPMessageContext ; 29 import javax.xml.rpc.server.ServiceLifecycle ; 30 import javax.xml.rpc.server.ServletEndpointContext ; 31 import javax.xml.soap.AttachmentPart ; 32 import javax.xml.soap.SOAPMessage ; 33 import javax.xml.transform.Source ; 34 import java.awt.*; 35 import java.rmi.RemoteException ; 36 import java.util.Iterator ; 37 38 51 public class AttachmentImpl implements Attachment, ServiceLifecycle 52 { 53 private ServletEndpointContext context; 54 55 56 public String sendMimeImageGIF(String message, Object mimepart) throws RemoteException 57 { 58 StringBuffer buffer = new StringBuffer (); 59 60 validateStringMessage(buffer, message); 61 62 String expContentType = "image/gif"; 63 validateAttachmentPart(buffer, expContentType, mimepart); 64 65 String resultStr = getResultStr(buffer, expContentType); 66 return resultStr; 67 } 68 69 70 public String sendMimeImageJPEG(String message, Object mimepart) throws RemoteException 71 { 72 StringBuffer buffer = new StringBuffer (); 73 74 validateStringMessage(buffer, message); 75 76 String expContentType = "image/jpeg"; 77 validateAttachmentPart(buffer, expContentType, mimepart); 78 79 String resultStr = getResultStr(buffer, expContentType); 80 return resultStr; 81 } 82 83 84 public String sendMimeTextPlain(String message, Object mimepart) throws RemoteException 85 { 86 StringBuffer buffer = new StringBuffer (); 87 88 validateStringMessage(buffer, message); 89 90 String expContentType = "text/plain"; 91 validateAttachmentPart(buffer, expContentType, mimepart); 92 93 String resultStr = getResultStr(buffer, expContentType); 94 return resultStr; 95 } 96 97 98 public String sendMimeMultipart(String message, Object mimepart) throws RemoteException 99 { 100 StringBuffer buffer = new StringBuffer (); 101 102 validateStringMessage(buffer, message); 103 104 String expContentType = "multipart/*"; 105 validateAttachmentPart(buffer, expContentType, mimepart); 106 107 String resultStr = getResultStr(buffer, expContentType); 108 return resultStr; 109 } 110 111 112 public String sendMimeTextXML(String message, Object mimepart) throws RemoteException 113 { 114 StringBuffer buffer = new StringBuffer (); 115 116 validateStringMessage(buffer, message); 117 118 String expContentType = "text/xml"; 119 validateAttachmentPart(buffer, expContentType, mimepart); 120 121 String resultStr = getResultStr(buffer, expContentType); 122 return resultStr; 123 } 124 125 126 public String sendMimeApplicationXML(String message, Object mimepart) throws RemoteException 127 { 128 StringBuffer buffer = new StringBuffer (); 129 130 validateStringMessage(buffer, message); 131 132 String expContentType = "application/xml"; 133 validateAttachmentPart(buffer, expContentType, mimepart); 134 135 String resultStr = getResultStr(buffer, expContentType); 136 return resultStr; 137 } 138 139 140 public Object echoMimeImageGIF(Object mimepart) throws RemoteException 141 { 142 StringBuffer buffer = new StringBuffer (); 143 String expContentType = "image/gif"; 144 validateAttachmentPart(buffer, expContentType, mimepart); 145 146 return mimepart; 147 } 148 149 150 public Object echoMimeImageJPEG(Object mimepart) throws RemoteException 151 { 152 StringBuffer buffer = new StringBuffer (); 153 String expContentType = "image/jpeg"; 154 validateAttachmentPart(buffer, expContentType, mimepart); 155 156 return mimepart; 157 } 158 159 160 public Object echoMimeTextPlain(Object mimepart) throws RemoteException 161 { 162 StringBuffer buffer = new StringBuffer (); 163 String expContentType = "text/plain"; 164 validateAttachmentPart(buffer, expContentType, mimepart); 165 166 return mimepart; 167 } 168 169 170 public Object echoMimeMultipart(Object mimepart) throws RemoteException 171 { 172 StringBuffer buffer = new StringBuffer (); 173 String expContentType = "multipart/*"; 174 validateAttachmentPart(buffer, expContentType, mimepart); 175 176 return mimepart; 177 } 178 179 180 public Object echoMimeTextXML(Object mimepart) throws RemoteException 181 { 182 StringBuffer buffer = new StringBuffer (); 183 String expContentType = "text/xml"; 184 validateAttachmentPart(buffer, expContentType, mimepart); 185 186 return mimepart; 187 } 188 189 190 public Source echoMimeApplicationXML(Source mimepart) throws RemoteException 191 { 192 StringBuffer buffer = new StringBuffer (); 193 String expContentType = "application/xml"; 194 validateAttachmentPart(buffer, expContentType, mimepart); 195 196 return mimepart; 197 } 198 199 public DataHandler echoHandler(DataHandler mimepart) throws RemoteException 200 { 201 return mimepart; 202 } 203 204 private void validateStringMessage(StringBuffer buffer, String message) 205 { 206 if ("Some text message".equals(message) == false) 207 buffer.append("[message=" + message + "]"); 208 } 209 210 private void validateAttachmentPart(StringBuffer buffer, String expContentType, Object mimepart) 211 { 212 SOAPMessageContext msgContext = (SOAPMessageContext )context.getMessageContext(); 213 SOAPMessage soapMessage = msgContext.getMessage(); 214 215 Iterator attachments = soapMessage.getAttachments(); 216 if (attachments.hasNext()) 217 { 218 AttachmentPart ap = (AttachmentPart )attachments.next(); 219 String contentType = ap.getContentType(); 220 221 if (expContentType.equals("multipart/*")) 222 { 223 if (contentType.startsWith("multipart/") == false) 224 buffer.append("[contentType=" + contentType + "]"); 225 } 226 else if (expContentType.equals("text/xml")) 227 { 228 if (contentType.equals("text/xml") == false && contentType.equals("application/xml") == false) 229 buffer.append("[contentType=" + contentType + "]"); 230 } 231 else 232 { 233 if (contentType.equals(expContentType) == false) 234 buffer.append("[contentType=" + contentType + "]"); 235 } 236 validateSinglePart(buffer, expContentType, ap); 237 } 238 else 239 { 240 buffer.append("[no attachments]"); 241 } 242 243 validateSinglePart(buffer, expContentType, mimepart); 244 } 245 246 private void validateSinglePart(StringBuffer buffer, String contentType, Object content) 247 { 248 try 249 { 250 if (content instanceof AttachmentPart ) 251 content = ((AttachmentPart )content).getContent(); 252 253 if (contentType.equals("image/gif") || contentType.equals("image/jpeg")) 254 { 255 if ((content instanceof Image) == false) 256 buffer.append("[content=" + content + "]"); 257 } 258 else if (contentType.equals("text/plain")) 259 { 260 if ((content instanceof String ) == false) 261 buffer.append("[content=" + content + "]"); 262 } 263 else if (contentType.startsWith("multipart/")) 264 { 265 if ((content instanceof MimeMultipart ) == false) 266 { 267 buffer.append("[content=" + content + "]"); 268 } 269 else 270 { 271 MimeMultipart mmp = (MimeMultipart )content; 272 273 int mmpCount = mmp.getCount(); 274 if (mmpCount < 1) 275 buffer.append("[count=" + mmpCount + "]"); 276 277 for (int i = 0; i < mmpCount; i++) 278 { 279 BodyPart bp = mmp.getBodyPart(i); 280 String bpct = bp.getContentType(); 281 Object bpc = bp.getContent(); 282 validateSinglePart(buffer, bpct, bpc); 283 } 284 } 285 } 286 else if (contentType.equals("text/xml") || contentType.equals("application/xml")) 287 { 288 if ((content instanceof Source ) == false) 289 buffer.append("[content=" + content + "]"); 290 } 291 else 292 { 293 throw new IllegalArgumentException ("Unsupported mime type: " + contentType); 294 } 295 } 296 catch (Exception e) 297 { 298 buffer.append("[" + e + "]"); 299 } 300 } 301 302 private String getResultStr(StringBuffer buffer, String expContentType) 303 { 304 String retStr = (buffer.length() == 0 ? "[pass]" : buffer.toString()); 305 System.out.println(expContentType + ": " + retStr); 306 return retStr; 307 } 308 309 311 public void init(Object context) throws ServiceException 312 { 313 this.context = (ServletEndpointContext )context; 314 } 315 316 public void destroy() 317 { 318 this.context = null; 319 } 320 } 321 | Popular Tags |