1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net.nntp;
  17. /***
  18. * This class is used to construct the bare minimum
  19. * acceptable header for most news readers. To construct more
  20. * complicated headers you should refer to RFC 822. When the
  21. * Java Mail API is finalized, you will be
  22. * able to use it to compose fully compliant Internet text messages.
  23. * <p>
  24. * The main purpose of the class is to faciliatate the article posting
  25. * process, by relieving the programmer from having to explicitly format
  26. * an article header. For example:
  27. * <pre>
  28. * writer = client.postArticle();
  29. * if(writer == null) // failure
  30. * return false;
  31. * header = new SimpleNNTPHeader("foobar@foo.com", "Just testing");
  32. * header.addNewsgroup("alt.test");
  33. * header.addHeaderField("Organization", "Foobar, Inc.");
  34. * writer.write(header.toString());
  35. * writer.write("This is just a test");
  36. * writer.close();
  37. * if(!client.completePendingCommand()) // failure
  38. * return false;
  39. * </pre>
  40. * <p>
  41. * <p>
  42. * @author Daniel F. Savarese
  43. * @see NNTPClient
  44. ***/
  45. public class SimpleNNTPHeader
  46. {
  47. private String __subject, __from;
  48. private StringBuffer __newsgroups;
  49. private StringBuffer __headerFields;
  50. private int __newsgroupCount;
  51. /***
  52. * Creates a new SimpleNNTPHeader instance initialized with the given
  53. * from and subject header field values.
  54. * <p>
  55. * @param from The value of the <code>From:</code> header field. This
  56. * should be the article poster's email address.
  57. * @param subject The value of the <code>Subject:</code> header field.
  58. * This should be the subject of the article.
  59. ***/
  60. public SimpleNNTPHeader(String from, String subject)
  61. {
  62. __from = from;
  63. __subject = subject;
  64. __newsgroups = new StringBuffer();
  65. __headerFields = new StringBuffer();
  66. __newsgroupCount = 0;
  67. }
  68. /***
  69. * Adds a newsgroup to the article <code>Newsgroups:</code> field.
  70. * <p>
  71. * @param newsgroup The newsgroup to add to the article's newsgroup
  72. * distribution list.
  73. ***/
  74. public void addNewsgroup(String newsgroup)
  75. {
  76. if (__newsgroupCount++ > 0)
  77. __newsgroups.append(',');
  78. __newsgroups.append(newsgroup);
  79. }
  80. /***
  81. * Adds an arbitrary header field with the given value to the article
  82. * header. These headers will be written after the From, Newsgroups,
  83. * and Subject fields when the SimpleNNTPHeader is convertered to a string.
  84. * An example use would be:
  85. * <pre>
  86. * header.addHeaderField("Organization", "Foobar, Inc.");
  87. * </pre>
  88. * <p>
  89. * @param headerField The header field to add, not including the colon.
  90. * @param value The value of the added header field.
  91. ***/
  92. public void addHeaderField(String headerField, String value)
  93. {
  94. __headerFields.append(headerField);
  95. __headerFields.append(": ");
  96. __headerFields.append(value);
  97. __headerFields.append('\n');
  98. }
  99. /***
  100. * Returns the address used in the <code> From: </code> header field.
  101. * <p>
  102. * @return The from address.
  103. ***/
  104. public String getFromAddress()
  105. {
  106. return __from;
  107. }
  108. /***
  109. * Returns the subject used in the <code> Subject: </code> header field.
  110. * <p>
  111. * @return The subject.
  112. ***/
  113. public String getSubject()
  114. {
  115. return __subject;
  116. }
  117. /***
  118. * Returns the contents of the <code> Newsgroups: </code> header field.
  119. * <p>
  120. * @return The comma-separated list of newsgroups to which the article
  121. * is being posted.
  122. ***/
  123. public String getNewsgroups()
  124. {
  125. return __newsgroups.toString();
  126. }
  127. /***
  128. * Converts the SimpleNNTPHeader to a properly formatted header in
  129. * the form of a String, including the blank line used to separate
  130. * the header from the article body.
  131. * <p>
  132. * @return The article header in the form of a String.
  133. ***/
  134. public String toString()
  135. {
  136. StringBuffer header = new StringBuffer();
  137. header.append("From: ");
  138. header.append(__from);
  139. header.append("\nNewsgroups: ");
  140. header.append(__newsgroups.toString());
  141. header.append("\nSubject: ");
  142. header.append(__subject);
  143. header.append('\n');
  144. if (__headerFields.length() > 0)
  145. header.append(__headerFields.toString());
  146. header.append('\n');
  147. return header.toString();
  148. }
  149. }