Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 10048 invoked from network); 23 Sep 2004 12:14:57 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 23 Sep 2004 12:14:56 -0000 Received: (qmail 43974 invoked by uid 500); 23 Sep 2004 12:14:51 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 43887 invoked by uid 500); 23 Sep 2004 12:14:50 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 43871 invoked by uid 500); 23 Sep 2004 12:14:49 -0000 Received: (qmail 43856 invoked by uid 99); 23 Sep 2004 12:14:49 -0000 X-ASF-Spam-Status: No, hits=-10.0 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Thu, 23 Sep 2004 05:14:48 -0700 Received: (qmail 9927 invoked by uid 1867); 23 Sep 2004 12:14:47 -0000 Date: 23 Sep 2004 12:14:47 -0000 Message-ID: <20040923121447.9926.qmail@minotaur.apache.org> From: rwinston@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/net/src/java/examples/nntp ExtendedNNTPOps.java MessageThreading.java NNTPUtils.java newsgroups.java post.java X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N rwinston 2004/09/23 05:14:47 Added: net/src/java/examples/nntp ExtendedNNTPOps.java MessageThreading.java NNTPUtils.java newsgroups.java post.java Log: Add a message threading example, and refactor the NNTP examples into their own package. Also added an NNTPUtils class. Revision Changes Path 1.1 jakarta-commons/net/src/java/examples/nntp/ExtendedNNTPOps.java Index: ExtendedNNTPOps.java =================================================================== /* * Copyright 2001-2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package examples.nntp; import java.io.IOException; import java.io.PrintWriter; import org.apache.commons.net.nntp.Article; import org.apache.commons.net.nntp.NNTPClient; import org.apache.commons.net.nntp.NewsgroupInfo; import examples.PrintCommandListener; /** * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE) * * @author Rory Winston */ public class ExtendedNNTPOps { NNTPClient client; public ExtendedNNTPOps() { client = new NNTPClient(); client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); } public void demo(String host, String user, String password) { try { client.connect(host); // AUTHINFO USER/AUTHINFO PASS boolean success = client.authenticate(user, password); if (success) { System.out.println("Authentication succeeded"); } else { System.out.println("Authentication failed, error =" + client.getReplyString()); } // XOVER NewsgroupInfo testGroup = new NewsgroupInfo(); client.selectNewsgroup("alt.test", testGroup); int lowArticleNumber = testGroup.getFirstArticle(); int highArticleNumber = lowArticleNumber + 100; Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber); for (int i = 0; i < articles.length; ++i) { System.out.println(articles[i].getSubject()); } // LIST ACTIVE NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*"); for (int i = 0; i < fanGroups.length; ++i) { System.out.println(fanGroups[i].getNewsgroup()); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { ExtendedNNTPOps ops; if (args.length != 3) { System.err.println("usage: ExtendedNNTPOps nntpserver username password"); System.exit(1); } ops = new ExtendedNNTPOps(); ops.demo(args[0], args[1], args[2]); } } /* Emacs configuration * Local variables: ** * mode: java ** * c-basic-offset: 4 ** * indent-tabs-mode: nil ** * End: ** */ 1.1 jakarta-commons/net/src/java/examples/nntp/MessageThreading.java Index: MessageThreading.java =================================================================== package examples.nntp; import java.io.IOException; import java.io.PrintWriter; import java.net.SocketException; import org.apache.commons.net.nntp.Article; import org.apache.commons.net.nntp.NNTPClient; import org.apache.commons.net.nntp.NewsgroupInfo; import org.apache.commons.net.nntp.Threader; import examples.PrintCommandListener; public class MessageThreading { public MessageThreading() { } public static void main(String[] args) throws SocketException, IOException { if (args.length != 3) usage(); String hostname = args[0]; String user = args[1]; String password = args[2]; 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); } NewsgroupInfo group = new NewsgroupInfo(); client.selectNewsgroup("comp.lang.lisp", group); int lowArticleNumber = group.getFirstArticle(); int highArticleNumber = lowArticleNumber + 100; System.out.println("Retrieving articles between [" + lowArticleNumber + "] and [" + highArticleNumber + "]"); 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); } public static void usage() { System.out.println("Usage: MessageThreading "); System.exit(0); } } 1.1 jakarta-commons/net/src/java/examples/nntp/NNTPUtils.java Index: NNTPUtils.java =================================================================== package examples.nntp; import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.util.StringTokenizer; import org.apache.commons.net.io.DotTerminatedMessageReader; import org.apache.commons.net.nntp.Article; import org.apache.commons.net.nntp.NNTPClient; /** * * Some convenience methods for NNTP example classes. * * @author Rory Winston */ 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(); } return sb.toString(); } } 1.1 jakarta-commons/net/src/java/examples/nntp/newsgroups.java Index: newsgroups.java =================================================================== /* * Copyright 2001-2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package examples.nntp; import java.io.IOException; import org.apache.commons.net.nntp.NNTPClient; import org.apache.commons.net.nntp.NewsgroupInfo; /*** * This is a trivial example using the NNTP package to approximate the * Unix newsgroups command. It merely connects to the specified news * server and issues fetches the list of newsgroups stored by the server. * On servers that store a lot of newsgroups, this command can take a very * long time (listing upwards of 30,000 groups). *

***/ public final class newsgroups { public final static void main(String[] args) { NNTPClient client; NewsgroupInfo[] list; if (args.length < 1) { System.err.println("Usage: newsgroups newsserver"); System.exit(1); } client = new NNTPClient(); try { client.connect(args[0]); list = client.listNewsgroups(); if (list != null) { for (int i = 0; i < list.length; i++) System.out.println(list[i].getNewsgroup()); } else { System.err.println("LIST command failed."); System.err.println("Server reply: " + client.getReplyString()); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (client.isConnected()) client.disconnect(); } catch (IOException e) { System.err.println("Error disconnecting from server."); e.printStackTrace(); System.exit(1); } } } } 1.1 jakarta-commons/net/src/java/examples/nntp/post.java Index: post.java =================================================================== /* * Copyright 2001-2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package examples.nntp; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.Writer; import org.apache.commons.net.io.Util; import org.apache.commons.net.nntp.NNTPClient; import org.apache.commons.net.nntp.NNTPReply; import org.apache.commons.net.nntp.SimpleNNTPHeader; import examples.PrintCommandListener; /*** * This is an example program using the NNTP package to post an article * to the specified newsgroup(s). It prompts you for header information and * a filename to post. *

***/ public final class post { public final static void main(String[] args) { String from, subject, newsgroup, filename, server, organization; String references; BufferedReader stdin; FileReader fileReader = null; SimpleNNTPHeader header; NNTPClient client; if (args.length < 1) { System.err.println("Usage: post newsserver"); System.exit(1); } server = args[0]; stdin = new BufferedReader(new InputStreamReader(System.in)); try { System.out.print("From: "); System.out.flush(); from = stdin.readLine(); System.out.print("Subject: "); System.out.flush(); subject = stdin.readLine(); header = new SimpleNNTPHeader(from, subject); System.out.print("Newsgroup: "); System.out.flush(); newsgroup = stdin.readLine(); header.addNewsgroup(newsgroup); while (true) { System.out.print("Additional Newsgroup : "); System.out.flush(); // Of course you don't want to do this because readLine() may be null newsgroup = stdin.readLine().trim(); if (newsgroup.length() == 0) break; header.addNewsgroup(newsgroup); } System.out.print("Organization: "); System.out.flush(); organization = stdin.readLine(); System.out.print("References: "); System.out.flush(); references = stdin.readLine(); if (organization != null && organization.length() > 0) header.addHeaderField("Organization", organization); if (references != null && organization.length() > 0) header.addHeaderField("References", references); header.addHeaderField("X-Newsreader", "NetComponents"); System.out.print("Filename: "); System.out.flush(); filename = stdin.readLine(); try { fileReader = new FileReader(filename); } catch (FileNotFoundException e) { System.err.println("File not found. " + e.getMessage()); System.exit(1); } client = new NNTPClient(); client.addProtocolCommandListener(new PrintCommandListener( new PrintWriter(System.out))); client.connect(server); if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) { client.disconnect(); System.err.println("NNTP server refused connection."); System.exit(1); } if (client.isAllowedToPost()) { Writer writer = client.postArticle(); if (writer != null) { writer.write(header.toString()); Util.copyReader(fileReader, writer); writer.close(); client.completePendingCommand(); } } fileReader.close(); client.logout(); client.disconnect(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org