1 16 17 package org.apache.axis; 18 19 import org.apache.axis.attachments.Attachments; 20 import org.apache.axis.components.logger.LogFactory; 21 import org.apache.axis.message.SOAPEnvelope; 22 import org.apache.axis.message.MimeHeaders; 23 import org.apache.axis.soap.SOAPConstants; 24 import org.apache.axis.transport.http.HTTPConstants; 25 import org.apache.axis.utils.ClassUtils; 26 import org.apache.axis.utils.Messages; 27 import org.apache.axis.utils.XMLUtils; 28 import org.apache.commons.logging.Log; 29 30 import javax.xml.soap.AttachmentPart ; 31 import javax.xml.soap.SOAPException ; 32 import javax.xml.soap.SOAPBody ; 33 import javax.xml.soap.SOAPHeader ; 34 import javax.xml.soap.SOAPMessage ; 35 36 import java.io.IOException ; 37 import java.lang.reflect.Constructor ; 38 import java.lang.reflect.InvocationTargetException ; 39 import java.util.Iterator ; 40 import java.util.Collections ; 41 42 56 public class Message extends javax.xml.soap.SOAPMessage 57 implements java.io.Serializable { 58 59 62 protected static Log log = 63 LogFactory.getLog(Message.class.getName()); 64 65 66 public static final String REQUEST = "request"; 67 68 69 public static final String RESPONSE = "response"; 70 71 72 public static final String MIME_MULTIPART_RELATED = "multipart/related"; 73 74 75 public static final String MIME_APPLICATION_DIME = "application/dime"; 76 77 78 public static final String DEFAULT_ATTACHMNET_IMPL="org.apache.axis.attachments.AttachmentsImpl"; 79 80 81 private static String mAttachmentsImplClassName=DEFAULT_ATTACHMNET_IMPL; 82 83 84 public static final String MIME_UNKNOWN = " "; 85 86 93 private String messageType; 94 95 98 private SOAPPart mSOAPPart; 99 100 104 private Attachments mAttachments = null; 105 106 private MimeHeaders headers; 107 108 private boolean saveRequired = true; 109 110 115 public static String getAttachmentImplClassName(){ 116 return mAttachmentsImplClassName; 117 } 118 119 private MessageContext msgContext; 120 121 126 public String getMessageType() { 127 return messageType; 128 } 129 130 135 public void setMessageType(String messageType) { 136 this.messageType = messageType; 137 } 138 139 144 public MessageContext getMessageContext() { 145 return msgContext; 146 } 147 148 153 public void setMessageContext(MessageContext msgContext) { 154 this.msgContext = msgContext; 155 } 156 157 171 public Message(Object initialContents, boolean bodyInStream) { 172 setup(initialContents, bodyInStream, null, null, null); 173 } 174 175 190 public Message(Object initialContents, boolean bodyInStream, javax.xml.soap.MimeHeaders headers) { 191 setup(initialContents, bodyInStream, null, null, headers); 192 } 193 194 207 public Message(Object initialContents, MimeHeaders headers) { 208 setup(initialContents, true, null, null, headers); 209 } 210 211 228 public Message(Object initialContents, 229 boolean bodyInStream, 230 String contentType, 231 String contentLocation) { 232 setup(initialContents, bodyInStream, contentType, contentLocation, null); 233 } 234 235 242 public Message(Object initialContents) { 243 setup(initialContents, false, null, null, null); 244 } 245 246 private static Class attachImpl = null; 247 248 private static boolean checkForAttachmentSupport = true; 250 251 private static boolean attachmentSupportEnabled = false; 252 253 private static synchronized boolean isAttachmentSupportEnabled(MessageContext mc) { 254 if (checkForAttachmentSupport) { 255 checkForAttachmentSupport = false; 257 try { 258 String attachImpName= AxisEngine.DEFAULT_ATTACHMENT_IMPL; 259 if(null != mc){ 260 AxisEngine ae= mc.getAxisEngine(); 261 if(null != ae){ 262 attachImpName= (String )ae.getOption( 263 AxisEngine.PROP_ATTACHMENT_IMPLEMENTATION); 264 } 265 } 266 if(null == attachImpName){ 267 attachImpName=AxisEngine.DEFAULT_ATTACHMENT_IMPL; 268 } 269 270 273 ClassUtils.forName("javax.activation.DataHandler"); 274 ClassUtils.forName("javax.mail.internet.MimeMultipart"); 275 276 attachImpl = ClassUtils.forName(attachImpName); 277 278 attachmentSupportEnabled = true; 279 } catch (ClassNotFoundException ex) { 280 } catch (java.lang.NoClassDefFoundError ex) { 282 } 284 log.debug(Messages.getMessage("attachEnabled") + " " + 285 attachmentSupportEnabled); 286 } 287 return attachmentSupportEnabled; 288 } 289 290 302 private void setup(Object initialContents, boolean bodyInStream, 303 String contentType, String contentLocation, 304 javax.xml.soap.MimeHeaders mimeHeaders) { 305 306 if(contentType == null && mimeHeaders != null) { 307 String contentTypes[] = mimeHeaders.getHeader("Content-Type"); 308 contentType = (contentTypes != null)? contentTypes[0] : null; 309 } 310 if(contentLocation == null && mimeHeaders != null) { 311 String contentLocations[] = mimeHeaders.getHeader("Content-Location"); 312 contentLocation = (contentLocations != null)? contentLocations[0] : null; 313 } 314 if (contentType != null) { 315 int delimiterIndex = contentType.lastIndexOf("charset"); 316 if (delimiterIndex > 0) { 317 String charsetPart = contentType.substring(delimiterIndex); 318 int charsetIndex = charsetPart.indexOf('='); 319 String charset = charsetPart.substring(charsetIndex + 1).trim(); 320 if ((charset.startsWith("\"") && charset.endsWith("\"")) 321 || (charset.startsWith("'") && charset.endsWith("'"))) { 322 charset = charset.substring(1, charset.length() - 1); 323 } 324 try { 325 setProperty(SOAPMessage.CHARACTER_SET_ENCODING, charset); 326 } catch (SOAPException e) { 327 } 328 } 329 } 330 if (isAttachmentSupportEnabled(getMessageContext())) { 336 Constructor attachImplConstr = attachImpl.getConstructors()[0]; 340 try { 341 mAttachments = (Attachments) attachImplConstr.newInstance( 342 new Object [] { initialContents, 343 contentType, contentLocation}); 344 345 mSOAPPart = (SOAPPart) mAttachments.getRootPart(); 347 } catch (InvocationTargetException ex) { 348 log.fatal(Messages.getMessage("invocationTargetException00"), 349 ex); 350 throw new RuntimeException (ex.getMessage()); 351 } catch (InstantiationException ex) { 352 log.fatal(Messages.getMessage("instantiationException00"), 353 ex); 354 throw new RuntimeException (ex.getMessage()); 355 } catch (IllegalAccessException ex) { 356 log.fatal(Messages.getMessage("illegalAccessException00"), 357 ex); 358 throw new RuntimeException (ex.getMessage()); 359 } 360 } else if (contentType != null && contentType.startsWith("multipart")){ 361 throw new RuntimeException (Messages.getMessage("noAttachments")); 362 } 363 364 if (null == mSOAPPart) { 366 mSOAPPart = new SOAPPart(this, initialContents, bodyInStream); 367 } 368 else 369 mSOAPPart.setMessage(this); 370 371 if(mAttachments!=null) mAttachments.setRootPart(mSOAPPart); 373 374 headers = (mimeHeaders == null) ? new MimeHeaders() : new MimeHeaders(mimeHeaders); 375 } 376 377 387 public javax.xml.soap.SOAPPart getSOAPPart() { 388 return mSOAPPart; 389 } 390 391 400 public String getSOAPPartAsString() throws org.apache.axis.AxisFault { 401 return mSOAPPart.getAsString(); 402 } 403 404 413 public byte[] getSOAPPartAsBytes() throws org.apache.axis.AxisFault { 414 return mSOAPPart.getAsBytes(); 415 } 416 417 423 public SOAPEnvelope getSOAPEnvelope() throws AxisFault { 424 return mSOAPPart.getAsSOAPEnvelope(); 425 } 426 427 437 public Attachments getAttachmentsImpl() { 438 return mAttachments; 439 } 440 441 450 public String getContentType(SOAPConstants sc) throws AxisFault { 451 boolean soap12 = false; 452 453 if(sc != null) { 454 if(sc == SOAPConstants.SOAP12_CONSTANTS) { 455 soap12 = true; 456 } 457 } else { 458 SOAPEnvelope envelope = getSOAPEnvelope(); 460 if (envelope != null) { 461 if (envelope.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS) { 462 soap12 = true; 463 } 464 } 465 } 466 467 String encoding = XMLUtils.getEncoding(this, msgContext);; 468 String ret = sc.getContentType() + "; charset=" + encoding.toLowerCase(); 469 470 if (soap12) { 472 ret = HTTPConstants.HEADER_ACCEPT_APPL_SOAP +"; charset=" + encoding; 473 } 474 475 if (getSendType() != Attachments.SEND_TYPE_NONE && mAttachments != null && 476 0 != mAttachments.getAttachmentCount()) { 477 ret = mAttachments.getContentType(); 478 } 479 return ret; 480 } 481 482 private int getSendType() { 483 int sendType = Attachments.SEND_TYPE_NOTSET; 484 if ((msgContext != null) && (msgContext.getService() != null)) { 485 sendType = msgContext.getService().getSendType(); 486 } 487 return sendType; 488 } 489 490 498 public long getContentLength() throws org.apache.axis.AxisFault { 499 long ret = mSOAPPart.getContentLength(); 500 if (mAttachments != null && 0 < mAttachments.getAttachmentCount()) { 501 ret = mAttachments.getContentLength(); 502 } 503 return ret; 504 } 505 506 522 public void writeTo(java.io.OutputStream os) throws SOAPException , IOException { 523 if (getSendType() == Attachments.SEND_TYPE_NONE || mAttachments == null || 0 == mAttachments.getAttachmentCount()) { 525 try { 526 String charEncoding = XMLUtils.getEncoding(this, msgContext);; 527 mSOAPPart.setEncoding(charEncoding); 528 mSOAPPart.writeTo(os); 529 } catch (java.io.IOException e) { 530 log.error(Messages.getMessage("javaIOException00"), e); 531 } 532 } else { 533 try { 534 mAttachments.writeContentToStream(os); 535 } catch (java.lang.Exception e) { 536 log.error(Messages.getMessage("exception00"), e); 537 } 538 } 539 } 540 541 private java.util.Hashtable mProps = new java.util.Hashtable (); 542 543 public SOAPBody getSOAPBody() throws SOAPException { 544 return mSOAPPart.getEnvelope().getBody(); 545 } 546 547 public SOAPHeader getSOAPHeader() throws SOAPException { 548 return mSOAPPart.getEnvelope().getHeader(); 549 } 550 551 public void setProperty(String property, Object value) throws SOAPException { 552 mProps.put(property, value); 553 } 554 555 public Object getProperty(String property) throws SOAPException { 556 return mProps.get(property); 557 } 558 559 567 public String getContentDescription() { 568 String values[] = headers.getHeader(HTTPConstants.HEADER_CONTENT_DESCRIPTION); 569 if(values != null && values.length > 0) 570 return values[0]; 571 return null; 572 } 573 574 581 public void setContentDescription(String description) { 582 headers.setHeader(HTTPConstants.HEADER_CONTENT_DESCRIPTION, description); 583 } 584 585 608 public void saveChanges() throws SOAPException { 609 if (mAttachments != null && 0 < mAttachments.getAttachmentCount()) { 610 try { 611 headers.setHeader("Content-Type",mAttachments.getContentType()); 612 } catch (AxisFault af){ 613 log.error(Messages.getMessage("exception00"), af); 614 } 615 } 616 saveRequired = false; 617 try { 618 619 mSOAPPart.saveChanges(); 620 } catch (AxisFault axisFault) { 621 log.error(Messages.getMessage("exception00"), axisFault); 622 } 623 } 624 625 633 public boolean saveRequired() { 634 return saveRequired; 635 } 636 637 644 public javax.xml.soap.MimeHeaders getMimeHeaders() { 645 return headers; 646 } 647 648 654 public void removeAllAttachments(){ 655 mAttachments.removeAllAttachments(); 656 } 657 658 665 public int countAttachments(){ 666 return mAttachments == null ? 0 : mAttachments.getAttachmentCount(); 667 } 668 669 675 public Iterator getAttachments(){ 676 try { 677 if (mAttachments != null && 0 != mAttachments.getAttachmentCount()) { 678 return mAttachments.getAttachments().iterator(); 679 } 680 } catch (AxisFault af){ 681 log.error(Messages.getMessage("exception00"), af); 682 } 683 return Collections.EMPTY_LIST.iterator(); 684 } 685 686 697 public Iterator getAttachments(javax.xml.soap.MimeHeaders headers){ 698 return mAttachments.getAttachments(headers); 699 } 700 701 711 public void addAttachmentPart(AttachmentPart attachmentpart){ 712 try { 713 mAttachments.addAttachmentPart((org.apache.axis.Part)attachmentpart); 714 } catch (AxisFault af){ 715 log.error(Messages.getMessage("exception00"), af); 716 } 717 } 718 719 729 public AttachmentPart createAttachmentPart() { 730 if (!isAttachmentSupportEnabled(getMessageContext())) { 731 throw new RuntimeException (Messages.getMessage("noAttachments")); 732 } 733 734 try { 735 return (AttachmentPart ) mAttachments.createAttachmentPart(); 736 } catch (AxisFault af){ 737 log.error(Messages.getMessage("exception00"), af); 738 } 739 return null; 740 } 741 742 745 public void dispose() { 746 if(mAttachments!=null) { 747 mAttachments.dispose(); 748 } 749 } 750 } 751 | Popular Tags |