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