home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / net / HttpURLConnection.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  8.4 KB  |  273 lines  |  [TEXT/CWIE]

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