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 018 package examples.nntp; 019 020 import java.io.IOException; 021 import java.io.PrintWriter; 022 023 import org.apache.commons.net.PrintCommandListener; 024 import org.apache.commons.net.nntp.Article; 025 import org.apache.commons.net.nntp.NNTPClient; 026 import org.apache.commons.net.nntp.NewsgroupInfo; 027 028 029 /** 030 * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE) 031 * 032 * @author Rory Winston <rwinston@checkfree.com> 033 */ 034 public class ExtendedNNTPOps { 035 036 037 NNTPClient client; 038 039 public ExtendedNNTPOps() { 040 client = new NNTPClient(); 041 client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); 042 } 043 044 045 public void demo(String host, String user, String password) { 046 try { 047 client.connect(host); 048 049 // AUTHINFO USER/AUTHINFO PASS 050 boolean success = client.authenticate(user, password); 051 if (success) { 052 System.out.println("Authentication succeeded"); 053 } else { 054 System.out.println("Authentication failed, error =" + client.getReplyString()); 055 } 056 057 // XOVER 058 NewsgroupInfo testGroup = new NewsgroupInfo(); 059 client.selectNewsgroup("alt.test", testGroup); 060 int lowArticleNumber = testGroup.getFirstArticle(); 061 int highArticleNumber = lowArticleNumber + 100; 062 Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber); 063 064 for (int i = 0; i < articles.length; ++i) { 065 System.out.println(articles[i].getSubject()); 066 } 067 068 // LIST ACTIVE 069 NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*"); 070 for (int i = 0; i < fanGroups.length; ++i) { 071 System.out.println(fanGroups[i].getNewsgroup()); 072 } 073 074 } catch (IOException e) { 075 e.printStackTrace(); 076 } 077 } 078 079 public static void main(String[] args) { 080 ExtendedNNTPOps ops; 081 082 if (args.length != 3) { 083 System.err.println("usage: ExtendedNNTPOps nntpserver username password"); 084 System.exit(1); 085 } 086 087 ops = new ExtendedNNTPOps(); 088 ops.demo(args[0], args[1], args[2]); 089 } 090 091 } 092 093 /* Emacs configuration 094 * Local variables: ** 095 * mode: java ** 096 * c-basic-offset: 4 ** 097 * indent-tabs-mode: nil ** 098 * End: ** 099 */