001    package examples.nntp;
002    
003    import java.io.BufferedReader;
004    import java.io.IOException;
005    import java.io.Reader;
006    import java.util.StringTokenizer;
007    
008    import org.apache.commons.net.io.DotTerminatedMessageReader;
009    import org.apache.commons.net.nntp.Article;
010    import org.apache.commons.net.nntp.NNTPClient;
011    
012    /**
013     * 
014     * Some convenience methods for NNTP example classes.
015     * 
016     * @author Rory Winston <rwinston@checkfree.com>
017     */
018    public class NNTPUtils {
019    
020            /**
021             * Given an {@link NNTPClient} instance, and an integer range of messages, return 
022             * an array of {@link Article} instances.
023             * @param client 
024             * @param lowArticleNumber
025             * @param highArticleNumber
026             * @return Article[] An array of Article
027             * @throws IOException
028             */
029            public  static Article[] getArticleInfo(NNTPClient client, int lowArticleNumber, int highArticleNumber)
030                            throws IOException {
031                            Reader reader = null;
032                            Article[] articles = null;
033                            reader =
034                                    (DotTerminatedMessageReader) client.retrieveArticleInfo(
035                                            lowArticleNumber,
036                                            highArticleNumber);
037    
038                            if (reader != null) {
039                                    String theInfo = readerToString(reader);
040                                    StringTokenizer st = new StringTokenizer(theInfo, "\n");
041    
042                                    // Extract the article information
043                                    // Mandatory format (from NNTP RFC 2980) is :
044                                    // Subject\tAuthor\tDate\tID\tReference(s)\tByte Count\tLine Count
045    
046                                    int count = st.countTokens();
047                                    articles = new Article[count];
048                                    int index = 0;
049    
050                                    while (st.hasMoreTokens()) {
051                                            StringTokenizer stt = new StringTokenizer(st.nextToken(), "\t");
052                                            Article article = new Article();
053                                            article.setArticleNumber(Integer.parseInt(stt.nextToken()));
054                                            article.setSubject(stt.nextToken());
055                                            article.setFrom(stt.nextToken());
056                                            article.setDate(stt.nextToken());
057                                            article.setArticleId(stt.nextToken());
058                                            article.addHeaderField("References", stt.nextToken());
059                                            articles[index++] = article;
060                                    }
061                            } else {
062                                    return null;
063                            }
064    
065                            return articles;
066                    }
067                    
068            
069            /**
070             * Convert a {@link Reader} instance to a String
071             * @param reader The Reader instance
072             * @return String
073             */             
074            public static String readerToString(Reader reader) {
075                    String temp = null;
076                    StringBuffer sb = null;
077                    BufferedReader bufReader = new BufferedReader(reader);
078    
079                    sb = new StringBuffer();
080                    try {
081                            temp = bufReader.readLine();
082                            while (temp != null) {
083                                    sb.append(temp);
084                                    sb.append("\n");
085                                    temp = bufReader.readLine();
086                            }
087                    } catch (IOException e) {
088                            e.printStackTrace();
089                    }
090    
091                    return sb.toString();
092            }
093    }