1 16 package org.jboss.axis.attachments; 17 18 import org.jboss.axis.Part; 19 import org.jboss.axis.components.image.ImageIOFactory; 20 import org.jboss.axis.transport.http.HTTPConstants; 21 import org.jboss.axis.utils.IOUtils; 22 import org.jboss.axis.utils.Messages; 23 import org.jboss.axis.utils.SessionUtils; 24 import org.jboss.logging.Logger; 25 26 import javax.activation.DataHandler ; 27 import javax.activation.DataSource ; 28 import javax.mail.internet.MimeMultipart ; 29 import javax.xml.soap.AttachmentPart ; 30 import javax.xml.soap.MimeHeaders ; 31 import javax.xml.soap.SOAPException ; 32 import javax.xml.transform.stream.StreamSource ; 33 import java.awt.*; 34 import java.io.ByteArrayInputStream ; 35 import java.io.ByteArrayOutputStream ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.util.Iterator ; 39 40 43 public class AttachmentPartImpl extends AttachmentPart implements Part 44 { 45 48 private static Logger log = Logger.getLogger(AttachmentPartImpl.class.getName()); 49 50 53 private DataHandler datahandler = null; 54 55 58 private MimeHeaders mimeHeaders = new MimeHeaders (); 59 60 63 private Object contentObject; 64 65 68 public AttachmentPartImpl() 69 { 70 setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, SessionUtils.generateSessionId()); 71 } 72 73 78 public AttachmentPartImpl(DataHandler dh) 79 { 80 setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, SessionUtils.generateSessionId()); 81 datahandler = dh; 82 83 if (dh != null) 84 setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, dh.getContentType()); 85 } 86 87 92 public DataHandler getActivationDataHandler() 93 { 94 return datahandler; 95 } 96 97 102 public String getContentType() 103 { 104 return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE); 105 } 106 107 public void setContentType(String contentType) 108 { 109 super.setContentType(contentType); 110 setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, contentType); 111 } 112 113 119 public void addMimeHeader(String header, String value) 120 { 121 mimeHeaders.addHeader(header, value); 122 } 123 124 130 public String getFirstMimeHeader(String header) 131 { 132 String [] values = mimeHeaders.getHeader(header.toLowerCase()); 133 if ((values != null) && (values.length > 0)) 134 { 135 return values[0]; 136 } 137 return null; 138 } 139 140 144 public boolean matches(javax.xml.soap.MimeHeaders headers) 145 { 146 for (Iterator i = headers.getAllHeaders(); i.hasNext();) 147 { 148 javax.xml.soap.MimeHeader hdr = (javax.xml.soap.MimeHeader )i.next(); 149 String values[] = mimeHeaders.getHeader(hdr.getName()); 150 boolean found = false; 151 if (values != null) 152 { 153 for (int j = 0; j < values.length; j++) 154 { 155 if (!hdr.getValue().equalsIgnoreCase(values[j])) 156 { 157 continue; 158 } 159 found = true; 160 break; 161 } 162 } 163 if (!found) 164 { 165 return false; 166 } 167 } 168 return true; 169 } 170 171 174 public String getContentLocation() 175 { 176 return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION); 177 } 178 179 184 public void setContentLocation(String loc) 185 { 186 setMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION, loc); 187 } 188 189 195 public void setContentId(String newCid) 196 { 197 setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, newCid); 198 } 199 200 205 public String getContentId() 206 { 207 return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_ID); 208 } 209 210 216 public java.util.Iterator getMatchingMimeHeaders(final String [] match) 217 { 218 return mimeHeaders.getMatchingHeaders(match); 219 } 220 221 227 public java.util.Iterator getNonMatchingMimeHeaders(final String [] match) 228 { 229 return mimeHeaders.getNonMatchingHeaders(match); 230 } 231 232 240 public Iterator getAllMimeHeaders() 241 { 242 return mimeHeaders.getAllHeaders(); 243 } 244 245 264 public void setMimeHeader(String name, String value) 265 { 266 mimeHeaders.setHeader(name, value); 267 } 268 269 272 public void removeAllMimeHeaders() 273 { 274 mimeHeaders.removeAllHeaders(); 275 } 276 277 283 public void removeMimeHeader(String header) 284 { 285 mimeHeaders.removeHeader(header); 286 } 287 288 297 public DataHandler getDataHandler() throws SOAPException 298 { 299 if (datahandler == null) 300 { 301 throw new SOAPException (Messages.getMessage("noContent")); 302 } 303 return datahandler; 304 } 305 306 321 public void setDataHandler(DataHandler datahandler) 322 { 323 if (datahandler == null) 324 { 325 throw new IllegalArgumentException (Messages.getMessage("illegalArgumentException00")); 326 } 327 this.datahandler = datahandler; 328 setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, datahandler.getContentType()); 329 } 330 331 365 public Object getContent() throws SOAPException 366 { 367 if (contentObject != null) 368 return contentObject; 369 370 try 371 { 372 if (datahandler != null) 373 { 374 DataSource ds = datahandler.getDataSource(); 375 InputStream is = ds.getInputStream(); 376 377 String contentType = ds.getContentType(); 378 if (contentType.equals("text/plain")) 379 { 380 byte[] bytes = IOUtils.toByteArray(is); 381 contentObject = new String (bytes); 382 } 383 else if (contentType.equals("text/xml") || contentType.equals("application/xml")) 384 { 385 contentObject = new StreamSource (is); 386 } 387 else if (contentType.startsWith("multipart/")) 388 { 389 MimeMultipart mmp = new MimeMultipart (ds); 390 contentObject = mmp; 391 } 392 else if (contentType.equals("image/gif") || contentType.equals("image/jpeg")) 393 { 394 contentObject = ImageIOFactory.getImageIO().loadImage(is); 395 } 396 else 397 { 398 contentObject = is; 399 } 400 } 401 } 402 catch (SOAPException e) 403 { 404 throw e; 405 } 406 catch (Exception e) 407 { 408 throw new SOAPException ("Cannot get content", e); 409 } 410 411 if (contentObject == null) 412 throw new SOAPException ("Content is not available"); 413 414 return contentObject; 415 } 416 417 436 public void setContent(Object object, String contentType) 437 { 438 try 439 { 440 contentObject = object; 441 442 if (object instanceof String ) 443 { 444 InputStream inputStream = new ByteArrayInputStream (((String )contentObject).getBytes()); 445 ManagedMemoryDataSource source = new ManagedMemoryDataSource(inputStream, contentType); 446 datahandler = new DataHandler (source); 447 } 448 else if (object instanceof StreamSource ) 449 { 450 InputStream inputStream = ((StreamSource )object).getInputStream(); 451 ManagedMemoryDataSource source = new ManagedMemoryDataSource(inputStream, contentType); 452 datahandler = new DataHandler (source); 453 } 454 else if (object instanceof Image) 455 { 456 datahandler = new DataHandler (object, contentType); 457 } 458 else if (object instanceof MimeMultipart ) 459 { 460 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 461 MimeMultipart mmp = (MimeMultipart )object; 462 mmp.writeTo(baos); 463 464 InputStream inputStream = new ByteArrayInputStream (baos.toByteArray()); 465 ManagedMemoryDataSource source = new ManagedMemoryDataSource(inputStream, contentType); 466 datahandler = new DataHandler (source); 467 } 468 else if (object instanceof InputStream ) 469 { 470 InputStream inputStream = ((InputStream )object); 471 ManagedMemoryDataSource source = new ManagedMemoryDataSource(inputStream, contentType); 472 datahandler = new DataHandler (source); 473 } 474 else 475 { 476 throw new IllegalArgumentException ("Cannot set content: " + object); 477 } 478 } 479 catch (IllegalArgumentException e) 480 { 481 throw e; 482 } 483 catch (Exception e) 484 { 485 throw new IllegalArgumentException (e.toString()); 486 } 487 } 488 489 public void dispose() 490 { 491 contentObject = null; 492 datahandler = null; 493 } 494 495 500 public void clearContent() 501 { 502 datahandler = null; 503 contentObject = null; 504 } 505 506 516 public int getSize() throws SOAPException 517 { 518 if (datahandler == null) 519 { 520 return 0; 521 } 522 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 523 try 524 { 525 datahandler.writeTo(bout); 526 } 527 catch (IOException ex) 528 { 529 log.error(Messages.getMessage("javaIOException00"), ex); 530 throw new SOAPException (Messages.getMessage("javaIOException01", ex.getMessage()), ex); 531 } 532 return bout.size(); 533 } 534 535 545 public String [] getMimeHeader(String name) 546 { 547 return mimeHeaders.getHeader(name); 548 } 549 550 557 public String getContentIdRef() 558 { 559 return Attachments.CIDprefix + getContentId(); 560 } 561 } 562 | Popular Tags |