    View Javadoc
    1   package examples;
    2
    3   /* ====================================================================
    4    * The Apache Software License, Version 1.1
    5    *
    6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
    7    * reserved.
    8    *
    9    * Redistribution and use in source and binary forms, with or without
    10   * modification, are permitted provided that the following conditions
    11   * are met:
    12   *
    13   * 1. Redistributions of source code must retain the above copyright
    14   *    notice, this list of conditions and the following disclaimer.
    15   *
    16   * 2. Redistributions in binary form must reproduce the above copyright
    17   *    notice, this list of conditions and the following disclaimer in
    18   *    the documentation and/or other materials provided with the
    19   *    distribution.
    20   *
    21   * 3. The end-user documentation included with the redistribution,
    22   *    if any, must include the following acknowledgment:
    23   *       "This product includes software developed by the
    24   *        Apache Software Foundation (http://www.apache.org/)."
    25   *    Alternately, this acknowledgment may appear in the software itself,
    26   *    if and wherever such third-party acknowledgments normally appear.
    27   *
    28   * 4. The names "Apache" and "Apache Software Foundation" and
    29   *    "Apache Commons" must not be used to endorse or promote products
    30   *    derived from this software without prior written permission. For
    31   *    written permission, please contact apache@apache.org.
    32   *
    33   * 5. Products derived from this software may not be called "Apache",
    34   *    nor may "Apache" appear in their name, without
    35   *    prior written permission of the Apache Software Foundation.
    36   *
    37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
    41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    48   * SUCH DAMAGE.
    49   * ====================================================================
    50   *
    51   * This software consists of voluntary contributions made by many
    52   * individuals on behalf of the Apache Software Foundation.  For more
    53   * information on the Apache Software Foundation, please see
    54   * <http://www.apache.org/>;.
    55   */
    56
    57  import java.io.BufferedReader;
    58  import java.io.FileNotFoundException;
    59  import java.io.FileReader;
    60  import java.io.IOException;
    61  import java.io.InputStreamReader;
    62  import java.io.PrintWriter;
    63  import java.io.Writer;
    64  import org.apache.commons.net.io.Util;
    65  import org.apache.commons.net.nntp.NNTPClient;
    66  import org.apache.commons.net.nntp.NNTPReply;
    67  import org.apache.commons.net.nntp.SimpleNNTPHeader;
    68
    69  /****
    70   * This is an example program using the NNTP package to post an article
    71   * to the specified newsgroup(s).  It prompts you for header information and
    72   * a filename to post.
    73   * <p>
    74   ***/
    75
    76  public final class post
    77  {
    78
    79      public final static void main(String[] args)
    80      {
    81          String from, subject, newsgroup, filename, server, organization;
    82          String references;
    83          BufferedReader stdin;
    84          FileReader fileReader = null;
    85          SimpleNNTPHeader header;
    86          NNTPClient client;
    87
    88          if (args.length < 1)
    89          {
    90              System.err.println("Usage: post newsserver");
    91              System.exit(1);
    92          }
    93
    94          server = args[0];
    95
    96          stdin = new BufferedReader(new InputStreamReader(System.in));
    97
    98          try
    99          {
    100             System.out.print("From: ");
    101             System.out.flush();
    102
    103             from = stdin.readLine();
    104
    105             System.out.print("Subject: ");
    106             System.out.flush();
    107
    108             subject = stdin.readLine();
    109
    110             header = new SimpleNNTPHeader(from, subject);
    111
    112             System.out.print("Newsgroup: ");
    113             System.out.flush();
    114
    115             newsgroup = stdin.readLine();
    116             header.addNewsgroup(newsgroup);
    117
    118             while (true)
    119             {
    120                 System.out.print("Additional Newsgroup <Hit enter to end>: ");
    121                 System.out.flush();
    122
    123                 // Of course you don't want to do this because readLine() may be null
    124                 newsgroup = stdin.readLine().trim();
    125
    126                 if (newsgroup.length() == 0)
    127                     break;
    128
    129                 header.addNewsgroup(newsgroup);
    130             }
    131
    132             System.out.print("Organization: ");
    133             System.out.flush();
    134
    135             organization = stdin.readLine();
    136
    137             System.out.print("References: ");
    138             System.out.flush();
    139
    140             references = stdin.readLine();
    141
    142             if (organization != null && organization.length() > 0)
    143                 header.addHeaderField("Organization", organization);
    144
    145             if (references != null && organization.length() > 0)
    146                 header.addHeaderField("References", references);
    147
    148             header.addHeaderField("X-Newsreader", "NetComponents");
    149
    150             System.out.print("Filename: ");
    151             System.out.flush();
    152
    153             filename = stdin.readLine();
    154
    155             try
    156             {
    157                 fileReader = new FileReader(filename);
    158             }
    159             catch (FileNotFoundException e)
    160             {
    161                 System.err.println("File not found. " + e.getMessage());
    162                 System.exit(1);
    163             }
    164
    165             client = new NNTPClient();
    166             client.addProtocolCommandListener(new PrintCommandListener(
    167                                                   new PrintWriter(System.out)));
    168
    169             client.connect(server);
    170
    171             if (!NNTPReply.isPositiveCompletion(client.getReplyCode()))
    172             {
    173                 client.disconnect();
    174                 System.err.println("NNTP server refused connection.");
    175                 System.exit(1);
    176             }
    177
    178             if (client.isAllowedToPost())
    179             {
    180                 Writer writer = client.postArticle();
    181
    182                 if (writer != null)
    183                 {
    184                     writer.write(header.toString());
    185                     Util.copyReader(fileReader, writer);
    186                     writer.close();
    187                     client.completePendingCommand();
    188                 }
    189             }
    190
    191             fileReader.close();
    192
    193             client.logout();
    194
    195             client.disconnect();
    196         }
    197         catch (IOException e)
    198         {
    199             e.printStackTrace();
    200             System.exit(1);
    201         }
    202     }
    203 }
    204
    205

This page was automatically generated by Maven