`

jetty关于http状态码处理的一个很有用的类

 
阅读更多

public class HttpStatus
{
    public final static int CONTINUE_100 = 100;
    public final static int SWITCHING_PROTOCOLS_101 = 101;
    public final static int PROCESSING_102 = 102;

    public final static int OK_200 = 200;
    public final static int CREATED_201 = 201;
    public final static int ACCEPTED_202 = 202;
    public final static int NON_AUTHORITATIVE_INFORMATION_203 = 203;
    public final static int NO_CONTENT_204 = 204;
    public final static int RESET_CONTENT_205 = 205;
    public final static int PARTIAL_CONTENT_206 = 206;
    public final static int MULTI_STATUS_207 = 207;

    public final static int MULTIPLE_CHOICES_300 = 300;
    public final static int MOVED_PERMANENTLY_301 = 301;
    public final static int MOVED_TEMPORARILY_302 = 302;
    public final static int FOUND_302 = 302;
    public final static int SEE_OTHER_303 = 303;
    public final static int NOT_MODIFIED_304 = 304;
    public final static int USE_PROXY_305 = 305;
    public final static int TEMPORARY_REDIRECT_307 = 307;

    public final static int BAD_REQUEST_400 = 400;
    public final static int UNAUTHORIZED_401 = 401;
    public final static int PAYMENT_REQUIRED_402 = 402;
    public final static int FORBIDDEN_403 = 403;
    public final static int NOT_FOUND_404 = 404;
    public final static int METHOD_NOT_ALLOWED_405 = 405;
    public final static int NOT_ACCEPTABLE_406 = 406;
    public final static int PROXY_AUTHENTICATION_REQUIRED_407 = 407;
    public final static int REQUEST_TIMEOUT_408 = 408;
    public final static int CONFLICT_409 = 409;
    public final static int GONE_410 = 410;
    public final static int LENGTH_REQUIRED_411 = 411;
    public final static int PRECONDITION_FAILED_412 = 412;
    public final static int REQUEST_ENTITY_TOO_LARGE_413 = 413;
    public final static int REQUEST_URI_TOO_LONG_414 = 414;
    public final static int UNSUPPORTED_MEDIA_TYPE_415 = 415;
    public final static int REQUESTED_RANGE_NOT_SATISFIABLE_416 = 416;
    public final static int EXPECTATION_FAILED_417 = 417;
    public final static int UNPROCESSABLE_ENTITY_422 = 422;
    public final static int LOCKED_423 = 423;
    public final static int FAILED_DEPENDENCY_424 = 424;

    public final static int INTERNAL_SERVER_ERROR_500 = 500;
    public final static int NOT_IMPLEMENTED_501 = 501;
    public final static int BAD_GATEWAY_502 = 502;
    public final static int SERVICE_UNAVAILABLE_503 = 503;
    public final static int GATEWAY_TIMEOUT_504 = 504;
    public final static int HTTP_VERSION_NOT_SUPPORTED_505 = 505;
    public final static int INSUFFICIENT_STORAGE_507 = 507;

    public static final int MAX_CODE = 507;


    private static final Code[] codeMap = new Code[MAX_CODE+1];

    static
    {
        for (Code code : Code.values())
        {
            codeMap[code._code] = code;
        }
    }


    public enum Code
    {
        /*
         * --------------------------------------------------------------------
         * Informational messages in 1xx series. As defined by ... RFC 1945 -
         * HTTP/1.0 RFC 2616 - HTTP/1.1 RFC 2518 - WebDAV
         */

        /** <code>100 Continue</code> */
        CONTINUE(CONTINUE_100, "Continue"),
        /** <code>101 Switching Protocols</code> */
        SWITCHING_PROTOCOLS(SWITCHING_PROTOCOLS_101, "Switching Protocols"),
        /** <code>102 Processing</code> */
        PROCESSING(PROCESSING_102, "Processing"),

        /*
         * --------------------------------------------------------------------
         * Success messages in 2xx series. As defined by ... RFC 1945 - HTTP/1.0
         * RFC 2616 - HTTP/1.1 RFC 2518 - WebDAV
         */

        /** <code>200 OK</code> */
        OK(OK_200, "OK"),
        /** <code>201 Created</code> */
        CREATED(CREATED_201, "Created"),
        /** <code>202 Accepted</code> */
        ACCEPTED(ACCEPTED_202, "Accepted"),
        /** <code>203 Non Authoritative Information</code> */
        NON_AUTHORITATIVE_INFORMATION(NON_AUTHORITATIVE_INFORMATION_203, "Non Authoritative Information"),
        /** <code>204 No Content</code> */
        NO_CONTENT(NO_CONTENT_204, "No Content"),
        /** <code>205 Reset Content</code> */
        RESET_CONTENT(RESET_CONTENT_205, "Reset Content"),
        /** <code>206 Partial Content</code> */
        PARTIAL_CONTENT(PARTIAL_CONTENT_206, "Partial Content"),
        /** <code>207 Multi-Status</code> */
        MULTI_STATUS(MULTI_STATUS_207, "Multi-Status"),

        /*
         * --------------------------------------------------------------------
         * Redirection messages in 3xx series. As defined by ... RFC 1945 -
         * HTTP/1.0 RFC 2616 - HTTP/1.1
         */

        /** <code>300 Mutliple Choices</code> */
        MULTIPLE_CHOICES(MULTIPLE_CHOICES_300, "Multiple Choices"),
        /** <code>301 Moved Permanently</code> */
        MOVED_PERMANENTLY(MOVED_PERMANENTLY_301, "Moved Permanently"),
        /** <code>302 Moved Temporarily</code> */
        MOVED_TEMPORARILY(MOVED_TEMPORARILY_302, "Moved Temporarily"),
        /** <code>302 Found</code> */
        FOUND(FOUND_302, "Found"),
        /** <code>303 See Other</code> */
        SEE_OTHER(SEE_OTHER_303, "See Other"),
        /** <code>304 Not Modified</code> */
        NOT_MODIFIED(NOT_MODIFIED_304, "Not Modified"),
        /** <code>305 Use Proxy</code> */
        USE_PROXY(USE_PROXY_305, "Use Proxy"),
        /** <code>307 Temporary Redirect</code> */
        TEMPORARY_REDIRECT(TEMPORARY_REDIRECT_307, "Temporary Redirect"),

        /*
         * --------------------------------------------------------------------
         * Client Error messages in 4xx series. As defined by ... RFC 1945 -
         * HTTP/1.0 RFC 2616 - HTTP/1.1 RFC 2518 - WebDAV
         */

        /** <code>400 Bad Request</code> */
        BAD_REQUEST(BAD_REQUEST_400, "Bad Request"),
        /** <code>401 Unauthorized</code> */
        UNAUTHORIZED(UNAUTHORIZED_401, "Unauthorized"),
        /** <code>402 Payment Required</code> */
        PAYMENT_REQUIRED(PAYMENT_REQUIRED_402, "Payment Required"),
        /** <code>403 Forbidden</code> */
        FORBIDDEN(FORBIDDEN_403, "Forbidden"),
        /** <code>404 Not Found</code> */
        NOT_FOUND(NOT_FOUND_404, "Not Found"),
        /** <code>405 Method Not Allowed</code> */
        METHOD_NOT_ALLOWED(METHOD_NOT_ALLOWED_405, "Method Not Allowed"),
        /** <code>406 Not Acceptable</code> */
        NOT_ACCEPTABLE(NOT_ACCEPTABLE_406, "Not Acceptable"),
        /** <code>407 Proxy Authentication Required</code> */
        PROXY_AUTHENTICATION_REQUIRED(PROXY_AUTHENTICATION_REQUIRED_407, "Proxy Authentication Required"),
        /** <code>408 Request Timeout</code> */
        REQUEST_TIMEOUT(REQUEST_TIMEOUT_408, "Request Timeout"),
        /** <code>409 Conflict</code> */
        CONFLICT(CONFLICT_409, "Conflict"),
        /** <code>410 Gone</code> */
        GONE(GONE_410, "Gone"),
        /** <code>411 Length Required</code> */
        LENGTH_REQUIRED(LENGTH_REQUIRED_411, "Length Required"),
        /** <code>412 Precondition Failed</code> */
        PRECONDITION_FAILED(PRECONDITION_FAILED_412, "Precondition Failed"),
        /** <code>413 Request Entity Too Large</code> */
        REQUEST_ENTITY_TOO_LARGE(REQUEST_ENTITY_TOO_LARGE_413, "Request Entity Too Large"),
        /** <code>414 Request-URI Too Long</code> */
        REQUEST_URI_TOO_LONG(REQUEST_URI_TOO_LONG_414, "Request-URI Too Long"),
        /** <code>415 Unsupported Media Type</code> */
        UNSUPPORTED_MEDIA_TYPE(UNSUPPORTED_MEDIA_TYPE_415, "Unsupported Media Type"),
        /** <code>416 Requested Range Not Satisfiable</code> */
        REQUESTED_RANGE_NOT_SATISFIABLE(REQUESTED_RANGE_NOT_SATISFIABLE_416, "Requested Range Not Satisfiable"),
        /** <code>417 Expectation Failed</code> */
        EXPECTATION_FAILED(EXPECTATION_FAILED_417, "Expectation Failed"),
        /** <code>422 Unprocessable Entity</code> */
        UNPROCESSABLE_ENTITY(UNPROCESSABLE_ENTITY_422, "Unprocessable Entity"),
        /** <code>423 Locked</code> */
        LOCKED(LOCKED_423, "Locked"),
        /** <code>424 Failed Dependency</code> */
        FAILED_DEPENDENCY(FAILED_DEPENDENCY_424, "Failed Dependency"),

        /*
         * --------------------------------------------------------------------
         * Server Error messages in 5xx series. As defined by ... RFC 1945 -
         * HTTP/1.0 RFC 2616 - HTTP/1.1 RFC 2518 - WebDAV
         */

        /** <code>500 Server Error</code> */
        INTERNAL_SERVER_ERROR(INTERNAL_SERVER_ERROR_500, "Server Error"),
        /** <code>501 Not Implemented</code> */
        NOT_IMPLEMENTED(NOT_IMPLEMENTED_501, "Not Implemented"),
        /** <code>502 Bad Gateway</code> */
        BAD_GATEWAY(BAD_GATEWAY_502, "Bad Gateway"),
        /** <code>503 Service Unavailable</code> */
        SERVICE_UNAVAILABLE(SERVICE_UNAVAILABLE_503, "Service Unavailable"),
        /** <code>504 Gateway Timeout</code> */
        GATEWAY_TIMEOUT(GATEWAY_TIMEOUT_504, "Gateway Timeout"),
        /** <code>505 HTTP Version Not Supported</code> */
        HTTP_VERSION_NOT_SUPPORTED(HTTP_VERSION_NOT_SUPPORTED_505, "HTTP Version Not Supported"),
        /** <code>507 Insufficient Storage</code> */
        INSUFFICIENT_STORAGE(INSUFFICIENT_STORAGE_507, "Insufficient Storage");

        private final int _code;
        private final String _message;

        private Code(int code, String message)
        {
            this._code = code;
            _message=message;
        }

        public int getCode()
        {
            return _code;
        }

        public String getMessage()
        {
            return _message;
        }


        public boolean equals(int code)
        {
            return (this._code == code);
        }

        @Override
        public String toString()
        {
            return String.format("[%03d %s]",this._code,this.getMessage());
        }

        /**
         * Simple test against an code to determine if it falls into the
         * <code>Informational</code> message category as defined in the <a
         * href="http://tools.ietf.org/html/rfc1945">RFC 1945 - HTTP/1.0</a>,
         * and <a href="http://tools.ietf.org/html/rfc2616">RFC 2616 -
         * HTTP/1.1</a>.
         *
         * @return true if within range of codes that belongs to
         *         <code>Informational</code> messages.
         */
        public boolean isInformational()
        {
            return HttpStatus.isInformational(this._code);
        }

        /**
         * Simple test against an code to determine if it falls into the
         * <code>Success</code> message category as defined in the <a
         * href="http://tools.ietf.org/html/rfc1945">RFC 1945 - HTTP/1.0</a>,
         * and <a href="http://tools.ietf.org/html/rfc2616">RFC 2616 -
         * HTTP/1.1</a>.
         *
         * @return true if within range of codes that belongs to
         *         <code>Success</code> messages.
         */
        public boolean isSuccess()
        {
            return HttpStatus.isSuccess(this._code);
        }

        /**
         * Simple test against an code to determine if it falls into the
         * <code>Redirection</code> message category as defined in the <a
         * href="http://tools.ietf.org/html/rfc1945">RFC 1945 - HTTP/1.0</a>,
         * and <a href="http://tools.ietf.org/html/rfc2616">RFC 2616 -
         * HTTP/1.1</a>.
         *
         * @return true if within range of codes that belongs to
         *         <code>Redirection</code> messages.
         */
        public boolean isRedirection()
        {
            return HttpStatus.isRedirection(this._code);
        }

        /**
         * Simple test against an code to determine if it falls into the
         * <code>Client Error</code> message category as defined in the <a
         * href="http://tools.ietf.org/html/rfc1945">RFC 1945 - HTTP/1.0</a>,
         * and <a href="http://tools.ietf.org/html/rfc2616">RFC 2616 -
         * HTTP/1.1</a>.
         *
         * @return true if within range of codes that belongs to
         *         <code>Client Error</code> messages.
         */
        public boolean isClientError()
        {
            return HttpStatus.isClientError(this._code);
        }

        /**
         * Simple test against an code to determine if it falls into the
         * <code>Server Error</code> message category as defined in the <a
         * href="http://tools.ietf.org/html/rfc1945">RFC 1945 - HTTP/1.0</a>,
         * and <a href="http://tools.ietf.org/html/rfc2616">RFC 2616 -
         * HTTP/1.1</a>.
         *
         * @return true if within range of codes that belongs to
         *         <code>Server Error</code> messages.
         */
        public boolean isServerError()
        {
            return HttpStatus.isServerError(this._code);
        }
    }


    /**
     * Get the HttpStatusCode for a specific code
     *
     * @param code
     *            the code to lookup.
     * @return the {@link HttpStatus} if found, or null if not found.
     */
    public static Code getCode(int code)
    {
        if (code <= MAX_CODE)
        {
            return codeMap[code];
        }
        return null;
    }

    /**
     * Get the status message for a specific code.
     *
     * @param code
     *            the code to look up
     * @return the specific message, or the code number itself if code
     *         does not match known list.
     */
    public static String getMessage(int code)
    {
        Code codeEnum = getCode(code);
        if (codeEnum != null)
        {
            return codeEnum.getMessage();
        }
        else
        {
            return Integer.toString(code);
        }
    }

    /**
     * Simple test against an code to determine if it falls into the
     * <code>Informational</code> message category as defined in the <a
     * href="http://tools.ietf.org/html/rfc1945">RFC 1945 - HTTP/1.0</a>, and <a
     * href="http://tools.ietf.org/html/rfc2616">RFC 2616 - HTTP/1.1</a>.
     *
     * @param code
     *            the code to test.
     * @return true if within range of codes that belongs to
     *         <code>Informational</code> messages.
     */
    public static boolean isInformational(int code)
    {
        return ((100 <= code) && (code <= 199));
    }

    /**
     * Simple test against an code to determine if it falls into the
     * <code>Success</code> message category as defined in the <a
     * href="http://tools.ietf.org/html/rfc1945">RFC 1945 - HTTP/1.0</a>, and <a
     * href="http://tools.ietf.org/html/rfc2616">RFC 2616 - HTTP/1.1</a>.
     *
     * @param code
     *            the code to test.
     * @return true if within range of codes that belongs to
     *         <code>Success</code> messages.
     */
    public static boolean isSuccess(int code)
    {
        return ((200 <= code) && (code <= 299));
    }

    /**
     * Simple test against an code to determine if it falls into the
     * <code>Redirection</code> message category as defined in the <a
     * href="http://tools.ietf.org/html/rfc1945">RFC 1945 - HTTP/1.0</a>, and <a
     * href="http://tools.ietf.org/html/rfc2616">RFC 2616 - HTTP/1.1</a>.
     *
     * @param code
     *            the code to test.
     * @return true if within range of codes that belongs to
     *         <code>Redirection</code> messages.
     */
    public static boolean isRedirection(int code)
    {
        return ((300 <= code) && (code <= 399));
    }

    /**
     * Simple test against an code to determine if it falls into the
     * <code>Client Error</code> message category as defined in the <a
     * href="http://tools.ietf.org/html/rfc1945">RFC 1945 - HTTP/1.0</a>, and <a
     * href="http://tools.ietf.org/html/rfc2616">RFC 2616 - HTTP/1.1</a>.
     *
     * @param code
     *            the code to test.
     * @return true if within range of codes that belongs to
     *         <code>Client Error</code> messages.
     */
    public static boolean isClientError(int code)
    {
        return ((400 <= code) && (code <= 499));
    }

    /**
     * Simple test against an code to determine if it falls into the
     * <code>Server Error</code> message category as defined in the <a
     * href="http://tools.ietf.org/html/rfc1945">RFC 1945 - HTTP/1.0</a>, and <a
     * href="http://tools.ietf.org/html/rfc2616">RFC 2616 - HTTP/1.1</a>.
     *
     * @param code
     *            the code to test.
     * @return true if within range of codes that belongs to
     *         <code>Server Error</code> messages.
     */
    public static boolean isServerError(int code)
    {
        return ((500 <= code) && (code <= 599));
    }
}
 
分享到:
评论

相关推荐

    Jetty权威指南.pdf

    - **ThreadPool**:提供一组预先创建好的线程,当有新的请求到来时,从线程池中取出一个空闲线程来处理请求,处理完成后线程会返回到线程池中等待下一个任务。 **3.5 目录结构** 了解Jetty的目录结构对于管理和...

    Jetty Servlet+ HttpClient 制作的 Java HttpProxy

    创建一个Servlet类,该类将接收HTTP请求并转发到目标URL。以下是一个基本的Servlet示例: ```java public class ProxyServlet extends HttpServlet { protected void service(HttpServletRequest req, ...

    jersey和jetty的restful服务程序

    Jersey是Java平台上的一个开源框架,用于实现 Representational State Transfer (REST) API,而Jetty则是一个轻量级的嵌入式Servlet容器,常被用来托管Web应用程序。 首先,我们需要理解RESTful架构风格。RESTful...

    jetty 8 嵌入web 及Jsoup测试

    Jsoup则是一个用于处理现实世界HTML的Java库,它提供了非常方便的API来解析、遍历以及修改HTML文档。在这篇内容中,我们将深入探讨Jetty 8的嵌入式使用方法以及如何结合Jsoup进行Web测试。 1. **Jetty 8嵌入式Web...

    jetty-admin:码头管理界面

    Jetty 是一个开源的 Java Web 服务器和Servlet容器,它以其轻量级、高效和易于集成的特点而受到开发者的欢迎。Jetty-admin 是一个用于管理 Jetty 服务器的工具,它提供了一个用户友好的界面,使得对服务器的配置、...

    实战HTTP Handler (1) -- 创建一个最简单的HTTP Handler 源码

    本实战将深入探讨如何创建一个最简单的HTTP Handler,理解其工作原理,并通过源码解析来深化对HTTP处理流程的理解。 首先,HTTP Handler是Java Servlet API中的一个接口,名为`javax.servlet.http.HttpServlet`。这...

    基于Java的实例源码-开发的简单WEB服务器源码.zip

    这是一个关于使用Java编程语言开发简单WEB服务器的实例源码包。在Java中,我们可以利用其强大的网络编程功能来实现一个基础的HTTP服务器。这个压缩包可能包含了一个或多个Java源代码文件,这些文件用于处理HTTP请求...

    java--http--web-server.zip_http server java_http服务_java http服务器

    5. **错误处理**:当请求无效或服务器遇到问题时,需要返回合适的HTTP状态码和错误信息。 压缩包中的“web server.doc”文件可能包含了关于该项目的文档说明,包括如何运行、如何测试以及可能遇到的问题和解决方案...

    java源码:一个支持servlet的web服务器.rar

    这通常涉及到ServerSocket类的使用,用于创建一个监听特定端口的服务器套接字。 2. **请求处理线程**:当服务器接收到请求时,它会创建一个新的线程来处理请求,以确保并发性能。这个线程会解析HTTP请求头和请求体...

    java编写的浏览器和服务器

    本项目涉及的是使用Java编写的一个简单的浏览器和服务器,这对于学习Java网络编程的初学者来说是一个很好的实践案例。 首先,我们要理解浏览器和服务器的基本工作原理。浏览器是客户端应用程序,用于向服务器发送...

    jsp错误处理页面的例子

    2. **HTTP状态码处理**: - **404错误**:表示服务器无法找到请求的资源。这可能是因为URL错误、资源已被移除或未正确配置。可以自定义404错误页面,提供友好的错误信息,帮助用户理解问题并提供可能的解决方案。 ...

    java实现http服务器实例

    这个例子中的`SimpleHttpServer`类创建了一个监听8080端口的服务器,并且使用`ClientHandler`类处理每个客户端的请求。`ClientHandler`会读取请求行,打印出来,然后返回一个简单的HTML响应。 在这个实例中,我们仅...

    booking_meeting.rar_http服务端_java http服务器_meeting

    不过,对于学习和理解HTTP工作原理,HTTP 1.0仍然是一个很好的起点。 6. **实现细节**: 编写HTTP服务器通常包括以下几个步骤: - 监听指定端口:使用Java的`ServerSocket`类监听指定的TCP端口。 - 接收请求:当...

    weather.zip

    例如,它可能包含了一个名为`WeatherService`的类,负责与百度天气API交互,以及一个`WeatherController`类,提供对外的REST接口。 为了确保良好的可维护性和测试性,可以遵循以下最佳实践: - 使用依赖注入:通过...

    Http Servlet一个简单的Http应用

    3. **HttpServlet类**:`HttpServlet`是Servlet接口的一个默认实现,它为处理HTTP请求提供了基本框架。开发者通常继承`HttpServlet`,重写`doGet()`和`doPost()`等方法,根据HTTP请求的方法(GET、POST等)来处理...

    java程序相关类

    `HttpServletResponse`则用于设置响应状态码、添加响应头和输出响应体。 3. **Employee类设计**:在员工管理系统中,我们可能有一个名为`Employee`的类,包含员工的基本信息如ID、姓名、职位等。这个类可能是POJO...

    Day09_http协议 & servlet

    HTTP协议是互联网应用层的一个重要协议,负责规定客户端与服务器之间传输的格式和规则,而Servlet则是一个Java编程模型,专门用来扩展服务器功能和处理客户端请求。下面我将详细介绍这两个知识点。 HTTP协议是...

    基于java httpparser实现的一个网络爬虫.zip

    在这个项目中,“基于java httpparser实现的一个网络爬虫”显然利用了HTTPParser库来处理网络通信,提取所需信息。 首先,我们需要理解HTTP协议。HTTP(超文本传输协议)是互联网上应用最广泛的一种协议,用于从...

    基于Javaweb垃圾分类管理系统源码

    在这个垃圾分类管理系统中,JavaWeb主要负责处理用户请求,生成动态网页,并与数据库进行交互。 2. **MySQL数据库**: MySQL是一款流行的开源关系型数据库管理系统,因其高效、稳定和易用性而被广泛应用。在本系统...

    Tomcat服务器报错对应值解析

    这一类状态码表示请求已被服务器接收,但还没有完成处理。 - **100 Continue**: 表示客户端应该继续发送请求的剩余部分。这是最常见的一种情况。 - **101 Switching Protocols**: 表示客户端已经请求服务器切换协议...

Global site tag (gtag.js) - Google Analytics