1 package org.apache.commons.net.nntp;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Commons" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 /****
58 * This class is used to construct the bare minimum
59 * acceptable header for most news readers. To construct more
60 * complicated headers you should refer to RFC 822. When the
61 * Java Mail API is finalized, you will be
62 * able to use it to compose fully compliant Internet text messages.
63 * <p>
64 * The main purpose of the class is to faciliatate the article posting
65 * process, by relieving the programmer from having to explicitly format
66 * an article header. For example:
67 * <pre>
68 * writer = client.postArticle();
69 * if(writer == null) // failure
70 * return false;
71 * header = new SimpleNNTPHeader("foobar@foo.com", "Just testing");
72 * header.addNewsgroup("alt.test");
73 * header.addHeaderField("Organization", "Foobar, Inc.");
74 * writer.write(header.toString());
75 * writer.write("This is just a test");
76 * writer.close();
77 * if(!client.completePendingCommand()) // failure
78 * return false;
79 * </pre>
80 * <p>
81 * <p>
82 * @author Daniel F. Savarese
83 * @see NNTPClient
84 ***/
85
86 public class SimpleNNTPHeader
87 {
88 private String __subject, __from;
89 private StringBuffer __newsgroups;
90 private StringBuffer __headerFields;
91 private int __newsgroupCount;
92
93 /****
94 * Creates a new SimpleNNTPHeader instance initialized with the given
95 * from and subject header field values.
96 * <p>
97 * @param from The value of the <code>From:</code> header field. This
98 * should be the article poster's email address.
99 * @param subject The value of the <code>Subject:</code> header field.
100 * This should be the subject of the article.
101 ***/
102 public SimpleNNTPHeader(String from, String subject)
103 {
104 __from = from;
105 __subject = subject;
106 __newsgroups = new StringBuffer();
107 __headerFields = new StringBuffer();
108 __newsgroupCount = 0;
109 }
110
111 /****
112 * Adds a newsgroup to the article <code>Newsgroups:</code> field.
113 * <p>
114 * @param newsgroup The newsgroup to add to the article's newsgroup
115 * distribution list.
116 ***/
117 public void addNewsgroup(String newsgroup)
118 {
119 if (__newsgroupCount++ > 0)
120 __newsgroups.append(',');
121 __newsgroups.append(newsgroup);
122 }
123
124 /****
125 * Adds an arbitrary header field with the given value to the article
126 * header. These headers will be written after the From, Newsgroups,
127 * and Subject fields when the SimpleNNTPHeader is convertered to a string.
128 * An example use would be:
129 * <pre>
130 * header.addHeaderField("Organization", "Foobar, Inc.");
131 * </pre>
132 * <p>
133 * @param headerField The header field to add, not including the colon.
134 * @param value The value of the added header field.
135 ***/
136 public void addHeaderField(String headerField, String value)
137 {
138 __headerFields.append(headerField);
139 __headerFields.append(": ");
140 __headerFields.append(value);
141 __headerFields.append('\n');
142 }
143
144
145 /****
146 * Returns the address used in the <code> From: </code> header field.
147 * <p>
148 * @return The from address.
149 ***/
150 public String getFromAddress()
151 {
152 return __from;
153 }
154
155 /****
156 * Returns the subject used in the <code> Subject: </code> header field.
157 * <p>
158 * @return The subject.
159 ***/
160 public String getSubject()
161 {
162 return __subject;
163 }
164
165 /****
166 * Returns the contents of the <code> Newsgroups: </code> header field.
167 * <p>
168 * @return The comma-separated list of newsgroups to which the article
169 * is being posted.
170 ***/
171 public String getNewsgroups()
172 {
173 return __newsgroups.toString();
174 }
175
176 /****
177 * Converts the SimpleNNTPHeader to a properly formatted header in
178 * the form of a String, including the blank line used to separate
179 * the header from the article body.
180 * <p>
181 * @return The article header in the form of a String.
182 ***/
183 public String toString()
184 {
185 StringBuffer header = new StringBuffer();
186
187 header.append("From: ");
188 header.append(__from);
189 header.append("\nNewsgroups: ");
190 header.append(__newsgroups.toString());
191 header.append("\nSubject: ");
192 header.append(__subject);
193 header.append('\n');
194 if (__headerFields.length() > 0)
195 header.append(__headerFields.toString());
196 header.append('\n');
197
198 return header.toString();
199 }
200 }
This page was automatically generated by Maven