001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.nntp;
017:
018: /***
019: * This class is used to construct the bare minimum
020: * acceptable header for most news readers. To construct more
021: * complicated headers you should refer to RFC 822. When the
022: * Java Mail API is finalized, you will be
023: * able to use it to compose fully compliant Internet text messages.
024: * <p>
025: * The main purpose of the class is to faciliatate the article posting
026: * process, by relieving the programmer from having to explicitly format
027: * an article header. For example:
028: * <pre>
029: * writer = client.postArticle();
030: * if(writer == null) // failure
031: * return false;
032: * header = new SimpleNNTPHeader("foobar@foo.com", "Just testing");
033: * header.addNewsgroup("alt.test");
034: * header.addHeaderField("Organization", "Foobar, Inc.");
035: * writer.write(header.toString());
036: * writer.write("This is just a test");
037: * writer.close();
038: * if(!client.completePendingCommand()) // failure
039: * return false;
040: * </pre>
041: * <p>
042: * <p>
043: * @author Daniel F. Savarese
044: * @see NNTPClient
045: ***/
046:
047: public class SimpleNNTPHeader {
048: private String __subject, __from;
049: private StringBuffer __newsgroups;
050: private StringBuffer __headerFields;
051: private int __newsgroupCount;
052:
053: /***
054: * Creates a new SimpleNNTPHeader instance initialized with the given
055: * from and subject header field values.
056: * <p>
057: * @param from The value of the <code>From:</code> header field. This
058: * should be the article poster's email address.
059: * @param subject The value of the <code>Subject:</code> header field.
060: * This should be the subject of the article.
061: ***/
062: public SimpleNNTPHeader(String from, String subject) {
063: __from = from;
064: __subject = subject;
065: __newsgroups = new StringBuffer();
066: __headerFields = new StringBuffer();
067: __newsgroupCount = 0;
068: }
069:
070: /***
071: * Adds a newsgroup to the article <code>Newsgroups:</code> field.
072: * <p>
073: * @param newsgroup The newsgroup to add to the article's newsgroup
074: * distribution list.
075: ***/
076: public void addNewsgroup(String newsgroup) {
077: if (__newsgroupCount++ > 0)
078: __newsgroups.append(',');
079: __newsgroups.append(newsgroup);
080: }
081:
082: /***
083: * Adds an arbitrary header field with the given value to the article
084: * header. These headers will be written after the From, Newsgroups,
085: * and Subject fields when the SimpleNNTPHeader is convertered to a string.
086: * An example use would be:
087: * <pre>
088: * header.addHeaderField("Organization", "Foobar, Inc.");
089: * </pre>
090: * <p>
091: * @param headerField The header field to add, not including the colon.
092: * @param value The value of the added header field.
093: ***/
094: public void addHeaderField(String headerField, String value) {
095: __headerFields.append(headerField);
096: __headerFields.append(": ");
097: __headerFields.append(value);
098: __headerFields.append('\n');
099: }
100:
101: /***
102: * Returns the address used in the <code> From: </code> header field.
103: * <p>
104: * @return The from address.
105: ***/
106: public String getFromAddress() {
107: return __from;
108: }
109:
110: /***
111: * Returns the subject used in the <code> Subject: </code> header field.
112: * <p>
113: * @return The subject.
114: ***/
115: public String getSubject() {
116: return __subject;
117: }
118:
119: /***
120: * Returns the contents of the <code> Newsgroups: </code> header field.
121: * <p>
122: * @return The comma-separated list of newsgroups to which the article
123: * is being posted.
124: ***/
125: public String getNewsgroups() {
126: return __newsgroups.toString();
127: }
128:
129: /***
130: * Converts the SimpleNNTPHeader to a properly formatted header in
131: * the form of a String, including the blank line used to separate
132: * the header from the article body.
133: * <p>
134: * @return The article header in the form of a String.
135: ***/
136: public String toString() {
137: StringBuffer header = new StringBuffer();
138:
139: header.append("From: ");
140: header.append(__from);
141: header.append("\nNewsgroups: ");
142: header.append(__newsgroups.toString());
143: header.append("\nSubject: ");
144: header.append(__subject);
145: header.append('\n');
146: if (__headerFields.length() > 0)
147: header.append(__headerFields.toString());
148: header.append('\n');
149:
150: return header.toString();
151: }
152: }
|