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.BufferedReader;
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.io.InputStreamReader;
24  import java.io.Reader;
25  import java.io.Writer;
26  import java.nio.charset.Charset;
27  import java.nio.file.Files;
28  import java.nio.file.Paths;
29  
30  import org.apache.commons.net.PrintCommandListener;
31  import org.apache.commons.net.io.Util;
32  import org.apache.commons.net.nntp.NNTPClient;
33  import org.apache.commons.net.nntp.NNTPReply;
34  import org.apache.commons.net.nntp.SimpleNNTPHeader;
35  
36  /**
37   * This is an example program using the NNTP package to post an article to the specified newsgroup(s). It prompts you for header information and a file name to
38   * post.
39   */
40  
41  public final class PostMessage {
42  
43      public static void main(final String[] args) {
44          final String from;
45          final String subject;
46          String newsgroup;
47          final String fileName;
48          final String server;
49          final String organization;
50          final String references;
51          final BufferedReader stdin;
52          Reader fileReader = null;
53          final SimpleNNTPHeader header;
54          final NNTPClient client;
55  
56          if (args.length < 1) {
57              System.err.println("Usage: post newsserver");
58              System.exit(1);
59          }
60  
61          server = args[0];
62  
63          stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
64  
65          try {
66              System.out.print("From: ");
67              System.out.flush();
68  
69              from = stdin.readLine();
70  
71              System.out.print("Subject: ");
72              System.out.flush();
73  
74              subject = stdin.readLine();
75  
76              header = new SimpleNNTPHeader(from, subject);
77  
78              System.out.print("Newsgroup: ");
79              System.out.flush();
80  
81              newsgroup = stdin.readLine();
82              header.addNewsgroup(newsgroup);
83  
84              while (true) {
85                  System.out.print("Additional Newsgroup <Hit enter to end>: ");
86                  System.out.flush();
87  
88                  newsgroup = stdin.readLine();
89                  if (newsgroup == null) {
90                      break;
91                  }
92  
93                  newsgroup = newsgroup.trim();
94  
95                  if (newsgroup.isEmpty()) {
96                      break;
97                  }
98  
99                  header.addNewsgroup(newsgroup);
100             }
101 
102             System.out.print("Organization: ");
103             System.out.flush();
104 
105             organization = stdin.readLine();
106 
107             System.out.print("References: ");
108             System.out.flush();
109 
110             references = stdin.readLine();
111 
112             if (organization != null && !organization.isEmpty()) {
113                 header.addHeaderField("Organization", organization);
114             }
115 
116             if (references != null && !references.isEmpty()) {
117                 header.addHeaderField("References", references);
118             }
119 
120             header.addHeaderField("X-Newsreader", "NetComponents");
121 
122             System.out.print("Filename: ");
123             System.out.flush();
124 
125             fileName = stdin.readLine();
126 
127             try {
128                 fileReader = Files.newBufferedReader(Paths.get(fileName), Charset.defaultCharset());
129             } catch (final FileNotFoundException e) {
130                 System.err.println("File not found. " + e.getMessage());
131                 System.exit(1);
132             }
133 
134             client = new NNTPClient();
135             client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));
136 
137             client.connect(server);
138 
139             if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
140                 client.disconnect();
141                 System.err.println("NNTP server refused connection.");
142                 System.exit(1);
143             }
144 
145             if (client.isAllowedToPost()) {
146                 final Writer writer = client.postArticle();
147 
148                 if (writer != null) {
149                     writer.write(header.toString());
150                     Util.copyReader(fileReader, writer);
151                     writer.close();
152                     client.completePendingCommand();
153                 }
154             }
155 
156             fileReader.close();
157 
158             client.logout();
159 
160             client.disconnect();
161         } catch (final IOException e) {
162             e.printStackTrace();
163             System.exit(1);
164         }
165     }
166 }