001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package examples.nntp;
018
019 import java.io.BufferedReader;
020 import java.io.IOException;
021 import java.io.Reader;
022 import java.util.ArrayList;
023 import java.util.List;
024 import java.util.NoSuchElementException;
025 import java.util.StringTokenizer;
026
027 import org.apache.commons.net.io.DotTerminatedMessageReader;
028 import org.apache.commons.net.nntp.Article;
029 import org.apache.commons.net.nntp.NNTPClient;
030
031 /**
032 *
033 * Some convenience methods for NNTP example classes.
034 *
035 * @author Rory Winston <rwinston@checkfree.com>
036 */
037 public class NNTPUtils {
038
039 /**
040 * Given an {@link NNTPClient} instance, and an integer range of messages, return
041 * an array of {@link Article} instances.
042 * @param client
043 * @param lowArticleNumber
044 * @param highArticleNumber
045 * @return Article[] An array of Article
046 * @throws IOException
047 */
048 public static List<Article> getArticleInfo(NNTPClient client, long lowArticleNumber, long highArticleNumber)
049 throws IOException {
050 Reader reader = null;
051 List<Article> articles = new ArrayList<Article>();
052 reader =
053 (DotTerminatedMessageReader) client.retrieveArticleInfo(
054 lowArticleNumber,
055 highArticleNumber);
056
057 if (reader != null) {
058 String theInfo = readerToString(reader);
059 StringTokenizer st = new StringTokenizer(theInfo, "\n");
060
061 // Extract the article information
062 // Mandatory format (from NNTP RFC 2980) is :
063 // Subject\tAuthor\tDate\tID\tReference(s)\tByte Count\tLine Count
064
065 int count = st.countTokens();
066 int index = 0;
067
068 while (st.hasMoreTokens()) {
069 String msg = st.nextToken();
070 System.out.println("Message:" + msg);
071 StringTokenizer stt = new StringTokenizer(msg, "\t");
072
073 try {
074 Article article = new Article();
075 article.setArticleNumber(Integer.parseInt(stt.nextToken()));
076 article.setSubject(stt.nextToken());
077 article.setFrom(stt.nextToken());
078 article.setDate(stt.nextToken());
079 article.setArticleId(stt.nextToken());
080 article.addHeaderField("References", stt.nextToken());
081 articles.add(article);
082 }
083 catch (NoSuchElementException nse) {
084 // ignore this message
085 }
086 }
087 } else {
088 return null;
089 }
090
091 return articles;
092 }
093
094
095 /**
096 * Convert a {@link Reader} instance to a String
097 * @param reader The Reader instance
098 * @return String
099 */
100 public static String readerToString(Reader reader) {
101 String temp = null;
102 BufferedReader bufReader = new BufferedReader(reader);
103 StringBuilder sb = new StringBuilder();
104 try {
105 temp = bufReader.readLine();
106 while (temp != null) {
107 sb.append(temp);
108 sb.append("\n");
109 temp = bufReader.readLine();
110 }
111 } catch (IOException e) {
112 e.printStackTrace();
113 }
114
115 return sb.toString();
116 }
117 }