DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Scaling Site Reliability Engineering (SRE) Teams the Right Way
  • Software Development: Best Practices and Methods
  • Using Render Log Streams to Log to Papertrail

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Scaling Site Reliability Engineering (SRE) Teams the Right Way
  • Software Development: Best Practices and Methods
  • Using Render Log Streams to Log to Papertrail
  1. DZone
  2. Coding
  3. Java
  4. My HTTP Response Java Enumeration Type

My HTTP Response Java Enumeration Type

Peter Pilgrim user avatar by
Peter Pilgrim
·
Nov. 30, 13 · Interview
Like (0)
Save
Tweet
Share
17.57K Views

Join the DZone community and get the full member experience.

Join For Free

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?

Java (programming language)

Published at DZone with permission of Peter Pilgrim, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Scaling Site Reliability Engineering (SRE) Teams the Right Way
  • Software Development: Best Practices and Methods
  • Using Render Log Streams to Log to Papertrail

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: