001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package javax.xml.soap;
021
022 import javax.activation.DataHandler;
023 import java.util.Iterator;
024
025 /**
026 * <P>A single attachment to a <CODE>SOAPMessage</CODE> object. A <CODE>SOAPMessage</CODE> object
027 * may contain zero, one, or many <CODE>AttachmentPart</CODE> objects. Each <CODE>
028 * AttachmentPart</CODE> object consists of two parts, application-specific content and associated
029 * MIME headers. The MIME headers consists of name/value pairs that can be used to identify and
030 * describe the content.</P>
031 * <p/>
032 * <P>An <CODE>AttachmentPart</CODE> object must conform to certain standards.</P>
033 * <p/>
034 * <OL> <LI>It must conform to <A href= "http://www.ietf.org/rfc/rfc2045.txt">MIME [RFC2045]
035 * standards</A></LI>
036 * <p/>
037 * <LI>It MUST contain content</LI>
038 * <p/>
039 * <LI> The header portion MUST include the following header:
040 * <p/>
041 * <UL> <LI> <CODE>Content-Type</CODE><BR> This header identifies the type of data in the content of
042 * an <CODE>AttachmentPart</CODE> object and MUST conform to [RFC2045]. The following is an example
043 * of a Content-Type header: <PRE> Content-Type: application/xml
044 * <p/>
045 * </PRE> The following line of code, in which <CODE>ap</CODE> is an <CODE>AttachmentPart</CODE>
046 * object, sets the header shown in the previous example. <PRE> ap.setMimeHeader("Content-Type",
047 * "application/xml");
048 * <p/>
049 * </PRE>
050 * <p/>
051 * <P></P> </LI> </UL> </LI> </OL>
052 * <p/>
053 * <P>There are no restrictions on the content portion of an <CODE>AttachmentPart</CODE> object. The
054 * content may be anything from a simple plain text object to a complex XML document or image
055 * file.</P>
056 * <p/>
057 * <P>An <CODE>AttachmentPart</CODE> object is created with the method
058 * <CODE>SOAPMessage.createAttachmentPart</CODE>. After setting its MIME headers, the
059 * <CODE>AttachmentPart</CODE> object is added to the message that created it with the method
060 * <CODE>SOAPMessage.addAttachmentPart</CODE>.</P>
061 * <p/>
062 * <P>The following code fragment, in which <CODE>m</CODE> is a <CODE>SOAPMessage</CODE> object and
063 * <CODE>contentStringl</CODE> is a <CODE>String</CODE>, creates an instance of <CODE>
064 * AttachmentPart</CODE>, sets the <CODE>AttachmentPart</CODE> object with some content and header
065 * information, and adds the <CODE>AttachmentPart</CODE> object to the <CODE> SOAPMessage</CODE>
066 * object.</P> <PRE> AttachmentPart ap1 = m.createAttachmentPart(); ap1.setContent(contentString1,
067 * "text/plain"); m.addAttachmentPart(ap1); </PRE>
068 * <p/>
069 * <P>The following code fragment creates and adds a second <CODE> AttachmentPart</CODE> instance to
070 * the same message. <CODE> jpegData</CODE> is a binary byte buffer representing the jpeg file.</P>
071 * <PRE> AttachmentPart ap2 = m.createAttachmentPart(); byte[] jpegData = ...; ap2.setContent(new
072 * ByteArrayInputStream(jpegData), "image/jpeg"); m.addAttachmentPart(ap2); </PRE>
073 * <p/>
074 * <P>The <CODE>getContent</CODE> method retrieves the contents and header from an
075 * <CODE>AttachmentPart</CODE> object. Depending on the <CODE>DataContentHandler</CODE> objects
076 * present, the returned <CODE>Object</CODE> can either be a typed Java object corresponding to the
077 * MIME type or an <CODE> InputStream</CODE> object that contains the content as bytes.</P> <PRE>
078 * String content1 = ap1.getContent(); java.io.InputStream content2 = ap2.getContent(); </PRE> The
079 * method <CODE>clearContent</CODE> removes all the content from an <CODE>AttachmentPart</CODE>
080 * object but does not affect its header information. <PRE> ap1.clearContent(); </PRE>
081 */
082 public abstract class AttachmentPart {
083
084 // fixme: should this constructor be protected?
085
086 /** Create a new AttachmentPart. */
087 public AttachmentPart() {
088 }
089
090 /**
091 * Returns the number of bytes in this <CODE> AttachmentPart</CODE> object.
092 *
093 * @return the size of this <CODE>AttachmentPart</CODE> object in bytes or -1 if the size cannot
094 * be determined
095 * @throws SOAPException if the content of this attachment is corrupted of if there was an
096 * exception while trying to determine the size.
097 */
098 public abstract int getSize() throws SOAPException;
099
100 /**
101 * Clears out the content of this <CODE> AttachmentPart</CODE> object. The MIME header portion
102 * is left untouched.
103 */
104 public abstract void clearContent();
105
106 /**
107 * Gets the content of this <code>AttachmentPart</code> object as a Java object. The type of the
108 * returned Java object depends on (1) the <code>DataContentHandler</code> object that is used
109 * to interpret the bytes and (2) the <code>Content-Type</code> given in the header.
110 * <p/>
111 * For the MIME content types "text/plain", "text/html" and "text/xml", the
112 * <code>DataContentHandler</code> object does the conversions to and from the Java types
113 * corresponding to the MIME types. For other MIME types,the <code>DataContentHandler</code>
114 * object can return an <code>InputStream</code> object that contains the content data as raw
115 * bytes.
116 * <p/>
117 * A JAXM-compliant implementation must, as a minimum, return a <code>java.lang.String</code>
118 * object corresponding to any content stream with a <code>Content-Type</code> value of
119 * <code>text/plain</code>, a <code>javax.xml.transform.StreamSource</code> object corresponding
120 * to a content stream with a <code>Content-Type</code> value of <code>text/xml</code>, a
121 * <code>java.awt.Image</code> object corresponding to a content stream with a
122 * <code>Content-Type</code> value of <code>image/gif</code> or <code>image/jpeg</code>. For
123 * those content types that an installed <code>DataContentHandler</code> object does not
124 * understand, the <code>DataContentHandler</code> object is required to return a
125 * <code>java.io.InputStream</code> object with the raw bytes.
126 *
127 * @return a Java object with the content of this <CODE> AttachmentPart</CODE> object
128 * @throws SOAPException if there is no content set into this <CODE>AttachmentPart</CODE> object
129 * or if there was a data transformation error
130 */
131 public abstract Object getContent() throws SOAPException;
132
133 /**
134 * Sets the content of this attachment part to that of the given <CODE>Object</CODE> and sets
135 * the value of the <CODE> Content-Type</CODE> header to the given type. The type of the
136 * <CODE>Object</CODE> should correspond to the value given for the <CODE>Content-Type</CODE>.
137 * This depends on the particular set of <CODE>DataContentHandler</CODE> objects in use.
138 *
139 * @param object the Java object that makes up the content for this attachment part
140 * @param contentType the MIME string that specifies the type of the content
141 * @throws IllegalArgumentException
142 * if the contentType does not match the type of the content object, or if there was no
143 * <CODE> DataContentHandler</CODE> object for this content object
144 * @see #getContent() getContent()
145 */
146 public abstract void setContent(Object object, String contentType);
147
148 /**
149 * Gets the <CODE>DataHandler</CODE> object for this <CODE> AttachmentPart</CODE> object.
150 *
151 * @return the <CODE>DataHandler</CODE> object associated with this <CODE>AttachmentPart</CODE>
152 * object
153 * @throws SOAPException if there is no data in this <CODE>AttachmentPart</CODE> object
154 */
155 public abstract DataHandler getDataHandler() throws SOAPException;
156
157 /**
158 * Sets the given <CODE>DataHandler</CODE> object as the data handler for this
159 * <CODE>AttachmentPart</CODE> object. Typically, on an incoming message, the data handler is
160 * automatically set. When a message is being created and populated with content, the
161 * <CODE>setDataHandler</CODE> method can be used to get data from various data sources into the
162 * message.
163 *
164 * @param datahandler <CODE>DataHandler</CODE> object to be set
165 * @throws IllegalArgumentException
166 * if there was a problem with the specified <CODE> DataHandler</CODE> object
167 */
168 public abstract void setDataHandler(DataHandler datahandler);
169
170 /**
171 * Gets the value of the MIME header whose name is "Content-Id".
172 *
173 * @return a <CODE>String</CODE> giving the value of the "Content-Id" header or
174 * <CODE>null</CODE> if there is none
175 * @see #setContentId(String) setContentId(java.lang.String)
176 */
177 public String getContentId() {
178
179 String as[] = getMimeHeader("Content-Id");
180
181 if (as != null && as.length > 0) {
182 return as[0];
183 } else {
184 return null;
185 }
186 }
187
188 /**
189 * Gets the value of the MIME header "Content-Location".
190 *
191 * @return a <CODE>String</CODE> giving the value of the "Content-Location" header or
192 * <CODE>null</CODE> if there is none
193 */
194 public String getContentLocation() {
195
196 String as[] = getMimeHeader("Content-Location");
197
198 if (as != null && as.length > 0) {
199 return as[0];
200 } else {
201 return null;
202 }
203 }
204
205 /**
206 * Gets the value of the MIME header "Content-Type".
207 *
208 * @return a <CODE>String</CODE> giving the value of the "Content-Type" header or
209 * <CODE>null</CODE> if there is none
210 */
211 public String getContentType() {
212
213 String as[] = getMimeHeader("Content-Type");
214
215 if (as != null && as.length > 0) {
216 return as[0];
217 } else {
218 return null;
219 }
220 }
221
222 /**
223 * Sets the MIME header "Content-Id" with the given value.
224 *
225 * @param contentId a <CODE>String</CODE> giving the value of the "Content-Id" header
226 * @throws IllegalArgumentException
227 * if there was a problem with the specified <CODE> contentId</CODE> value
228 * @see #getContentId() getContentId()
229 */
230 public void setContentId(String contentId) {
231 setMimeHeader("Content-Id", contentId);
232 }
233
234 /**
235 * Sets the MIME header "Content-Location" with the given value.
236 *
237 * @param contentLocation a <CODE>String</CODE> giving the value of the "Content-Location"
238 * header
239 * @throws IllegalArgumentException
240 * if there was a problem with the specified content location
241 */
242 public void setContentLocation(String contentLocation) {
243 setMimeHeader("Content-Location", contentLocation);
244 }
245
246 /**
247 * Sets the MIME header "Content-Type" with the given value.
248 *
249 * @param contentType a <CODE>String</CODE> giving the value of the "Content-Type" header
250 * @throws IllegalArgumentException
251 * if there was a problem with the specified content type
252 */
253 public void setContentType(String contentType) {
254 setMimeHeader("Content-Type", contentType);
255 }
256
257 /**
258 * Removes all MIME headers that match the given name.
259 *
260 * @param header - the string name of the MIME header/s to be removed
261 */
262 public abstract void removeMimeHeader(String header);
263
264 /** Removes all the MIME header entries. */
265 public abstract void removeAllMimeHeaders();
266
267 /**
268 * Gets all the values of the header identified by the given <CODE>String</CODE>.
269 *
270 * @param name the name of the header; example: "Content-Type"
271 * @return a <CODE>String</CODE> array giving the value for the specified header
272 * @see #setMimeHeader(String, String) setMimeHeader(java.lang.String,
273 * java.lang.String)
274 */
275 public abstract String[] getMimeHeader(String name);
276
277 /**
278 * Changes the first header entry that matches the given name to the given value, adding a new
279 * header if no existing header matches. This method also removes all matching headers but the
280 * first.
281 * <p/>
282 * <P>Note that RFC822 headers can only contain US-ASCII characters.</P>
283 *
284 * @param name a <CODE>String</CODE> giving the name of the header for which to search
285 * @param value a <CODE>String</CODE> giving the value to be set for the header whose name
286 * matches the given name
287 * @throws IllegalArgumentException
288 * if there was a problem with the specified mime header name or value
289 */
290 public abstract void setMimeHeader(String name, String value);
291
292 /**
293 * Adds a MIME header with the specified name and value to this <CODE>AttachmentPart</CODE>
294 * object.
295 * <p/>
296 * <P>Note that RFC822 headers can contain only US-ASCII characters.</P>
297 *
298 * @param name a <CODE>String</CODE> giving the name of the header to be added
299 * @param value a <CODE>String</CODE> giving the value of the header to be added
300 * @throws IllegalArgumentException
301 * if there was a problem with the specified mime header name or value
302 */
303 public abstract void addMimeHeader(String name, String value);
304
305 /**
306 * Retrieves all the headers for this <CODE> AttachmentPart</CODE> object as an iterator over
307 * the <CODE> MimeHeader</CODE> objects.
308 *
309 * @return an <CODE>Iterator</CODE> object with all of the Mime headers for this
310 * <CODE>AttachmentPart</CODE> object
311 */
312 public abstract Iterator getAllMimeHeaders();
313
314 /**
315 * Retrieves all <CODE>MimeHeader</CODE> objects that match a name in the given array.
316 *
317 * @param names a <CODE>String</CODE> array with the name(s) of the MIME headers to be returned
318 * @return all of the MIME headers that match one of the names in the given array as an
319 * <CODE>Iterator</CODE> object
320 */
321 public abstract Iterator getMatchingMimeHeaders(String names[]);
322
323 /**
324 * Retrieves all <CODE>MimeHeader</CODE> objects whose name does not match a name in the given
325 * array.
326 *
327 * @param names a <CODE>String</CODE> array with the name(s) of the MIME headers not to be
328 * returned
329 * @return all of the MIME headers in this <CODE> AttachmentPart</CODE> object except those that
330 * match one of the names in the given array. The nonmatching MIME headers are returned
331 * as an <CODE>Iterator</CODE> object.
332 */
333 public abstract Iterator getNonMatchingMimeHeaders(String names[]);
334
335 public abstract java.io.InputStream getBase64Content()
336 throws SOAPException;
337
338 public abstract java.io.InputStream getRawContent()
339 throws SOAPException;
340
341 public abstract byte[] getRawContentBytes()
342 throws SOAPException;
343
344 public abstract void setBase64Content(java.io.InputStream inputstream,
345 java.lang.String s)
346 throws SOAPException;
347
348 public abstract void setRawContent(java.io.InputStream inputstream,
349 java.lang.String s)
350 throws SOAPException;
351
352 public abstract void setRawContentBytes(byte[] abyte0,
353 int i,
354 int j,
355 java.lang.String s)
356 throws SOAPException;
357 }