Skip to content

Commit

Permalink
Fall back on default server response status code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rstoyanchev committed Nov 13, 2018
1 parent 4182935 commit 75b1396
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public boolean setStatusCode(@Nullable HttpStatus status) {
@Override
@Nullable
public HttpStatus getStatusCode() {
return (this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null);
return this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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;
Expand Down Expand Up @@ -60,12 +61,23 @@ public <T> T getNativeResponse() {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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;
Expand Down Expand Up @@ -96,6 +97,12 @@ public <T> T getNativeResponse() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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;
Expand Down Expand Up @@ -80,6 +81,12 @@ public <T> T getNativeResponse() {
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() {
Expand Down

0 comments on commit 75b1396

Please sign in to comment.