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   * <p>
40   ***/
41  
42  public final class post
43  {
44  
45      public final static void main(String[] args)
46      {
47          String from, subject, newsgroup, filename, server, organization;
48          String references;
49          BufferedReader stdin;
50          FileReader fileReader = null;
51          SimpleNNTPHeader header;
52          NNTPClient client;
53  
54          if (args.length < 1)
55          {
56              System.err.println("Usage: post newsserver");
57              System.exit(1);
58          }
59  
60          server = args[0];
61  
62          stdin = new BufferedReader(new InputStreamReader(System.in));
63  
64          try
65          {
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              {
86                  System.out.print("Additional Newsgroup <Hit enter to end>: ");
87                  System.out.flush();
88  
89                  // Of course you don't want to do this because readLine() may be null
90                  newsgroup = stdin.readLine().trim();
91  
92                  if (newsgroup.length() == 0)
93                      break;
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             if (references != null && organization.length() > 0)
112                 header.addHeaderField("References", references);
113 
114             header.addHeaderField("X-Newsreader", "NetComponents");
115 
116             System.out.print("Filename: ");
117             System.out.flush();
118 
119             filename = stdin.readLine();
120 
121             try
122             {
123                 fileReader = new FileReader(filename);
124             }
125             catch (FileNotFoundException e)
126             {
127                 System.err.println("File not found. " + e.getMessage());
128                 System.exit(1);
129             }
130 
131             client = new NNTPClient();
132             client.addProtocolCommandListener(new PrintCommandListener(
133                                                   new PrintWriter(System.out)));
134 
135             client.connect(server);
136 
137             if (!NNTPReply.isPositiveCompletion(client.getReplyCode()))
138             {
139                 client.disconnect();
140                 System.err.println("NNTP server refused connection.");
141                 System.exit(1);
142             }
143 
144             if (client.isAllowedToPost())
145             {
146                 Writer writer = client.postArticle();
147 
148                 if (writer != null)
149                 {
150                     writer.write(header.toString());
151                     Util.copyReader(fileReader, writer);
152                     writer.close();
153                     client.completePendingCommand();
154                 }
155             }
156 
157             fileReader.close();
158 
159             client.logout();
160 
161             client.disconnect();
162         }
163         catch (IOException e)
164         {
165             e.printStackTrace();
166             System.exit(1);
167         }
168     }
169 }
170 
171