1 16 17 18 19 package org.jboss.axis.attachments; 20 21 import org.jboss.axis.AxisFault; 22 import org.jboss.axis.Message; 23 import org.jboss.axis.MessagePart; 24 import org.jboss.axis.Part; 25 import org.jboss.axis.utils.Messages; 26 import org.jboss.logging.Logger; 27 28 import javax.activation.DataHandler ; 29 import java.io.InputStream ; 30 import java.util.Collection ; 31 import java.util.HashMap ; 32 import java.util.LinkedList ; 33 import java.util.StringTokenizer ; 34 35 39 public class AttachmentsImpl implements Attachments 40 { 41 private static Logger log = Logger.getLogger(AttachmentsImpl.class.getName()); 42 43 46 private HashMap attachments = new java.util.HashMap (); 47 48 51 private LinkedList orderedAttachments = new LinkedList (); 52 53 56 protected MessagePart soapPart = null; 57 58 61 protected MultiPartInputStream mpartStream = 62 null; 63 66 protected int sendtype = Attachments.SEND_TYPE_NOTSET; 67 68 72 protected String contentLocation = null; 73 74 77 private HashMap stackDataHandler = new HashMap (); 78 79 101 public AttachmentsImpl(Object intialContents, String contentType, String contentLocation) 102 throws org.jboss.axis.AxisFault 103 { 104 105 if (contentLocation != null) 106 { 107 contentLocation = contentLocation.trim(); 108 109 if (contentLocation.length() == 0) 110 { 111 contentLocation = null; 112 } 113 } 114 115 this.contentLocation = contentLocation; 116 117 if (contentType != null) 118 { 119 if (contentType.equals(Message.MIME_UNKNOWN)) 120 { 121 log.warn("Unknown contentType"); 122 } 123 else 124 { 125 StringTokenizer st = new StringTokenizer (contentType, " \t;"); 126 127 if (st.hasMoreTokens()) 128 { 129 String mimetype = st.nextToken(); 130 131 if (mimetype.equalsIgnoreCase(Message.MIME_MULTIPART_RELATED)) 132 { 133 sendtype = SEND_TYPE_MIME; 134 mpartStream = new MultiPartRelatedInputStream(contentType, (InputStream )intialContents); 135 136 if (null == contentLocation) 137 { 138 139 contentLocation = mpartStream.getContentLocation(); 142 143 if (contentLocation != null) 144 { 145 contentLocation = contentLocation.trim(); 146 147 if (contentLocation.length() == 0) 148 { 149 contentLocation = null; 150 } 151 } 152 } 153 154 soapPart = new MessagePart(null, mpartStream, false); 155 } 156 else if (mimetype.equalsIgnoreCase(Message.MIME_APPLICATION_DIME)) 157 { 158 try 159 { 160 mpartStream = new MultiPartDimeInputStream((InputStream )intialContents); 161 soapPart = new MessagePart(null, mpartStream, false); 162 } 163 catch (Exception e) 164 { 165 throw AxisFault.makeFault(e); 166 } 167 sendtype = SEND_TYPE_DIME; 168 } 169 } 170 } 171 } 172 } 173 174 180 private void mergeinAttachments() throws AxisFault 181 { 182 183 if (mpartStream != null) 184 { 185 Collection atts = mpartStream.getAttachments(); 186 187 if (contentLocation == null) 188 contentLocation = mpartStream.getContentLocation(); 189 190 mpartStream = null; 191 192 setAttachmentParts(atts); 193 } 194 } 195 196 205 public Part removeAttachmentPart(String reference) 206 throws org.jboss.axis.AxisFault 207 { 208 209 multipart = null; 210 211 dimemultipart = null; 212 213 mergeinAttachments(); 214 215 Part removedPart = getAttachmentByReference(reference); 216 217 if (removedPart != null) 218 { 219 attachments.remove(removedPart.getContentId()); 220 attachments.remove(removedPart.getContentLocation()); 221 orderedAttachments.remove(removedPart); 222 } 223 224 return removedPart; 225 } 226 227 235 public Part addAttachmentPart(Part newPart) 236 throws org.jboss.axis.AxisFault 237 { 238 239 240 multipart = null; 241 dimemultipart = null; 242 243 mergeinAttachments(); 244 245 Part oldPart = (Part)attachments.put(newPart.getContentId(), newPart); 246 247 if (oldPart != null) 248 { 249 orderedAttachments.remove(oldPart); 250 attachments.remove(oldPart.getContentLocation()); 251 } 252 253 orderedAttachments.add(newPart); 254 255 if (newPart.getContentLocation() != null) 256 { 257 attachments.put(newPart.getContentLocation(), newPart); 258 } 259 260 return oldPart; 261 } 262 263 public Part createAttachmentPart(Object datahandler) 264 throws org.jboss.axis.AxisFault 265 { 266 267 Integer key = new Integer (datahandler.hashCode()); 269 if (stackDataHandler.containsKey(key)) 270 { 271 return (Part)stackDataHandler.get(key); 272 } 273 274 multipart = null; 275 276 dimemultipart = null; 277 278 mergeinAttachments(); 279 280 if (!(datahandler instanceof javax.activation.DataHandler )) 281 { 282 throw new org.jboss.axis.AxisFault(Messages.getMessage("unsupportedAttach", datahandler.getClass().getName(), 283 javax.activation.DataHandler .class.getName())); 284 } 285 286 Part ret = 287 new AttachmentPartImpl((javax.activation.DataHandler )datahandler); 288 289 addAttachmentPart(ret); 290 291 stackDataHandler.put(key, ret); 293 294 return ret; 295 } 296 297 303 public void setAttachmentParts(java.util.Collection parts) 304 throws org.jboss.axis.AxisFault 305 { 306 307 removeAllAttachments(); 308 309 if ((parts != null) && !parts.isEmpty()) 310 { 311 for (java.util.Iterator i = parts.iterator(); i.hasNext();) 312 { 313 Object part = i.next(); 314 315 if (null != part) 316 { 317 if (part instanceof Part) 318 addAttachmentPart((Part)part); 319 else 320 createAttachmentPart(part); 321 } 322 } 323 } 324 } 325 326 338 public Part getAttachmentByReference(String reference) 339 throws org.jboss.axis.AxisFault 340 { 341 342 if (null == reference) 343 { 344 return null; 345 } 346 347 reference = reference.trim(); 348 349 if (0 == reference.length()) 350 { 351 return null; 352 } 353 354 mergeinAttachments(); 355 356 359 Part ret = (Part)attachments.get(reference); 360 if (null != ret) return ret; 361 362 363 if (!reference.startsWith(Attachments.CIDprefix) && (null != contentLocation)) 364 { 365 367 String fqreference = contentLocation; 368 369 if (!fqreference.endsWith("/")) 370 { 371 fqreference += "/"; 372 } 373 374 if (reference.startsWith("/")) 375 { 376 fqreference += reference.substring(1); 377 } 378 else 379 { 380 fqreference += reference; 381 } 382 383 ret = (AttachmentPartImpl)attachments.get(fqreference); 385 } 386 387 if (null == ret && reference.startsWith(Attachments.CIDprefix)) 388 { 389 ret = (Part)attachments.get(reference.substring(4)); 391 } 392 393 return ret; 394 } 395 396 402 public java.util.Collection getAttachments() 403 throws org.jboss.axis.AxisFault 404 { 405 406 mergeinAttachments(); 407 408 return new LinkedList (orderedAttachments); 409 } 410 411 417 public Part getRootPart() 418 { 419 return soapPart; 420 } 421 422 public void setRootPart(Part newRoot) 423 { 424 425 try 426 { 427 this.soapPart = (MessagePart)newRoot; 428 multipart = null; 429 dimemultipart = null; 430 } 431 catch (ClassCastException e) 432 { 433 throw new ClassCastException (Messages.getMessage("onlySOAPParts")); 434 } 435 } 436 437 440 javax.mail.internet.MimeMultipart multipart = null; 441 DimeMultiPart dimemultipart = null; 442 443 449 public long getContentLength() throws org.jboss.axis.AxisFault 450 { 451 452 mergeinAttachments(); 453 454 int sendtype = this.sendtype == SEND_TYPE_NOTSET ? SEND_TYPE_DEFAULT : this.sendtype; 455 456 try 457 { 458 if (sendtype == SEND_TYPE_MIME) 459 return org.jboss.axis.attachments.MimeUtils.getContentLength(multipart != null ? multipart : (multipart = org.jboss.axis.attachments.MimeUtils.createMP(soapPart.getAsString(), orderedAttachments))); 460 else if (sendtype == SEND_TYPE_DIME) return createDimeMessage().getTransmissionSize(); 461 } 462 catch (Exception e) 463 { 464 throw AxisFault.makeFault(e); 465 } 466 return 0; 467 } 468 469 475 protected DimeMultiPart createDimeMessage() throws org.jboss.axis.AxisFault 476 { 477 int sendtype = this.sendtype == SEND_TYPE_NOTSET ? SEND_TYPE_DEFAULT : this.sendtype; 478 if (sendtype == SEND_TYPE_DIME) 479 { 480 if (dimemultipart == null) 481 { 482 483 dimemultipart = new DimeMultiPart(); 484 dimemultipart.addBodyPart(new DimeBodyPart(soapPart.getAsBytes(), DimeTypeNameFormat.URI, 485 "http://schemas.xmlsoap.org/soap/envelope/", 486 "uuid:714C6C40-4531-442E-A498-3AC614200295")); 487 488 for (java.util.Iterator i = orderedAttachments.iterator(); 489 i.hasNext();) 490 { 491 AttachmentPartImpl part = (AttachmentPartImpl)i.next(); 492 DataHandler dh = AttachmentUtils. 493 getActivationDataHandler(part); 494 dimemultipart.addBodyPart(new 495 DimeBodyPart(dh, part.getContentId())); 496 } 497 } 498 } 499 return dimemultipart; 500 } 501 502 508 public void writeContentToStream(java.io.OutputStream os) 509 throws org.jboss.axis.AxisFault 510 { 511 int sendtype = this.sendtype == SEND_TYPE_NOTSET ? 512 SEND_TYPE_DEFAULT : this.sendtype; 513 try 514 { 515 516 mergeinAttachments(); 517 if (sendtype == SEND_TYPE_MIME) 518 { 519 org.jboss.axis.attachments.MimeUtils.writeToMultiPartStream(os, 520 (multipart != null) ? multipart : (multipart = 521 org.jboss.axis.attachments.MimeUtils.createMP(soapPart.getAsString(), orderedAttachments))); 522 } 523 else if (sendtype == SEND_TYPE_DIME) createDimeMessage().write(os); 524 } 525 catch (Exception e) 526 { 527 throw org.jboss.axis.AxisFault.makeFault(e); 528 } 529 } 530 531 537 public String getContentType() throws org.jboss.axis.AxisFault 538 { 539 540 mergeinAttachments(); 541 542 int sendtype = this.sendtype == SEND_TYPE_NOTSET ? SEND_TYPE_DEFAULT : 543 this.sendtype; 544 if (sendtype == SEND_TYPE_MIME) 545 return org.jboss.axis.attachments.MimeUtils.getContentType((multipart 546 != null) 547 ? multipart 548 : (multipart = 549 org.jboss.axis.attachments.MimeUtils.createMP(soapPart.getAsString(), 550 orderedAttachments))); 551 else 552 return org.jboss.axis.Message.MIME_APPLICATION_DIME; 553 } 554 555 560 public int getAttachmentCount() 561 { 562 563 try 564 { 565 mergeinAttachments(); 566 567 soapPart.getAsString(); 570 571 return orderedAttachments.size(); 572 } 573 catch (AxisFault e) 574 { 575 log.warn(Messages.getMessage("exception00"), e); 576 } 577 578 return 0; 579 } 580 581 588 public boolean isAttachment(Object value) 589 { 590 return AttachmentUtils.isAttachment(value); 591 } 592 593 599 public void removeAllAttachments() 600 { 601 try 602 { 603 multipart = null; 604 dimemultipart = null; 605 mergeinAttachments(); 606 attachments.clear(); 607 orderedAttachments.clear(); 608 stackDataHandler.clear(); 609 } 610 catch (AxisFault af) 611 { 612 log.warn(Messages.getMessage("exception00"), af); 613 } 614 } 615 616 628 public java.util.Iterator getAttachments(javax.xml.soap.MimeHeaders headers) 629 { 630 java.util.Vector vecParts = new java.util.Vector (); 631 java.util.Iterator iterator = GetAttachmentsIterator(); 632 while (iterator.hasNext()) 633 { 634 Part part = (Part)iterator.next(); 635 if (part instanceof AttachmentPartImpl) 636 { 637 if (((AttachmentPartImpl)part).matches(headers)) 638 { 639 vecParts.add(part); 640 } 641 } 642 } 643 return vecParts.iterator(); 644 } 645 646 654 private java.util.Iterator GetAttachmentsIterator() 655 { 656 java.util.Iterator iterator = attachments.values().iterator(); 657 return iterator; 658 } 659 660 667 public Part createAttachmentPart() throws org.jboss.axis.AxisFault 668 { 669 return new AttachmentPartImpl(); 670 } 671 672 public void setSendType(int sendtype) 673 { 674 if (sendtype < 1) 675 throw new IllegalArgumentException (""); 676 if (sendtype > SEND_TYPE_MAX) 677 throw new IllegalArgumentException (""); 678 this.sendtype = sendtype; 679 } 680 681 public int getSendType() 682 { 683 return sendtype; 684 } 685 686 690 691 public void dispose() 692 { 693 java.util.Iterator iterator = GetAttachmentsIterator(); 694 while (iterator.hasNext()) 695 { 696 Part part = (Part)iterator.next(); 697 if (part instanceof AttachmentPartImpl) 698 { 699 AttachmentPartImpl apart = (AttachmentPartImpl)part; 700 apart.dispose(); 701 } 702 } 703 704 } 705 706 715 public static int getSendType(String value) 716 { 717 if (value.equalsIgnoreCase("MIME")) return SEND_TYPE_MIME; 718 if (value.equalsIgnoreCase("DIME")) return SEND_TYPE_DIME; 719 if (value.equalsIgnoreCase("NONE")) return SEND_TYPE_NONE; 720 return SEND_TYPE_NOTSET; 721 } 722 723 729 public static String getSendTypeString(int value) 730 { 731 if (value == SEND_TYPE_MIME) 732 { 733 return "MIME"; 734 } 735 if (value == SEND_TYPE_DIME) 736 { 737 return "DIME"; 738 } 739 if (value == SEND_TYPE_NONE) 740 { 741 return "NONE"; 742 } 743 return null; 744 } 745 } | Popular Tags |