home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / net / HttpURLConnection.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  7.1 KB  |  241 lines

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