001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * @(#)Part.java 1.21 07/05/04
039: */
040:
041: package javax.mail;
042:
043: import java.io.*;
044: import java.util.Enumeration;
045: import javax.activation.DataHandler;
046:
047: /**
048: * The <code>Part</code> interface is the common base interface for
049: * Messages and BodyParts. <p>
050: *
051: * Part consists of a set of attributes and a "Content".<p>
052: *
053: * <strong> Attributes: </strong> <p>
054: *
055: * The JavaMail API defines a set of standard Part attributes that are
056: * considered to be common to most existing Mail systems. These
057: * attributes have their own settor and gettor methods. Mail systems
058: * may support other Part attributes as well, these are represented as
059: * name-value pairs where both the name and value are Strings.<p>
060: *
061: * <strong> Content: </strong> <p>
062: *
063: * The <strong>data type</strong> of the "content" is returned by
064: * the <code>getContentType()</code> method. The MIME typing system
065: * is used to name data types. <p>
066: *
067: * The "content" of a Part is available in various formats:
068: * <ul>
069: * <li> As a DataHandler - using the <code>getDataHandler()</code> method.
070: * The "content" of a Part is also available through a
071: * <code>javax.activation.DataHandler</code> object. The DataHandler
072: * object allows clients to discover the operations available on the
073: * content, and to instantiate the appropriate component to perform
074: * those operations.
075: *
076: * <li> As an input stream - using the <code>getInputStream()</code> method.
077: * Any mail-specific encodings are decoded before this stream is returned.
078: *
079: * <li> As a Java object - using the <code>getContent()</code> method.
080: * This method returns the "content" as a Java object.
081: * The returned object is of course dependent on the content
082: * itself. In particular, a "multipart" Part's content is always a
083: * Multipart or subclass thereof. That is, <code>getContent()</code> on a
084: * "multipart" type Part will always return a Multipart (or subclass) object.
085: * </ul>
086: *
087: * Part provides the <code>writeTo()</code> method that streams
088: * out its bytestream in mail-safe form suitable for transmission.
089: * This bytestream is typically an aggregation of the Part attributes
090: * and its content's bytestream. <p>
091: *
092: * Message and BodyPart implement the Part interface. Note that in
093: * MIME parlance, Part models an Entity (RFC 2045, Section 2.4).
094: *
095: * @author John Mani
096: */
097:
098: public interface Part {
099:
100: /**
101: * Return the size of the content of this part in bytes.
102: * Return -1 if the size cannot be determined. <p>
103: *
104: * Note that the size may not be an exact measure of the content
105: * size and may or may not account for any transfer encoding
106: * of the content. The size is appropriate for display in a
107: * user interface to give the user a rough idea of the size
108: * of this part.
109: *
110: * @return size of content in bytes
111: * @exception MessagingException
112: */
113: public int getSize() throws MessagingException;
114:
115: /**
116: * Return the number of lines in the content of this part.
117: * Return -1 if the number cannot be determined.
118: *
119: * Note that this number may not be an exact measure of the
120: * content length and may or may not account for any transfer
121: * encoding of the content.
122: *
123: * @return number of lines in the content.
124: * @exception MessagingException
125: */
126: public int getLineCount() throws MessagingException;
127:
128: /**
129: * Returns the Content-Type of the content of this part.
130: * Returns null if the Content-Type could not be determined. <p>
131: *
132: * The MIME typing system is used to name Content-types.
133: *
134: * @return The ContentType of this part
135: * @exception MessagingException
136: * @see javax.activation.DataHandler
137: */
138: public String getContentType() throws MessagingException;
139:
140: /**
141: * Is this Part of the specified MIME type? This method
142: * compares <strong>only the <code>primaryType</code> and
143: * <code>subType</code></strong>.
144: * The parameters of the content types are ignored. <p>
145: *
146: * For example, this method will return <code>true</code> when
147: * comparing a Part of content type <strong>"text/plain"</strong>
148: * with <strong>"text/plain; charset=foobar"</strong>. <p>
149: *
150: * If the <code>subType</code> of <code>mimeType</code> is the
151: * special character '*', then the subtype is ignored during the
152: * comparison.
153: */
154: public boolean isMimeType(String mimeType)
155: throws MessagingException;
156:
157: /**
158: * This part should be presented as an attachment.
159: * @see #getDisposition
160: * @see #setDisposition
161: */
162: public static final String ATTACHMENT = "attachment";
163:
164: /**
165: * This part should be presented inline.
166: * @see #getDisposition
167: * @see #setDisposition
168: */
169: public static final String INLINE = "inline";
170:
171: /**
172: * Return the disposition of this part. The disposition
173: * describes how the part should be presented to the user.
174: * (See RFC 2183.) The return value should be considered
175: * without regard to case. For example: <p>
176: * <blockquote><pre>
177: * String disp = part.getDisposition();
178: * if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT))
179: * // treat as attachment if not first part
180: * </pre></blockquote>
181: *
182: * @return disposition of this part, or null if unknown
183: * @exception MessagingException
184: * @see #ATTACHMENT
185: * @see #INLINE
186: * @see #getFileName
187: */
188: public String getDisposition() throws MessagingException;
189:
190: /**
191: * Set the disposition of this part.
192: *
193: * @param disposition disposition of this part
194: * @exception MessagingException
195: * @exception IllegalWriteException if the underlying implementation
196: * does not support modification of this header
197: * @exception IllegalStateException if this Part is obtained
198: * from a READ_ONLY folder
199: * @see #ATTACHMENT
200: * @see #INLINE
201: * @see #setFileName
202: */
203: public void setDisposition(String disposition)
204: throws MessagingException;
205:
206: /**
207: * Return a description String for this part. This typically
208: * associates some descriptive information with this part.
209: * Returns null if none is available.
210: *
211: * @return description of this part
212: * @exception MessagingException
213: */
214: public String getDescription() throws MessagingException;
215:
216: /**
217: * Set a description String for this part. This typically
218: * associates some descriptive information with this part.
219: *
220: * @param description description of this part
221: * @exception MessagingException
222: * @exception IllegalWriteException if the underlying implementation
223: * does not support modification of this header
224: * @exception IllegalStateException if this Part is obtained
225: * from a READ_ONLY folder
226: */
227: public void setDescription(String description)
228: throws MessagingException;
229:
230: /**
231: * Get the filename associated with this part, if possible.
232: * Useful if this part represents an "attachment" that was
233: * loaded from a file. The filename will usually be a simple
234: * name, not including directory components.
235: *
236: * @return Filename to associate with this part
237: */
238: public String getFileName() throws MessagingException;
239:
240: /**
241: * Set the filename associated with this part, if possible.
242: * Useful if this part represents an "attachment" that was
243: * loaded from a file. The filename will usually be a simple
244: * name, not including directory components.
245: *
246: * @param filename Filename to associate with this part
247: * @exception IllegalWriteException if the underlying implementation
248: * does not support modification of this header
249: * @exception IllegalStateException if this Part is obtained
250: * from a READ_ONLY folder
251: */
252: public void setFileName(String filename) throws MessagingException;
253:
254: /**
255: * Return an input stream for this part's "content". Any
256: * mail-specific transfer encodings will be decoded before the
257: * input stream is provided. <p>
258: *
259: * This is typically a convenience method that just invokes
260: * the DataHandler's <code>getInputStream()</code> method.
261: *
262: * @return an InputStream
263: * @exception IOException this is typically thrown by the
264: * DataHandler. Refer to the documentation for
265: * javax.activation.DataHandler for more details.
266: * @exception MessagingException
267: * @see #getDataHandler
268: * @see javax.activation.DataHandler#getInputStream
269: */
270: public InputStream getInputStream() throws IOException,
271: MessagingException;
272:
273: /**
274: * Return a DataHandler for the content within this part. The
275: * DataHandler allows clients to operate on as well as retrieve
276: * the content.
277: *
278: * @return DataHandler for the content
279: * @exception MessagingException
280: */
281: public DataHandler getDataHandler() throws MessagingException;
282:
283: /**
284: * Return the content as a Java object. The type of the returned
285: * object is of course dependent on the content itself. For example,
286: * the object returned for "text/plain" content is usually a String
287: * object. The object returned for a "multipart" content is always a
288: * Multipart subclass. For content-types that are unknown to the
289: * DataHandler system, an input stream is returned as the content <p>
290: *
291: * This is a convenience method that just invokes the DataHandler's
292: * getContent() method
293: *
294: * @return Object
295: * @exception MessagingException
296: * @exception IOException this is typically thrown by the
297: * DataHandler. Refer to the documentation for
298: * javax.activation.DataHandler for more details.
299: *
300: * @see javax.activation.DataHandler#getContent
301: */
302: public Object getContent() throws IOException, MessagingException;
303:
304: /**
305: * This method provides the mechanism to set this part's content.
306: * The DataHandler wraps around the actual content.
307: *
308: * @param dh The DataHandler for the content.
309: * @exception MessagingException
310: * @exception IllegalWriteException if the underlying implementation
311: * does not support modification of existing values
312: * @exception IllegalStateException if this Part is obtained
313: * from a READ_ONLY folder
314: */
315: public void setDataHandler(DataHandler dh)
316: throws MessagingException;
317:
318: /**
319: * A convenience method for setting this part's content. The part
320: * internally wraps the content in a DataHandler. <p>
321: *
322: * Note that a DataContentHandler class for the specified type should
323: * be available to the JavaMail implementation for this to work right.
324: * i.e., to do <code>setContent(foobar, "application/x-foobar")</code>,
325: * a DataContentHandler for "application/x-foobar" should be installed.
326: * Refer to the Java Activation Framework for more information.
327: *
328: * @param obj A java object.
329: * @param type MIME type of this object.
330: * @exception IllegalWriteException if the underlying implementation
331: * does not support modification of existing values
332: * @exception IllegalStateException if this Part is obtained
333: * from a READ_ONLY folder
334: */
335: public void setContent(Object obj, String type)
336: throws MessagingException;
337:
338: /**
339: * A convenience method that sets the given String as this
340: * part's content with a MIME type of "text/plain".
341: *
342: * @param text The text that is the Message's content.
343: * @exception IllegalWriteException if the underlying
344: * implementation does not support modification of
345: * existing values
346: * @exception IllegalStateException if this Part is obtained
347: * from a READ_ONLY folder
348: */
349: public void setText(String text) throws MessagingException;
350:
351: /**
352: * This method sets the given Multipart object as this message's
353: * content.
354: *
355: * @param mp The multipart object that is the Message's content
356: * @exception IllegalWriteException if the underlying
357: * implementation does not support modification of
358: * existing values
359: * @exception IllegalStateException if this Part is obtained
360: * from a READ_ONLY folder
361: */
362: public void setContent(Multipart mp) throws MessagingException;
363:
364: /**
365: * Output a bytestream for this Part. This bytestream is
366: * typically an aggregration of the Part attributes and
367: * an appropriately encoded bytestream from its 'content'. <p>
368: *
369: * Classes that implement the Part interface decide on
370: * the appropriate encoding algorithm to be used. <p>
371: *
372: * The bytestream is typically used for sending.
373: *
374: * @exception IOException if an error occurs writing to the
375: * stream or if an error is generated
376: * by the javax.activation layer.
377: * @exception MessagingException if an error occurs fetching the
378: * data to be written
379: *
380: * @see javax.activation.DataHandler#writeTo
381: */
382: public void writeTo(OutputStream os) throws IOException,
383: MessagingException;
384:
385: /**
386: * Get all the headers for this header name. Returns <code>null</code>
387: * if no headers for this header name are available.
388: *
389: * @param header_name the name of this header
390: * @return the value fields for all headers with
391: * this name
392: * @exception MessagingException
393: */
394: public String[] getHeader(String header_name)
395: throws MessagingException;
396:
397: /**
398: * Set the value for this header_name. Replaces all existing
399: * header values with this new value.
400: *
401: * @param header_name the name of this header
402: * @param header_value the value for this header
403: * @exception MessagingException
404: * @exception IllegalWriteException if the underlying
405: * implementation does not support modification
406: * of existing values
407: * @exception IllegalStateException if this Part is
408: * obtained from a READ_ONLY folder
409: */
410: public void setHeader(String header_name, String header_value)
411: throws MessagingException;
412:
413: /**
414: * Add this value to the existing values for this header_name.
415: *
416: * @param header_name the name of this header
417: * @param header_value the value for this header
418: * @exception MessagingException
419: * @exception IllegalWriteException if the underlying
420: * implementation does not support modification
421: * of existing values
422: * @exception IllegalStateException if this Part is
423: * obtained from a READ_ONLY folder
424: */
425: public void addHeader(String header_name, String header_value)
426: throws MessagingException;
427:
428: /**
429: * Remove all headers with this name.
430: *
431: * @param header_name the name of this header
432: * @exception MessagingException
433: * @exception IllegalWriteException if the underlying
434: * implementation does not support modification
435: * of existing values
436: * @exception IllegalStateException if this Part is
437: * obtained from a READ_ONLY folder
438: */
439: public void removeHeader(String header_name)
440: throws MessagingException;
441:
442: /**
443: * Return all the headers from this part as an Enumeration of
444: * Header objects.
445: *
446: * @return enumeration of Header objects
447: * @exception MessagingException
448: */
449: public Enumeration getAllHeaders() throws MessagingException;
450:
451: /**
452: * Return matching headers from this part as an Enumeration of
453: * Header objects.
454: *
455: * @return enumeration of Header objects
456: * @exception MessagingException
457: */
458: public Enumeration getMatchingHeaders(String[] header_names)
459: throws MessagingException;
460:
461: /**
462: * Return non-matching headers from this envelope as an Enumeration
463: * of Header objects.
464: *
465: * @return enumeration of Header objects
466: * @exception MessagingException
467: */
468: public Enumeration getNonMatchingHeaders(String[] header_names)
469: throws MessagingException;
470: }
|