We're very pleased that you want to get in touch with us. Please fill in the form below:



or   Close this form  
Some content

Peter Pilgrim :: Java Champion :: Digital Developer Architect

I design Java EE and Scala software solutions for the blue-chip clients and private sector

Hey all! Thanks for visiting. I provide fringe benefits to interested readers: checkout consultancy, training or mentorship Please make enquiries by email or call +44 (0)7397 067 658.

Due to the Off-Payroll Working plan for the UK government, I am enforcing stricter measures on contracts. All potential public sector GOV.UK contracts engagements must be approved by QDOS and/or SJD Accounting. Please enquire for further information.

My HTTP Response Java Enumeration Type (Contracting;-)

27 November 2013 4 comments

3 minutes

672

Here is a HTTP Response Java Enumeration type from yesteryear.
In some client’s project, I have seen various constants like this:

final static int OK = 200
final static int OK_STRING = "200"

Which is absolutely awful and these constants are not consistently used in the code base. Worse of all, the code I have refactored will use literal constants.

assertEquals(200, webResponse.getStatusCode() );
assertEquals("404", someOtherResponse.getStatus() );

Even more worse, is the fact these bad coding practices appear in unit test code, if you are lucky to find any testing code at all. (It seems code should just work with no proof of measurement or check of behaviour 😉

So here is my solution for clients:

// LICENSE GPL 3.0 Peter Pilgrim, Xenonique.co.uk, 2012
// http://www.gnu.org/licenses/gpl.html GNU PUBLIC LICENSE 3.0
package uk.co.xenonique.http.net;

/**
 * Implementation type HttpStatusCode
 *
 * (Only codes 400 to 417 and codes 500 to 524 implemented)
 *
 * @see "http://en.wikipedia.org/wiki/List_of_HTTP_status_codes"
 */
public enum HttpStatusCode {
    CONTINUE(100, "Continue"),
    SWITCHING_PROTOCOL(101, "Switching Protocols"),
    PROCESSING(102, "Processing"),

    OK(200, "OK"),
    CREATED(201, "Created"),
    ACCEPTED(202, "Accepted"),
    NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information"),
    NO_CONTENT(204,  "No Content"),
    RESET_CONTENT(205, "Reset Content"),
    PARTIAL_CONTENT(206, "Partial Content"),
    MULTI_STATUS(207, "Multi-Status (WebDAV; RFC 4918"),
    ALREADY_REPORTED(208, "Already Reported (WebDAV; RFC 5842)" ),
    IM_USED(226, "IM Used (RFC 3229)"),

    MULTIPLE_CHOICES(300, "Multiple Choices"),
    MOVED_PERMANENTLY(301, "Moved Permanently"),
    FOUND(302, "Found"),
    SEE_OTHER(303, "See Other (since HTTP/1.1)"),
    NOT_MODIFIED(304, "Not Modified"),
    USE_PROXY(305, "Use Proxy (since HTTP/1.1)"),
    SWITCH_PROXY(306, "Switch Proxy"),
    TEMPORARY_REDIRECT(307, "Temporary Redirect (since HTTP/1.1)"),
    PERMANENT_REDIRECT(308, "Permanent Redirect (approved as experimental RFC)[12]"),

    BAD_REQUEST(400, "Bad Request"),
    UNAUTHORIZED(401, "Unauthorized"),
    PAYMENT_REQUIRED(402, "Payment Required"),
    FORBIDDEN(403, "Forbidden"),
    NOT_FOUND(404, "Not Found"),
    METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
    NOT_ACCEPTABLE(406, "Not Acceptable"),
    PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"),
    REQUEST_TIMEOUT(408, "Request Timeout"),
    CONFLICT(409, "Conflict"),
    GONE(410, "Gone"),
    LENGTH_REQUIRED(411, "Length Required"),
    PRECONDITION_FAILED(412, "Precondition Failed"),
    REQUEST_ENTITY_TOO_LARGE(413, "Request Entity Too Large"),
    REQUEST_URI_TOO_LONG(414, "Request-URI Too Long"),
    UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
    REQUESTED_RANGE_NOT_SATISFIABLE(416, "Requested Range Not Satisfiable"),
    EXPECTATION_FAILED(417, "Expectation Failed"),

    INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
    NOT_IMPLEMENTED(501, "Not Implemented"),
    BAD_GATEWAY(502, "Bad Gateway"),
    SERVICE_UNAVAILABLE(503, "Service Unavailable"),
    GATEWAY_TIMEOUT(504, "Gateway Timeout"),
    HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version Not Supported"),
    VARIANT_ALSO_NEGOTIATES(506, "Variant Also Negotiates (RFC 2295)"),
    INSUFFICIENT_STORAGE(507, "Insufficient Storage (WebDAV; RFC 4918)"),
    LOOP_DETECTED(508, "Loop Detected (WebDAV; RFC 5842)"),
    BANDWIDTH_LIMIT_EXCEEDED(509, "Bandwidth Limit Exceeded (Apache bw/limited extension)"),
    NOT_EXTEND(510, "Not Extended (RFC 2774)"),
    NETWORK_AUTHENTICATION_REQUIRED(511, "Network Authentication Required (RFC 6585)"),
    CONNECTION_TIMED_OUT(522, "Connection timed out"),
    PROXY_DECLINED_REQUEST(523, "Proxy Declined Request"),
    TIMEOUT_OCCURRED(524, "A timeout occurred")
    ;

    private int code;
    private String desc;
    private String text;

    HttpStatusCode(int code, String desc) {
        this.code = code;
        this.desc = desc;
        this.text = Integer.toString(code);
    }

    /**
     * Gets the HTTP status code
     * @return the status code number
     */
    public int getCode() {
        return code;
    }

    /**
     * Gets the HTTP status code as a text string
     * @return the status code as a text string
     */
    public String asText() {
        return text;
    }

    /**
     * Get the description
     * @return the description of the status code
     */
    public String getDesc() {
        return desc;
    }

}

On the other hand, if you work with JAX-RS (Jersey or RestEasy) in Java EE, you get these constants for free!

assertEquals(HttpStatusCode.getCode(), webResponse.getStatusCode() );
assertEquals(HttpStatusCode.asText(), someOtherResponse.getStatus() );

Please tell me, as a Java enterprise engineer, what you would prefer?

+PP+

4 Comments

  1. You’re missing 418 I’m a teapot :)… http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/http/HttpStatus.html

    Comment by Oliver Gierke — 27 November 2013 @ 11:56 am

  2. +1 for the teapot!

    Comment by Jacek Jackowiak — 30 November 2013 @ 12:47 pm

  3. While your choice of an enumeration is a good one, its worth noting that if they just want a standard definition of the http status codes even without using JAX-RS the HttpServletResponse class provides definition for the common ones.

    http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html

    Comment by Richard_Warburton — 01 December 2013 @ 11:30 pm

  4. You are absolutely correct there. The original constants were created pre Java SE 5, therefore they used integer constants instead of Enum. Maybe Java EE 8 or better will fix this one day.

    Because of Java frameworks like Play and others, maybe javax.servlet.* and javax.servlet.http.* has had it’s day or needs a rethink to direct REST principles.

    Comment by Peter Pilgrim — 02 December 2013 @ 2:55 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Contents of this blog entry are under copyright © 2017 by Peter Pilgrim and associates. For enquiries after republishing, please contact us for permission. All requests for syndicated content will be ignored /dev/null, consider yourself warned!

I help to design, create and build JVM components and services that are behind popular e-commerce websites.

My Blurb

Please get in touch , directly, to establish hire availability, contract & consulting opportunities.

Speaking at Your Conference

Contact by invitation

What Peter Does

Contact