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 * https://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
18 package org.apache.commons.net.nntp;
19
20 /**
21 * 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.
22 * When the Java Mail API is finalized, you will be 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 process, by relieving the programmer from having to explicitly format an article header.
25 * For example:
26 * </p>
27 *
28 * <pre>
29 * writer = client.postArticle();
30 * if (writer == null) // failure
31 * return false;
32 * header = new SimpleNNTPHeader("foobar@foo.com", "Just testing");
33 * header.addNewsgroup("alt.test");
34 * header.addHeaderField("Organization", "Foobar, Inc.");
35 * writer.write(header.toString());
36 * writer.write("This is just a test");
37 * writer.close();
38 * if (!client.completePendingCommand()) // failure
39 * return false;
40 * </pre>
41 *
42 * @see NNTPClient
43 */
44
45 public class SimpleNNTPHeader {
46 private final String subject;
47 private final String from;
48 private final StringBuilder newsgroups;
49 private final StringBuilder headerFields;
50 private int newsgroupCount;
51
52 /**
53 * Creates a new SimpleNNTPHeader instance initialized with the given from and subject header field values.
54 *
55 * @param from The value of the {@code From:} header field. This should be the article poster's email address.
56 * @param subject The value of the {@code Subject:} header field. This should be the subject of the article.
57 */
58 public SimpleNNTPHeader(final String from, final String subject) {
59 this.from = from;
60 this.subject = subject;
61 this.newsgroups = new StringBuilder();
62 this.headerFields = new StringBuilder();
63 this.newsgroupCount = 0;
64 }
65
66 /**
67 * Adds an arbitrary header field with the given value to the article header.
68 * These headers will be written after the {@code From}, Newsgroups, and Subject fields
69 * when the SimpleNNTPHeader is converted to a string. An example use would be:
70 *
71 * <pre>
72 * header.addHeaderField("Organization", "Foobar, Inc.");
73 * </pre>
74 *
75 * @param headerField The header field to add, not including the colon.
76 * @param value The value of the added header field.
77 */
78 public void addHeaderField(final String headerField, final String value) {
79 headerFields.append(headerField);
80 headerFields.append(": ");
81 headerFields.append(value);
82 headerFields.append('\n');
83 }
84
85 /**
86 * Adds a newsgroup to the article {@code Newsgroups:} field.
87 *
88 * @param newsgroup The newsgroup to add to the article's newsgroup distribution list.
89 */
90 public void addNewsgroup(final String newsgroup) {
91 if (newsgroupCount++ > 0) {
92 newsgroups.append(',');
93 }
94 newsgroups.append(newsgroup);
95 }
96
97 /**
98 * Gets the address used in the {@code From:} header field.
99 *
100 * @return The from address.
101 */
102 public String getFromAddress() {
103 return from;
104 }
105
106 /**
107 * Gets the contents of the {@code Newsgroups:} header field.
108 *
109 * @return The comma-separated list of newsgroups to which the article is being posted.
110 */
111 public String getNewsgroups() {
112 return newsgroups.toString();
113 }
114
115 /**
116 * Gets the subject used in the {@code Subject:} header field.
117 *
118 * @return The subject.
119 */
120 public String getSubject() {
121 return subject;
122 }
123
124 /**
125 * 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
126 * article body.
127 *
128 * @return The article header in the form of a String.
129 */
130 @Override
131 public String toString() {
132 final StringBuilder header = new StringBuilder();
133
134 header.append("From: ");
135 header.append(from);
136 header.append("\nNewsgroups: ");
137 header.append(newsgroups.toString());
138 header.append("\nSubject: ");
139 header.append(subject);
140 header.append('\n');
141 if (headerFields.length() > 0) {
142 header.append(headerFields.toString());
143 }
144 header.append('\n');
145
146 return header.toString();
147 }
148 }