View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.examples.nntp;
19  
20  import java.io.IOException;
21  import java.io.PrintWriter;
22  
23  import org.apache.commons.net.PrintCommandListener;
24  import org.apache.commons.net.nntp.Article;
25  import org.apache.commons.net.nntp.NNTPClient;
26  import org.apache.commons.net.nntp.NewsgroupInfo;
27  
28  /**
29   * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
30   */
31  public class ExtendedNNTPOps {
32  
33      public static void main(final String[] args) {
34          final ExtendedNNTPOps ops;
35  
36          final int argc = args.length;
37          if (argc < 1) {
38              System.err.println("usage: ExtendedNNTPOps nntpserver [user password]");
39              System.exit(1);
40          }
41  
42          ops = new ExtendedNNTPOps();
43          ops.demo(args[0], argc >= 3 ? args[1] : null, argc >= 3 ? args[2] : null);
44      }
45  
46      private final NNTPClient client;
47  
48      public ExtendedNNTPOps() {
49          client = new NNTPClient();
50          client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
51      }
52  
53      private void demo(final String host, final String user, final String password) {
54          try {
55              client.connect(host);
56  
57              // AUTHINFO USER/AUTHINFO PASS
58              if (user != null && password != null) {
59                  final boolean success = client.authenticate(user, password);
60                  if (success) {
61                      System.out.println("Authentication succeeded");
62                  } else {
63                      System.out.println("Authentication failed, error =" + client.getReplyString());
64                  }
65              }
66  
67              // XOVER
68              final NewsgroupInfo testGroup = new NewsgroupInfo();
69              client.selectNewsgroup("alt.test", testGroup);
70              final long lowArticleNumber = testGroup.getFirstArticleLong();
71              final long highArticleNumber = lowArticleNumber + 100;
72              final Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);
73  
74              for (final Article article : articles) {
75                  if (article.isDummy()) { // Subject will contain raw response
76                      System.out.println("Could not parse: " + article.getSubject());
77                  } else {
78                      System.out.println(article.getSubject());
79                  }
80              }
81  
82              // LIST ACTIVE
83              final NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
84              for (final NewsgroupInfo fanGroup : fanGroups) {
85                  System.out.println(fanGroup.getNewsgroup());
86              }
87  
88          } catch (final IOException e) {
89              e.printStackTrace();
90          }
91      }
92  
93  }