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