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