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 examples.nntp;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.net.SocketException;
24  
25  import org.apache.commons.net.PrintCommandListener;
26  import org.apache.commons.net.nntp.NNTPClient;
27  import org.apache.commons.net.nntp.NewsgroupInfo;
28  
29  /**
30   * Sample program demonstrating the use of article header and body retrieval
31   */
32  public class ArticleReader {
33  
34      public static void main(String[] args) throws SocketException, IOException {
35  
36          if (args.length != 2 && args.length != 3 && args.length != 5) {
37              System.out.println("Usage: MessageThreading <hostname> <groupname> [<article specifier> [<user> <password>]]");
38              return;
39          }
40  
41          String hostname = args[0];
42          String newsgroup = args[1];
43          // Article specifier can be numeric or Id in form <m.n.o.x@host>
44          String articleSpec = args.length >= 3 ? args[2] : null;
45  
46          NNTPClient client = new NNTPClient();
47          client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
48          client.connect(hostname);
49  
50          if (args.length == 5) { // Optional auth
51              String user = args[3];
52              String password = args[4];
53              if(!client.authenticate(user, password)) {
54                  System.out.println("Authentication failed for user " + user + "!");
55                  System.exit(1);
56              }
57          }
58  
59          NewsgroupInfo group = new NewsgroupInfo();
60          client.selectNewsgroup(newsgroup, group);
61  
62          BufferedReader br;
63          String line;
64          if (articleSpec != null) {
65              br = (BufferedReader) client.retrieveArticleHeader(articleSpec);
66          } else {
67              long articleNum = group.getLastArticleLong();
68              br = client.retrieveArticleHeader(articleNum);
69          }
70          if (br != null) {
71              while((line=br.readLine()) != null) {
72                  System.out.println(line);
73              }
74              br.close();
75          }
76          if (articleSpec != null) {
77              br = (BufferedReader) client.retrieveArticleBody(articleSpec);
78          } else {
79              long articleNum = group.getLastArticleLong();
80              br = client.retrieveArticleBody(articleNum);
81          }
82          if (br != null) {
83              while((line=br.readLine()) != null) {
84                  System.out.println(line);
85              }
86              br.close();
87          }
88      }
89  
90  }