Browse Source

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
pull/2017/head
Rossen Stoyanchev 6 years ago
parent
commit
75b1396768
  1. 2
      spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java
  2. 14
      spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpResponse.java
  3. 12
      spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponse.java
  4. 7
      spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpResponse.java
  5. 7
      spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpResponse.java

2
spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpResponse.java

@ -106,7 +106,7 @@ public abstract class AbstractServerHttpResponse implements ServerHttpResponse { @@ -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;
}
/**

14
spring-web/src/main/java/org/springframework/http/server/reactive/ReactorServerHttpResponse.java

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

12
spring-web/src/main/java/org/springframework/http/server/reactive/ServerHttpResponse.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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();

7
spring-web/src/main/java/org/springframework/http/server/reactive/ServletServerHttpResponse.java

@ -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.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 { @@ -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();

7
spring-web/src/main/java/org/springframework/http/server/reactive/UndertowServerHttpResponse.java

@ -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.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 @@ -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() {

Loading…
Cancel
Save