home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VPage / Java.bin / CLASSES.ZIP / sun / net / nntp / NntpClient.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-08  |  4.0 KB  |  167 lines

  1. package sun.net.nntp;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.PrintStream;
  6. import java.util.StringTokenizer;
  7. import java.util.Vector;
  8. import sun.net.TelnetInputStream;
  9. import sun.net.TransferProtocolClient;
  10.  
  11. public class NntpClient extends TransferProtocolClient {
  12.    public static final int NNTP_PORT = 119;
  13.    String serverName;
  14.    int serverPort;
  15.  
  16.    public NntpClient() {
  17.    }
  18.  
  19.    public NntpClient(String var1) throws IOException {
  20.       this.openServer(var1, 119);
  21.    }
  22.  
  23.    public void openServer(String var1, int var2) throws IOException {
  24.       this.serverName = var1;
  25.       this.serverPort = var2;
  26.       super.openServer(var1, var2);
  27.       if (((TransferProtocolClient)this).readServerResponse() >= 300) {
  28.          throw new NntpProtocolException("Welcome message");
  29.       }
  30.    }
  31.  
  32.    public int askServer(String var1) throws IOException {
  33.       int var2 = 503;
  34.       int var3 = 3;
  35.  
  36.       while(true) {
  37.          --var3;
  38.          if (var3 < 0) {
  39.             return var2;
  40.          }
  41.  
  42.          try {
  43.             super.serverOutput.print(var1);
  44.             var2 = ((TransferProtocolClient)this).readServerResponse();
  45.             if (var2 < 500) {
  46.                return var2;
  47.             }
  48.          } catch (Exception var5) {
  49.          }
  50.  
  51.          try {
  52.             super.serverOutput.close();
  53.          } catch (Exception var4) {
  54.          }
  55.  
  56.          this.openServer(this.serverName, this.serverPort);
  57.       }
  58.    }
  59.  
  60.    InputStream makeStreamRequest(String var1, int var2) throws IOException {
  61.       int var3 = this.askServer(var1 + "\r\n");
  62.       if (var3 != var2) {
  63.          String var4 = null;
  64.  
  65.          try {
  66.             for(int var5 = 0; var5 < 99; ++var5) {
  67.                String var6 = (String)super.serverResponse.elementAt(var5);
  68.                if (var4 == null) {
  69.                   var4 = var6;
  70.                } else {
  71.                   var4 = var4 + "\n" + var6;
  72.                }
  73.             }
  74.          } catch (Exception var7) {
  75.          }
  76.  
  77.          if (var4 == null) {
  78.             var4 = "Command " + var1 + " yielded " + var3 + "; expecting " + var2;
  79.          }
  80.  
  81.          throw new NntpProtocolException(var4);
  82.       } else {
  83.          switch (var3 / 100) {
  84.             case 1:
  85.             case 2:
  86.             default:
  87.                return new NntpInputStream(new TelnetInputStream(super.serverInput, false));
  88.             case 3:
  89.                throw new NntpProtocolException("More input to command expected");
  90.             case 4:
  91.                throw new NntpProtocolException("Server error - cmd OK");
  92.             case 5:
  93.                throw new NntpProtocolException("Error in command: " + var1);
  94.          }
  95.       }
  96.    }
  97.  
  98.    String[] tokenize(String var1) {
  99.       Vector var2 = new Vector();
  100.       StringTokenizer var3 = new StringTokenizer(var1);
  101.  
  102.       while(var3.hasMoreTokens()) {
  103.          var2.addElement(var3.nextToken());
  104.       }
  105.  
  106.       String[] var4 = new String[var2.size()];
  107.  
  108.       for(int var5 = 0; var5 < var4.length; ++var5) {
  109.          var4[var5] = (String)var2.elementAt(var5);
  110.       }
  111.  
  112.       return var4;
  113.    }
  114.  
  115.    public NewsgroupInfo getGroup(String var1) throws IOException {
  116.       switch (this.askServer("group " + var1 + "\r\n")) {
  117.          case 211:
  118.             String[] var2 = this.tokenize(((TransferProtocolClient)this).getResponseString());
  119.             int var3 = Integer.parseInt(var2[2]);
  120.             int var4 = Integer.parseInt(var2[3]);
  121.             return new NewsgroupInfo(var1, var3, var4);
  122.          case 411:
  123.             throw new UnknownNewsgroupException(var1);
  124.          default:
  125.             throw new NntpProtocolException("unexpected reply: " + ((TransferProtocolClient)this).getResponseString());
  126.       }
  127.    }
  128.  
  129.    public void setGroup(String var1) throws IOException {
  130.       if (this.askServer("group " + var1 + "\r\n") != 211) {
  131.          throw new UnknownNewsgroupException(var1);
  132.       }
  133.    }
  134.  
  135.    public InputStream getArticle(int var1) throws IOException {
  136.       return this.makeStreamRequest("article " + var1, 220);
  137.    }
  138.  
  139.    public InputStream getArticle(String var1) throws IOException {
  140.       if (var1.charAt(0) != '<') {
  141.          var1 = "<" + var1 + ">";
  142.       }
  143.  
  144.       return this.makeStreamRequest("article " + var1, 220);
  145.    }
  146.  
  147.    public InputStream getHeader(int var1) throws IOException {
  148.       return this.makeStreamRequest("head " + var1, 221);
  149.    }
  150.  
  151.    public InputStream getHeader(String var1) throws IOException {
  152.       if (var1.charAt(0) != '<') {
  153.          var1 = "<" + var1 + ">";
  154.       }
  155.  
  156.       return this.makeStreamRequest("head " + var1, 221);
  157.    }
  158.  
  159.    public PrintStream startPost() throws IOException {
  160.       return this.askServer("post\r\n") == 340 ? super.serverOutput : null;
  161.    }
  162.  
  163.    public boolean finishPost() throws IOException {
  164.       return this.askServer(".\r\n") == 240;
  165.    }
  166. }
  167.