home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / net / nntp / NntpClient.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  6.2 KB  |  208 lines

  1. /*
  2.  * @(#)NntpClient.java    1.16 95/08/29 Jonathan Payne, James Gosling
  3.  * 
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for NON-COMMERCIAL purposes and without fee is hereby
  8.  * granted provided that this copyright notice appears in all copies. Please
  9.  * refer to the file "copyright.html" for further important copyright and
  10.  * licensing information.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  15.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  16.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  17.  * ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.net.nntp;
  21.  
  22. import java.io.*;
  23. import java.util.*;
  24. import java.net.*;
  25. import sun.net.TransferProtocolClient;
  26. import sun.net.TelnetInputStream;
  27.  
  28. /**
  29.  * This class implements network news clients (NNTP).
  30.  *
  31.  * @version    1.9, 12 Dec 1994
  32.  * @author    Jonathan Payne, James Gosling
  33.  */
  34. public class NntpClient extends TransferProtocolClient {
  35.     public static final int NNTP_PORT = 119;
  36.  
  37.     String serverName;        /* for re-opening server connections */
  38.     int serverPort;
  39.  
  40.     public NntpClient () {
  41.     }
  42.  
  43.     /** Create new NNTP Client connected to host <i>host</i> */
  44.     public NntpClient (String host) throws IOException {
  45.     super();
  46.     openServer(host, NNTP_PORT);
  47.     }
  48.  
  49.     /**
  50.      * Open a connection to the NNTP server.
  51.      * @exception NntpProtocolException did not get the correct welcome message
  52.      */
  53.     public void openServer(String name, int port) throws IOException {
  54.     serverName = name;
  55.     serverPort = port;
  56.     super.openServer(name, port);
  57.     if (readServerResponse() >= 300)
  58.         throw new NntpProtocolException("Welcome message");
  59.     }
  60.  
  61.     /** Sends command <i>cmd</i> to the server. */
  62.     public int askServer(String cmd) throws IOException {
  63.     int code = 503;
  64.     for (int tries = 3; --tries >= 0;) {
  65.         try {
  66.         serverOutput.print(cmd);
  67.         code = readServerResponse();
  68.         if (code < 500)
  69.             return code;
  70.  
  71.         /*
  72.          * errors codes >500 usually result from something happening
  73.          * on the net.  Its usually profitable to disconnect and
  74.          * reconnect
  75.          */
  76.         } catch(Exception e) {
  77.         }
  78.         /* reconnect to the server */
  79.         try {
  80.         serverOutput.close();
  81.         } catch(Exception e2) {
  82.         }
  83.         openServer(serverName, serverPort);
  84.     }
  85.     return code;
  86.     }
  87.  
  88.     InputStream makeStreamRequest(String cmd, int reply) throws IOException {
  89.     int response;
  90.  
  91.     response = askServer(cmd + "\r\n");
  92.     if (response != reply) {
  93.         String msg = null;
  94.         try {
  95.         for (int i = 0; i < 99; i++) {
  96.             String n = (String) serverResponse.elementAt(i);
  97.             if (msg == null)
  98.             msg = n;
  99.             else
  100.             msg = msg + "\n" + n;
  101.         }
  102.         } catch(Exception e) {
  103.         };
  104.         if (msg == null)
  105.         msg = "Command " + cmd + " yielded " + response + "; expecting " + reply;
  106.         throw new NntpProtocolException(msg);
  107.     }
  108.     switch (response / 100) {
  109.       case 1:
  110.       case 2:
  111.         break;
  112.  
  113.       case 3:
  114.         throw new NntpProtocolException("More input to command expected");
  115.  
  116.       case 4:
  117.         throw new NntpProtocolException("Server error - cmd OK");
  118.  
  119.       case 5:
  120.         throw new NntpProtocolException("Error in command: " + cmd);
  121.     }
  122.     return new NntpInputStream(new TelnetInputStream(serverInput, false));
  123.     }
  124.     String tokenize(String input)[] {
  125.     Vector v = new Vector();
  126.     StringTokenizer t = new StringTokenizer(input);
  127.     String cmd[];
  128.  
  129.     while (t.hasMoreTokens())
  130.         v.addElement(t.nextToken());
  131.     cmd = new String[v.size()];
  132.     for (int i = 0; i < cmd.length; i++)
  133.         cmd[i] = (String) v.elementAt(i);
  134.  
  135.     return cmd;
  136.     }
  137.  
  138.     /**
  139.      * Get information about group <i>name</i>.
  140.      * @exception UnknownNewsgroupException the group name wasn't active.
  141.      * @exception NntpProtocolException received an unexpected reply.
  142.      */
  143.     public NewsgroupInfo getGroup(String name) throws IOException {
  144.     switch (askServer("group " + name + "\r\n")) {
  145.       case 411:
  146.         throw new UnknownNewsgroupException(name);
  147.  
  148.       default:
  149.         throw new NntpProtocolException("unexpected reply: "
  150.                         + getResponseString());
  151.  
  152.       case 211:
  153.         {
  154.         String tokens[] = tokenize(getResponseString());
  155.         int start;
  156.         int end;
  157.  
  158.         start = Integer.parseInt(tokens[2]);
  159.         end = Integer.parseInt(tokens[3]);
  160.         return new NewsgroupInfo(name, start, end);
  161.         }
  162.     }
  163.     }
  164.  
  165.     /** Set the current group to <i>name</i> */
  166.     public void setGroup(String name) throws IOException {
  167.     if (askServer("group " + name + "\r\n") != 211)
  168.         throw new UnknownNewsgroupException(name);
  169.     }
  170.  
  171.     /** get article <i>n</i> from the current group. */
  172.     public InputStream getArticle(int n) throws IOException {
  173.     return makeStreamRequest("article " + n, 220);
  174.     }
  175.  
  176.     /** get article <i>id</i> from the current group. */
  177.     public InputStream getArticle(String id) throws IOException {
  178.     if (id.charAt(0) != '<')
  179.         id = "<" + id + ">";
  180.     return makeStreamRequest("article " + id, 220);
  181.     }
  182.  
  183.     /** get header of article <i>n</i> from the current group. */
  184.     public InputStream getHeader(int n) throws IOException {
  185.     return makeStreamRequest("head " + n, 221);
  186.     }
  187.     /** get header of article <i>id</i> from the current group. */
  188.     public InputStream getHeader(String id) throws IOException {
  189.     if (id.charAt(0) != '<')
  190.         id = "<" + id + ">";
  191.     return makeStreamRequest("head " + id, 221);
  192.     }
  193.     /** Setup to post a message.  It returns a stream
  194.         to which the article should be written.  Returns null if the post
  195.     is disallowed.  The article must have a properly formed RFC850 header
  196.     and end-of-lines must by sent as \r\n.  The Article must end with
  197.     \r\n */
  198.     public PrintStream startPost() throws IOException {
  199.     return askServer("post\r\n") == 340 ? serverOutput : null;
  200.     }
  201.     /** Finish posting a message.  Must be called after calling startPost
  202.     and writing the article.  Returns true if the article is posted
  203.     successfully. */
  204.     public boolean finishPost() throws IOException {
  205.     return askServer(".\r\n") == 240;
  206.     }
  207. }
  208.