View Javadoc

    1   package examples.nntp;
    2
    3   import java.io.BufferedReader;
    4   import java.io.IOException;
    5   import java.io.Reader;
    6   import java.util.StringTokenizer;
    7
    8   import org.apache.commons.net.io.DotTerminatedMessageReader;
    9   import org.apache.commons.net.nntp.Article;
    10  import org.apache.commons.net.nntp.NNTPClient;
    11
    12  /***
    13   *
    14   * Some convenience methods for NNTP example classes.
    15   *
    16   * @author Rory Winston <rwinston@checkfree.com>
    17   */
    18  public class NNTPUtils {
    19
    20  	/***
    21  	 * Given an {@link NNTPClient} instance, and an integer range of messages, return
    22  	 * an array of {@link Article} instances.
    23  	 * @param client
    24  	 * @param lowArticleNumber
    25  	 * @param highArticleNumber
    26  	 * @return Article[] An array of Article
    27  	 * @throws IOException
    28  	 */
    29  	public  static Article[] getArticleInfo(NNTPClient client, int lowArticleNumber, int highArticleNumber)
    30  			throws IOException {
    31  			Reader reader = null;
    32  			Article[] articles = null;
    33  			reader =
    34  				(DotTerminatedMessageReader) client.retrieveArticleInfo(
    35  					lowArticleNumber,
    36  					highArticleNumber);
    37
    38  			if (reader != null) {
    39  				String theInfo = readerToString(reader);
    40  				StringTokenizer st = new StringTokenizer(theInfo, "\n");
    41
    42  				// Extract the article information
    43  				// Mandatory format (from NNTP RFC 2980) is :
    44  				// Subject\tAuthor\tDate\tID\tReference(s)\tByte Count\tLine Count
    45
    46  				int count = st.countTokens();
    47  				articles = new Article[count];
    48  				int index = 0;
    49
    50  				while (st.hasMoreTokens()) {
    51  					StringTokenizer stt = new StringTokenizer(st.nextToken(), "\t");
    52  					Article article = new Article();
    53  					article.setArticleNumber(Integer.parseInt(stt.nextToken()));
    54  					article.setSubject(stt.nextToken());
    55  					article.setFrom(stt.nextToken());
    56  					article.setDate(stt.nextToken());
    57  					article.setArticleId(stt.nextToken());
    58  					article.addHeaderField("References", stt.nextToken());
    59  					articles[index++] = article;
    60  				}
    61  			} else {
    62  				return null;
    63  			}
    64
    65  			return articles;
    66  		}
    67
    68
    69  	/***
    70  	 * Convert a {@link Reader} instance to a String
    71  	 * @param reader The Reader instance
    72  	 * @return String
    73  	 */
    74  	public static String readerToString(Reader reader) {
    75  		String temp = null;
    76  		StringBuffer sb = null;
    77  		BufferedReader bufReader = new BufferedReader(reader);
    78
    79  		sb = new StringBuffer();
    80  		try {
    81  			temp = bufReader.readLine();
    82  			while (temp != null) {
    83  				sb.append(temp);
    84  				sb.append("\n");
    85  				temp = bufReader.readLine();
    86  			}
    87  		} catch (IOException e) {
    88  			e.printStackTrace();
    89  		}
    90
    91  		return sb.toString();
    92  	}
    93  }