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    *      https://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  
22  import org.apache.commons.net.examples.PrintCommandListeners;
23  import org.apache.commons.net.nntp.Article;
24  import org.apache.commons.net.nntp.NNTPClient;
25  import org.apache.commons.net.nntp.NewsgroupInfo;
26  
27  /**
28   * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
29   */
30  public class ExtendedNNTPOps {
31  
32      public static void main(final String[] args) {
33          final ExtendedNNTPOps ops;
34  
35          final int argc = args.length;
36          if (argc < 1) {
37              System.err.println("usage: ExtendedNNTPOps nntpserver [user password]");
38              System.exit(1);
39          }
40  
41          ops = new ExtendedNNTPOps();
42          ops.demo(args[0], argc >= 3 ? args[1] : null, argc >= 3 ? args[2] : null);
43      }
44  
45      private final NNTPClient client;
46  
47      public ExtendedNNTPOps() {
48          client = new NNTPClient();
49          client.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener());
50      }
51  
52      private void demo(final String host, final String user, final String password) {
53          try {
54              client.connect(host);
55  
56              // AUTHINFO USER/AUTHINFO PASS
57              if (user != null && password != null) {
58                  final boolean success = client.authenticate(user, password);
59                  if (success) {
60                      System.out.println("Authentication succeeded");
61                  } else {
62                      System.out.println("Authentication failed, error =" + client.getReplyString());
63                  }
64              }
65  
66              // XOVER
67              final NewsgroupInfo testGroup = new NewsgroupInfo();
68              client.selectNewsgroup("alt.test", testGroup);
69              final long lowArticleNumber = testGroup.getFirstArticleLong();
70              final long highArticleNumber = lowArticleNumber + 100;
71              final Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);
72  
73              for (final Article article : articles) {
74                  if (article.isDummy()) { // Subject will contain raw response
75                      System.out.println("Could not parse: " + article.getSubject());
76                  } else {
77                      System.out.println(article.getSubject());
78                  }
79              }
80  
81              // LIST ACTIVE
82              final NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
83              for (final NewsgroupInfo fanGroup : fanGroups) {
84                  System.out.println(fanGroup.getNewsgroup());
85              }
86  
87          } catch (final IOException e) {
88              e.printStackTrace();
89          }
90      }
91  
92  }