You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by rw...@apache.org on 2009/04/04 10:30:43 UTC

svn commit: r761877 - in /commons/proper/net/branches/NET_2_0/src/main/java: examples/nntp/ org/apache/commons/net/nntp/

Author: rwinston
Date: Sat Apr  4 08:30:42 2009
New Revision: 761877

URL: http://svn.apache.org/viewvc?rev=761877&view=rev
Log:
* Improve handling of invalid messages
* Add TODO for converting recursive flush() to iterative version

Modified:
    commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/ExtendedNNTPOps.java
    commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/MessageThreading.java
    commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/NNTPUtils.java
    commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/nntp/ThreadContainer.java
    commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/nntp/Threader.java

Modified: commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/ExtendedNNTPOps.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/ExtendedNNTPOps.java?rev=761877&r1=761876&r2=761877&view=diff
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/ExtendedNNTPOps.java (original)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/ExtendedNNTPOps.java Sat Apr  4 08:30:42 2009
@@ -19,6 +19,7 @@
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.List;
 
 import org.apache.commons.net.PrintCommandListener;
 import org.apache.commons.net.nntp.Article;
@@ -29,7 +30,7 @@
 /**
  * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
  * 
- * @author Rory Winston <rw...@checkfree.com>
+ * @author Rory Winston <rw...@apache.org>
  */
 public class ExtendedNNTPOps {
 
@@ -59,10 +60,10 @@
             client.selectNewsgroup("alt.test", testGroup);
             int lowArticleNumber = testGroup.getFirstArticle();
             int highArticleNumber = lowArticleNumber + 100;
-            Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
+            List<Article> articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
 
-            for (int i = 0; i < articles.length; ++i) {
-                System.out.println(articles[i].getSubject());
+            for (Article article : articles) {
+                System.out.println(article.getSubject());
             }
 
             // LIST ACTIVE

Modified: commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/MessageThreading.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/MessageThreading.java?rev=761877&r1=761876&r2=761877&view=diff
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/MessageThreading.java (original)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/MessageThreading.java Sat Apr  4 08:30:42 2009
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.net.SocketException;
+import java.util.List;
 
 import org.apache.commons.net.PrintCommandListener;
 import org.apache.commons.net.nntp.Article;
@@ -45,27 +46,27 @@
         NNTPClient client = new NNTPClient();
         client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
         client.connect(hostname);
-        
-        if(!client.authenticate(user, password)) {
-            System.out.println("Authentication failed for user " + user + "!");
-            System.exit(1);
-        }
-        
+//      optional authentication
+//        
+//        if(!client.authenticate(user, password)) {
+//            System.out.println("Authentication failed for user " + user + "!");
+//          //  System.exit(1);
+//        }
+//        
         NewsgroupInfo group = new NewsgroupInfo();
-        client.selectNewsgroup("comp.lang.lisp", group);
+        client.selectNewsgroup("alt.test", group);
         
         int lowArticleNumber = group.getFirstArticle();
-        int highArticleNumber = lowArticleNumber + 100;
+        int highArticleNumber = lowArticleNumber + 5000;
         
         System.out.println("Retrieving articles between [" + lowArticleNumber + "] and [" + highArticleNumber + "]");
-        Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
+        List<Article> articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
         
         System.out.println("Building message thread tree...");
         Threader threader = new Threader();
         Article root = (Article)threader.thread(articles);
         
         Article.printThread(root, 0);
-        
     }
     
     

Modified: commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/NNTPUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/NNTPUtils.java?rev=761877&r1=761876&r2=761877&view=diff
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/NNTPUtils.java (original)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/nntp/NNTPUtils.java Sat Apr  4 08:30:42 2009
@@ -19,6 +19,9 @@
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
 import java.util.StringTokenizer;
 
 import org.apache.commons.net.io.DotTerminatedMessageReader;
@@ -33,77 +36,84 @@
  */
 public class NNTPUtils {
 
-    /**
-     * Given an {@link NNTPClient} instance, and an integer range of messages, return 
-     * an array of {@link Article} instances.
-     * @param client 
-     * @param lowArticleNumber
-     * @param highArticleNumber
-     * @return Article[] An array of Article
-     * @throws IOException
-     */
-    public  static Article[] getArticleInfo(NNTPClient client, int lowArticleNumber, int highArticleNumber)
-            throws IOException {
-            Reader reader = null;
-            Article[] articles = null;
-            reader =
-                (DotTerminatedMessageReader) client.retrieveArticleInfo(
-                    lowArticleNumber,
-                    highArticleNumber);
-
-            if (reader != null) {
-                String theInfo = readerToString(reader);
-                StringTokenizer st = new StringTokenizer(theInfo, "\n");
-
-                // Extract the article information
-                // Mandatory format (from NNTP RFC 2980) is :
-                // Subject\tAuthor\tDate\tID\tReference(s)\tByte Count\tLine Count
-
-                int count = st.countTokens();
-                articles = new Article[count];
-                int index = 0;
-
-                while (st.hasMoreTokens()) {
-                    StringTokenizer stt = new StringTokenizer(st.nextToken(), "\t");
-                    Article article = new Article();
-                    article.setArticleNumber(Integer.parseInt(stt.nextToken()));
-                    article.setSubject(stt.nextToken());
-                    article.setFrom(stt.nextToken());
-                    article.setDate(stt.nextToken());
-                    article.setArticleId(stt.nextToken());
-                    article.addHeaderField("References", stt.nextToken());
-                    articles[index++] = article;
-                }
-            } else {
-                return null;
-            }
-
-            return articles;
-        }
-        
-    
-    /**
-     * Convert a {@link Reader} instance to a String
-     * @param reader The Reader instance
-     * @return String
-     */
-    public static String readerToString(Reader reader) {
-        String temp = null;
-        StringBuffer sb = null;
-        BufferedReader bufReader = new BufferedReader(reader);
-
-        sb = new StringBuffer();
-        try {
-            temp = bufReader.readLine();
-            while (temp != null) {
-                sb.append(temp);
-                sb.append("\n");
-                temp = bufReader.readLine();
-            }
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
+	/**
+	 * Given an {@link NNTPClient} instance, and an integer range of messages, return 
+	 * an array of {@link Article} instances.
+	 * @param client 
+	 * @param lowArticleNumber
+	 * @param highArticleNumber
+	 * @return Article[] An array of Article
+	 * @throws IOException
+	 */
+	public  static List<Article> getArticleInfo(NNTPClient client, int lowArticleNumber, int highArticleNumber)
+	throws IOException {
+		Reader reader = null;
+		List<Article> articles = new ArrayList<Article>();
+		reader =
+			(DotTerminatedMessageReader) client.retrieveArticleInfo(
+					lowArticleNumber,
+					highArticleNumber);
+
+		if (reader != null) {
+			String theInfo = readerToString(reader);
+			StringTokenizer st = new StringTokenizer(theInfo, "\n");
+
+			// Extract the article information
+			// Mandatory format (from NNTP RFC 2980) is :
+			// Subject\tAuthor\tDate\tID\tReference(s)\tByte Count\tLine Count
+
+			int count = st.countTokens();
+			int index = 0;
+
+			while (st.hasMoreTokens()) {
+				String msg = st.nextToken();
+				System.out.println("Message:" + msg);
+				StringTokenizer stt = new StringTokenizer(msg, "\t");
+
+				try {
+					Article article = new Article();
+					article.setArticleNumber(Integer.parseInt(stt.nextToken()));
+					article.setSubject(stt.nextToken());
+					article.setFrom(stt.nextToken());
+					article.setDate(stt.nextToken());
+					article.setArticleId(stt.nextToken());
+					article.addHeaderField("References", stt.nextToken());
+					articles.add(article); 
+				}
+				catch (NoSuchElementException nse) {
+					// ignore this message
+				}
+			}
+		} else {
+			return null;
+		}
+
+		return articles;
+	}
+
+
+	/**
+	 * Convert a {@link Reader} instance to a String
+	 * @param reader The Reader instance
+	 * @return String
+	 */
+	public static String readerToString(Reader reader) {
+		String temp = null;
+		StringBuffer sb = null;
+		BufferedReader bufReader = new BufferedReader(reader);
+
+		sb = new StringBuffer();
+		try {
+			temp = bufReader.readLine();
+			while (temp != null) {
+				sb.append(temp);
+				sb.append("\n");
+				temp = bufReader.readLine();
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
 
-        return sb.toString();
-    }
+		return sb.toString();
+	}
 }

Modified: commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/nntp/ThreadContainer.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/nntp/ThreadContainer.java?rev=761877&r1=761876&r2=761877&view=diff
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/nntp/ThreadContainer.java (original)
+++ commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/nntp/ThreadContainer.java Sat Apr  4 08:30:42 2009
@@ -31,6 +31,8 @@
 
     // Copy the ThreadContainer tree structure down into the underlying Threadable objects
     // (Make the Threadable tree look like the ThreadContainer tree)
+    // TODO convert this to an iterative function - this can blow the stack 
+    // with very large Threadable trees
     void flush() {
         if (parent != null && threadable == null)
             throw new RuntimeException("no threadable in " + this.toString());

Modified: commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/nntp/Threader.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/nntp/Threader.java?rev=761877&r1=761876&r2=761877&view=diff
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/nntp/Threader.java (original)
+++ commons/proper/net/branches/NET_2_0/src/main/java/org/apache/commons/net/nntp/Threader.java Sat Apr  4 08:30:42 2009
@@ -29,6 +29,7 @@
 
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 
 public class Threader {
     private ThreadContainer root;
@@ -41,16 +42,16 @@
      * @param messages
      * @return null if messages == null or root.child == null
      */
-    public Threadable thread(Threadable[] messages) {
+    public Threadable thread(List<? extends Threadable> messages) {
         if (messages == null)
             return null;
 
         idTable = new HashMap<String,ThreadContainer>();
 
-        // walk through each Threadable element
-        for (int i = 0; i < messages.length; ++i) {
-            if (!messages[i].isDummy())
-                buildContainer(messages[i]);
+        // walk through each Threadable element    
+        for (Threadable t : messages) {
+        	if (!t.isDummy())
+        		buildContainer(t);
         }
 
         root = findRootSet();