PostMessage.java

  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. package org.apache.commons.net.examples.nntp;

  18. import java.io.BufferedReader;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.io.Reader;
  23. import java.io.Writer;
  24. import java.nio.charset.Charset;
  25. import java.nio.file.Files;
  26. import java.nio.file.Paths;

  27. import org.apache.commons.net.PrintCommandListener;
  28. import org.apache.commons.net.io.Util;
  29. import org.apache.commons.net.nntp.NNTPClient;
  30. import org.apache.commons.net.nntp.NNTPReply;
  31. import org.apache.commons.net.nntp.SimpleNNTPHeader;

  32. /**
  33.  * 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
  34.  * post.
  35.  */

  36. public final class PostMessage {

  37.     public static void main(final String[] args) {
  38.         final String from;
  39.         final String subject;
  40.         String newsgroup;
  41.         final String fileName;
  42.         final String server;
  43.         final String organization;
  44.         final String references;
  45.         final BufferedReader stdin;
  46.         Reader fileReader = null;
  47.         final SimpleNNTPHeader header;
  48.         final NNTPClient client;

  49.         if (args.length < 1) {
  50.             System.err.println("Usage: post newsserver");
  51.             System.exit(1);
  52.         }

  53.         server = args[0];

  54.         stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));

  55.         try {
  56.             System.out.print("From: ");
  57.             System.out.flush();

  58.             from = stdin.readLine();

  59.             System.out.print("Subject: ");
  60.             System.out.flush();

  61.             subject = stdin.readLine();

  62.             header = new SimpleNNTPHeader(from, subject);

  63.             System.out.print("Newsgroup: ");
  64.             System.out.flush();

  65.             newsgroup = stdin.readLine();
  66.             header.addNewsgroup(newsgroup);

  67.             while (true) {
  68.                 System.out.print("Additional Newsgroup <Hit enter to end>: ");
  69.                 System.out.flush();

  70.                 newsgroup = stdin.readLine();
  71.                 if (newsgroup == null) {
  72.                     break;
  73.                 }

  74.                 newsgroup = newsgroup.trim();

  75.                 if (newsgroup.isEmpty()) {
  76.                     break;
  77.                 }

  78.                 header.addNewsgroup(newsgroup);
  79.             }

  80.             System.out.print("Organization: ");
  81.             System.out.flush();

  82.             organization = stdin.readLine();

  83.             System.out.print("References: ");
  84.             System.out.flush();

  85.             references = stdin.readLine();

  86.             if (organization != null && !organization.isEmpty()) {
  87.                 header.addHeaderField("Organization", organization);
  88.             }

  89.             if (references != null && !references.isEmpty()) {
  90.                 header.addHeaderField("References", references);
  91.             }

  92.             header.addHeaderField("X-Newsreader", "NetComponents");

  93.             System.out.print("Filename: ");
  94.             System.out.flush();

  95.             fileName = stdin.readLine();

  96.             try {
  97.                 fileReader = Files.newBufferedReader(Paths.get(fileName), Charset.defaultCharset());
  98.             } catch (final FileNotFoundException e) {
  99.                 System.err.println("File not found. " + e.getMessage());
  100.                 System.exit(1);
  101.             }

  102.             client = new NNTPClient();
  103.             client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));

  104.             client.connect(server);

  105.             if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
  106.                 client.disconnect();
  107.                 System.err.println("NNTP server refused connection.");
  108.                 System.exit(1);
  109.             }

  110.             if (client.isAllowedToPost()) {
  111.                 final Writer writer = client.postArticle();

  112.                 if (writer != null) {
  113.                     writer.write(header.toString());
  114.                     Util.copyReader(fileReader, writer);
  115.                     writer.close();
  116.                     client.completePendingCommand();
  117.                 }
  118.             }

  119.             fileReader.close();

  120.             client.logout();

  121.             client.disconnect();
  122.         } catch (final IOException e) {
  123.             e.printStackTrace();
  124.             System.exit(1);
  125.         }
  126.     }
  127. }