home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / HttpURLConnection.java < prev    next >
Text File  |  1997-10-01  |  8KB  |  252 lines

  1. /*
  2.  * @(#)HttpURLConnection.java    1.9 97/06/12
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  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
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.net;
  24. import java.io.IOException;
  25.  
  26. /**
  27.  * A URLConnection with support for HTTP-specific features. See
  28.  * <A HREF="http://www.w3.org/pub/WWW/Protocols/"> the spec </A> for
  29.  * details.  
  30.  * @since JDK1.1
  31.  */
  32. abstract public class HttpURLConnection extends URLConnection {
  33.     /* instance variables */
  34.  
  35.     /**
  36.      * @since   JDK1.1
  37.      */
  38.     protected String method = "GET";
  39.  
  40.     /**
  41.      * @since   JDK1.1
  42.      */
  43.     protected int responseCode = -1;
  44.  
  45.     /**
  46.      * @since   JDK1.1
  47.      */
  48.     protected String responseMessage = null;
  49.  
  50.  
  51.     /* static variables */
  52.  
  53.     /* do we automatically follow redirects? The default is true. */
  54.     private static boolean followRedirects = true;
  55.  
  56.     /* valid HTTP methods */
  57.     private static final String[] methods = {
  58.     "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE"
  59.     };
  60.  
  61.     /**
  62.      * Constructor for the URLStreamHandler.
  63.      * @since   JDK1.1
  64.      */
  65.     protected HttpURLConnection (URL u) {
  66.     super(u);
  67.     }
  68.     
  69.     /**
  70.      * Sets whether HTTP redirects  (requests with response code 3xx) should 
  71.      * be automatically followed by this class.  True by default.  Applets
  72.      * cannot change this variable.
  73.      * @since   JDK1.1
  74.      */
  75.     public static void setFollowRedirects(boolean set) {
  76.     SecurityManager sec = System.getSecurityManager();
  77.     if (sec != null) {
  78.         // seems to be the best check here...
  79.         sec.checkSetFactory();
  80.     }
  81.     followRedirects = set;
  82.     }
  83.  
  84.     /**
  85.      * @since   JDK1.1
  86.      */
  87.     public static boolean getFollowRedirects() {
  88.     return followRedirects;
  89.     }
  90.  
  91.     /**
  92.      * Set the method for the URL request, one of:
  93.      * <UL>
  94.      *  <LI>GET
  95.      *  <LI>POST
  96.      *  <LI>HEAD
  97.      *  <LI>OPTIONS
  98.      *  <LI>PUT
  99.      *  <LI>DELETE
  100.      *  <LI>TRACE
  101.      * </UL> are legal, subject to protocol restrictions.  The default
  102.      * method is GET.
  103.      * 
  104.      * @exception ProtocolException if the method cannot be reset or if
  105.      *              the requested method isn't valid for HTTP.
  106.      * @since     JDK1.1
  107.      */
  108.     public void setRequestMethod(String method) throws ProtocolException {
  109.     if (connected) {
  110.         throw new ProtocolException("Can't reset method: already connected");
  111.     }
  112.     // This restriction will prevent people from using this class to 
  113.     // experiment w/ new HTTP methods using java.  But it should 
  114.     // be placed for security - the request String could be
  115.     // arbitrarily long.
  116.  
  117.     for (int i = 0; i < methods.length; i++) {
  118.         if (methods[i].equals(method)) {
  119.         this.method = method;
  120.         return;
  121.         }
  122.     }
  123.     throw new ProtocolException("Invalid HTTP method: " + method);
  124.     }
  125.  
  126.     /**
  127.      * Get the request method.
  128.      * @since   JDK1.1
  129.      */
  130.     public String getRequestMethod() {
  131.     return method;
  132.     }
  133.     
  134.     /**
  135.      * Gets HTTP response status.  From responses like:
  136.      * <PRE>
  137.      * HTTP/1.0 200 OK
  138.      * HTTP/1.0 401 Unauthorized
  139.      * </PRE>
  140.      * Extracts the ints 200 and 401 respectively.
  141.      * Returns -1 if none can be discerned
  142.      * from the response (i.e., the response is not valid HTTP).
  143.      * @throws IOException if an error occurred connecting to the server.
  144.      * @since   JDK1.1
  145.      */
  146.     public int getResponseCode() throws IOException {
  147.     if (responseCode != -1) {
  148.         return responseCode;
  149.     }
  150.     // make sure we've gotten the headers
  151.     getInputStream();
  152.  
  153.     String resp = getHeaderField(0);
  154.     /* should have no leading/trailing LWS
  155.      * expedite the typical case by assuming it has
  156.      * form "HTTP/1.x <WS> 2XX <mumble>"
  157.      */
  158.     int ind;
  159.     try {    
  160.         ind = resp.indexOf(' ');
  161.         while(resp.charAt(ind) == ' ')
  162.         ind++;
  163.         responseCode = Integer.parseInt(resp.substring(ind, ind + 3));
  164.         responseMessage = resp.substring(ind + 4).trim();
  165.         return responseCode;
  166.     } catch (Exception e) { 
  167.         return responseCode;
  168.     }
  169.     }
  170.  
  171.     /**
  172.      * Gets the HTTP response message, if any, returned along with the
  173.      * response code from a server.  From responses like:
  174.      * <PRE>
  175.      * HTTP/1.0 200 OK
  176.      * HTTP/1.0 404 Not Found
  177.      * </PRE>
  178.      * Extracts the Strings "OK" and "Not Found" respectively.
  179.      * Returns null if none could be discerned from the responses 
  180.      * (the result was not valid HTTP).
  181.      * @throws IOException if an error occurred connecting to the server.
  182.      * @since  JDK1.1
  183.      */
  184.     public String getResponseMessage() throws IOException {
  185.     getResponseCode();
  186.     return responseMessage;
  187.     }
  188.  
  189.     /**
  190.      * Close the connection to the server.
  191.      * @since JDK1.1
  192.      */
  193.     public abstract void disconnect();
  194.  
  195.     /**
  196.      * Indicates if the connection is going through a proxy.
  197.      * @since   JDK1.1
  198.      */
  199.     public abstract boolean usingProxy();
  200.  
  201.     /**
  202.      * The response codes for HTTP, as of version 1.1.
  203.      */
  204.  
  205.     // REMIND: do we want all these??
  206.     // Others not here that we do want??
  207.  
  208.     /** 2XX: generally "OK" */
  209.     public static final int HTTP_OK = 200;
  210.     public static final int HTTP_CREATED = 201;
  211.     public static final int HTTP_ACCEPTED = 202;
  212.     public static final int HTTP_NOT_AUTHORITATIVE = 203; 
  213.     public static final int HTTP_NO_CONTENT = 204;
  214.     public static final int HTTP_RESET = 205;
  215.     public static final int HTTP_PARTIAL = 206;
  216.  
  217.     /** 3XX: relocation/redirect */
  218.     public static final int HTTP_MULT_CHOICE = 300;
  219.     public static final int HTTP_MOVED_PERM = 301;
  220.     public static final int HTTP_MOVED_TEMP = 302;
  221.     public static final int HTTP_SEE_OTHER = 303;
  222.     public static final int HTTP_NOT_MODIFIED = 304;
  223.     public static final int HTTP_USE_PROXY = 305;
  224.  
  225.     /** 4XX: client error */
  226.     public static final int HTTP_BAD_REQUEST = 400;
  227.     public static final int HTTP_UNAUTHORIZED = 401;
  228.     public static final int HTTP_PAYMENT_REQUIRED = 402;
  229.     public static final int HTTP_FORBIDDEN = 403;
  230.     public static final int HTTP_NOT_FOUND = 404;
  231.     public static final int HTTP_BAD_METHOD = 405;
  232.     public static final int HTTP_NOT_ACCEPTABLE = 406;
  233.     public static final int HTTP_PROXY_AUTH = 407;
  234.     public static final int HTTP_CLIENT_TIMEOUT = 408;
  235.     public static final int HTTP_CONFLICT = 409;
  236.     public static final int HTTP_GONE = 410;
  237.     public static final int HTTP_LENGTH_REQUIRED = 411;
  238.     public static final int HTTP_PRECON_FAILED = 412;
  239.     public static final int HTTP_ENTITY_TOO_LARGE = 413;
  240.     public static final int HTTP_REQ_TOO_LONG = 414;
  241.     public static final int HTTP_UNSUPPORTED_TYPE = 415;
  242.     
  243.     /** 5XX: server error */
  244.     public static final int HTTP_SERVER_ERROR = 500;
  245.     public static final int HTTP_INTERNAL_ERROR = 501;
  246.     public static final int HTTP_BAD_GATEWAY = 502;
  247.     public static final int HTTP_UNAVAILABLE = 503;
  248.     public static final int HTTP_GATEWAY_TIMEOUT = 504;
  249.     public static final int HTTP_VERSION = 505;
  250.  
  251. }
  252.