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