| 1 | // HttpURLConnection.java - Subclass of communications links using
|
|---|
| 2 | // Hypertext Transfer Protocol.
|
|---|
| 3 |
|
|---|
| 4 | /* Copyright (C) 1999, 2000 Free Software Foundation
|
|---|
| 5 |
|
|---|
| 6 | This file is part of libgcj.
|
|---|
| 7 |
|
|---|
| 8 | This software is copyrighted work licensed under the terms of the
|
|---|
| 9 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 10 | details. */
|
|---|
| 11 |
|
|---|
| 12 | package java.net;
|
|---|
| 13 |
|
|---|
| 14 | import java.io.*;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * @author Warren Levy <[email protected]>
|
|---|
| 18 | * @date March 29, 1999.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Written using on-line Java Platform 1.2 API Specification, as well
|
|---|
| 23 | * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
|
|---|
| 24 | * Status: Believed complete and correct.
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | public abstract class HttpURLConnection extends URLConnection
|
|---|
| 28 | {
|
|---|
| 29 | /* HTTP Success Response Codes */
|
|---|
| 30 | public static final int HTTP_OK = 200;
|
|---|
| 31 | public static final int HTTP_CREATED = 201;
|
|---|
| 32 | public static final int HTTP_ACCEPTED = 202;
|
|---|
| 33 | public static final int HTTP_NOT_AUTHORITATIVE = 203;
|
|---|
| 34 | public static final int HTTP_NO_CONTENT = 204;
|
|---|
| 35 | public static final int HTTP_RESET = 205;
|
|---|
| 36 | public static final int HTTP_PARTIAL = 206;
|
|---|
| 37 |
|
|---|
| 38 | /* HTTP Redirection Response Codes */
|
|---|
| 39 | public static final int HTTP_MULT_CHOICE = 300;
|
|---|
| 40 | public static final int HTTP_MOVED_PERM = 301;
|
|---|
| 41 | public static final int HTTP_MOVED_TEMP = 302;
|
|---|
| 42 | public static final int HTTP_SEE_OTHER = 303;
|
|---|
| 43 | public static final int HTTP_NOT_MODIFIED = 304;
|
|---|
| 44 | public static final int HTTP_USE_PROXY = 305;
|
|---|
| 45 |
|
|---|
| 46 | /* HTTP Client Error Response Codes */
|
|---|
| 47 | public static final int HTTP_BAD_REQUEST = 400;
|
|---|
| 48 | public static final int HTTP_UNAUTHORIZED = 401;
|
|---|
| 49 | public static final int HTTP_PAYMENT_REQUIRED = 402;
|
|---|
| 50 | public static final int HTTP_FORBIDDEN = 403;
|
|---|
| 51 | public static final int HTTP_NOT_FOUND = 404;
|
|---|
| 52 | public static final int HTTP_BAD_METHOD = 405;
|
|---|
| 53 | public static final int HTTP_NOT_ACCEPTABLE = 406;
|
|---|
| 54 | public static final int HTTP_PROXY_AUTH = 407;
|
|---|
| 55 | public static final int HTTP_CLIENT_TIMEOUT = 408;
|
|---|
| 56 | public static final int HTTP_CONFLICT = 409;
|
|---|
| 57 | public static final int HTTP_GONE = 410;
|
|---|
| 58 | public static final int HTTP_LENGTH_REQUIRED = 411;
|
|---|
| 59 | public static final int HTTP_PRECON_FAILED = 412;
|
|---|
| 60 | public static final int HTTP_ENTITY_TOO_LARGE = 413;
|
|---|
| 61 | public static final int HTTP_REQ_TOO_LONG = 414;
|
|---|
| 62 | public static final int HTTP_UNSUPPORTED_TYPE = 415;
|
|---|
| 63 |
|
|---|
| 64 | /* HTTP Server Error Response Codes */
|
|---|
| 65 | public static final int HTTP_SERVER_ERROR = 500;
|
|---|
| 66 | public static final int HTTP_INTERNAL_ERROR = 501;
|
|---|
| 67 | public static final int HTTP_BAD_GATEWAY = 502;
|
|---|
| 68 | public static final int HTTP_UNAVAILABLE = 503;
|
|---|
| 69 | public static final int HTTP_GATEWAY_TIMEOUT = 504;
|
|---|
| 70 | public static final int HTTP_VERSION = 505;
|
|---|
| 71 |
|
|---|
| 72 | static boolean followRedirects = true;
|
|---|
| 73 |
|
|---|
| 74 | protected String method = "GET";
|
|---|
| 75 | protected int responseCode = -1;
|
|---|
| 76 | protected String responseMessage;
|
|---|
| 77 | protected boolean instanceFollowRedirects = followRedirects;
|
|---|
| 78 |
|
|---|
| 79 | private boolean gotResponseVals = false;
|
|---|
| 80 |
|
|---|
| 81 | protected HttpURLConnection(URL url)
|
|---|
| 82 | {
|
|---|
| 83 | super(url);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | public abstract void disconnect();
|
|---|
| 87 |
|
|---|
| 88 | public abstract boolean usingProxy();
|
|---|
| 89 |
|
|---|
| 90 | public static void setFollowRedirects(boolean set)
|
|---|
| 91 | {
|
|---|
| 92 | // Throw an exception if an extant security mgr precludes
|
|---|
| 93 | // setting the factory.
|
|---|
| 94 | SecurityManager s = System.getSecurityManager();
|
|---|
| 95 | if (s != null)
|
|---|
| 96 | s.checkSetFactory();
|
|---|
| 97 |
|
|---|
| 98 | followRedirects = set;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | public static boolean getFollowRedirects()
|
|---|
| 102 | {
|
|---|
| 103 | return followRedirects;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | public void setRequestMethod(String method) throws ProtocolException
|
|---|
| 107 | {
|
|---|
| 108 | if (connected)
|
|---|
| 109 | throw new ProtocolException("Already connected");
|
|---|
| 110 |
|
|---|
| 111 | if (method.equals("GET") || method.equals("POST") ||
|
|---|
| 112 | method.equals("HEAD") || method.equals("OPTIONS") ||
|
|---|
| 113 | method.equals("PUT") || method.equals("DELETE") ||
|
|---|
| 114 | method.equals("TRACE"))
|
|---|
| 115 | this.method = method;
|
|---|
| 116 | else
|
|---|
| 117 | throw new ProtocolException("Invalid HTTP request method");
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | public String getRequestMethod()
|
|---|
| 121 | {
|
|---|
| 122 | return method;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | public int getResponseCode() throws IOException
|
|---|
| 126 | {
|
|---|
| 127 | if (!gotResponseVals)
|
|---|
| 128 | getResponseVals();
|
|---|
| 129 | return responseCode;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | public String getResponseMessage() throws IOException
|
|---|
| 133 | {
|
|---|
| 134 | if (!gotResponseVals)
|
|---|
| 135 | getResponseVals();
|
|---|
| 136 | return responseMessage;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | private void getResponseVals() throws IOException
|
|---|
| 140 | {
|
|---|
| 141 | // getHeaderField() will connect for us, but do it here first in
|
|---|
| 142 | // order to pick up IOExceptions.
|
|---|
| 143 | if (!connected)
|
|---|
| 144 | connect();
|
|---|
| 145 |
|
|---|
| 146 | gotResponseVals = true;
|
|---|
| 147 | // Response is the first header received from the connection.
|
|---|
| 148 | String respField = getHeaderField(0);
|
|---|
| 149 |
|
|---|
| 150 | if (respField == null || ! respField.startsWith("HTTP/"))
|
|---|
| 151 | {
|
|---|
| 152 | // Set to default values on failure.
|
|---|
| 153 | responseCode = -1;
|
|---|
| 154 | responseMessage = null;
|
|---|
| 155 | return;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | int firstSpc, nextSpc;
|
|---|
| 159 | firstSpc = respField.indexOf(' ');
|
|---|
| 160 | nextSpc = respField.indexOf(' ', firstSpc + 1);
|
|---|
| 161 | responseMessage = respField.substring(nextSpc + 1);
|
|---|
| 162 | String codeStr = respField.substring(firstSpc + 1, nextSpc);
|
|---|
| 163 | try
|
|---|
| 164 | {
|
|---|
| 165 | responseCode = Integer.parseInt(codeStr);
|
|---|
| 166 | }
|
|---|
| 167 | catch (NumberFormatException e)
|
|---|
| 168 | {
|
|---|
| 169 | // Set to default values on failure.
|
|---|
| 170 | responseCode = -1;
|
|---|
| 171 | responseMessage = null;
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | // TODO12: public Permission getPermission() throws IOException
|
|---|
| 176 | // {
|
|---|
| 177 | // }
|
|---|
| 178 |
|
|---|
| 179 | // TODO12: public InputStream getErrorStream()
|
|---|
| 180 | // {
|
|---|
| 181 | // }
|
|---|
| 182 | }
|
|---|