SimpleNNTPHeader.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.nntp;

  18. /**
  19.  * This class is used to construct the bare minimum acceptable header for most newsreaders. To construct more complicated headers you should refer to RFC 822.
  20.  * When the Java Mail API is finalized, you will be able to use it to compose fully compliant Internet text messages.
  21.  * <p>
  22.  * The main purpose of the class is to faciliatate the article posting process, by relieving the programmer from having to explicitly format an article header.
  23.  * For example:
  24.  * </p>
  25.  *
  26.  * <pre>
  27.  * writer = client.postArticle();
  28.  * if (writer == null) // failure
  29.  *     return false;
  30.  * header = new SimpleNNTPHeader("foobar@foo.com", "Just testing");
  31.  * header.addNewsgroup("alt.test");
  32.  * header.addHeaderField("Organization", "Foobar, Inc.");
  33.  * writer.write(header.toString());
  34.  * writer.write("This is just a test");
  35.  * writer.close();
  36.  * if (!client.completePendingCommand()) // failure
  37.  *     return false;
  38.  * </pre>
  39.  *
  40.  * @see NNTPClient
  41.  */

  42. public class SimpleNNTPHeader {
  43.     private final String subject, from;
  44.     private final StringBuilder newsgroups;
  45.     private final StringBuilder headerFields;
  46.     private int newsgroupCount;

  47.     /**
  48.      * Creates a new SimpleNNTPHeader instance initialized with the given from and subject header field values.
  49.      *
  50.      * @param from    The value of the <code>From:</code> header field. This should be the article poster's email address.
  51.      * @param subject The value of the <code>Subject:</code> header field. This should be the subject of the article.
  52.      */
  53.     public SimpleNNTPHeader(final String from, final String subject) {
  54.         this.from = from;
  55.         this.subject = subject;
  56.         this.newsgroups = new StringBuilder();
  57.         this.headerFields = new StringBuilder();
  58.         this.newsgroupCount = 0;
  59.     }

  60.     /**
  61.      * Adds an arbitrary header field with the given value to the article header.
  62.      * These headers will be written after the {@code From}, Newsgroups, and Subject fields
  63.      * when the SimpleNNTPHeader is converted to a string. An example use would be:
  64.      *
  65.      * <pre>
  66.      * header.addHeaderField("Organization", "Foobar, Inc.");
  67.      * </pre>
  68.      *
  69.      * @param headerField The header field to add, not including the colon.
  70.      * @param value       The value of the added header field.
  71.      */
  72.     public void addHeaderField(final String headerField, final String value) {
  73.         headerFields.append(headerField);
  74.         headerFields.append(": ");
  75.         headerFields.append(value);
  76.         headerFields.append('\n');
  77.     }

  78.     /**
  79.      * Adds a newsgroup to the article <code>Newsgroups:</code> field.
  80.      *
  81.      * @param newsgroup The newsgroup to add to the article's newsgroup distribution list.
  82.      */
  83.     public void addNewsgroup(final String newsgroup) {
  84.         if (newsgroupCount++ > 0) {
  85.             newsgroups.append(',');
  86.         }
  87.         newsgroups.append(newsgroup);
  88.     }

  89.     /**
  90.      * Returns the address used in the <code>From:</code> header field.
  91.      *
  92.      * @return The from address.
  93.      */
  94.     public String getFromAddress() {
  95.         return from;
  96.     }

  97.     /**
  98.      * Returns the contents of the <code>Newsgroups:</code> header field.
  99.      *
  100.      * @return The comma-separated list of newsgroups to which the article is being posted.
  101.      */
  102.     public String getNewsgroups() {
  103.         return newsgroups.toString();
  104.     }

  105.     /**
  106.      * Returns the subject used in the <code>Subject:</code> header field.
  107.      *
  108.      * @return The subject.
  109.      */
  110.     public String getSubject() {
  111.         return subject;
  112.     }

  113.     /**
  114.      * Converts the SimpleNNTPHeader to a properly formatted header in the form of a String, including the blank line used to separate the header from the
  115.      * article body.
  116.      *
  117.      * @return The article header in the form of a String.
  118.      */
  119.     @Override
  120.     public String toString() {
  121.         final StringBuilder header = new StringBuilder();

  122.         header.append("From: ");
  123.         header.append(from);
  124.         header.append("\nNewsgroups: ");
  125.         header.append(newsgroups.toString());
  126.         header.append("\nSubject: ");
  127.         header.append(subject);
  128.         header.append('\n');
  129.         if (headerFields.length() > 0) {
  130.             header.append(headerFields.toString());
  131.         }
  132.         header.append('\n');

  133.         return header.toString();
  134.     }
  135. }