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.util.Vector;
  7. /**
  8. * Clients use a FetchProfile to list the Message attributes that
  9. * it wishes to prefetch from the server for a range of messages.<p>
  10. *
  11. * Messages obtained from a Folder are light-weight objects that
  12. * typically start off as empty references to the actual messages.
  13. * Such a Message object is filled in "on-demand" when the appropriate
  14. * get*() methods are invoked on that particular Message. Certain
  15. * server-based message access protocols (Ex: IMAP) allow batch
  16. * fetching of message attributes for a range of messages in a single
  17. * request. Clients that want to use message attributes for a range of
  18. * Messages (Example: to display the top-level headers in a headerlist)
  19. * might want to use the optimization provided by such servers. The
  20. * <code>FetchProfile</code> allows the client to indicate this desire
  21. * to the server. <p>
  22. *
  23. * Note that implementations are not obligated to support
  24. * FetchProfiles, since there might be cases where the backend service
  25. * does not allow easy, efficient fetching of such profiles. <p>
  26. *
  27. * Sample code that illustrates the use of a FetchProfile is given
  28. * below: <p>
  29. * <blockquote>
  30. * <pre>
  31. *
  32. * Message[] msgs = folder.getMessages();
  33. *
  34. * FetchProfile fp = new FetchProfile();
  35. * fp.add(FetchProfile.Item.ENVELOPE);
  36. * fp.add("X-mailer");
  37. * folder.fetch(msgs, fp);
  38. *
  39. * </pre></blockquote><p>
  40. *
  41. * @see javax.mail.Folder#fetch
  42. * @author John Mani
  43. * @author Bill Shannon
  44. */
  45. public class FetchProfile {
  46. private Vector specials; // specials
  47. private Vector headers; // vector of header names
  48. /**
  49. * This inner class is the base class of all items that
  50. * can be requested in a FetchProfile. The items currently
  51. * defined here are <code>ENVELOPE</code>, <code>CONTENT_INFO</code>
  52. * and <code>FLAGS</code>. The <code>UIDFolder</code> interface
  53. * defines the <code>UID</code> Item as well. <p>
  54. *
  55. * Note that this class only has a protected constructor, therby
  56. * restricting new Item types to either this class or subclasses.
  57. * This effectively implements a enumeration of allowed Item types.
  58. *
  59. * @see UIDFolder
  60. */
  61. public static class Item {
  62. /**
  63. * This is the Envelope item. <p>
  64. *
  65. * The Envelope is an aggregration of the common attributes
  66. * of a Message. Implementations should include the following
  67. * attributes: From, To, Cc, Bcc, ReplyTo, Subject and Date.
  68. * More items may be included as well. <p>
  69. *
  70. * For implementations of the IMAP4 protocol (RFC 2060), the
  71. * Envelope should include the ENVELOPE data item. More items
  72. * may be included too.
  73. */
  74. public static final Item ENVELOPE = new Item("ENVELOPE");
  75. /**
  76. * This item is for fetching information about the
  77. * content of the message. <p>
  78. *
  79. * This includes all the attributes that describe the content
  80. * of the message. Implementations should include the following
  81. * attributes: ContentType, ContentDisposition,
  82. * ContentDescription, Size and LineCount. Other items may be
  83. * included as well.
  84. */
  85. public static final Item CONTENT_INFO = new Item("CONTENT_INFO");
  86. /**
  87. * This is the Flags item.
  88. */
  89. public static final Item FLAGS = new Item("FLAGS");
  90. private String name;
  91. /**
  92. * Constructor for an item. The name is used only for debugging.
  93. */
  94. protected Item(String name) {
  95. this.name = name;
  96. }
  97. }
  98. /**
  99. * Create an empty FetchProfile.
  100. */
  101. public FetchProfile() {
  102. specials = null;
  103. headers = null;
  104. }
  105. /**
  106. * Add the given special item as one of the attributes to
  107. * be prefetched.
  108. *
  109. * @param item the special item to be fetched
  110. * @see FetchProfile.Item#ENVELOPE
  111. * @see FetchProfile.Item#CONTENT_INFO
  112. * @see FetchProfile.Item#FLAGS
  113. */
  114. public void add(Item item) {
  115. if (specials == null)
  116. specials = new Vector();
  117. specials.addElement(item);
  118. }
  119. /**
  120. * Add the specified header-field to the list of attributes
  121. * to be prefetched.
  122. *
  123. * @param headerName header to be prefetched
  124. */
  125. public void add(String headerName) {
  126. if (headers == null)
  127. headers = new Vector();
  128. headers.addElement(headerName);
  129. }
  130. /**
  131. * Returns true if the fetch profile contains given special item.
  132. */
  133. public boolean contains(Item item) {
  134. return specials != null && specials.contains(item);
  135. }
  136. /**
  137. * Returns true if the fetch profile contains given header name.
  138. */
  139. public boolean contains(String headerName) {
  140. return headers != null && headers.contains(headerName);
  141. }
  142. /**
  143. * Get the items set in this profile.
  144. *
  145. * @return items set in this profile
  146. */
  147. public Item[] getItems() {
  148. if (specials == null)
  149. return new Item[0];
  150. Item[] s = new Item[specials.size()];
  151. specials.copyInto(s);
  152. return s;
  153. }
  154. /**
  155. * Get the names of the header-fields set in this profile.
  156. *
  157. * @return headers set in this profile
  158. */
  159. public String[] getHeaderNames() {
  160. if (headers == null)
  161. return new String[0];
  162. String[] s = new String[headers.size()];
  163. headers.copyInto(s);
  164. return s;
  165. }
  166. }