Spring程序员Java 杂谈

Spring Http源码

2018-01-27  本文已影响22人  紫霞等了至尊宝五百年

/*

package org.springframework.http;

/**

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.
 * <p>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);
    }
}

}

上一篇 下一篇

猜你喜欢

热点阅读