1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.mail;
  6. import java.io.*;
  7. import java.util.Enumeration;
  8. import javax.activation.DataHandler;
  9. /**
  10. * The <code>Part</code> interface is the common base interface for
  11. * Messages and BodyParts. <p>
  12. *
  13. * Part consists of a set of attributes and a "Content".<p>
  14. *
  15. * <strong> Attributes: </strong> <p>
  16. *
  17. * The JavaMail API defines a set of standard Part attributes that are
  18. * considered to be common to most existing Mail systems. These
  19. * attributes have their own settor and gettor methods. Mail systems
  20. * may support other Part attributes as well, these are represented as
  21. * name-value pairs where both the name and value are Strings.<p>
  22. *
  23. * <strong> Content: </strong> <p>
  24. *
  25. * The <strong>data type</strong> of the "content" is returned by
  26. * the <code>getContentType()</code> method. The MIME typing system
  27. * is used to name data types. <p>
  28. *
  29. * The "content" of a Part is available in various formats:
  30. * <ul>
  31. * <li> As a DataHandler - using the <code>getDataHandler()</code> method.
  32. * The "content" of a Part is also available through a
  33. * <code>javax.activation.DataHandler</code> object. The DataHandler
  34. * object allows clients to discover the operations available on the
  35. * content, and to instantiate the appropriate component to perform
  36. * those operations.
  37. *
  38. * <li> As an input stream - using the <code>getInputStream()</code> method.
  39. * Any mail-specific encodings are decoded before this stream is returned.
  40. *
  41. * <li> As a Java object - using the <code>getContent()</code> method.
  42. * This method returns the "content" as a Java object.
  43. * The returned object is of course dependent on the content
  44. * itself. In particular, a "multipart" Part's content is always a
  45. * Multipart or subclass thereof. That is, <code>getContent()</code> on a
  46. * "multipart" type Part will always return a Multipart (or subclass) object.
  47. * </ul>
  48. *
  49. * Part provides the <code>writeTo()</code> method that streams
  50. * out its bytestream in mail-safe form suitable for transmission.
  51. * This bytestream is typically an aggregation of the Part attributes
  52. * and its content's bytestream. <p>
  53. *
  54. * Message and BodyPart implement the Part interface. Note that in
  55. * MIME parlance, Part models an Entity (RFC 2045, Section 2.4).
  56. *
  57. * @author John Mani
  58. */
  59. public interface Part {
  60. /**
  61. * Return the size of the content of this part in bytes.
  62. * Return -1 if the size cannot be determined. <p>
  63. *
  64. * Note that the size may not be an exact measure of the content
  65. * size and may or may not account for any transfer encoding
  66. * of the content. The size is appropriate for display in a
  67. * user interface to give the user a rough idea of the size
  68. * of this part.
  69. *
  70. * @return size of content in bytes
  71. * @exception MessagingException
  72. */
  73. public int getSize() throws MessagingException;
  74. /**
  75. * Return the number of lines in the content of this part.
  76. * Return -1 if the number cannot be determined.
  77. *
  78. * Note that this number may not be an exact measure of the
  79. * content length and may or may not account for any transfer
  80. * encoding of the content.
  81. *
  82. * @return number of lines in the content.
  83. * @exception MessagingException
  84. */
  85. public int getLineCount() throws MessagingException;
  86. /**
  87. * Returns the Content-Type of the content of this part.
  88. * Returns null if the Content-Type could not be determined. <p>
  89. *
  90. * The MIME typing system is used to name Content-types.
  91. *
  92. * @return The ContentType of this part
  93. * @exception MessagingException
  94. * @see javax.activation.DataHandler
  95. */
  96. public String getContentType() throws MessagingException;
  97. /**
  98. * Is this Part of the specified MIME type? This method
  99. * compares <strong>only the <code>primaryType</code> and
  100. * <code>subType</code></strong>.
  101. * The parameters of the content types are ignored. <p>
  102. *
  103. * For example, this method will return <code>true</code> when
  104. * comparing a Part of content type <strong>"text/plain"</strong>
  105. * with <strong>"text/plain; charset=foobar"</strong>. <p>
  106. *
  107. * If the <code>subType</code> of <code>mimeType</code> is the
  108. * special character '*', then the subtype is ignored during the
  109. * comparison.
  110. */
  111. public boolean isMimeType(String mimeType) throws MessagingException;
  112. /**
  113. * This part should be presented as an attachment.
  114. * @see #getDisposition
  115. * @see #setDisposition
  116. */
  117. public static final String ATTACHMENT = "attachment";
  118. /**
  119. * This part should be presented inline.
  120. * @see #getDisposition
  121. * @see #setDisposition
  122. */
  123. public static final String INLINE = "inline";
  124. /**
  125. * Return the disposition of this part. The disposition
  126. * describes how the part should be presented to the user.
  127. * (See RFC 2183.) The return value should be considered
  128. * without regard to case. For example: <p>
  129. * <blockquote><pre>
  130. * String disp = part.getDisposition();
  131. * if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT))
  132. * // treat as attachment if not first part
  133. * </pre></blockquote>
  134. *
  135. * @return disposition of this part, or null if unknown
  136. * @exception MessagingException
  137. * @see #ATTACHMENT
  138. * @see #INLINE
  139. * @see #getFileName
  140. */
  141. public String getDisposition() throws MessagingException;
  142. /**
  143. * Set the disposition of this part.
  144. *
  145. * @param disposition disposition of this part
  146. * @exception MessagingException
  147. * @exception IllegalWriteException if the underlying implementation
  148. * does not support modification of this header
  149. * @exception IllegalStateException if this Part is obtained
  150. * from a READ_ONLY folder
  151. * @see #ATTACHMENT
  152. * @see #INLINE
  153. * @see #setFileName
  154. */
  155. public void setDisposition(String disposition) throws MessagingException;
  156. /**
  157. * Return a description String for this part. This typically
  158. * associates some descriptive information with this part.
  159. * Returns null if none is available.
  160. *
  161. * @return description of this part
  162. * @exception MessagingException
  163. */
  164. public String getDescription() throws MessagingException;
  165. /**
  166. * Set a description String for this part. This typically
  167. * associates some descriptive information with this part.
  168. *
  169. * @param description description of this part
  170. * @exception MessagingException
  171. * @exception IllegalWriteException if the underlying implementation
  172. * does not support modification of this header
  173. * @exception IllegalStateException if this Part is obtained
  174. * from a READ_ONLY folder
  175. */
  176. public void setDescription(String description) throws MessagingException;
  177. /**
  178. * Get the filename associated with this part, if possible.
  179. * Useful if this part represents an "attachment" that was
  180. * loaded from a file. The filename will usually be a simple
  181. * name, not including directory components.
  182. *
  183. * @return Filename to associate with this part
  184. */
  185. public String getFileName() throws MessagingException;
  186. /**
  187. * Set the filename associated with this part, if possible.
  188. * Useful if this part represents an "attachment" that was
  189. * loaded from a file. The filename will usually be a simple
  190. * name, not including directory components.
  191. *
  192. * @param filename Filename to associate with this part
  193. * @exception IllegalWriteException if the underlying implementation
  194. * does not support modification of this header
  195. * @exception IllegalStateException if this Part is obtained
  196. * from a READ_ONLY folder
  197. */
  198. public void setFileName(String filename) throws MessagingException;
  199. /**
  200. * Return an input stream for this part's "content". Any
  201. * mail-specific transfer encodings will be decoded before the
  202. * input stream is provided. <p>
  203. *
  204. * This is typically a convenience method that just invokes
  205. * the DataHandler's <code>getInputStream()</code> method.
  206. *
  207. * @return an InputStream
  208. * @exception IOException this is typically thrown by the
  209. * DataHandler. Refer to the documentation for
  210. * javax.activation.DataHandler for more details.
  211. * @exception MessagingException
  212. * @see #getDataHandler
  213. * @see javax.activation.DataHandler#getInputStream
  214. */
  215. public InputStream getInputStream()
  216. throws IOException, MessagingException;
  217. /**
  218. * Return a DataHandler for the content within this part. The
  219. * DataHandler allows clients to operate on as well as retrieve
  220. * the content.
  221. *
  222. * @return DataHandler for the content
  223. * @exception MessagingException
  224. */
  225. public DataHandler getDataHandler() throws MessagingException;
  226. /**
  227. * Return the content as a Java object. The type of the returned
  228. * object is of course dependent on the content itself. For example,
  229. * the object returned for "text/plain" content is usually a String
  230. * object. The object returned for a "multipart" content is always a
  231. * Multipart subclass. For content-types that are unknown to the
  232. * DataHandler system, an input stream is returned as the content <p>
  233. *
  234. * This is a convenience method that just invokes the DataHandler's
  235. * getContent() method
  236. *
  237. * @return Object
  238. * @exception MessagingException
  239. * @exception IOException this is typically thrown by the
  240. * DataHandler. Refer to the documentation for
  241. * javax.activation.DataHandler for more details.
  242. *
  243. * @see javax.activation.DataHandler#getContent
  244. */
  245. public Object getContent() throws IOException, MessagingException;
  246. /**
  247. * This method provides the mechanism to set this part's content.
  248. * The DataHandler wraps around the actual content.
  249. *
  250. * @param dh The DataHandler for the content.
  251. * @exception MessagingException
  252. * @exception IllegalWriteException if the underlying implementation
  253. * does not support modification of existing values
  254. * @exception IllegalStateException if this Part is obtained
  255. * from a READ_ONLY folder
  256. */
  257. public void setDataHandler(DataHandler dh) throws MessagingException;
  258. /**
  259. * A convenience method for setting this part's content. The part
  260. * internally wraps the content in a DataHandler. <p>
  261. *
  262. * Note that a DataContentHandler class for the specified type should
  263. * be available to the JavaMail implementation for this to work right.
  264. * i.e., to do <code>setContent(foobar, "application/x-foobar")</code>,
  265. * a DataContentHandler for "application/x-foobar" should be installed.
  266. * Refer to the Java Activation Framework for more information.
  267. *
  268. * @param obj A java object.
  269. * @param type MIME type of this object.
  270. * @exception IllegalWriteException if the underlying implementation
  271. * does not support modification of existing values
  272. * @exception IllegalStateException if this Part is obtained
  273. * from a READ_ONLY folder
  274. */
  275. public void setContent(Object obj, String type)
  276. throws MessagingException;
  277. /**
  278. * A convenience method that sets the given String as this
  279. * part's content with a MIME type of "text/plain".
  280. *
  281. * @param text The text that is the Message's content.
  282. * @exception IllegalWriteException if the underlying
  283. * implementation does not support modification of
  284. * existing values
  285. * @exception IllegalStateException if this Part is obtained
  286. * from a READ_ONLY folder
  287. */
  288. public void setText(String text) throws MessagingException;
  289. /**
  290. * This method sets the given Multipart object as this message's
  291. * content.
  292. *
  293. * @param mp The multipart object that is the Message's content
  294. * @exception IllegalWriteException if the underlying
  295. * implementation does not support modification of
  296. * existing values
  297. * @exception IllegalStateException if this Part is obtained
  298. * from a READ_ONLY folder
  299. */
  300. public void setContent(Multipart mp) throws MessagingException;
  301. /**
  302. * Output a bytestream for this Part. This bytestream is
  303. * typically an aggregration of the Part attributes and
  304. * an appropriately encoded bytestream from its 'content'. <p>
  305. *
  306. * Classes that implement the Part interface decide on
  307. * the appropriate encoding algorithm to be used. <p>
  308. *
  309. * The bytestream is typically used for sending.
  310. *
  311. * @exception IOException if an error occurs writing to the
  312. * stream or if an error is generated
  313. * by the javax.activation layer.
  314. * @exception MessagingException if an error occurs fetching the
  315. * data to be written
  316. *
  317. * @see javax.activation.DataHandler#writeTo
  318. */
  319. public void writeTo(OutputStream os) throws IOException, MessagingException;
  320. /**
  321. * Get all the headers for this header name. Returns <code>null</code>
  322. * if no headers for this header name are available.
  323. *
  324. * @param header_name the name of this header
  325. * @return the value fields for all headers with
  326. * this name
  327. * @exception MessagingException
  328. */
  329. public String[] getHeader(String header_name)
  330. throws MessagingException;
  331. /**
  332. * Set the value for this header_name. Replaces all existing
  333. * header values with this new value.
  334. *
  335. * @param header_name the name of this header
  336. * @param header_value the value for this header
  337. * @exception MessagingException
  338. * @exception IllegalWriteException if the underlying
  339. * implementation does not support modification
  340. * of existing values
  341. * @exception IllegalStateException if this Part is
  342. * obtained from a READ_ONLY folder
  343. */
  344. public void setHeader(String header_name, String header_value)
  345. throws MessagingException;
  346. /**
  347. * Add this value to the existing values for this header_name.
  348. *
  349. * @param header_name the name of this header
  350. * @param header_value the value for this header
  351. * @exception MessagingException
  352. * @exception IllegalWriteException if the underlying
  353. * implementation does not support modification
  354. * of existing values
  355. * @exception IllegalStateException if this Part is
  356. * obtained from a READ_ONLY folder
  357. */
  358. public void addHeader(String header_name, String header_value)
  359. throws MessagingException;
  360. /**
  361. * Remove all headers with this name.
  362. *
  363. * @param header_name the name of this header
  364. * @exception MessagingException
  365. * @exception IllegalWriteException if the underlying
  366. * implementation does not support modification
  367. * of existing values
  368. * @exception IllegalStateException if this Part is
  369. * obtained from a READ_ONLY folder
  370. */
  371. public void removeHeader(String header_name)
  372. throws MessagingException;
  373. /**
  374. * Return all the headers from this part as an Enumeration of
  375. * Header objects.
  376. *
  377. * @return array of Header objects
  378. * @exception MessagingException
  379. */
  380. public Enumeration getAllHeaders() throws MessagingException;
  381. /**
  382. * Return matching headers from this part as an Enumeration of
  383. * Header objects.
  384. *
  385. * @return array of Header objects
  386. * @exception MessagingException
  387. */
  388. public Enumeration getMatchingHeaders(String[] header_names)
  389. throws MessagingException;
  390. /**
  391. * Return non-matching headers from this envelope as an Enumeration
  392. * of Header objects.
  393. *
  394. * @return array of Header objects
  395. * @exception MessagingException
  396. */
  397. public Enumeration getNonMatchingHeaders(String[] header_names)
  398. throws MessagingException;
  399. }