1 16 package org.apache.axis.attachments; 17 18 import org.apache.axis.Part; 19 import org.apache.axis.components.logger.LogFactory; 20 import org.apache.axis.components.image.ImageIOFactory; 21 import org.apache.axis.transport.http.HTTPConstants; 22 import org.apache.axis.utils.Messages; 23 import org.apache.axis.utils.SessionUtils; 24 import org.apache.axis.utils.IOUtils; 25 import org.apache.commons.logging.Log; 26 27 import javax.activation.DataHandler ; 28 import javax.xml.soap.SOAPException ; 29 import javax.xml.transform.stream.StreamSource ; 30 import java.util.Iterator ; 31 import java.io.ByteArrayOutputStream ; 32 import java.io.File ; 33 import java.io.InputStream ; 34 35 40 public class AttachmentPart extends javax.xml.soap.AttachmentPart 41 implements Part { 42 43 44 protected static Log log = 45 LogFactory.getLog(AttachmentPart.class.getName()); 46 47 52 javax.activation.DataHandler datahandler = null; 53 54 55 private javax.xml.soap.MimeHeaders mimeHeaders = 56 new javax.xml.soap.MimeHeaders (); 57 58 private Object contentObject; 59 60 63 private String attachmentFile; 64 65 68 public AttachmentPart() { 69 setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, SessionUtils.generateSessionId()); 70 } 71 72 77 public AttachmentPart(javax.activation.DataHandler dh) { 78 setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, 79 SessionUtils.generateSessionId()); 80 datahandler = dh; 81 if(dh != null) { 82 setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, dh.getContentType()); 83 javax.activation.DataSource ds = dh.getDataSource(); 84 if (ds instanceof ManagedMemoryDataSource) { 85 extractFilename((ManagedMemoryDataSource)ds); 87 } 88 } 89 } 90 91 97 protected void finalize() throws Throwable { 98 dispose(); 99 } 100 101 106 public javax.activation.DataHandler getActivationDataHandler() { 107 return datahandler; 108 } 109 110 115 public String getContentType() { 116 return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE); 117 } 118 119 125 public void addMimeHeader(String header, String value) { 126 mimeHeaders.addHeader(header, value); 127 } 128 129 136 public String getFirstMimeHeader(String header) { 137 String [] values = mimeHeaders.getHeader(header.toLowerCase()); 138 if ((values != null) && (values.length > 0)) { 139 return values[0]; 140 } 141 return null; 142 } 143 144 152 public boolean matches(javax.xml.soap.MimeHeaders headers) { 153 for (Iterator i = headers.getAllHeaders(); i.hasNext();) { 154 javax.xml.soap.MimeHeader hdr = (javax.xml.soap.MimeHeader ) i.next(); 155 String values[] = mimeHeaders.getHeader(hdr.getName()); 156 boolean found = false; 157 if (values != null) { 158 for (int j = 0; j < values.length; j++) { 159 if (!hdr.getValue().equalsIgnoreCase(values[j])) { 160 continue; 161 } 162 found = true; 163 break; 164 } 165 } 166 if (!found) { 167 return false; 168 } 169 } 170 return true; 171 } 172 173 public String getContentLocation() { 174 return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION); 175 } 176 177 public void setContentLocation(String loc) { 178 setMimeHeader(HTTPConstants.HEADER_CONTENT_LOCATION, loc); 179 } 180 181 public void setContentId(String newCid) { 182 setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, newCid); 183 } 184 185 public String getContentId() { 186 return getFirstMimeHeader(HTTPConstants.HEADER_CONTENT_ID); 187 } 188 189 public java.util.Iterator getMatchingMimeHeaders(final String [] match) { 190 return mimeHeaders.getMatchingHeaders(match); 191 } 192 193 public java.util.Iterator getNonMatchingMimeHeaders(final String [] match) { 194 return mimeHeaders.getNonMatchingHeaders(match); 195 } 196 197 public Iterator getAllMimeHeaders() { 198 return mimeHeaders.getAllHeaders(); 199 } 200 201 218 public void setMimeHeader(String name, String value) { 219 mimeHeaders.setHeader(name, value); 220 } 221 222 223 public void removeAllMimeHeaders() { 224 mimeHeaders.removeAllHeaders(); 225 } 226 227 232 public void removeMimeHeader(String header) { 233 mimeHeaders.removeHeader(header); 234 } 235 236 244 public DataHandler getDataHandler() throws SOAPException { 245 if(datahandler == null) { 246 throw new SOAPException (Messages.getMessage("noContent")); 247 } 248 return datahandler; 249 } 250 251 265 public void setDataHandler(DataHandler datahandler) { 266 if(datahandler == null) { 267 throw new java.lang.IllegalArgumentException ( 268 Messages.getMessage("illegalArgumentException00")); 269 } 270 this.datahandler = datahandler; 271 setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, datahandler.getContentType()); 272 javax.activation.DataSource ds = datahandler.getDataSource(); 274 if (ds instanceof ManagedMemoryDataSource) { 275 extractFilename((ManagedMemoryDataSource)ds); 277 } 278 279 280 } 281 282 315 public Object getContent() throws SOAPException { 316 if(contentObject != null) { 317 return contentObject; 318 } 319 320 if(datahandler == null) { 321 throw new SOAPException (Messages.getMessage("noContent")); 322 } 323 324 javax.activation.DataSource ds = datahandler.getDataSource(); 325 InputStream is = null; 326 try { 327 is = ds.getInputStream();; 328 } catch (java.io.IOException io) { 329 log.error(Messages.getMessage("javaIOException00"), io); 330 throw new SOAPException (io); 331 } 332 if (ds.getContentType().equals("text/plain")) { 333 try { 334 byte[] bytes = new byte[is.available()]; 335 IOUtils.readFully(is, bytes); 336 return new String (bytes); 337 } catch (java.io.IOException io) { 338 log.error(Messages.getMessage("javaIOException00"), io); 339 throw new SOAPException (io); 340 } 341 } else if (ds.getContentType().equals("text/xml")) { 342 return new StreamSource (is); 343 } else if (ds.getContentType().equals("image/gif") || 344 ds.getContentType().equals("image/jpeg")) { 345 try { 346 return ImageIOFactory.getImageIO().loadImage(is); 347 } catch (Exception ex) { 348 log.error(Messages.getMessage("javaIOException00"), ex); 349 throw new SOAPException (ex); 350 } 351 } 352 return is; 353 } 354 355 356 374 public void setContent(Object object, String contentType) { 375 ManagedMemoryDataSource source = null; 376 setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, contentType); 377 if (object instanceof String ) { 378 try { 379 String s = (String ) object; 380 java.io.ByteArrayInputStream bais = 381 new java.io.ByteArrayInputStream (s.getBytes()); 382 source = new ManagedMemoryDataSource(bais, 383 ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, 384 contentType, true); 385 extractFilename(source); 386 datahandler = new DataHandler (source); 387 contentObject = object; 388 return; 389 } catch (java.io.IOException io) { 390 log.error(Messages.getMessage("javaIOException00"), io); 391 throw new java.lang.IllegalArgumentException ( 392 Messages.getMessage("illegalArgumentException00")); 393 } 394 } else if (object instanceof java.io.InputStream ) { 395 try { 396 source = new ManagedMemoryDataSource((java.io.InputStream ) object, 397 ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, 398 contentType, true); 399 extractFilename(source); 400 datahandler = new DataHandler (source); 401 contentObject = null; return; 403 } catch (java.io.IOException io) { 404 log.error(Messages.getMessage("javaIOException00"), io); 405 throw new java.lang.IllegalArgumentException (Messages.getMessage 406 ("illegalArgumentException00")); 407 } 408 } else if (object instanceof StreamSource ) { 409 try { 410 source = new ManagedMemoryDataSource(((StreamSource )object).getInputStream(), 411 ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, 412 contentType, true); 413 extractFilename(source); 414 datahandler = new DataHandler (source); 415 contentObject = null; return; 417 } catch (java.io.IOException io) { 418 log.error(Messages.getMessage("javaIOException00"), io); 419 throw new java.lang.IllegalArgumentException (Messages.getMessage 420 ("illegalArgumentException00")); 421 } 422 } else { 423 throw new java.lang.IllegalArgumentException ( 424 Messages.getMessage("illegalArgumentException00")); 425 } 426 } 427 428 433 public void clearContent() { 434 datahandler = null; 435 contentObject = null; 436 } 437 438 447 public int getSize() throws SOAPException { 448 if (datahandler == null) { 449 return 0; 450 } 451 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 452 try { 453 datahandler.writeTo(bout); 454 } catch (java.io.IOException ex) { 455 log.error(Messages.getMessage("javaIOException00"), ex); 456 throw new SOAPException (Messages.getMessage("javaIOException01", ex.getMessage()), ex); 457 } 458 return bout.size(); 459 } 460 461 470 public String [] getMimeHeader(String name) { 471 return mimeHeaders.getHeader(name); 472 } 473 474 481 public String getContentIdRef() { 482 return Attachments.CIDprefix + getContentId(); 483 } 484 485 490 491 private void extractFilename(ManagedMemoryDataSource source) { 492 if(source.getDiskCacheFile()!=null) { 494 String path = source.getDiskCacheFile().getAbsolutePath(); 495 setAttachmentFile(path); 496 } 497 } 498 499 504 protected void setAttachmentFile(String path) { 505 attachmentFile=path; 506 } 507 508 513 public void detachAttachmentFile() { 514 attachmentFile=null; 515 } 516 517 522 public String getAttachmentFile() { 523 return attachmentFile; 524 } 525 526 531 public synchronized void dispose() { 532 if (attachmentFile != null) { 533 javax.activation.DataSource ds = datahandler.getDataSource(); 534 if (ds instanceof ManagedMemoryDataSource) { 535 ((ManagedMemoryDataSource) ds).delete(); } else { 537 File f = new File (attachmentFile); 538 f.delete(); 540 } 541 setAttachmentFile(null); 543 } 544 datahandler = null; 548 } 549 } 550 | Popular Tags |