| 1 |
/* |
| 2 |
* Copyright 2001-2004 The Apache Software Foundation. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 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 |
/** |
| 43 |
* A complete SOAP (and/or XML-RPC, eventually) message. |
| 44 |
* Includes both the root part (as a SOAPPart), and zero or |
| 45 |
* more MIME attachments (as AttachmentParts). |
| 46 |
* <p> |
| 47 |
* Eventually should be refactored to generalize SOAPPart |
| 48 |
* for multiple protocols (XML-RPC?). |
| 49 |
* |
| 50 |
* @author Rob Jellinghaus ([email protected]) |
| 51 |
* @author Doug Davis ([email protected]) |
| 52 |
* @author Glen Daniels ([email protected]) |
| 53 |
* @author Rick Rineholt |
| 54 |
* @author Heejune Ahn ([email protected]) |
| 55 |
*/ |
| 56 |
public class Message extends javax.xml.soap.SOAPMessage |
| 57 |
implements java.io.Serializable { |
| 58 |
|
| 59 |
/** |
| 60 |
* The <code>Log</code> that this class uses for logging all messages. |
| 61 |
*/ |
| 62 |
protected static Log log = |
| 63 |
LogFactory.getLog(Message.class.getName()); |
| 64 |
|
| 65 |
/** Message is a request. */ |
| 66 |
public static final String REQUEST = "request"; |
| 67 |
|
| 68 |
/** Message is a a response. */ |
| 69 |
public static final String RESPONSE = "response"; |
| 70 |
|
| 71 |
/** MIME parts defined for messages. */ |
| 72 |
public static final String MIME_MULTIPART_RELATED = "multipart/related"; |
| 73 |
|
| 74 |
/** DIME parts defined for messages. */ |
| 75 |
public static final String MIME_APPLICATION_DIME = "application/dime"; |
| 76 |
|
| 77 |
/** Content Type for MTOM/XOP */ |
| 78 |
public static final String CONTENT_TYPE_MTOM = "application/xop+xml"; |
| 79 |
|
| 80 |
/** Default Attachments Implementation class. */ |
| 81 |
public static final String DEFAULT_ATTACHMNET_IMPL="org.apache.axis.attachments.AttachmentsImpl"; |
| 82 |
|
| 83 |
/** Current Attachment implementation. */ |
| 84 |
private static String mAttachmentsImplClassName=DEFAULT_ATTACHMNET_IMPL; |
| 85 |
|
| 86 |
/** Look at the input stream to find the headers to decide the mime type. */ |
| 87 |
public static final String MIME_UNKNOWN = " "; |
| 88 |
|
| 89 |
// fixme: is this constrained to two values - request/response (e.g. |
| 90 |
// REQUEST and RESPONSE)? If so, this needs documenting in the get/set |
| 91 |
// methods and/or converting into a type-safe e-num. Potentially get/set |
| 92 |
// methods should check these values & throw IllegalArgumentException |
| 93 |
/** |
| 94 |
* The messageType indicates whether this is request or response. |
| 95 |
*/ |
| 96 |
private String messageType; |
| 97 |
|
| 98 |
/** |
| 99 |
* This Message's SOAPPart. Will always be here. |
| 100 |
*/ |
| 101 |
private SOAPPart mSOAPPart; |
| 102 |
|
| 103 |
/** |
| 104 |
* This Message's Attachments object, which manages the attachments |
| 105 |
* contained in this Message. |
| 106 |
*/ |
| 107 |
private Attachments mAttachments = null; |
| 108 |
|
| 109 |
private MimeHeaders headers; |
| 110 |
|
| 111 |
private boolean saveRequired = true; |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns the name of the class prividing Attachment Implementation. |
| 115 |
* |
| 116 |
* @return class name |
| 117 |
*/ |
| 118 |
public static String getAttachmentImplClassName(){ |
| 119 |
return mAttachmentsImplClassName; |
| 120 |
} |
| 121 |
|
| 122 |
private MessageContext msgContext; |
| 123 |
|
| 124 |
/** |
| 125 |
* Get the message type. |
| 126 |
* |
| 127 |
* @return the message type <code>String</code> |
| 128 |
*/ |
| 129 |
public String getMessageType() { |
| 130 |
return messageType; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Set the message type. |
| 135 |
* |
| 136 |
* @param messageType the message type <code>String</code> |
| 137 |
*/ |
| 138 |
public void setMessageType(String messageType) { |
| 139 |
this.messageType = messageType; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get the context associated with this message. |
| 144 |
* |
| 145 |
* @return the message context for this message |
| 146 |
*/ |
| 147 |
public MessageContext getMessageContext() { |
| 148 |
return msgContext; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Set the context associated with this message. |
| 153 |
* |
| 154 |
* @param msgContext the message context for this message |
| 155 |
*/ |
| 156 |
public void setMessageContext(MessageContext msgContext) { |
| 157 |
this.msgContext = msgContext; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Construct a Message, using the provided initialContents as the |
| 162 |
* contents of the Message's SOAPPart. |
| 163 |
* <p> |
| 164 |
* Eventually, genericize this to |
| 165 |
* return the RootPart instead, which will have some kind of |
| 166 |
* EnvelopeFactory to enable support for things other than SOAP. |
| 167 |
* But that all will come later, with lots of additional refactoring. |
| 168 |
* |
| 169 |
* @param initialContents may be String, byte[], InputStream, SOAPEnvelope, |
| 170 |
* or AxisFault. |
| 171 |
* @param bodyInStream is true if initialContents is an InputStream |
| 172 |
* containing just the SOAP body (no SOAP-ENV). |
| 173 |
*/ |
| 174 |
public Message(Object initialContents, boolean bodyInStream) { |
| 175 |
setup(initialContents, bodyInStream, null, null, null); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Construct a Message, using the provided initialContents as the |
| 180 |
* contents of the Message's SOAPPart. |
| 181 |
* <p> |
| 182 |
* Eventually, genericize this to |
| 183 |
* return the RootPart instead, which will have some kind of |
| 184 |
* EnvelopeFactory to enable support for things other than SOAP. |
| 185 |
* But that all will come later, with lots of additional refactoring. |
| 186 |
* |
| 187 |
* @param initialContents may be String, byte[], InputStream, SOAPEnvelope, |
| 188 |
* or AxisFault. |
| 189 |
* @param bodyInStream is true if initialContents is an InputStream |
| 190 |
* containing just the SOAP body (no SOAP-ENV). |
| 191 |
* @param headers Mime Headers. |
| 192 |
*/ |
| 193 |
public Message(Object initialContents, boolean bodyInStream, javax.xml.soap.MimeHeaders headers) { |
| 194 |
setup(initialContents, bodyInStream, null, null, headers); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Construct a Message, using the provided initialContents as the |
| 199 |
* contents of the Message's SOAPPart. |
| 200 |
* <p> |
| 201 |
* Eventually, genericize this to |
| 202 |
* return the RootPart instead, which will have some kind of |
| 203 |
* EnvelopeFactory to enable support for things other than SOAP. |
| 204 |
* But that all will come later, with lots of additional refactoring. |
| 205 |
* |
| 206 |
* @param initialContents may be String, byte[], InputStream, SOAPEnvelope, |
| 207 |
* or AxisFault. |
| 208 |
* @param headers Mime Headers. |
| 209 |
*/ |
| 210 |
public Message(Object initialContents, MimeHeaders headers) { |
| 211 |
setup(initialContents, true, null, null, headers); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Construct a Message, using the provided initialContents as the |
| 216 |
* contents of the Message's SOAPPart. |
| 217 |
* <p> |
| 218 |
* Eventually, genericize this to |
| 219 |
* return the RootPart instead, which will have some kind of |
| 220 |
* EnvelopeFactory to enable support for things other than SOAP. |
| 221 |
* But that all will come later, with lots of additional refactoring. |
| 222 |
* |
| 223 |
* @param initialContents may be String, byte[], InputStream, SOAPEnvelope, |
| 224 |
* or AxisFault |
| 225 |
* @param bodyInStream is true if initialContents is an InputStream |
| 226 |
* containing just the SOAP body (no SOAP-ENV) |
| 227 |
* @param contentType this if the contentType has been already determined |
| 228 |
* (as in the case of servlets) |
| 229 |
* @param contentLocation the location of the content |
| 230 |
*/ |
| 231 |
public Message(Object initialContents, |
| 232 |
boolean bodyInStream, |
| 233 |
String contentType, |
| 234 |
String contentLocation) { |
| 235 |
setup(initialContents, bodyInStream, contentType, contentLocation, null); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Construct a Message. An overload of Message(Object, boolean), |
| 240 |
* defaulting bodyInStream to false. |
| 241 |
* |
| 242 |
* @param initialContents may be String, byte[], InputStream, SOAPEnvelope, |
| 243 |
* or AxisFault |
| 244 |
*/ |
| 245 |
public Message(Object initialContents) { |
| 246 |
setup(initialContents, false, null, null, null); |
| 247 |
} |
| 248 |
|
| 249 |
private static Class attachImpl = null; |
| 250 |
|
| 251 |
//aviod testing and possibly failing everytime. |
| 252 |
private static boolean checkForAttachmentSupport = true; |
| 253 |
|
| 254 |
private static boolean attachmentSupportEnabled = false; |
| 255 |
|
| 256 |
private static synchronized boolean isAttachmentSupportEnabled(MessageContext mc) { |
| 257 |
if (checkForAttachmentSupport) { |
| 258 |
//aviod testing and possibly failing everytime. |
| 259 |
checkForAttachmentSupport = false; |
| 260 |
try { |
| 261 |
// Get the default setting from AxisProperties |
| 262 |
String attachImpName = AxisProperties.getProperty(AxisEngine.PROP_ATTACHMENT_IMPLEMENTATION, |
| 263 |
AxisEngine.DEFAULT_ATTACHMENT_IMPL); |
| 264 |
// override the default with any changes specified in the engine configuration |
| 265 |
if (null != mc) { |
| 266 |
AxisEngine ae = mc.getAxisEngine(); |
| 267 |
if (null != ae) { |
| 268 |
attachImpName = (String) ae.getOption( |
| 269 |
AxisEngine.PROP_ATTACHMENT_IMPLEMENTATION); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Attempt to resolve class name, verify that these are present... |
| 275 |
*/ |
| 276 |
ClassUtils.forName("javax.activation.DataHandler"); |
| 277 |
ClassUtils.forName("javax.mail.internet.MimeMultipart"); |
| 278 |
|
| 279 |
attachImpl = ClassUtils.forName(attachImpName); |
| 280 |
|
| 281 |
attachmentSupportEnabled = true; |
| 282 |
} catch (ClassNotFoundException ex) { |
| 283 |
// no support for it, leave mAttachments null. |
| 284 |
} catch (java.lang.NoClassDefFoundError ex) { |
| 285 |
// no support for it, leave mAttachments null. |
| 286 |
} catch (java.lang.SecurityException ex) { |
| 287 |
// no support for it, leave mAttachments null. |
| 288 |
} |
| 289 |
log.debug(Messages.getMessage("attachEnabled") + " " + |
| 290 |
attachmentSupportEnabled); |
| 291 |
} |
| 292 |
return attachmentSupportEnabled; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Do the work of construction. |
| 297 |
* |
| 298 |
* @param initialContents may be String, byte[], InputStream, SOAPEnvelope, |
| 299 |
* or AxisFault |
| 300 |
* @param bodyInStream is true if initialContents is an InputStream |
| 301 |
* containing just the SOAP body (no SOAP-ENV) |
| 302 |
* @param contentType this if the contentType has been already determined |
| 303 |
* (as in the case of servlets) |
| 304 |
* @param contentLocation the location of the content |
| 305 |
* @param mimeHeaders mime headers for attachments |
| 306 |
*/ |
| 307 |
private void setup(Object initialContents, boolean bodyInStream, |
| 308 |
String contentType, String contentLocation, |
| 309 |
javax.xml.soap.MimeHeaders mimeHeaders) { |
| 310 |
|
| 311 |
if(contentType == null && mimeHeaders != null) { |
| 312 |
String contentTypes[] = mimeHeaders.getHeader("Content-Type"); |
| 313 |
contentType = (contentTypes != null)? contentTypes[0] : null; |
| 314 |
} |
| 315 |
if(contentLocation == null && mimeHeaders != null) { |
| 316 |
String contentLocations[] = mimeHeaders.getHeader("Content-Location"); |
| 317 |
contentLocation = (contentLocations != null)? contentLocations[0] : null; |
| 318 |
} |
| 319 |
if (contentType != null) { |
| 320 |
int delimiterIndex = contentType.lastIndexOf("charset"); |
| 321 |
if (delimiterIndex > 0) { |
| 322 |
String charsetPart = contentType.substring(delimiterIndex); |
| 323 |
int delimiterIndex2 = charsetPart.indexOf(';'); |
| 324 |
if(delimiterIndex2 != -1){ |
| 325 |
charsetPart = charsetPart.substring(0, delimiterIndex2); |
| 326 |
} |
| 327 |
int charsetIndex = charsetPart.indexOf('='); |
| 328 |
String charset = charsetPart.substring(charsetIndex + 1).trim(); |
| 329 |
if ((charset.startsWith("\"") || charset.startsWith("\'"))) { |
| 330 |
charset = charset.substring(1, charset.length()); |
| 331 |
} |
| 332 |
if ((charset.endsWith("\"") || charset.endsWith("\'"))) { |
| 333 |
charset = charset.substring(0, charset.length()-1); |
| 334 |
} |
| 335 |
try { |
| 336 |
setProperty(SOAPMessage.CHARACTER_SET_ENCODING, charset); |
| 337 |
} catch (SOAPException e) { |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
// Try to construct an AttachmentsImpl object for attachment |
| 342 |
// functionality. |
| 343 |
// If there is no org.apache.axis.attachments.AttachmentsImpl class, |
| 344 |
// it must mean activation.jar is not present and attachments are not |
| 345 |
// supported. |
| 346 |
if (isAttachmentSupportEnabled(getMessageContext())) { |
| 347 |
// Construct one, and cast to Attachments. |
| 348 |
// There must be exactly one constructor of AttachmentsImpl, which |
| 349 |
// must take an org.apache.axis.Message! |
| 350 |
Constructor attachImplConstr = attachImpl.getConstructors()[0]; |
| 351 |
try { |
| 352 |
mAttachments = (Attachments) attachImplConstr.newInstance( |
| 353 |
new Object[] { initialContents, |
| 354 |
contentType, contentLocation}); |
| 355 |
|
| 356 |
//If it can't support it, it wont have a root part. |
| 357 |
mSOAPPart = (SOAPPart) mAttachments.getRootPart(); |
| 358 |
} catch (InvocationTargetException ex) { |
| 359 |
log.fatal(Messages.getMessage("invocationTargetException00"), |
| 360 |
ex); |
| 361 |
throw new RuntimeException(ex.getMessage()); |
| 362 |
} catch (InstantiationException ex) { |
| 363 |
log.fatal(Messages.getMessage("instantiationException00"), |
| 364 |
ex); |
| 365 |
throw new RuntimeException(ex.getMessage()); |
| 366 |
} catch (IllegalAccessException ex) { |
| 367 |
log.fatal(Messages.getMessage("illegalAccessException00"), |
| 368 |
ex); |
| 369 |
throw new RuntimeException(ex.getMessage()); |
| 370 |
} |
| 371 |
} else if (contentType != null && contentType.startsWith("multipart")){ |
| 372 |
throw new RuntimeException(Messages.getMessage("noAttachments")); |
| 373 |
} |
| 374 |
|
| 375 |
// text/xml |
| 376 |
if (null == mSOAPPart) { |
| 377 |
mSOAPPart = new SOAPPart(this, initialContents, bodyInStream); |
| 378 |
} |
| 379 |
else |
| 380 |
mSOAPPart.setMessage(this); |
| 381 |
|
| 382 |
// The stream was not determined by a more complex type so default to |
| 383 |
if(mAttachments!=null) mAttachments.setRootPart(mSOAPPart); |
| 384 |
|
| 385 |
headers = (mimeHeaders == null) ? new MimeHeaders() : new MimeHeaders(mimeHeaders); |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Get this message's SOAPPart. |
| 390 |
* <p> |
| 391 |
* Eventually, this should be generalized beyond just SOAP, |
| 392 |
* but it's hard to know how to do that without necessitating |
| 393 |
* a lot of casts in client code. Refactoring keeps getting |
| 394 |
* easier anyhow. |
| 395 |
* |
| 396 |
* @return the soap part of this message |
| 397 |
*/ |
| 398 |
public javax.xml.soap.SOAPPart getSOAPPart() { |
| 399 |
return mSOAPPart; |
| 400 |
} |
| 401 |
|
| 402 |
// fixme: do we realy need this? Can client code not just call |
| 403 |
// getSOAPPart().getAsString() or is there some future optimization that |
| 404 |
// could be hooked in here? |
| 405 |
/** |
| 406 |
* Get a string representation of this message's SOAPPart. |
| 407 |
* |
| 408 |
* @return the soap part of this message as a <code>String</code> |
| 409 |
* @throws org.apache.axis.AxisFault if the stringification failed |
| 410 |
*/ |
| 411 |
public String getSOAPPartAsString() throws org.apache.axis.AxisFault { |
| 412 |
return mSOAPPart.getAsString(); |
| 413 |
} |
| 414 |
|
| 415 |
// fixme: do we realy need this? Can client code not just call |
| 416 |
// getSOAPPart().getAsBytes() or is there some future optimization that |
| 417 |
// could be hooked in here? |
| 418 |
/** |
| 419 |
* Get a byte array representation of this message's SOAPPart. |
| 420 |
* |
| 421 |
* @return the soap part of this message as a <code>byte[]</code> |
| 422 |
* @throws org.apache.axis.AxisFault if creating the byte[] failed |
| 423 |
*/ |
| 424 |
public byte[] getSOAPPartAsBytes() throws org.apache.axis.AxisFault { |
| 425 |
return mSOAPPart.getAsBytes(); |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Get this message's SOAPPart as a SOAPEnvelope. |
| 430 |
* |
| 431 |
* @return a SOAPEnvelope containing this message's SOAPPart |
| 432 |
* @throws AxisFault if this failed |
| 433 |
*/ |
| 434 |
public SOAPEnvelope getSOAPEnvelope() throws AxisFault { |
| 435 |
return mSOAPPart.getAsSOAPEnvelope(); |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Get the Attachments of this Message. |
| 440 |
* <p> |
| 441 |
* If this returns null, then NO ATTACHMENT SUPPORT EXISTS in this |
| 442 |
* configuration of Axis, and no attachment operations may be |
| 443 |
* performed. |
| 444 |
* |
| 445 |
* @return the <code>Attachments</code> if attachments are supported, null |
| 446 |
* otherwise |
| 447 |
*/ |
| 448 |
public Attachments getAttachmentsImpl() { |
| 449 |
return mAttachments; |
| 450 |
} |
| 451 |
|
| 452 |
/** |
| 453 |
* Get the content type of the attachments. |
| 454 |
* |
| 455 |
* @param sc provides the default content type |
| 456 |
* @return a <code>String</code> giving the content type of the |
| 457 |
* attachment |
| 458 |
* @throws AxisFault if there was an error deducing the content type from |
| 459 |
* this message |
| 460 |
*/ |
| 461 |
public String getContentType(SOAPConstants sc) throws AxisFault { |
| 462 |
boolean soap12 = false; |
| 463 |
|
| 464 |
if(sc != null) { |
| 465 |
if(sc == SOAPConstants.SOAP12_CONSTANTS) { |
| 466 |
soap12 = true; |
| 467 |
} |
| 468 |
} else { |
| 469 |
// Support of SOAP 1.2 HTTP binding |
| 470 |
SOAPEnvelope envelope = getSOAPEnvelope(); |
| 471 |
if (envelope != null) { |
| 472 |
if (envelope.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS) { |
| 473 |
soap12 = true; |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
String encoding = XMLUtils.getEncoding(this, msgContext);; |
| 479 |
String ret = sc.getContentType() + "; charset=" + encoding.toLowerCase(); |
| 480 |
|
| 481 |
// Support of SOAP 1.2 HTTP binding |
| 482 |
if (soap12) { |
| 483 |
ret = HTTPConstants.HEADER_ACCEPT_APPL_SOAP +"; charset=" + encoding; |
| 484 |
} |
| 485 |
|
| 486 |
if (getSendType() != Attachments.SEND_TYPE_NONE && mAttachments != null && |
| 487 |
0 != mAttachments.getAttachmentCount()) { |
| 488 |
ret = mAttachments.getContentType(); |
| 489 |
} |
| 490 |
return ret; |
| 491 |
} |
| 492 |
|
| 493 |
private int getSendType() { |
| 494 |
int sendType = Attachments.SEND_TYPE_NOTSET; |
| 495 |
if ((msgContext != null) && (msgContext.getService() != null)) { |
| 496 |
sendType = msgContext.getService().getSendType(); |
| 497 |
} |
| 498 |
return sendType; |
| 499 |
} |
| 500 |
|
| 501 |
//This will have to give way someday to HTTP Chunking but for now kludge. |
| 502 |
/** |
| 503 |
* Get the content length, including both soap and any attachments. |
| 504 |
* |
| 505 |
* @return the total length of this message in bytes |
| 506 |
* @throws org.apache.axis.AxisFault if there was a problem that prevented |
| 507 |
* the length being calculated |
| 508 |
*/ |
| 509 |
public long getContentLength() throws org.apache.axis.AxisFault { |
| 510 |
long ret = mSOAPPart.getContentLength(); |
| 511 |
if (mAttachments != null && 0 < mAttachments.getAttachmentCount()) { |
| 512 |
ret = mAttachments.getContentLength(); |
| 513 |
} |
| 514 |
return ret; |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* Writes this <CODE>SOAPMessage</CODE> object to the given |
| 519 |
* output stream. The externalization format is as defined by |
| 520 |
* the SOAP 1.1 with Attachments specification. |
| 521 |
* |
| 522 |
* <P>If there are no attachments, just an XML stream is |
| 523 |
* written out. For those messages that have attachments, |
| 524 |
* <CODE>writeTo</CODE> writes a MIME-encoded byte stream.</P> |
| 525 |
* @param os the <CODE>OutputStream</CODE> |
| 526 |
* object to which this <CODE>SOAPMessage</CODE> object will |
| 527 |
* be written |
| 528 |
* @throws SOAPException if there was a problem in |
| 529 |
* externalizing this SOAP message |
| 530 |
* @throws IOException if an I/O error |
| 531 |
* occurs |
| 532 |
*/ |
| 533 |
public void writeTo(java.io.OutputStream os) throws SOAPException, IOException { |
| 534 |
//Do it the old fashion way. |
| 535 |
if (getSendType() == Attachments.SEND_TYPE_NONE || mAttachments == null || 0 == mAttachments.getAttachmentCount()) { |
| 536 |
try { |
| 537 |
String charEncoding = XMLUtils.getEncoding(this, msgContext);; |
| 538 |
mSOAPPart.setEncoding(charEncoding); |
| 539 |
mSOAPPart.writeTo(os); |
| 540 |
} catch (java.io.IOException e) { |
| 541 |
log.error(Messages.getMessage("javaIOException00"), e); |
| 542 |
} |
| 543 |
} else { |
| 544 |
try { |
| 545 |
mAttachments.writeContentToStream(os); |
| 546 |
} catch (java.lang.Exception e) { |
| 547 |
log.error(Messages.getMessage("exception00"), e); |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
private java.util.Hashtable mProps = new java.util.Hashtable(); |
| 553 |
|
| 554 |
public SOAPBody getSOAPBody() throws SOAPException { |
| 555 |
return mSOAPPart.getEnvelope().getBody(); |
| 556 |
} |
| 557 |
|
| 558 |
public SOAPHeader getSOAPHeader() throws SOAPException { |
| 559 |
return mSOAPPart.getEnvelope().getHeader(); |
| 560 |
} |
| 561 |
|
| 562 |
public void setProperty(String property, Object value) throws SOAPException { |
| 563 |
mProps.put(property, value); |
| 564 |
} |
| 565 |
|
| 566 |
public Object getProperty(String property) throws SOAPException { |
| 567 |
return mProps.get(property); |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* Retrieves a description of this <CODE>SOAPMessage</CODE> |
| 572 |
* object's content. |
| 573 |
* @return a <CODE>String</CODE> describing the content of this |
| 574 |
* message or <CODE>null</CODE> if no description has been |
| 575 |
* set |
| 576 |
* @see #setContentDescription(java.lang.String) setContentDescription(java.lang.String) |
| 577 |
*/ |
| 578 |
public String getContentDescription() { |
| 579 |
String values[] = headers.getHeader(HTTPConstants.HEADER_CONTENT_DESCRIPTION); |
| 580 |
if(values != null && values.length > 0) |
| 581 |
return values[0]; |
| 582 |
return null; |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* Sets the description of this <CODE>SOAPMessage</CODE> |
| 587 |
* object's content with the given description. |
| 588 |
* @param description a <CODE>String</CODE> |
| 589 |
* describing the content of this message |
| 590 |
* @see #getContentDescription() getContentDescription() |
| 591 |
*/ |
| 592 |
public void setContentDescription(String description) { |
| 593 |
headers.setHeader(HTTPConstants.HEADER_CONTENT_DESCRIPTION, description); |
| 594 |
} |
| 595 |
|
| 596 |
/** |
| 597 |
* Updates this <CODE>SOAPMessage</CODE> object with all the |
| 598 |
* changes that have been made to it. This method is called |
| 599 |
* automatically when a message is sent or written to by the |
| 600 |
* methods <CODE>ProviderConnection.send</CODE>, <CODE> |
| 601 |
* SOAPConnection.call</CODE>, or <CODE> |
| 602 |
* SOAPMessage.writeTo</CODE>. However, if changes are made to |
| 603 |
* a message that was received or to one that has already been |
| 604 |
* sent, the method <CODE>saveChanges</CODE> needs to be |
| 605 |
* called explicitly in order to save the changes. The method |
| 606 |
* <CODE>saveChanges</CODE> also generates any changes that |
| 607 |
* can be read back (for example, a MessageId in profiles that |
| 608 |
* support a message id). All MIME headers in a message that |
| 609 |
* is created for sending purposes are guaranteed to have |
| 610 |
* valid values only after <CODE>saveChanges</CODE> has been |
| 611 |
* called. |
| 612 |
* |
| 613 |
* <P>In addition, this method marks the point at which the |
| 614 |
* data from all constituent <CODE>AttachmentPart</CODE> |
| 615 |
* objects are pulled into the message.</P> |
| 616 |
* @throws SOAPException if there |
| 617 |
* was a problem saving changes to this message. |
| 618 |
*/ |
| 619 |
public void saveChanges() throws SOAPException { |
| 620 |
headers.removeHeader("Content-Length"); |
| 621 |
if (mAttachments != null && 0 < mAttachments.getAttachmentCount()) { |
| 622 |
try { |
| 623 |
headers.setHeader("Content-Type",mAttachments.getContentType()); |
| 624 |
} catch (AxisFault af){ |
| 625 |
log.error(Messages.getMessage("exception00"), af); |
| 626 |
} |
| 627 |
} |
| 628 |
saveRequired = false; |
| 629 |
try { |
| 630 |
/* Fix for Bug 16418 - Start from scratch */ |
| 631 |
mSOAPPart.saveChanges(); |
| 632 |
} catch (AxisFault axisFault) { |
| 633 |
log.error(Messages.getMessage("exception00"), axisFault); |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
/** |
| 638 |
* Indicates whether this <CODE>SOAPMessage</CODE> object |
| 639 |
* has had the method <CODE>saveChanges</CODE> called on |
| 640 |
* it. |
| 641 |
* @return <CODE>true</CODE> if <CODE>saveChanges</CODE> has |
| 642 |
* been called on this message at least once; <CODE> |
| 643 |
* false</CODE> otherwise. |
| 644 |
*/ |
| 645 |
public boolean saveRequired() { |
| 646 |
return saveRequired; |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
* Returns all the transport-specific MIME headers for this |
| 651 |
* <CODE>SOAPMessage</CODE> object in a transport-independent |
| 652 |
* fashion. |
| 653 |
* @return a <CODE>MimeHeaders</CODE> object containing the |
| 654 |
* <CODE>MimeHeader</CODE> objects |
| 655 |
*/ |
| 656 |
public javax.xml.soap.MimeHeaders getMimeHeaders() { |
| 657 |
return headers; |
| 658 |
} |
| 659 |
|
| 660 |
/** |
| 661 |
* Removes all <CODE>AttachmentPart</CODE> objects that have |
| 662 |
* been added to this <CODE>SOAPMessage</CODE> object. |
| 663 |
* |
| 664 |
* <P>This method does not touch the SOAP part.</P> |
| 665 |
*/ |
| 666 |
public void removeAllAttachments(){ |
| 667 |
mAttachments.removeAllAttachments(); |
| 668 |
} |
| 669 |
|
| 670 |
/** |
| 671 |
* Gets a count of the number of attachments in this |
| 672 |
* message. This count does not include the SOAP part. |
| 673 |
* @return the number of <CODE>AttachmentPart</CODE> objects |
| 674 |
* that are part of this <CODE>SOAPMessage</CODE> |
| 675 |
* object |
| 676 |
*/ |
| 677 |
public int countAttachments(){ |
| 678 |
return mAttachments == null ? 0 : mAttachments.getAttachmentCount(); |
| 679 |
} |
| 680 |
|
| 681 |
/** |
| 682 |
* Retrieves all the <CODE>AttachmentPart</CODE> objects |
| 683 |
* that are part of this <CODE>SOAPMessage</CODE> object. |
| 684 |
* @return an iterator over all the attachments in this |
| 685 |
* message |
| 686 |
*/ |
| 687 |
public Iterator getAttachments(){ |
| 688 |
try { |
| 689 |
if (mAttachments != null && 0 != mAttachments.getAttachmentCount()) { |
| 690 |
return mAttachments.getAttachments().iterator(); |
| 691 |
} |
| 692 |
} catch (AxisFault af){ |
| 693 |
log.error(Messages.getMessage("exception00"), af); |
| 694 |
} |
| 695 |
return Collections.EMPTY_LIST.iterator(); |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Retrieves all the <CODE>AttachmentPart</CODE> objects |
| 700 |
* that have header entries that match the specified headers. |
| 701 |
* Note that a returned attachment could have headers in |
| 702 |
* addition to those specified. |
| 703 |
* @param headers a <CODE>MimeHeaders</CODE> |
| 704 |
* object containing the MIME headers for which to |
| 705 |
* search |
| 706 |
* @return an iterator over all attachments that have a header |
| 707 |
* that matches one of the given headers |
| 708 |
*/ |
| 709 |
public Iterator getAttachments(javax.xml.soap.MimeHeaders headers){ |
| 710 |
return mAttachments.getAttachments(headers); |
| 711 |
} |
| 712 |
|
| 713 |
/** |
| 714 |
* Adds the given <CODE>AttachmentPart</CODE> object to this |
| 715 |
* <CODE>SOAPMessage</CODE> object. An <CODE> |
| 716 |
* AttachmentPart</CODE> object must be created before it can be |
| 717 |
* added to a message. |
| 718 |
* @param attachmentpart an <CODE> |
| 719 |
* AttachmentPart</CODE> object that is to become part of |
| 720 |
* this <CODE>SOAPMessage</CODE> object |
| 721 |
* @throws java.lang.IllegalArgumentException |
| 722 |
*/ |
| 723 |
public void addAttachmentPart(AttachmentPart attachmentpart){ |
| 724 |
try { |
| 725 |
mAttachments.addAttachmentPart((org.apache.axis.Part)attachmentpart); |
| 726 |
} catch (AxisFault af){ |
| 727 |
log.error(Messages.getMessage("exception00"), af); |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
/** |
| 732 |
* Creates a new empty <CODE>AttachmentPart</CODE> object. |
| 733 |
* Note that the method <CODE>addAttachmentPart</CODE> must be |
| 734 |
* called with this new <CODE>AttachmentPart</CODE> object as |
| 735 |
* the parameter in order for it to become an attachment to this |
| 736 |
* <CODE>SOAPMessage</CODE> object. |
| 737 |
* @return a new <CODE>AttachmentPart</CODE> object that can be |
| 738 |
* populated and added to this <CODE>SOAPMessage</CODE> |
| 739 |
* object |
| 740 |
*/ |
| 741 |
public AttachmentPart createAttachmentPart() { |
| 742 |
if (!isAttachmentSupportEnabled(getMessageContext())) { |
| 743 |
throw new RuntimeException(Messages.getMessage("noAttachments")); |
| 744 |
} |
| 745 |
|
| 746 |
try { |
| 747 |
return (AttachmentPart) mAttachments.createAttachmentPart(); |
| 748 |
} catch (AxisFault af){ |
| 749 |
log.error(Messages.getMessage("exception00"), af); |
| 750 |
} |
| 751 |
return null; |
| 752 |
} |
| 753 |
|
| 754 |
/** |
| 755 |
* Dispose of attachments. |
| 756 |
*/ |
| 757 |
public void dispose() { |
| 758 |
if(mAttachments!=null) { |
| 759 |
mAttachments.dispose(); |
| 760 |
} |
| 761 |
} |
| 762 |
} |