Skip to content

Commit 316270d

Browse files
author
Rory Winston
committed
* Improve handling of invalid messages
* Add TODO for converting recursive flush() to iterative version git-svn-id: https://svn.apache.org/repos/asf/commons/proper/net/branches/NET_2_0@761877 13f79535-47bb-0310-9956-ffa450edef68
1 parent 80852c2 commit 316270d

File tree

5 files changed

+100
-85
lines changed

5 files changed

+100
-85
lines changed

src/main/java/examples/nntp/ExtendedNNTPOps.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.IOException;
2121
import java.io.PrintWriter;
22+
import java.util.List;
2223

2324
import org.apache.commons.net.PrintCommandListener;
2425
import org.apache.commons.net.nntp.Article;
@@ -29,7 +30,7 @@
2930
/**
3031
* Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
3132
*
32-
* @author Rory Winston <rwinston@checkfree.com>
33+
* @author Rory Winston <rwinston@apache.org>
3334
*/
3435
public class ExtendedNNTPOps {
3536

@@ -59,10 +60,10 @@ public void demo(String host, String user, String password) {
5960
client.selectNewsgroup("alt.test", testGroup);
6061
int lowArticleNumber = testGroup.getFirstArticle();
6162
int highArticleNumber = lowArticleNumber + 100;
62-
Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
63+
List<Article> articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
6364

64-
for (int i = 0; i < articles.length; ++i) {
65-
System.out.println(articles[i].getSubject());
65+
for (Article article : articles) {
66+
System.out.println(article.getSubject());
6667
}
6768

6869
// LIST ACTIVE

src/main/java/examples/nntp/MessageThreading.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.io.PrintWriter;
2323
import java.net.SocketException;
24+
import java.util.List;
2425

2526
import org.apache.commons.net.PrintCommandListener;
2627
import org.apache.commons.net.nntp.Article;
@@ -45,27 +46,27 @@ public static void main(String[] args) throws SocketException, IOException {
4546
NNTPClient client = new NNTPClient();
4647
client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
4748
client.connect(hostname);
48-
49-
if(!client.authenticate(user, password)) {
50-
System.out.println("Authentication failed for user " + user + "!");
51-
System.exit(1);
52-
}
53-
49+
// optional authentication
50+
//
51+
// if(!client.authenticate(user, password)) {
52+
// System.out.println("Authentication failed for user " + user + "!");
53+
// // System.exit(1);
54+
// }
55+
//
5456
NewsgroupInfo group = new NewsgroupInfo();
55-
client.selectNewsgroup("comp.lang.lisp", group);
57+
client.selectNewsgroup("alt.test", group);
5658

5759
int lowArticleNumber = group.getFirstArticle();
58-
int highArticleNumber = lowArticleNumber + 100;
60+
int highArticleNumber = lowArticleNumber + 5000;
5961

6062
System.out.println("Retrieving articles between [" + lowArticleNumber + "] and [" + highArticleNumber + "]");
61-
Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
63+
List<Article> articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
6264

6365
System.out.println("Building message thread tree...");
6466
Threader threader = new Threader();
6567
Article root = (Article)threader.thread(articles);
6668

6769
Article.printThread(root, 0);
68-
6970
}
7071

7172

src/main/java/examples/nntp/NNTPUtils.java

Lines changed: 76 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.io.BufferedReader;
2020
import java.io.IOException;
2121
import java.io.Reader;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.NoSuchElementException;
2225
import java.util.StringTokenizer;
2326

2427
import org.apache.commons.net.io.DotTerminatedMessageReader;
@@ -33,77 +36,84 @@
3336
*/
3437
public class NNTPUtils {
3538

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);
39+
/**
40+
* Given an {@link NNTPClient} instance, and an integer range of messages, return
41+
* an array of {@link Article} instances.
42+
* @param client
43+
* @param lowArticleNumber
44+
* @param highArticleNumber
45+
* @return Article[] An array of Article
46+
* @throws IOException
47+
*/
48+
public static List<Article> getArticleInfo(NNTPClient client, int lowArticleNumber, int highArticleNumber)
49+
throws IOException {
50+
Reader reader = null;
51+
List<Article> articles = new ArrayList<Article>();
52+
reader =
53+
(DotTerminatedMessageReader) client.retrieveArticleInfo(
54+
lowArticleNumber,
55+
highArticleNumber);
5356

54-
if (reader != null) {
55-
String theInfo = readerToString(reader);
56-
StringTokenizer st = new StringTokenizer(theInfo, "\n");
57+
if (reader != null) {
58+
String theInfo = readerToString(reader);
59+
StringTokenizer st = new StringTokenizer(theInfo, "\n");
5760

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+
// Extract the article information
62+
// Mandatory format (from NNTP RFC 2980) is :
63+
// Subject\tAuthor\tDate\tID\tReference(s)\tByte Count\tLine Count
6164

62-
int count = st.countTokens();
63-
articles = new Article[count];
64-
int index = 0;
65+
int count = st.countTokens();
66+
int index = 0;
6567

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-
}
68+
while (st.hasMoreTokens()) {
69+
String msg = st.nextToken();
70+
System.out.println("Message:" + msg);
71+
StringTokenizer stt = new StringTokenizer(msg, "\t");
8072

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);
73+
try {
74+
Article article = new Article();
75+
article.setArticleNumber(Integer.parseInt(stt.nextToken()));
76+
article.setSubject(stt.nextToken());
77+
article.setFrom(stt.nextToken());
78+
article.setDate(stt.nextToken());
79+
article.setArticleId(stt.nextToken());
80+
article.addHeaderField("References", stt.nextToken());
81+
articles.add(article);
82+
}
83+
catch (NoSuchElementException nse) {
84+
// ignore this message
85+
}
86+
}
87+
} else {
88+
return null;
89+
}
9490

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-
}
91+
return articles;
92+
}
10693

107-
return sb.toString();
108-
}
94+
95+
/**
96+
* Convert a {@link Reader} instance to a String
97+
* @param reader The Reader instance
98+
* @return String
99+
*/
100+
public static String readerToString(Reader reader) {
101+
String temp = null;
102+
StringBuffer sb = null;
103+
BufferedReader bufReader = new BufferedReader(reader);
104+
105+
sb = new StringBuffer();
106+
try {
107+
temp = bufReader.readLine();
108+
while (temp != null) {
109+
sb.append(temp);
110+
sb.append("\n");
111+
temp = bufReader.readLine();
112+
}
113+
} catch (IOException e) {
114+
e.printStackTrace();
115+
}
116+
117+
return sb.toString();
118+
}
109119
}

src/main/java/org/apache/commons/net/nntp/ThreadContainer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ else if (child == target)
3131

3232
// Copy the ThreadContainer tree structure down into the underlying Threadable objects
3333
// (Make the Threadable tree look like the ThreadContainer tree)
34+
// TODO convert this to an iterative function - this can blow the stack
35+
// with very large Threadable trees
3436
void flush() {
3537
if (parent != null && threadable == null)
3638
throw new RuntimeException("no threadable in " + this.toString());

src/main/java/org/apache/commons/net/nntp/Threader.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.util.HashMap;
3131
import java.util.Iterator;
32+
import java.util.List;
3233

3334
public class Threader {
3435
private ThreadContainer root;
@@ -41,16 +42,16 @@ public class Threader {
4142
* @param messages
4243
* @return null if messages == null or root.child == null
4344
*/
44-
public Threadable thread(Threadable[] messages) {
45+
public Threadable thread(List<? extends Threadable> messages) {
4546
if (messages == null)
4647
return null;
4748

4849
idTable = new HashMap<String,ThreadContainer>();
4950

50-
// walk through each Threadable element
51-
for (int i = 0; i < messages.length; ++i) {
52-
if (!messages[i].isDummy())
53-
buildContainer(messages[i]);
51+
// walk through each Threadable element
52+
for (Threadable t : messages) {
53+
if (!t.isDummy())
54+
buildContainer(t);
5455
}
5556

5657
root = findRootSet();

0 commit comments

Comments
 (0)