Fall back on default server response status code

Update the ServerHttpRespnose contract to indicate that server specific
sub-classes should fall back on the default status, if a status code
has not been set explicitly.

Issue: SPR-17368
This commit is contained in:
Rossen Stoyanchev
2018-11-13 14:10:48 -05:00
parent 4182935b7a
commit 75b1396768
5 changed files with 36 additions and 6 deletions

View File

@@ -106,7 +106,7 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
@Override
@Nullable
public HttpStatus getStatusCode() {
return (this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null);
return this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null;
}
/**

View File

@@ -31,6 +31,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.util.Assert;
@@ -60,12 +61,23 @@ class ReactorServerHttpResponse extends AbstractServerHttpResponse implements Ze
return (T) this.response;
}
@Override
@SuppressWarnings("ConstantConditions")
public HttpStatus getStatusCode() {
HttpStatus httpStatus = super.getStatusCode();
if (httpStatus == null) {
HttpResponseStatus status = this.response.status();
httpStatus = status != null ? HttpStatus.resolve(status.code()) : null;
}
return httpStatus;
}
@Override
protected void applyStatusCode() {
Integer statusCode = getStatusCodeValue();
if (statusCode != null) {
this.response.status(HttpResponseStatus.valueOf(statusCode));
this.response.status(statusCode);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -34,13 +34,17 @@ public interface ServerHttpResponse extends ReactiveHttpOutputMessage {
/**
* Set the HTTP status code of the response.
* @param status the HTTP status as an {@link HttpStatus} enum value
* @return {@code false} if the status code has not been set because the HTTP response
* is already committed, {@code true} if it has been set correctly.
* @return {@code false} if the status code has not been set because the
* HTTP response is already committed, {@code true} if successfully set.
*/
boolean setStatusCode(@Nullable HttpStatus status);
/**
* Return the HTTP status code or {@code null} if not set.
* Return the status code set via {@link #setStatusCode}, or if the status
* has not been set, return the default status code from the underlying
* server response. The return value may be {@code null} if the status code
* value is outside the {@link HttpStatus} enum range, or if the underlying
* server response does not have a default value.
*/
@Nullable
HttpStatus getStatusCode();

View File

@@ -34,6 +34,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable;
@@ -96,6 +97,12 @@ class ServletServerHttpResponse extends AbstractListenerServerHttpResponse {
return (T) this.response;
}
@Override
public HttpStatus getStatusCode() {
HttpStatus httpStatus = super.getStatusCode();
return httpStatus != null ? httpStatus : HttpStatus.resolve(this.response.getStatus());
}
@Override
protected void applyStatusCode() {
Integer statusCode = getStatusCodeValue();

View File

@@ -35,6 +35,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.lang.Nullable;
@@ -80,6 +81,12 @@ class UndertowServerHttpResponse extends AbstractListenerServerHttpResponse impl
return (T) this.exchange;
}
@Override
public HttpStatus getStatusCode() {
HttpStatus httpStatus = super.getStatusCode();
return httpStatus != null ? httpStatus : HttpStatus.resolve(this.exchange.getStatusCode());
}
@Override
protected void applyStatusCode() {