001: // $Id: HTTP.java,v 1.10 2001/01/25 02:17:09 nconway Exp $
002: package tornado;
003:
004: import java.text.SimpleDateFormat;
005: import java.util.Date;
006: import java.util.TimeZone;
007:
008: public final class HTTP {
009: private static SimpleDateFormat dateMaker = new SimpleDateFormat(
010: "EEE, dd MMM yyyy HH:mm:ss z");
011: private static TimeZone gmt = TimeZone.getTimeZone("GMT");
012:
013: public static int CONTINUE = 100;
014: public static int OK = 200;
015: public static int CREATED = 201;
016: public static int ACCEPTED = 202;
017: public static int NO_CONTENT = 204;
018: public static int RESET_CONTENT = 205;
019: public static int PARTIAL_CONTENT = 206;
020: public static int MULTIPLE_CHOICES = 300;
021: public static int MOVED = 301;
022: public static int FOUND = 302;
023: public static int SEE_OTHER = 303;
024: public static int NOT_MODIFIED = 304;
025: public static int USE_PROXY = 305;
026: public static int TEMP_REDIRECT = 306;
027: public static int BAD_REQUEST = 400;
028: public static int UNAUTH = 401;
029: public static int FORBIDDEN = 403;
030: public static int NOT_FOUND = 404;
031: public static int METHOD_NOT_ALLOWED = 405;
032: public static int NOT_ACCEPTABLE = 406;
033: public static int TIMEOUT = 408;
034: public static int CONFLICT = 409;
035: public static int GONE = 410;
036: public static int LENGTH_REQUIRED = 411;
037: public static int PRECONDITION_FAILED = 412;
038: public static int REQUEST_TOO_LARGE = 413;
039: public static int URI_TOO_LONG = 414;
040: public static int BAD_MEDIA_TYPE = 415;
041: public static int EXPECTATION_FAILED = 417;
042: public static int SERVER_ERROR = 500;
043: public static int NOT_IMPLEMENTED = 501;
044: public static int UNAVAILABLE = 503;
045: public static int BAD_HTTP_VERSION = 505;
046:
047: public static String STATUS_100 = "Continue";
048: public static String STATUS_200 = "OK";
049: public static String STATUS_201 = "Created";
050: public static String STATUS_202 = "Accepted";
051: public static String STATUS_203 = "No Content";
052: public static String STATUS_204 = "Reset Content";
053: public static String STATUS_206 = "Partial Content";
054: public static String STATUS_300 = "Multiple Choices";
055: public static String STATUS_301 = "Moved";
056: public static String STATUS_302 = "Found";
057: public static String STATUS_303 = "See Other";
058: public static String STATUS_304 = "Not Modified";
059: public static String STATUS_305 = "Use Proxy";
060: public static String STATUS_306 = "Temporary Redirect";
061: public static String STATUS_400 = "Bad Request";
062: public static String STATUS_401 = "Unauthorized";
063: public static String STATUS_403 = "Forbidden";
064: public static String STATUS_404 = "Not Found";
065: public static String STATUS_405 = "Method Not Allowed";
066: public static String STATUS_406 = "Not Acceptable";
067: public static String STATUS_408 = "Request Timeout";
068: public static String STATUS_409 = "Conflict";
069: public static String STATUS_410 = "Gone";
070: public static String STATUS_411 = "Length Required";
071: public static String STATUS_412 = "Precondition Failed";
072: public static String STATUS_413 = "Request Entity Too Large";
073: public static String STATUS_414 = "Request-URI Too Long";
074: public static String STATUS_415 = "Unsupported Media Type";
075: public static String STATUS_417 = "Expectation Failed";
076: public static String STATUS_500 = "Internal Server Error";
077: public static String STATUS_501 = "Not Implemented";
078: public static String STATUS_503 = "Unavailable";
079: public static String STATUS_505 = "HTTP Version Not Supported";
080:
081: /** Returns the <code>String</code> form of the specified statusCode.
082: * In order to do this, it uses reflection to return the value
083: * of one the appropriate STATUS_xxx constants.
084: */
085: public static String getStatusStr(int statusCode) {
086: String fieldName = "STATUS_" + statusCode;
087: try {
088: String statusStr = (String) Class.forName("tornado.HTTP")
089: .getField(fieldName).get(null);
090: return statusStr;
091: } catch (NoSuchFieldException e) {
092: throw new IllegalArgumentException("Invalid status code: "
093: + statusCode);
094: } catch (Exception e) {
095: throw new RuntimeException(e.getMessage());
096: }
097: }
098:
099: public static String formatDate(Date date) {
100: // bad, don't call this every time
101: dateMaker.setTimeZone(gmt);
102: return dateMaker.format(date);
103: }
104: }
|