View Javadoc

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