1 16 17 package org.jboss.axis; 18 19 import org.jboss.axis.attachments.AttachmentSupport; 20 import org.jboss.axis.attachments.Attachments; 21 import org.jboss.axis.message.MimeHeadersImpl; 22 import org.jboss.axis.message.SOAPEnvelopeAxisImpl; 23 import org.jboss.axis.message.SOAPMessageAxisImpl; 24 import org.jboss.axis.soap.SOAPConstants; 25 import org.jboss.axis.transport.http.HTTPConstants; 26 import org.jboss.axis.utils.Messages; 27 import org.jboss.axis.utils.XMLUtils; 28 import org.jboss.logging.Logger; 29 30 import javax.xml.soap.AttachmentPart ; 31 import javax.xml.soap.SOAPBody ; 32 import javax.xml.soap.SOAPException ; 33 import javax.xml.soap.SOAPHeader ; 34 import javax.xml.soap.SOAPMessage ; 35 import java.io.BufferedWriter ; 36 import java.io.IOException ; 37 import java.io.OutputStreamWriter ; 38 import java.io.Writer ; 39 import java.lang.reflect.Constructor ; 40 import java.lang.reflect.InvocationTargetException ; 41 import java.util.Collections ; 42 import java.util.Iterator ; 43 44 58 public class Message extends SOAPMessageAxisImpl 59 { 60 63 private static Logger log = Logger.getLogger(Message.class.getName()); 64 65 68 public static final String REQUEST = "request"; 69 70 73 public static final String RESPONSE = "response"; 74 75 78 public static final String MIME_MULTIPART_RELATED = "multipart/related"; 79 80 83 public static final String MIME_APPLICATION_DIME = "application/dime"; 84 85 88 public static final String DEFAULT_ATTACHMNET_IMPL = "org.jboss.axis.attachments.AttachmentsImpl"; 89 90 93 private static String mAttachmentsImplClassName = DEFAULT_ATTACHMNET_IMPL; 94 95 98 public static final String MIME_UNKNOWN = " "; 99 100 107 private String messageType; 108 109 112 private MessagePart mSOAPPart; 113 114 118 private Attachments mAttachments = null; 119 120 private MimeHeadersImpl headers; 121 122 private boolean saveRequired = true; 123 124 129 public static String getAttachmentImplClassName() 130 { 131 return mAttachmentsImplClassName; 132 } 133 134 private MessageContext msgContext; 135 136 141 public String getMessageType() 142 { 143 return messageType; 144 } 145 146 151 public void setMessageType(String messageType) 152 { 153 this.messageType = messageType; 154 } 155 156 161 public MessageContext getMessageContext() 162 { 163 return msgContext; 164 } 165 166 171 public void setMessageContext(MessageContext msgContext) 172 { 173 this.msgContext = msgContext; 174 } 175 176 190 public Message(Object initialContents, boolean bodyInStream) 191 { 192 setup(initialContents, bodyInStream, null, null, null); 193 } 194 195 210 public Message(Object initialContents, boolean bodyInStream, javax.xml.soap.MimeHeaders headers) 211 { 212 setup(initialContents, bodyInStream, null, null, headers); 213 } 214 215 228 public Message(Object initialContents, MimeHeadersImpl headers) 229 { 230 setup(initialContents, true, null, null, headers); 231 } 232 233 250 public Message(Object initialContents, 251 boolean bodyInStream, 252 String contentType, 253 String contentLocation) 254 { 255 setup(initialContents, bodyInStream, contentType, contentLocation, null); 256 } 257 258 265 public Message(Object initialContents) 266 { 267 setup(initialContents, false, null, null, null); 268 } 269 270 282 private void setup(Object initialContents, boolean bodyInStream, 283 String contentType, String contentLocation, 284 javax.xml.soap.MimeHeaders mimeHeaders) 285 { 286 287 if (contentType == null && mimeHeaders != null) 288 { 289 String contentTypes[] = mimeHeaders.getHeader("Content-Type"); 290 contentType = (contentTypes != null) ? contentTypes[0] : null; 291 } 292 if (contentLocation == null && mimeHeaders != null) 293 { 294 String contentLocations[] = mimeHeaders.getHeader("Content-Location"); 295 contentLocation = (contentLocations != null) ? contentLocations[0] : null; 296 } 297 if (contentType != null) 298 { 299 int delimiterIndex = contentType.lastIndexOf("charset"); 300 if (delimiterIndex > 0) 301 { 302 String charsetPart = contentType.substring(delimiterIndex); 303 int charsetIndex = charsetPart.indexOf('='); 304 String charset = charsetPart.substring(charsetIndex + 1).trim(); 305 if ((charset.startsWith("\"") && charset.endsWith("\"")) 306 || (charset.startsWith("'") && charset.endsWith("'"))) 307 { 308 charset = charset.substring(1, charset.length() - 1); 309 } 310 try 311 { 312 setProperty(SOAPMessage.CHARACTER_SET_ENCODING, charset); 313 } 314 catch (SOAPException e) 315 { 316 } 317 } 318 } 319 if (AttachmentSupport.isAttachmentSupportEnabled(getMessageContext())) 325 { 326 Class attachImpl = AttachmentSupport.getImplementationClass(); 330 Constructor attachImplConstr = attachImpl.getConstructors()[0]; 331 try 332 { 333 mAttachments = (Attachments)attachImplConstr.newInstance(new Object []{initialContents, 334 contentType, contentLocation}); 335 336 mSOAPPart = (MessagePart)mAttachments.getRootPart(); 338 } 339 catch (InvocationTargetException ex) 340 { 341 log.fatal(Messages.getMessage("invocationTargetException00"), 342 ex); 343 throw new RuntimeException (ex.getMessage()); 344 } 345 catch (InstantiationException ex) 346 { 347 log.fatal(Messages.getMessage("instantiationException00"), 348 ex); 349 throw new RuntimeException (ex.getMessage()); 350 } 351 catch (IllegalAccessException ex) 352 { 353 log.fatal(Messages.getMessage("illegalAccessException00"), 354 ex); 355 throw new RuntimeException (ex.getMessage()); 356 } 357 } 358 else if (contentType != null && contentType.startsWith("multipart")) 359 { 360 throw new RuntimeException (Messages.getMessage("noAttachments")); 361 } 362 363 if (null == mSOAPPart) 365 { 366 mSOAPPart = new MessagePart(this, initialContents, bodyInStream); 367 } 368 else 369 mSOAPPart.setMessage(this); 370 371 if (mAttachments != null) mAttachments.setRootPart(mSOAPPart); 373 374 headers = (mimeHeaders == null) ? new MimeHeadersImpl() : new MimeHeadersImpl(mimeHeaders); 375 } 376 377 387 public javax.xml.soap.SOAPPart getSOAPPart() 388 { 389 return mSOAPPart; 390 } 391 392 401 public String getSOAPPartAsString() throws org.jboss.axis.AxisFault 402 { 403 return mSOAPPart.getAsString(); 404 } 405 406 415 public byte[] getSOAPPartAsBytes() throws org.jboss.axis.AxisFault 416 { 417 return mSOAPPart.getAsBytes(); 418 } 419 420 426 public SOAPEnvelopeAxisImpl getSOAPEnvelope() throws AxisFault 427 { 428 return mSOAPPart.getAsSOAPEnvelope(); 429 } 430 431 441 public Attachments getAttachmentsImpl() 442 { 443 return mAttachments; 444 } 445 446 455 public String getContentType(SOAPConstants sc) throws AxisFault 456 { 457 458 int sendType = Attachments.SEND_TYPE_NOTSET; 459 if ((msgContext != null) && (msgContext.getService() != null)) 460 { 461 sendType = msgContext.getService().getSendType(); 462 } 463 464 if (sendType != Attachments.SEND_TYPE_NONE) 465 { 466 if (mSOAPPart != null) 470 { 471 mSOAPPart.getAsBytes(); 472 } 473 } 474 475 String encoding = null; 479 try 480 { 481 encoding = (String )getProperty(SOAPMessage.CHARACTER_SET_ENCODING); 482 } 483 catch (SOAPException ignore) 484 { 485 } 486 if (encoding == null) 487 { 488 encoding = XMLUtils.getEncoding().toLowerCase(); 489 } 490 491 String ret = sc.getContentType(); 492 493 SOAPEnvelopeAxisImpl envelope = getSOAPEnvelope(); 495 if (envelope != null) 496 { 497 if (envelope.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS) 498 { 499 ret = HTTPConstants.HEADER_ACCEPT_APPL_SOAP + "; charset=" + encoding; 500 } 501 } 502 503 if (mAttachments != null && 0 != mAttachments.getAttachmentCount()) 504 { 505 ret = mAttachments.getContentType(); 506 } 507 508 return ret; 509 } 510 511 519 public long getContentLength() throws org.jboss.axis.AxisFault 520 { 521 long ret = mSOAPPart.getAsBytes().length; 522 if (mAttachments != null && 0 < mAttachments.getAttachmentCount()) 523 { 524 ret = mAttachments.getContentLength(); 525 } 526 return ret; 527 } 528 529 546 public void writeTo(java.io.OutputStream os) throws SOAPException , IOException 547 { 548 if (mAttachments == null || 0 == mAttachments.getAttachmentCount()) 550 { 551 try 552 { 553 String charEncoding = (String )getProperty(SOAPMessage.CHARACTER_SET_ENCODING); 554 if (charEncoding == null) 555 { 556 charEncoding = "UTF-8"; 557 } 558 Writer writer = new OutputStreamWriter (os, charEncoding); 559 writer = new BufferedWriter (writer); 560 561 String incXMLDecl = (String )getProperty(SOAPMessage.WRITE_XML_DECLARATION); 563 if (incXMLDecl == null) 564 { 565 incXMLDecl = "false"; 566 } 567 if (incXMLDecl.equalsIgnoreCase("true")) 568 { 569 writer.write("<?xml version=\"1.0\" encoding=\"" + charEncoding + "\"?>"); 570 } 571 mSOAPPart.writeTo(writer); 572 writer.flush(); 573 } 574 catch (java.io.IOException e) 575 { 576 log.error(Messages.getMessage("javaIOException00"), e); 577 } 578 } 579 else 580 { 581 try 582 { 583 mAttachments.writeContentToStream(os); 584 } 585 catch (java.lang.Exception e) 586 { 587 log.error(Messages.getMessage("exception00"), e); 588 } 589 } 590 } 591 592 private java.util.Hashtable mProps = new java.util.Hashtable (); 593 594 public SOAPBody getSOAPBody() throws SOAPException 595 { 596 return mSOAPPart.getEnvelope().getBody(); 597 } 598 599 public SOAPHeader getSOAPHeader() throws SOAPException 600 { 601 return mSOAPPart.getEnvelope().getHeader(); 602 } 603 604 public void setProperty(String property, Object value) throws SOAPException 605 { 606 mProps.put(property, value); 607 } 608 609 public Object getProperty(String property) throws SOAPException 610 { 611 return mProps.get(property); 612 } 613 614 623 public String getContentDescription() 624 { 625 String values[] = headers.getHeader(HTTPConstants.HEADER_CONTENT_DESCRIPTION); 626 if (values != null && values.length > 0) 627 return values[0]; 628 return null; 629 } 630 631 639 public void setContentDescription(String description) 640 { 641 headers.setHeader(HTTPConstants.HEADER_CONTENT_DESCRIPTION, description); 642 } 643 644 668 public void saveChanges() throws SOAPException 669 { 670 if (mAttachments != null && 0 < mAttachments.getAttachmentCount()) 671 { 672 try 673 { 674 headers.setHeader("Content-Type", mAttachments.getContentType()); 675 } 676 catch (AxisFault af) 677 { 678 log.error(Messages.getMessage("exception00"), af); 679 } 680 } 681 saveRequired = false; 682 try 683 { 684 685 getSOAPPartAsString(); 686 } 687 catch (AxisFault axisFault) 688 { 689 log.error(Messages.getMessage("exception00"), axisFault); 690 } 691 } 692 693 702 public boolean saveRequired() 703 { 704 return saveRequired; 705 } 706 707 715 public javax.xml.soap.MimeHeaders getMimeHeaders() 716 { 717 return headers; 718 } 719 720 726 public void removeAllAttachments() 727 { 728 mAttachments.removeAllAttachments(); 729 } 730 731 739 public int countAttachments() 740 { 741 return mAttachments == null ? 0 : mAttachments.getAttachmentCount(); 742 } 743 744 751 public Iterator getAttachments() 752 { 753 try 754 { 755 if (mAttachments != null && 0 != mAttachments.getAttachmentCount()) 756 { 757 return mAttachments.getAttachments().iterator(); 758 } 759 } 760 catch (AxisFault af) 761 { 762 log.error(Messages.getMessage("exception00"), af); 763 } 764 return Collections.EMPTY_LIST.iterator(); 765 } 766 767 779 public Iterator getAttachments(javax.xml.soap.MimeHeaders headers) 780 { 781 return mAttachments.getAttachments(headers); 782 } 783 784 796 public void addAttachmentPart(AttachmentPart attachmentpart) 797 { 798 try 799 { 800 mAttachments.addAttachmentPart((org.jboss.axis.Part)attachmentpart); 801 } 802 catch (AxisFault af) 803 { 804 log.error(Messages.getMessage("exception00"), af); 805 } 806 } 807 808 819 public AttachmentPart createAttachmentPart() 820 { 821 if (!AttachmentSupport.isAttachmentSupportEnabled(getMessageContext())) 822 { 823 throw new RuntimeException (Messages.getMessage("noAttachments")); 824 } 825 826 try 827 { 828 return (AttachmentPart )mAttachments.createAttachmentPart(); 829 } 830 catch (AxisFault af) 831 { 832 log.error(Messages.getMessage("exception00"), af); 833 } 834 return null; 835 } 836 837 840 public void dispose() 841 { 842 if (mAttachments != null) 843 { 844 mAttachments.dispose(); 845 } 846 } 847 } 848 | Popular Tags |