1 16 17 18 19 package org.apache.axis.attachments; 20 21 import org.apache.axis.AxisFault; 22 import org.apache.axis.Part; 23 import org.apache.axis.SOAPPart; 24 import org.apache.axis.components.logger.LogFactory; 25 import org.apache.axis.utils.Messages; 26 import org.apache.commons.logging.Log; 27 28 import javax.activation.DataHandler ; 29 import javax.activation.DataSource ; 30 import java.util.Collection ; 31 import java.util.HashMap ; 32 import java.util.LinkedList ; 33 34 38 public class AttachmentsImpl implements Attachments { 39 protected static Log log = 40 LogFactory.getLog(AttachmentsImpl.class.getName()); 41 42 43 private HashMap attachments = new java.util.HashMap (); 44 45 46 private LinkedList orderedAttachments = new LinkedList (); 47 48 49 protected SOAPPart soapPart = null; 50 51 54 protected MultiPartInputStream mpartStream = 55 null; 56 59 protected int sendtype= Attachments.SEND_TYPE_NOTSET; 60 61 65 protected String contentLocation = null; 66 67 70 private HashMap stackDataHandler = new HashMap (); 71 72 84 public AttachmentsImpl( 85 Object intialContents, String contentType, String contentLocation) 86 throws org.apache.axis.AxisFault { 87 88 if (contentLocation != null) { 89 contentLocation = contentLocation.trim(); 90 91 if (contentLocation.length() == 0) { 92 contentLocation = null; 93 } 94 } 95 96 this.contentLocation = contentLocation; 97 98 if (contentType != null) { 99 if (contentType.equals(org.apache.axis.Message.MIME_UNKNOWN)) { 100 101 } else { 102 java.util.StringTokenizer st = 103 new java.util.StringTokenizer (contentType, " \t;"); 104 105 if (st.hasMoreTokens()) { 106 String mimetype = st.nextToken(); 107 108 if (mimetype.equalsIgnoreCase( 109 org.apache.axis.Message.MIME_MULTIPART_RELATED)) { 110 sendtype= SEND_TYPE_MIME; 111 mpartStream = 112 new org.apache.axis.attachments.MultiPartRelatedInputStream( 113 contentType, 114 (java.io.InputStream ) intialContents); 115 116 if (null == contentLocation) { 117 118 contentLocation = mpartStream.getContentLocation(); 121 122 if (contentLocation != null) { 123 contentLocation = contentLocation.trim(); 124 125 if (contentLocation.length() == 0) { 126 contentLocation = null; 127 } 128 } 129 } 130 131 soapPart = new org.apache.axis.SOAPPart(null, 132 mpartStream, 133 false); 134 } else if (mimetype.equalsIgnoreCase(org.apache.axis.Message.MIME_APPLICATION_DIME)) { 135 try{ 136 mpartStream= 137 new MultiPartDimeInputStream( (java.io.InputStream ) intialContents); 138 soapPart = new org.apache.axis.SOAPPart(null, mpartStream, false); 139 }catch(Exception e){ throw org.apache.axis.AxisFault.makeFault(e);} 140 sendtype= SEND_TYPE_DIME; 141 } 142 } 143 } 144 } 145 } 146 147 153 private void mergeinAttachments() throws AxisFault { 154 155 if (mpartStream != null) { 156 Collection atts = mpartStream.getAttachments(); 157 158 if(contentLocation == null) 159 contentLocation= mpartStream.getContentLocation(); 160 161 mpartStream = null; 162 163 setAttachmentParts(atts); 164 } 165 } 166 167 177 public Part removeAttachmentPart(String reference) 178 throws org.apache.axis.AxisFault { 179 180 multipart = null; 181 182 dimemultipart = null; 183 184 mergeinAttachments(); 185 186 Part removedPart = getAttachmentByReference(reference); 187 188 if (removedPart != null) { 189 attachments.remove(removedPart.getContentId()); 190 attachments.remove(removedPart.getContentLocation()); 191 orderedAttachments.remove(removedPart); 192 } 193 194 return removedPart; 195 } 196 197 205 public Part addAttachmentPart(Part newPart) 206 throws org.apache.axis.AxisFault { 207 208 209 multipart = null; 210 dimemultipart = null; 211 212 mergeinAttachments(); 213 214 Part oldPart = (Part) attachments.put(newPart.getContentId(), newPart); 215 216 if (oldPart != null) { 217 orderedAttachments.remove(oldPart); 218 attachments.remove(oldPart.getContentLocation()); 219 } 220 221 orderedAttachments.add(newPart); 222 223 if (newPart.getContentLocation() != null) { 224 attachments.put(newPart.getContentLocation(), newPart); 225 } 226 227 return oldPart; 228 } 229 230 public Part createAttachmentPart(Object datahandler) 231 throws org.apache.axis.AxisFault { 232 233 Integer key = new Integer (datahandler.hashCode()); 235 if (stackDataHandler.containsKey(key)) { 236 return (Part)stackDataHandler.get(key); 237 } 238 239 multipart = null; 240 241 dimemultipart = null; 242 243 mergeinAttachments(); 244 245 if (!(datahandler instanceof javax.activation.DataHandler )) { 246 throw new org.apache.axis.AxisFault( 247 Messages.getMessage( 248 "unsupportedAttach", datahandler.getClass().getName(), 249 javax.activation.DataHandler .class.getName())); 250 } 251 252 Part ret = 253 new AttachmentPart((javax.activation.DataHandler ) datahandler); 254 255 addAttachmentPart(ret); 256 257 stackDataHandler.put(key, ret); 259 260 return ret; 261 } 262 263 270 public void setAttachmentParts(java.util.Collection parts) 271 throws org.apache.axis.AxisFault { 272 273 removeAllAttachments(); 274 275 if ((parts != null) && !parts.isEmpty()) { 276 for (java.util.Iterator i = parts.iterator(); i.hasNext();) { 277 Object part = i.next(); 278 279 if (null != part) { 280 if(part instanceof Part) 281 addAttachmentPart((Part)part); 282 else 283 createAttachmentPart(part); 284 } 285 } 286 } 287 } 288 289 302 public Part getAttachmentByReference(String reference) 303 throws org.apache.axis.AxisFault { 304 305 if (null == reference) { 306 return null; 307 } 308 309 reference = reference.trim(); 310 311 if (0 == reference.length()) { 312 return null; 313 } 314 315 mergeinAttachments(); 316 317 320 Part ret = (Part) attachments.get(reference); 321 if( null != ret) return ret; 322 323 324 if (!reference.startsWith(Attachments.CIDprefix) && (null != contentLocation)) { 325 327 String fqreference = contentLocation; 328 329 if (!fqreference.endsWith("/")) { 330 fqreference += "/"; 331 } 332 333 if (reference.startsWith("/")) { 334 fqreference += reference.substring(1); 335 } else { 336 fqreference += reference; 337 } 338 339 ret = (AttachmentPart) attachments.get(fqreference); 341 } 342 343 if( null == ret && reference.startsWith(Attachments.CIDprefix)){ 344 ret = (Part) attachments.get( reference.substring(4)); 346 } 347 348 return ret; 349 } 350 351 358 public java.util.Collection getAttachments() 359 throws org.apache.axis.AxisFault { 360 361 mergeinAttachments(); 362 363 return new LinkedList (orderedAttachments); 364 } 365 366 372 public Part getRootPart() { 373 return soapPart; 374 } 375 376 public void setRootPart(Part newRoot) { 377 378 try { 379 this.soapPart = (SOAPPart) newRoot; 380 multipart = null; 381 dimemultipart = null; 382 } catch (ClassCastException e) { 383 throw new ClassCastException (Messages.getMessage("onlySOAPParts")); 384 } 385 } 386 387 388 javax.mail.internet.MimeMultipart multipart = null; 389 DimeMultiPart dimemultipart = null; 390 391 398 public long getContentLength() throws org.apache.axis.AxisFault { 399 400 mergeinAttachments(); 401 402 int sendtype= this.sendtype == SEND_TYPE_NOTSET ? SEND_TYPE_DEFAULT : this.sendtype; 403 404 try { 405 if(sendtype == SEND_TYPE_MIME) 406 return org.apache.axis.attachments.MimeUtils.getContentLength( 407 multipart != null ? multipart : (multipart = org.apache.axis.attachments.MimeUtils.createMP(soapPart.getAsString(), orderedAttachments))); 408 else if (sendtype == SEND_TYPE_DIME)return createDimeMessage().getTransmissionSize(); 409 } catch (Exception e) { 410 throw AxisFault.makeFault(e); 411 } 412 return 0; 413 } 414 415 422 protected DimeMultiPart createDimeMessage() throws org.apache.axis.AxisFault{ 423 int sendtype= this.sendtype == SEND_TYPE_NOTSET ? SEND_TYPE_DEFAULT : this.sendtype; 424 if (sendtype == SEND_TYPE_DIME){ 425 if(dimemultipart== null){ 426 427 dimemultipart= new DimeMultiPart(); 428 dimemultipart.addBodyPart(new DimeBodyPart( 429 soapPart.getAsBytes(), DimeTypeNameFormat.URI, 430 "http://schemas.xmlsoap.org/soap/envelope/", 431 "uuid:714C6C40-4531-442E-A498-3AC614200295")); 432 433 for( java.util.Iterator i= orderedAttachments.iterator(); 434 i.hasNext(); ){ 435 AttachmentPart part= (AttachmentPart)i.next(); 436 DataHandler dh= AttachmentUtils. 437 getActivationDataHandler(part); 438 dimemultipart.addBodyPart(new 439 DimeBodyPart(dh,part.getContentId())); 440 } 441 } 442 } 443 return dimemultipart; 444 } 445 446 453 public void writeContentToStream(java.io.OutputStream os) 454 throws org.apache.axis.AxisFault { 455 int sendtype= this.sendtype == SEND_TYPE_NOTSET ? 456 SEND_TYPE_DEFAULT : this.sendtype; 457 try{ 458 459 mergeinAttachments(); 460 if(sendtype == SEND_TYPE_MIME){ 461 org.apache.axis.attachments.MimeUtils.writeToMultiPartStream(os, 462 (multipart != null) 463 ? multipart 464 : (multipart = 465 org.apache.axis.attachments.MimeUtils.createMP( 466 soapPart.getAsString(), orderedAttachments))); 467 468 for (java.util.Iterator i = orderedAttachments.iterator(); 469 i.hasNext();) { 470 AttachmentPart part = (AttachmentPart) i.next(); 471 DataHandler dh = 472 AttachmentUtils.getActivationDataHandler(part); 473 DataSource ds = dh.getDataSource(); 474 475 if ((ds != null) && (ds instanceof ManagedMemoryDataSource)) { 476 ((ManagedMemoryDataSource) ds).delete(); 477 } 478 } 479 }else if (sendtype == SEND_TYPE_DIME)createDimeMessage().write(os); 480 }catch(Exception e){ throw org.apache.axis.AxisFault.makeFault(e);} 481 } 482 483 490 public String getContentType() throws org.apache.axis.AxisFault { 491 492 mergeinAttachments(); 493 494 int sendtype= this.sendtype == SEND_TYPE_NOTSET ? SEND_TYPE_DEFAULT : 495 this.sendtype; 496 if(sendtype == SEND_TYPE_MIME) 497 return org.apache.axis.attachments.MimeUtils.getContentType((multipart 498 != null) 499 ? multipart 500 : (multipart = 501 org.apache.axis.attachments.MimeUtils.createMP( 502 soapPart.getAsString(), 503 orderedAttachments))); 504 else return org.apache.axis.Message.MIME_APPLICATION_DIME; 505 } 506 507 512 public int getAttachmentCount() { 513 514 try { 515 mergeinAttachments(); 516 517 soapPart.saveChanges(); 520 521 return orderedAttachments.size(); 522 } catch (AxisFault e) { 523 log.warn(Messages.getMessage("exception00"),e); 524 } 525 526 return 0; 527 } 528 529 537 public boolean isAttachment(Object value) { 538 return AttachmentUtils.isAttachment(value); 539 } 540 541 547 public void removeAllAttachments() { 548 try { 549 multipart = null; 550 dimemultipart = null; 551 mergeinAttachments(); 552 attachments.clear(); 553 orderedAttachments.clear(); 554 stackDataHandler.clear(); 555 } catch (AxisFault af){ 556 log.warn(Messages.getMessage("exception00"),af); 557 } 558 } 559 560 571 public java.util.Iterator getAttachments( 572 javax.xml.soap.MimeHeaders headers) { 573 java.util.Vector vecParts = new java.util.Vector (); 574 java.util.Iterator iterator = GetAttachmentsIterator(); 575 while(iterator.hasNext()){ 576 Part part = (Part) iterator.next(); 577 if(part instanceof AttachmentPart){ 578 if(((AttachmentPart)part).matches(headers)){ 579 vecParts.add(part); 580 } 581 } 582 } 583 return vecParts.iterator(); 584 } 585 586 593 private java.util.Iterator GetAttachmentsIterator() { 594 java.util.Iterator iterator = attachments.values().iterator(); 595 return iterator; 596 } 597 598 606 public Part createAttachmentPart() throws org.apache.axis.AxisFault { 607 return new AttachmentPart(); 608 } 609 610 public void setSendType( int sendtype){ 611 if( sendtype < 1) 612 throw new IllegalArgumentException (""); 613 if( sendtype > SEND_TYPE_MAX ) 614 throw new IllegalArgumentException (""); 615 this.sendtype= sendtype; 616 } 617 618 public int getSendType(){ 619 return sendtype; 620 } 621 622 626 627 public void dispose() { 628 java.util.Iterator iterator = GetAttachmentsIterator(); 629 while (iterator.hasNext()) { 630 Part part = (Part) iterator.next(); 631 if (part instanceof AttachmentPart) { 632 AttachmentPart apart=(AttachmentPart)part; 633 apart.dispose(); 634 } 635 } 636 637 } 638 639 648 public static int getSendType(String value) { 649 if (value.equalsIgnoreCase("MIME")) return SEND_TYPE_MIME; 650 if (value.equalsIgnoreCase("DIME")) return SEND_TYPE_DIME; 651 if (value.equalsIgnoreCase("NONE")) return SEND_TYPE_NONE; 652 return SEND_TYPE_NOTSET; 653 } 654 655 661 public static String getSendTypeString(int value) { 662 if (value == SEND_TYPE_MIME) { 663 return "MIME"; 664 } 665 if (value == SEND_TYPE_DIME) { 666 return "DIME"; 667 } 668 if (value == SEND_TYPE_NONE) { 669 return "NONE"; 670 } 671 return null; 672 } 673 } 674 | Popular Tags |