001 /* 002 * Copyright 2001-2005 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package examples.nntp; 017 018 import java.io.IOException; 019 import java.io.PrintWriter; 020 021 import org.apache.commons.net.nntp.Article; 022 import org.apache.commons.net.nntp.NNTPClient; 023 import org.apache.commons.net.nntp.NewsgroupInfo; 024 025 import examples.PrintCommandListener; 026 027 /** 028 * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE) 029 * 030 * @author Rory Winston <rwinston@checkfree.com> 031 */ 032 public class ExtendedNNTPOps { 033 034 035 NNTPClient client; 036 037 public ExtendedNNTPOps() { 038 client = new NNTPClient(); 039 client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); 040 } 041 042 043 public void demo(String host, String user, String password) { 044 try { 045 client.connect(host); 046 047 // AUTHINFO USER/AUTHINFO PASS 048 boolean success = client.authenticate(user, password); 049 if (success) { 050 System.out.println("Authentication succeeded"); 051 } else { 052 System.out.println("Authentication failed, error =" + client.getReplyString()); 053 } 054 055 // XOVER 056 NewsgroupInfo testGroup = new NewsgroupInfo(); 057 client.selectNewsgroup("alt.test", testGroup); 058 int lowArticleNumber = testGroup.getFirstArticle(); 059 int highArticleNumber = lowArticleNumber + 100; 060 Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber); 061 062 for (int i = 0; i < articles.length; ++i) { 063 System.out.println(articles[i].getSubject()); 064 } 065 066 // LIST ACTIVE 067 NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*"); 068 for (int i = 0; i < fanGroups.length; ++i) { 069 System.out.println(fanGroups[i].getNewsgroup()); 070 } 071 072 } catch (IOException e) { 073 e.printStackTrace(); 074 } 075 } 076 077 public static void main(String[] args) { 078 ExtendedNNTPOps ops; 079 080 if (args.length != 3) { 081 System.err.println("usage: ExtendedNNTPOps nntpserver username password"); 082 System.exit(1); 083 } 084 085 ops = new ExtendedNNTPOps(); 086 ops.demo(args[0], args[1], args[2]); 087 } 088 089 } 090 091 /* Emacs configuration 092 * Local variables: ** 093 * mode: java ** 094 * c-basic-offset: 4 ** 095 * indent-tabs-mode: nil ** 096 * End: ** 097 */