You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2019/09/16 12:51:07 UTC

[httpcomponents-core] branch master updated: Added statusClass property to StatusLine (#145)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 03b610a  Added statusClass property to StatusLine (#145)
03b610a is described below

commit 03b610acb87245f621e9f517454791da1a0f33b6
Author: Behrang <18...@users.noreply.github.com>
AuthorDate: Mon Sep 16 22:51:03 2019 +1000

    Added statusClass property to StatusLine (#145)
    
    It is common in apps that use HTTP clients to check if a response
    status code is 2xx, 3xx, 4xx, 5xx, etc. and conditionally execute
    code based on that.
    
    Spring's [HttpStatus](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpStatus.html)
    for example, provides these methods:
    
    ```
    public boolean is1xxInformational()
    public boolean is2xxSuccessful()
    public boolean is3xxRedirection()
    public boolean is4xxClientError()
    public boolean is5xxServerError()
    ```
    
    as well as
    
    ```
    public HttpStatus.Series series()
    ```
    
    which returns an instance of [HttpStatus.Series](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/HttpStatus.Series.html).
    
    If I am not mistaken, HTTP Components does not have a similar feature,
    hence the code in this PR.
---
 .../apache/hc/core5/http/message/StatusLine.java   | 112 +++++++++++++++++++--
 1 file changed, 105 insertions(+), 7 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/message/StatusLine.java b/httpcore5/src/main/java/org/apache/hc/core5/http/message/StatusLine.java
index 51c9588..fe6023c 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/message/StatusLine.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/message/StatusLine.java
@@ -46,13 +46,24 @@ public final class StatusLine implements Serializable {
 
     private static final long serialVersionUID = -2443303766890459269L;
 
-    /** The protocol version. */
+    /**
+     * The protocol version.
+     */
     private final ProtocolVersion protoVersion;
 
-    /** The status code. */
+    /**
+     * The status code.
+     */
     private final int statusCode;
 
-    /** The reason phrase. */
+    /**
+     * The status code class.
+     */
+    private final StatusClass statusClass;
+
+    /**
+     * The reason phrase.
+     */
     private final String reasonPhrase;
 
     public StatusLine(final HttpResponse response) {
@@ -60,20 +71,23 @@ public final class StatusLine implements Serializable {
         Args.notNull(response, "Response");
         this.protoVersion = response.getVersion() != null ? response.getVersion() : HttpVersion.HTTP_1_1;
         this.statusCode = response.getCode();
+        this.statusClass = StatusClass.from(this.statusCode);
         this.reasonPhrase = response.getReasonPhrase();
     }
+
     /**
      * Creates a new status line with the given version, status, and reason.
      *
-     * @param version           the protocol version of the response
-     * @param statusCode        the status code of the response
-     * @param reasonPhrase      the reason phrase to the status code, or
-     *                          {@code null}
+     * @param version      the protocol version of the response
+     * @param statusCode   the status code of the response
+     * @param reasonPhrase the reason phrase to the status code, or
+     *                     {@code null}
      */
     public StatusLine(final ProtocolVersion version, final int statusCode,
                       final String reasonPhrase) {
         super();
         this.statusCode = Args.notNegative(statusCode, "Status code");
+        this.statusClass = StatusClass.from(this.statusCode);
         this.protoVersion = version != null ? version : HttpVersion.HTTP_1_1;
         this.reasonPhrase = reasonPhrase;
     }
@@ -82,6 +96,10 @@ public final class StatusLine implements Serializable {
         return this.statusCode;
     }
 
+    public StatusClass getStatusClass() {
+        return this.statusClass;
+    }
+
     public ProtocolVersion getProtocolVersion() {
         return this.protoVersion;
     }
@@ -100,4 +118,84 @@ public final class StatusLine implements Serializable {
         return buf.toString();
     }
 
+    /**
+     * Standard classes of HTTP status codes, plus {@code OTHER} for non-standard codes.
+     *
+     * @see <a href="https://tools.ietf.org/html/rfc2616#section-10">HTTP/1.1, Section 10</a>
+     */
+    public enum StatusClass {
+
+        /**
+         * Informational {@code 1xx} HTTP status codes.
+         *
+         * @see <a href="https://tools.ietf.org/html/rfc2616#section-10.1">HTTP/1.1, Section 10.1</a>
+         */
+        INFORMATIONAL,
+
+        /**
+         * Successful {@code 2xx} HTTP status codes.
+         *
+         * @see <a href="https://tools.ietf.org/html/rfc2616#section-10.2">HTTP/1.1, Section 10.2</a>
+         */
+        SUCCESSFUL,
+
+        /**
+         * Redirection {@code 3xx} HTTP status codes.
+         *
+         * @see <a href="https://tools.ietf.org/html/rfc2616#section-10.3">HTTP/1.1, Section 10.3</a>
+         */
+        REDIRECTION,
+
+        /**
+         * Client Error {@code 4xx} HTTP status codes.
+         *
+         * @see <a href="https://tools.ietf.org/html/rfc2616#section-10.4">HTTP/1.1, Section 10.4</a>
+         */
+        CLIENT_ERROR,
+
+        /**
+         * {@code 5xx} HTTP status codes.
+         *
+         * @see <a href="https://tools.ietf.org/html/rfc2616#section-10.5">HTTP/1.1, Section 10.5</a>
+         */
+        SERVER_ERROR,
+
+        /**
+         * Any other HTTP status codes (e.g. non-standard status codes).
+         */
+        OTHER;
+
+        /**
+         * Gets the response status class for the given status code.
+         *
+         * @param statusCode response status code to get the class for.
+         * @return class of the response status code.
+         */
+        public static StatusClass from(final int statusCode) {
+            final StatusClass statusClass;
+
+            switch (statusCode / 100) {
+                case 1:
+                    statusClass = INFORMATIONAL;
+                    break;
+                case 2:
+                    statusClass = SUCCESSFUL;
+                    break;
+                case 3:
+                    statusClass = REDIRECTION;
+                    break;
+                case 4:
+                    statusClass = CLIENT_ERROR;
+                    break;
+                case 5:
+                    statusClass = SERVER_ERROR;
+                    break;
+                default:
+                    statusClass = OTHER;
+                    break;
+            }
+
+            return statusClass;
+        }
+    }
 }