Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxws/src/share/jaxws_classes/javax/xml/soap/AttachmentPart.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;2627import java.io.InputStream;28import java.io.Reader;29import java.util.Iterator;3031import javax.activation.DataHandler;3233/**34* A single attachment to a <code>SOAPMessage</code> object. A <code>SOAPMessage</code>35* object may contain zero, one, or many <code>AttachmentPart</code> objects.36* Each <code>AttachmentPart</code> object consists of two parts,37* application-specific content and associated MIME headers. The38* MIME headers consists of name/value pairs that can be used to39* identify and describe the content.40* <p>41* An <code>AttachmentPart</code> object must conform to certain standards.42* <OL>43* <LI>It must conform to <a href="http://www.ietf.org/rfc/rfc2045.txt">44* MIME [RFC2045] standards</a>45* <LI>It MUST contain content46* <LI>The header portion MUST include the following header:47* <UL>48* <LI><code>Content-Type</code><br>49* This header identifies the type of data in the content of an50* <code>AttachmentPart</code> object and MUST conform to [RFC2045].51* The following is an example of a Content-Type header:52* <PRE>53* Content-Type: application/xml54* </PRE>55* The following line of code, in which <code>ap</code> is an56* <code>AttachmentPart</code> object, sets the header shown in57* the previous example.58* <PRE>59* ap.setMimeHeader("Content-Type", "application/xml");60* </PRE>61* <p>62* </UL>63* </OL>64* <p>65* There are no restrictions on the content portion of an <code>66* AttachmentPart</code> object. The content may be anything from a67* simple plain text object to a complex XML document or image file.68*69* <p>70* An <code>AttachmentPart</code> object is created with the method71* <code>SOAPMessage.createAttachmentPart</code>. After setting its MIME headers,72* the <code>AttachmentPart</code> object is added to the message73* that created it with the method <code>SOAPMessage.addAttachmentPart</code>.74*75* <p>76* The following code fragment, in which <code>m</code> is a77* <code>SOAPMessage</code> object and <code>contentStringl</code> is a78* <code>String</code>, creates an instance of <code>AttachmentPart</code>,79* sets the <code>AttachmentPart</code> object with some content and80* header information, and adds the <code>AttachmentPart</code> object to81* the <code>SOAPMessage</code> object.82* <PRE>83* AttachmentPart ap1 = m.createAttachmentPart();84* ap1.setContent(contentString1, "text/plain");85* m.addAttachmentPart(ap1);86* </PRE>87*88*89* <p>90* The following code fragment creates and adds a second91* <code>AttachmentPart</code> instance to the same message. <code>jpegData</code>92* is a binary byte buffer representing the jpeg file.93* <PRE>94* AttachmentPart ap2 = m.createAttachmentPart();95* byte[] jpegData = ...;96* ap2.setContent(new ByteArrayInputStream(jpegData), "image/jpeg");97* m.addAttachmentPart(ap2);98* </PRE>99* <p>100* The <code>getContent</code> method retrieves the contents and header from101* an <code>AttachmentPart</code> object. Depending on the102* <code>DataContentHandler</code> objects present, the returned103* <code>Object</code> can either be a typed Java object corresponding104* to the MIME type or an <code>InputStream</code> object that contains the105* content as bytes.106* <PRE>107* String content1 = ap1.getContent();108* java.io.InputStream content2 = ap2.getContent();109* </PRE>110*111* The method <code>clearContent</code> removes all the content from an112* <code>AttachmentPart</code> object but does not affect its header information.113* <PRE>114* ap1.clearContent();115* </PRE>116*/117118public abstract class AttachmentPart {119/**120* Returns the number of bytes in this <code>AttachmentPart</code>121* object.122*123* @return the size of this <code>AttachmentPart</code> object in bytes124* or -1 if the size cannot be determined125* @exception SOAPException if the content of this attachment is126* corrupted of if there was an exception while trying127* to determine the size.128*/129public abstract int getSize() throws SOAPException;130131/**132* Clears out the content of this <code>AttachmentPart</code> object.133* The MIME header portion is left untouched.134*/135public abstract void clearContent();136137/**138* Gets the content of this <code>AttachmentPart</code> object as a Java139* object. The type of the returned Java object depends on (1) the140* <code>DataContentHandler</code> object that is used to interpret the bytes141* and (2) the <code>Content-Type</code> given in the header.142* <p>143* For the MIME content types "text/plain", "text/html" and "text/xml", the144* <code>DataContentHandler</code> object does the conversions to and145* from the Java types corresponding to the MIME types.146* For other MIME types,the <code>DataContentHandler</code> object147* can return an <code>InputStream</code> object that contains the content data148* as raw bytes.149* <p>150* A SAAJ-compliant implementation must, as a minimum, return a151* <code>java.lang.String</code> object corresponding to any content152* stream with a <code>Content-Type</code> value of153* <code>text/plain</code>, a154* <code>javax.xml.transform.stream.StreamSource</code> object corresponding to a155* content stream with a <code>Content-Type</code> value of156* <code>text/xml</code>, a <code>java.awt.Image</code> object157* corresponding to a content stream with a158* <code>Content-Type</code> value of <code>image/gif</code> or159* <code>image/jpeg</code>. For those content types that an160* installed <code>DataContentHandler</code> object does not understand, the161* <code>DataContentHandler</code> object is required to return a162* <code>java.io.InputStream</code> object with the raw bytes.163*164* @return a Java object with the content of this <code>AttachmentPart</code>165* object166*167* @exception SOAPException if there is no content set into this168* <code>AttachmentPart</code> object or if there was a data169* transformation error170*/171public abstract Object getContent() throws SOAPException;172173/**174* Gets the content of this <code>AttachmentPart</code> object as an175* InputStream as if a call had been made to <code>getContent</code> and no176* <code>DataContentHandler</code> had been registered for the177* <code>content-type</code> of this <code>AttachmentPart</code>.178*<p>179* Note that reading from the returned InputStream would result in consuming180* the data in the stream. It is the responsibility of the caller to reset181* the InputStream appropriately before calling a Subsequent API. If a copy182* of the raw attachment content is required then the {@link #getRawContentBytes} API183* should be used instead.184*185* @return an <code>InputStream</code> from which the raw data contained by186* the <code>AttachmentPart</code> can be accessed.187*188* @throws SOAPException if there is no content set into this189* <code>AttachmentPart</code> object or if there was a data190* transformation error.191*192* @since SAAJ 1.3193* @see #getRawContentBytes194*/195public abstract InputStream getRawContent() throws SOAPException;196197/**198* Gets the content of this <code>AttachmentPart</code> object as a199* byte[] array as if a call had been made to <code>getContent</code> and no200* <code>DataContentHandler</code> had been registered for the201* <code>content-type</code> of this <code>AttachmentPart</code>.202*203* @return a <code>byte[]</code> array containing the raw data of the204* <code>AttachmentPart</code>.205*206* @throws SOAPException if there is no content set into this207* <code>AttachmentPart</code> object or if there was a data208* transformation error.209*210* @since SAAJ 1.3211*/212public abstract byte[] getRawContentBytes() throws SOAPException;213214/**215* Returns an <code>InputStream</code> which can be used to obtain the216* content of <code>AttachmentPart</code> as Base64 encoded217* character data, this method would base64 encode the raw bytes218* of the attachment and return.219*220* @return an <code>InputStream</code> from which the Base64 encoded221* <code>AttachmentPart</code> can be read.222*223* @throws SOAPException if there is no content set into this224* <code>AttachmentPart</code> object or if there was a data225* transformation error.226*227* @since SAAJ 1.3228*/229public abstract InputStream getBase64Content() throws SOAPException;230231/**232* Sets the content of this attachment part to that of the given233* <code>Object</code> and sets the value of the <code>Content-Type</code>234* header to the given type. The type of the235* <code>Object</code> should correspond to the value given for the236* <code>Content-Type</code>. This depends on the particular237* set of <code>DataContentHandler</code> objects in use.238*239*240* @param object the Java object that makes up the content for241* this attachment part242* @param contentType the MIME string that specifies the type of243* the content244*245* @exception IllegalArgumentException may be thrown if the contentType246* does not match the type of the content object, or if there247* was no <code>DataContentHandler</code> object for this248* content object249*250* @see #getContent251*/252public abstract void setContent(Object object, String contentType);253254/**255* Sets the content of this attachment part to that contained by the256* <code>InputStream</code> <code>content</code> and sets the value of the257* <code>Content-Type</code> header to the value contained in258* <code>contentType</code>.259* <P>260* A subsequent call to getSize() may not be an exact measure261* of the content size.262*263* @param content the raw data to add to the attachment part264* @param contentType the value to set into the <code>Content-Type</code>265* header266*267* @exception SOAPException if an there is an error in setting the content268* @exception NullPointerException if <code>content</code> is null269* @since SAAJ 1.3270*/271public abstract void setRawContent(InputStream content, String contentType) throws SOAPException;272273/**274* Sets the content of this attachment part to that contained by the275* <code>byte[]</code> array <code>content</code> and sets the value of the276* <code>Content-Type</code> header to the value contained in277* <code>contentType</code>.278*279* @param content the raw data to add to the attachment part280* @param contentType the value to set into the <code>Content-Type</code>281* header282* @param offset the offset in the byte array of the content283* @param len the number of bytes that form the content284*285* @exception SOAPException if an there is an error in setting the content286* or content is null287* @since SAAJ 1.3288*/289public abstract void setRawContentBytes(290byte[] content, int offset, int len, String contentType)291throws SOAPException;292293294/**295* Sets the content of this attachment part from the Base64 source296* <code>InputStream</code> and sets the value of the297* <code>Content-Type</code> header to the value contained in298* <code>contentType</code>, This method would first decode the base64299* input and write the resulting raw bytes to the attachment.300* <P>301* A subsequent call to getSize() may not be an exact measure302* of the content size.303*304* @param content the base64 encoded data to add to the attachment part305* @param contentType the value to set into the <code>Content-Type</code>306* header307*308* @exception SOAPException if an there is an error in setting the content309* @exception NullPointerException if <code>content</code> is null310*311* @since SAAJ 1.3312*/313public abstract void setBase64Content(314InputStream content, String contentType) throws SOAPException;315316317/**318* Gets the <code>DataHandler</code> object for this <code>AttachmentPart</code>319* object.320*321* @return the <code>DataHandler</code> object associated with this322* <code>AttachmentPart</code> object323*324* @exception SOAPException if there is no data in325* this <code>AttachmentPart</code> object326*/327public abstract DataHandler getDataHandler()328throws SOAPException;329330/**331* Sets the given <code>DataHandler</code> object as the data handler332* for this <code>AttachmentPart</code> object. Typically, on an incoming333* message, the data handler is automatically set. When334* a message is being created and populated with content, the335* <code>setDataHandler</code> method can be used to get data from336* various data sources into the message.337*338* @param dataHandler the <code>DataHandler</code> object to be set339*340* @exception IllegalArgumentException if there was a problem with341* the specified <code>DataHandler</code> object342*/343public abstract void setDataHandler(DataHandler dataHandler);344345346/**347* Gets the value of the MIME header whose name is "Content-ID".348*349* @return a <code>String</code> giving the value of the350* "Content-ID" header or <code>null</code> if there351* is none352* @see #setContentId353*/354public String getContentId() {355String[] values = getMimeHeader("Content-ID");356if (values != null && values.length > 0)357return values[0];358return null;359}360361/**362* Gets the value of the MIME header whose name is "Content-Location".363*364* @return a <code>String</code> giving the value of the365* "Content-Location" header or <code>null</code> if there366* is none367*/368public String getContentLocation() {369String[] values = getMimeHeader("Content-Location");370if (values != null && values.length > 0)371return values[0];372return null;373}374375/**376* Gets the value of the MIME header whose name is "Content-Type".377*378* @return a <code>String</code> giving the value of the379* "Content-Type" header or <code>null</code> if there380* is none381*/382public String getContentType() {383String[] values = getMimeHeader("Content-Type");384if (values != null && values.length > 0)385return values[0];386return null;387}388389/**390* Sets the MIME header whose name is "Content-ID" with the given value.391*392* @param contentId a <code>String</code> giving the value of the393* "Content-ID" header394*395* @exception IllegalArgumentException if there was a problem with396* the specified <code>contentId</code> value397* @see #getContentId398*/399public void setContentId(String contentId)400{401setMimeHeader("Content-ID", contentId);402}403404405/**406* Sets the MIME header whose name is "Content-Location" with the given value.407*408*409* @param contentLocation a <code>String</code> giving the value of the410* "Content-Location" header411* @exception IllegalArgumentException if there was a problem with412* the specified content location413*/414public void setContentLocation(String contentLocation)415{416setMimeHeader("Content-Location", contentLocation);417}418419/**420* Sets the MIME header whose name is "Content-Type" with the given value.421*422* @param contentType a <code>String</code> giving the value of the423* "Content-Type" header424*425* @exception IllegalArgumentException if there was a problem with426* the specified content type427*/428public void setContentType(String contentType)429{430setMimeHeader("Content-Type", contentType);431}432433/**434* Removes all MIME headers that match the given name.435*436* @param header the string name of the MIME header/s to437* be removed438*/439public abstract void removeMimeHeader(String header);440441/**442* Removes all the MIME header entries.443*/444public abstract void removeAllMimeHeaders();445446447/**448* Gets all the values of the header identified by the given449* <code>String</code>.450*451* @param name the name of the header; example: "Content-Type"452* @return a <code>String</code> array giving the value for the453* specified header454* @see #setMimeHeader455*/456public abstract String[] getMimeHeader(String name);457458459/**460* Changes the first header entry that matches the given name461* to the given value, adding a new header if no existing header462* matches. This method also removes all matching headers but the first. <p>463*464* Note that RFC822 headers can only contain US-ASCII characters.465*466* @param name a <code>String</code> giving the name of the header467* for which to search468* @param value a <code>String</code> giving the value to be set for469* the header whose name matches the given name470*471* @exception IllegalArgumentException if there was a problem with472* the specified mime header name or value473*/474public abstract void setMimeHeader(String name, String value);475476477/**478* Adds a MIME header with the specified name and value to this479* <code>AttachmentPart</code> object.480* <p>481* Note that RFC822 headers can contain only US-ASCII characters.482*483* @param name a <code>String</code> giving the name of the header484* to be added485* @param value a <code>String</code> giving the value of the header486* to be added487*488* @exception IllegalArgumentException if there was a problem with489* the specified mime header name or value490*/491public abstract void addMimeHeader(String name, String value);492493/**494* Retrieves all the headers for this <code>AttachmentPart</code> object495* as an iterator over the <code>MimeHeader</code> objects.496*497* @return an <code>Iterator</code> object with all of the Mime498* headers for this <code>AttachmentPart</code> object499*/500public abstract Iterator getAllMimeHeaders();501502/**503* Retrieves all <code>MimeHeader</code> objects that match a name in504* the given array.505*506* @param names a <code>String</code> array with the name(s) of the507* MIME headers to be returned508* @return all of the MIME headers that match one of the names in the509* given array as an <code>Iterator</code> object510*/511public abstract Iterator getMatchingMimeHeaders(String[] names);512513/**514* Retrieves all <code>MimeHeader</code> objects whose name does515* not match a name in the given array.516*517* @param names a <code>String</code> array with the name(s) of the518* MIME headers not to be returned519* @return all of the MIME headers in this <code>AttachmentPart</code> object520* except those that match one of the names in the521* given array. The nonmatching MIME headers are returned as an522* <code>Iterator</code> object.523*/524public abstract Iterator getNonMatchingMimeHeaders(String[] names);525}526527528