提交 75b13967 编写于 作者: R Rossen Stoyanchev

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
上级 4182935b
...@@ -106,7 +106,7 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse { ...@@ -106,7 +106,7 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse {
@Override @Override
@Nullable @Nullable
public HttpStatus getStatusCode() { public HttpStatus getStatusCode() {
return (this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null); return this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null;
} }
/** /**
......
...@@ -31,6 +31,7 @@ import org.springframework.core.io.buffer.DataBuffer; ...@@ -31,6 +31,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.NettyDataBufferFactory; import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie; import org.springframework.http.ResponseCookie;
import org.springframework.http.ZeroCopyHttpOutputMessage; import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.util.Assert; import org.springframework.util.Assert;
...@@ -60,12 +61,23 @@ class ReactorServerHttpResponse extends AbstractServerHttpResponse implements Ze ...@@ -60,12 +61,23 @@ class ReactorServerHttpResponse extends AbstractServerHttpResponse implements Ze
return (T) this.response; 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 @Override
protected void applyStatusCode() { protected void applyStatusCode() {
Integer statusCode = getStatusCodeValue(); Integer statusCode = getStatusCodeValue();
if (statusCode != null) { if (statusCode != null) {
this.response.status(HttpResponseStatus.valueOf(statusCode)); this.response.status(statusCode);
} }
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -34,13 +34,17 @@ public interface ServerHttpResponse extends ReactiveHttpOutputMessage { ...@@ -34,13 +34,17 @@ public interface ServerHttpResponse extends ReactiveHttpOutputMessage {
/** /**
* Set the HTTP status code of the response. * Set the HTTP status code of the response.
* @param status the HTTP status as an {@link HttpStatus} enum value * @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 * @return {@code false} if the status code has not been set because the
* is already committed, {@code true} if it has been set correctly. * HTTP response is already committed, {@code true} if successfully set.
*/ */
boolean setStatusCode(@Nullable HttpStatus status); 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 @Nullable
HttpStatus getStatusCode(); HttpStatus getStatusCode();
......
...@@ -34,6 +34,7 @@ import org.springframework.core.io.buffer.DataBuffer; ...@@ -34,6 +34,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie; import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
...@@ -96,6 +97,12 @@ class ServletServerHttpResponse extends AbstractListenerServerHttpResponse { ...@@ -96,6 +97,12 @@ class ServletServerHttpResponse extends AbstractListenerServerHttpResponse {
return (T) this.response; return (T) this.response;
} }
@Override
public HttpStatus getStatusCode() {
HttpStatus httpStatus = super.getStatusCode();
return httpStatus != null ? httpStatus : HttpStatus.resolve(this.response.getStatus());
}
@Override @Override
protected void applyStatusCode() { protected void applyStatusCode() {
Integer statusCode = getStatusCodeValue(); Integer statusCode = getStatusCodeValue();
......
...@@ -35,6 +35,7 @@ import org.springframework.core.io.buffer.DataBuffer; ...@@ -35,6 +35,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie; import org.springframework.http.ResponseCookie;
import org.springframework.http.ZeroCopyHttpOutputMessage; import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
...@@ -80,6 +81,12 @@ class UndertowServerHttpResponse extends AbstractListenerServerHttpResponse impl ...@@ -80,6 +81,12 @@ class UndertowServerHttpResponse extends AbstractListenerServerHttpResponse impl
return (T) this.exchange; return (T) this.exchange;
} }
@Override
public HttpStatus getStatusCode() {
HttpStatus httpStatus = super.getStatusCode();
return httpStatus != null ? httpStatus : HttpStatus.resolve(this.exchange.getStatusCode());
}
@Override @Override
protected void applyStatusCode() { protected void applyStatusCode() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册