Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxws/src/share/jaxws_classes/javax/xml/soap/SOAPMessage.java
38890 views
/*1* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.soap;26import java.io.OutputStream;27import java.io.IOException;2829import java.util.Iterator;3031import javax.activation.DataHandler;3233/**34* The root class for all SOAP messages. As transmitted on the "wire", a SOAP35* message is an XML document or a MIME message whose first body part is an36* XML/SOAP document.37* <P>38* A <code>SOAPMessage</code> object consists of a SOAP part and optionally39* one or more attachment parts. The SOAP part for a <code>SOAPMessage</code>40* object is a <code>SOAPPart</code> object, which contains information used41* for message routing and identification, and which can contain42* application-specific content. All data in the SOAP Part of a message must be43* in XML format.44* <P>45* A new <code>SOAPMessage</code> object contains the following by default:46* <UL>47* <LI>A <code>SOAPPart</code> object48* <LI>A <code>SOAPEnvelope</code> object49* <LI>A <code>SOAPBody</code> object50* <LI>A <code>SOAPHeader</code> object51* </UL>52* The SOAP part of a message can be retrieved by calling the method <code>SOAPMessage.getSOAPPart()</code>.53* The <code>SOAPEnvelope</code> object is retrieved from the <code>SOAPPart</code>54* object, and the <code>SOAPEnvelope</code> object is used to retrieve the55* <code>SOAPBody</code> and <code>SOAPHeader</code> objects.56*57* <PRE>58* SOAPPart sp = message.getSOAPPart();59* SOAPEnvelope se = sp.getEnvelope();60* SOAPBody sb = se.getBody();61* SOAPHeader sh = se.getHeader();62* </PRE>63*64* <P>65* In addition to the mandatory <code>SOAPPart</code> object, a <code>SOAPMessage</code>66* object may contain zero or more <code>AttachmentPart</code> objects, each67* of which contains application-specific data. The <code>SOAPMessage</code>68* interface provides methods for creating <code>AttachmentPart</code>69* objects and also for adding them to a <code>SOAPMessage</code> object. A70* party that has received a <code>SOAPMessage</code> object can examine its71* contents by retrieving individual attachment parts.72* <P>73* Unlike the rest of a SOAP message, an attachment is not required to be in74* XML format and can therefore be anything from simple text to an image file.75* Consequently, any message content that is not in XML format must be in an76* <code>AttachmentPart</code> object.77* <P>78* A <code>MessageFactory</code> object may create <code>SOAPMessage</code>79* objects with behavior that is specialized to a particular implementation or80* application of SAAJ. For instance, a <code>MessageFactory</code> object81* may produce <code>SOAPMessage</code> objects that conform to a particular82* Profile such as ebXML. In this case a <code>MessageFactory</code> object83* might produce <code>SOAPMessage</code> objects that are initialized with84* ebXML headers.85* <P>86* In order to ensure backward source compatibility, methods that are added to87* this class after version 1.1 of the SAAJ specification are all concrete88* instead of abstract and they all have default implementations. Unless89* otherwise noted in the JavaDocs for those methods the default90* implementations simply throw an <code>UnsupportedOperationException</code>91* and the SAAJ implementation code must override them with methods that92* provide the specified behavior. Legacy client code does not have this93* restriction, however, so long as there is no claim made that it conforms to94* some later version of the specification than it was originally written for.95* A legacy class that extends the SOAPMessage class can be compiled and/or run96* against succeeding versions of the SAAJ API without modification. If such a97* class was correctly implemented then it will continue to behave correctly98* relative to the version of the specification against which it was written.99*100* @see MessageFactory101* @see AttachmentPart102*/103public abstract class SOAPMessage {104/**105* Specifies the character type encoding for the SOAP Message. Valid values106* include "utf-8" and "utf-16". See vendor documentation for additional107* supported values. The default is "utf-8".108*109* @see SOAPMessage#setProperty(String, Object) SOAPMessage.setProperty110* @since SAAJ 1.2111*/112public static final String CHARACTER_SET_ENCODING =113"javax.xml.soap.character-set-encoding";114115/**116* Specifies whether the SOAP Message will contain an XML declaration when117* it is sent. The only valid values are "true" and "false". The default is118* "false".119*120* @see SOAPMessage#setProperty(String, Object) SOAPMessage.setProperty121* @since SAAJ 1.2122*/123public static final String WRITE_XML_DECLARATION =124"javax.xml.soap.write-xml-declaration";125126/**127* Sets the description of this <code>SOAPMessage</code> object's128* content with the given description.129*130* @param description a <code>String</code> describing the content of this131* message132* @see #getContentDescription133*/134public abstract void setContentDescription(String description);135136/**137* Retrieves a description of this <code>SOAPMessage</code> object's138* content.139*140* @return a <code>String</code> describing the content of this141* message or <code>null</code> if no description has been set142* @see #setContentDescription143*/144public abstract String getContentDescription();145146/**147* Gets the SOAP part of this <code>SOAPMessage</code> object.148* <P>149* <code>SOAPMessage</code> object contains one or more attachments, the150* SOAP Part must be the first MIME body part in the message.151*152* @return the <code>SOAPPart</code> object for this <code>SOAPMessage</code>153* object154*/155public abstract SOAPPart getSOAPPart();156157/**158* Gets the SOAP Body contained in this <code>SOAPMessage</code> object.159* <p>160*161* @return the <code>SOAPBody</code> object contained by this <code>SOAPMessage</code>162* object163* @exception SOAPException164* if the SOAP Body does not exist or cannot be retrieved165* @since SAAJ 1.2166*/167public SOAPBody getSOAPBody() throws SOAPException {168throw new UnsupportedOperationException("getSOAPBody must be overridden by all subclasses of SOAPMessage");169}170171/**172* Gets the SOAP Header contained in this <code>SOAPMessage</code>173* object.174* <p>175*176* @return the <code>SOAPHeader</code> object contained by this <code>SOAPMessage</code>177* object178* @exception SOAPException179* if the SOAP Header does not exist or cannot be retrieved180* @since SAAJ 1.2181*/182public SOAPHeader getSOAPHeader() throws SOAPException {183throw new UnsupportedOperationException("getSOAPHeader must be overridden by all subclasses of SOAPMessage");184}185186/**187* Removes all <code>AttachmentPart</code> objects that have been added188* to this <code>SOAPMessage</code> object.189* <P>190* This method does not touch the SOAP part.191*/192public abstract void removeAllAttachments();193194/**195* Gets a count of the number of attachments in this message. This count196* does not include the SOAP part.197*198* @return the number of <code>AttachmentPart</code> objects that are199* part of this <code>SOAPMessage</code> object200*/201public abstract int countAttachments();202203/**204* Retrieves all the <code>AttachmentPart</code> objects that are part of205* this <code>SOAPMessage</code> object.206*207* @return an iterator over all the attachments in this message208*/209public abstract Iterator getAttachments();210211/**212* Retrieves all the <code>AttachmentPart</code> objects that have header213* entries that match the specified headers. Note that a returned214* attachment could have headers in addition to those specified.215*216* @param headers217* a <code>MimeHeaders</code> object containing the MIME218* headers for which to search219* @return an iterator over all attachments that have a header that matches220* one of the given headers221*/222public abstract Iterator getAttachments(MimeHeaders headers);223224/**225* Removes all the <code>AttachmentPart</code> objects that have header226* entries that match the specified headers. Note that the removed227* attachment could have headers in addition to those specified.228*229* @param headers230* a <code>MimeHeaders</code> object containing the MIME231* headers for which to search232* @since SAAJ 1.3233*/234public abstract void removeAttachments(MimeHeaders headers);235236237/**238* Returns an <code>AttachmentPart</code> object that is associated with an239* attachment that is referenced by this <code>SOAPElement</code> or240* <code>null</code> if no such attachment exists. References can be made241* via an <code>href</code> attribute as described in242* {@link <a href="http://www.w3.org/TR/SOAP-attachments#SOAPReferenceToAttachements">SOAP Messages with Attachments</a>},243* or via a single <code>Text</code> child node containing a URI as244* described in the WS-I Attachments Profile 1.0 for elements of schema245* type {@link <a href="http://www.ws-i.org/Profiles/AttachmentsProfile-1.0-2004-08-24.html">ref:swaRef</a>}. These two mechanisms must be supported.246* The support for references via <code>href</code> attribute also implies that247* this method should also be supported on an element that is an248* <i>xop:Include</i> element (249* {@link <a href="http://www.w3.org/2000/xp/Group/3/06/Attachments/XOP.html">XOP</a>}).250* other reference mechanisms may be supported by individual251* implementations of this standard. Contact your vendor for details.252*253* @param element The <code>SOAPElement</code> containing the reference to an Attachment254* @return the referenced <code>AttachmentPart</code> or null if no such255* <code>AttachmentPart</code> exists or no reference can be256* found in this <code>SOAPElement</code>.257* @throws SOAPException if there is an error in the attempt to access the258* attachment259*260* @since SAAJ 1.3261*/262public abstract AttachmentPart getAttachment(SOAPElement element) throws SOAPException;263264265/**266* Adds the given <code>AttachmentPart</code> object to this <code>SOAPMessage</code>267* object. An <code>AttachmentPart</code> object must be created before268* it can be added to a message.269*270* @param AttachmentPart271* an <code>AttachmentPart</code> object that is to become part272* of this <code>SOAPMessage</code> object273* @exception IllegalArgumentException274*/275public abstract void addAttachmentPart(AttachmentPart AttachmentPart);276277/**278* Creates a new empty <code>AttachmentPart</code> object. Note that the279* method <code>addAttachmentPart</code> must be called with this new280* <code>AttachmentPart</code> object as the parameter in order for it to281* become an attachment to this <code>SOAPMessage</code> object.282*283* @return a new <code>AttachmentPart</code> object that can be populated284* and added to this <code>SOAPMessage</code> object285*/286public abstract AttachmentPart createAttachmentPart();287288/**289* Creates an <code>AttachmentPart</code> object and populates it using290* the given <code>DataHandler</code> object.291*292* @param dataHandler293* the <code>javax.activation.DataHandler</code> object that294* will generate the content for this <code>SOAPMessage</code>295* object296* @return a new <code>AttachmentPart</code> object that contains data297* generated by the given <code>DataHandler</code> object298* @exception IllegalArgumentException299* if there was a problem with the specified <code>DataHandler</code>300* object301* @see javax.activation.DataHandler302* @see javax.activation.DataContentHandler303*/304public AttachmentPart createAttachmentPart(DataHandler dataHandler) {305AttachmentPart attachment = createAttachmentPart();306attachment.setDataHandler(dataHandler);307return attachment;308}309310/**311* Returns all the transport-specific MIME headers for this <code>SOAPMessage</code>312* object in a transport-independent fashion.313*314* @return a <code>MimeHeaders</code> object containing the <code>MimeHeader</code>315* objects316*/317public abstract MimeHeaders getMimeHeaders();318319/**320* Creates an <code>AttachmentPart</code> object and populates it with321* the specified data of the specified content type. The type of the322* <code>Object</code> should correspond to the value given for the323* <code>Content-Type</code>.324*325* @param content326* an <code>Object</code> containing the content for the327* <code>AttachmentPart</code> object to be created328* @param contentType329* a <code>String</code> object giving the type of content;330* examples are "text/xml", "text/plain", and "image/jpeg"331* @return a new <code>AttachmentPart</code> object that contains the332* given data333* @exception IllegalArgumentException334* may be thrown if the contentType does not match the type335* of the content object, or if there was no336* <code>DataContentHandler</code> object for the given337* content object338* @see javax.activation.DataHandler339* @see javax.activation.DataContentHandler340*/341public AttachmentPart createAttachmentPart(342Object content,343String contentType) {344AttachmentPart attachment = createAttachmentPart();345attachment.setContent(content, contentType);346return attachment;347}348349/**350* Updates this <code>SOAPMessage</code> object with all the changes that351* have been made to it. This method is called automatically when352* {@link SOAPMessage#writeTo(OutputStream)} is called. However, if353* changes are made to a message that was received or to one that has354* already been sent, the method <code>saveChanges</code> needs to be355* called explicitly in order to save the changes. The method <code>saveChanges</code>356* also generates any changes that can be read back (for example, a357* MessageId in profiles that support a message id). All MIME headers in a358* message that is created for sending purposes are guaranteed to have359* valid values only after <code>saveChanges</code> has been called.360* <P>361* In addition, this method marks the point at which the data from all362* constituent <code>AttachmentPart</code> objects are pulled into the363* message.364* <P>365*366* @exception <code>SOAPException</code> if there was a problem saving367* changes to this message.368*/369public abstract void saveChanges() throws SOAPException;370371/**372* Indicates whether this <code>SOAPMessage</code> object needs to have373* the method <code>saveChanges</code> called on it.374*375* @return <code>true</code> if <code>saveChanges</code> needs to be376* called; <code>false</code> otherwise.377*/378public abstract boolean saveRequired();379380/**381* Writes this <code>SOAPMessage</code> object to the given output382* stream. The externalization format is as defined by the SOAP 1.1 with383* Attachments specification.384* <P>385* If there are no attachments, just an XML stream is written out. For386* those messages that have attachments, <code>writeTo</code> writes a387* MIME-encoded byte stream.388* <P>389* Note that this method does not write the transport-specific MIME Headers390* of the Message391*392* @param out393* the <code>OutputStream</code> object to which this <code>SOAPMessage</code>394* object will be written395* @exception IOException396* if an I/O error occurs397* @exception SOAPException398* if there was a problem in externalizing this SOAP message399*/400public abstract void writeTo(OutputStream out)401throws SOAPException, IOException;402403/**404* Associates the specified value with the specified property. If there was405* already a value associated with this property, the old value is406* replaced.407* <p>408* The valid property names include409* {@link SOAPMessage#WRITE_XML_DECLARATION} and410* {@link SOAPMessage#CHARACTER_SET_ENCODING}. All of these standard SAAJ411* properties are prefixed by "javax.xml.soap". Vendors may also add412* implementation specific properties. These properties must be prefixed413* with package names that are unique to the vendor.414* <p>415* Setting the property <code>WRITE_XML_DECLARATION</code> to <code>"true"</code>416* will cause an XML Declaration to be written out at the start of the SOAP417* message. The default value of "false" suppresses this declaration.418* <p>419* The property <code>CHARACTER_SET_ENCODING</code> defaults to the value420* <code>"utf-8"</code> which causes the SOAP message to be encoded using421* UTF-8. Setting <code>CHARACTER_SET_ENCODING</code> to <code>"utf-16"</code>422* causes the SOAP message to be encoded using UTF-16.423* <p>424* Some implementations may allow encodings in addition to UTF-8 and425* UTF-16. Refer to your vendor's documentation for details.426*427* @param property428* the property with which the specified value is to be429* associated.430* @param value431* the value to be associated with the specified property432* @exception SOAPException433* if the property name is not recognized.434* @since SAAJ 1.2435*/436public void setProperty(String property, Object value)437throws SOAPException {438throw new UnsupportedOperationException("setProperty must be overridden by all subclasses of SOAPMessage");439}440441/**442* Retrieves value of the specified property.443*444* @param property445* the name of the property to retrieve446* @return the value associated with the named property or <code>null</code>447* if no such property exists.448* @exception SOAPException449* if the property name is not recognized.450* @since SAAJ 1.2451*/452public Object getProperty(String property) throws SOAPException {453throw new UnsupportedOperationException("getProperty must be overridden by all subclasses of SOAPMessage");454}455}456457458