Skip to content

Latest commit

 

History

History
541 lines (502 loc) · 18 KB

File metadata and controls

541 lines (502 loc) · 18 KB

/*

  • Copyright 2002-2017 the original author or authors.
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  •  http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License. */

package org.springframework.http;

/**

  • Enumeration of HTTP status codes.

  • The HTTP status code series can be retrieved via {@link #series()}.

  • @author Arjen Poutsma

  • @author Sebastien Deleuze

  • @author Brian Clozel

  • @since 3.0

  • @see HttpStatus.Series

  • @see HTTP Status Code Registry

  • @see List of HTTP status codes - Wikipedia */ public enum HttpStatus {

    // 1xx Informational

    /**

    // 2xx Success

    /**

    // 3xx Redirection

    /**

    // --- 4xx Client Error ---

    /**

    // --- 5xx Server Error ---

    /**

    private final int value;

    private final String reasonPhrase;

    HttpStatus(int value, String reasonPhrase) { this.value = value; this.reasonPhrase = reasonPhrase; }

    /**

    • Return the integer value of this status code. */ public int value() { return this.value; }

    /**

    • Return the reason phrase of this status code. */ public String getReasonPhrase() { return this.reasonPhrase; }

    /**

    • Whether this status code is in the HTTP series
    • {@link org.springframework.http.HttpStatus.Series#INFORMATIONAL}.
    • This is a shortcut for checking the value of {@link #series()}. */ public boolean is1xxInformational() { return Series.INFORMATIONAL.equals(series()); }

    /**

    • Whether this status code is in the HTTP series
    • {@link org.springframework.http.HttpStatus.Series#SUCCESSFUL}.
    • This is a shortcut for checking the value of {@link #series()}. */ public boolean is2xxSuccessful() { return Series.SUCCESSFUL.equals(series()); }

    /**

    • Whether this status code is in the HTTP series
    • {@link org.springframework.http.HttpStatus.Series#REDIRECTION}.
    • This is a shortcut for checking the value of {@link #series()}. */ public boolean is3xxRedirection() { return Series.REDIRECTION.equals(series()); }

    /**

    • Whether this status code is in the HTTP series
    • {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}.
    • This is a shortcut for checking the value of {@link #series()}. */ public boolean is4xxClientError() { return Series.CLIENT_ERROR.equals(series()); }

    /**

    • Whether this status code is in the HTTP series
    • {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}.
    • This is a shortcut for checking the value of {@link #series()}. */ public boolean is5xxServerError() { return Series.SERVER_ERROR.equals(series()); }

    /**

    • Returns the HTTP status series of this status code.
    • @see HttpStatus.Series */ public Series series() { return Series.valueOf(this); }

    /**

    • Return a string representation of this status code. */ @Override public String toString() { return Integer.toString(this.value); }

    /**

    • Return the enum constant of this type with the specified numeric value.
    • @param statusCode the numeric value of the enum to be returned
    • @return the enum constant with the specified numeric value
    • @throws IllegalArgumentException if this enum has no constant for the specified numeric value */ public static HttpStatus valueOf(int statusCode) { for (HttpStatus status : values()) { if (status.value == statusCode) { return status; } } throw new IllegalArgumentException("No matching constant for [" + statusCode + "]"); }

    /**

    • Enumeration of HTTP status series.
    • Retrievable via {@link HttpStatus#series()}.

    */ public enum Series {

     INFORMATIONAL(1),
     SUCCESSFUL(2),
     REDIRECTION(3),
     CLIENT_ERROR(4),
     SERVER_ERROR(5);
    
     private final int value;
    
     Series(int value) {
     	this.value = value;
     }
    
     /**
      * Return the integer value of this status series. Ranges from 1 to 5.
      */
     public int value() {
     	return this.value;
     }
    
     public static Series valueOf(int status) {
     	int seriesCode = status / 100;
     	for (Series series : values()) {
     		if (series.value == seriesCode) {
     			return series;
     		}
     	}
     	throw new IllegalArgumentException("No matching constant for [" + status + "]");
     }
    
     public static Series valueOf(HttpStatus status) {
     	return valueOf(status.value);
     }
    

    }

}