Skip to content

Commit

Permalink
Status code as Integer methods in ServerHttpResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Acvrock committed Jan 30, 2020
1 parent f13dbc6 commit 0d29448
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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 @@ -135,7 +135,7 @@ private ServerHttpResponse prepareResponse(ServerHttpResponse response, ServerHt
}

private ClientHttpResponse adaptResponse(MockServerHttpResponse response, Flux<DataBuffer> body) {
Integer status = response.getStatusCodeValue();
Integer status = response.getRawStatusCode();
MockClientHttpResponse clientResponse = new MockClientHttpResponse((status != null) ? status : 200);
clientResponse.getHeaders().putAll(response.getHeaders());
clientResponse.getCookies().putAll(response.getCookies());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,44 @@ public HttpStatus getStatusCode() {
return (this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null);
}

@Override
public boolean setRawStatusCode(@Nullable Integer statusCode) {
if (this.state.get() == State.COMMITTED) {
return false;
}
else {
this.statusCode = statusCode;
return true;
}
}

@Override
@Nullable
public Integer getRawStatusCode() {
return this.statusCode;
}

/**
* Set the HTTP status code of the response.
* @param statusCode the HTTP status as an integer value
* @since 5.0.1
* @deprecated as of 5.2.4 in favor of {@link ServerHttpResponse#setRawStatusCode(Integer)}.
*/
@Deprecated
public void setStatusCodeValue(@Nullable Integer statusCode) {
this.statusCode = statusCode;
if (this.state.get() != State.COMMITTED) {
this.statusCode = statusCode;
}
}

/**
* Return the HTTP status code of the response.
* @return the HTTP status as an integer value
* @since 5.0.1
* @deprecated as of 5.2.4 in favor of {@link ServerHttpResponse#getRawStatusCode()}.
*/
@Nullable
@Deprecated
public Integer getStatusCodeValue() {
return this.statusCode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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 @@ -61,16 +61,21 @@ public <T> T getNativeResponse() {

@Override
public HttpStatus getStatusCode() {
HttpStatus httpStatus = super.getStatusCode();
return (httpStatus != null ? httpStatus : HttpStatus.resolve(this.response.status().code()));
HttpStatus status = super.getStatusCode();
return (status != null ? status : HttpStatus.resolve(this.response.status().code()));
}

@Override
public Integer getRawStatusCode() {
Integer status = super.getRawStatusCode();
return (status != null ? status : this.response.status().code());
}

@Override
protected void applyStatusCode() {
Integer statusCode = getStatusCodeValue();
if (statusCode != null) {
this.response.status(statusCode);
Integer status = super.getRawStatusCode();
if (status != null) {
this.response.status(status);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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 @@ -27,28 +27,63 @@
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @since 5.0
*/
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 successfully set.
* @return {@code false} if the status code change wasn't processed because
* the HTTP response is committed, {@code true} if successfully set.
*/
boolean setStatusCode(@Nullable HttpStatus status);

/**
* 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.
* Return the status code that has been set, or otherwise fall back on the
* status of the response from the underlying server. The return value may
* be {@code null} if the status code value is outside the
* {@link HttpStatus} enum range, or if there is no default value from the
* underlying server.
*/
@Nullable
HttpStatus getStatusCode();

/**
* Set the HTTP status code to the given value (potentially non-standard and
* not resolvable through the {@link HttpStatus} enum) as an integer.
* @param value the status code value
* @return {@code false} if the status code change wasn't processed because
* the HTTP response is committed, {@code true} if successfully set.
* @since 5.2.4
*/
default boolean setRawStatusCode(@Nullable Integer value) {
if (value == null) {
return setStatusCode(null);
}
else {
HttpStatus httpStatus = HttpStatus.resolve(value);
if (httpStatus == null) {
throw new IllegalStateException(
"Unresolvable HttpStatus for general ServerHttpResponse: " + value);
}
return setStatusCode(httpStatus);
}
}

/**
* Return the status code that has been set, or otherwise fall back on the
* status of the response from the underlying server. The return value may
* be {@code null} if there is no default value from the underlying server.
* @since 5.2.4
*/
@Nullable
default Integer getRawStatusCode() {
HttpStatus httpStatus = getStatusCode();
return (httpStatus != null ? httpStatus.value() : null);
}

/**
* Return a mutable map with the cookies to send to the server.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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 @@ -100,15 +100,21 @@ public <T> T getNativeResponse() {

@Override
public HttpStatus getStatusCode() {
HttpStatus httpStatus = super.getStatusCode();
return (httpStatus != null ? httpStatus : HttpStatus.resolve(this.response.getStatus()));
HttpStatus status = super.getStatusCode();
return (status != null ? status : HttpStatus.resolve(this.response.getStatus()));
}

@Override
public Integer getRawStatusCode() {
Integer status = super.getRawStatusCode();
return (status != null ? status : this.response.getStatus());
}

@Override
protected void applyStatusCode() {
Integer statusCode = getStatusCodeValue();
if (statusCode != null) {
this.response.setStatus(statusCode);
Integer status = super.getRawStatusCode();
if (status != null) {
this.response.setStatus(status);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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 @@ -82,16 +82,21 @@ public <T> T getNativeResponse() {

@Override
public HttpStatus getStatusCode() {
HttpStatus httpStatus = super.getStatusCode();
return (httpStatus != null ? httpStatus : HttpStatus.resolve(this.exchange.getStatusCode()));
HttpStatus status = super.getStatusCode();
return (status != null ? status : HttpStatus.resolve(this.exchange.getStatusCode()));
}

@Override
public Integer getRawStatusCode() {
Integer status = super.getRawStatusCode();
return (status != null ? status : this.exchange.getStatusCode());
}

@Override
protected void applyStatusCode() {
Integer statusCode = getStatusCodeValue();
if (statusCode != null) {
this.exchange.setStatusCode(statusCode);
Integer status = super.getRawStatusCode();
if (status != null) {
this.exchange.setStatusCode(status);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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 @@ -45,7 +45,6 @@
import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.http.ResponseCookie;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -354,17 +353,7 @@ public final Mono<Void> writeTo(ServerWebExchange exchange, Context context) {
}

private void writeStatusAndHeaders(ServerHttpResponse response) {
if (response instanceof AbstractServerHttpResponse) {
((AbstractServerHttpResponse) response).setStatusCodeValue(this.statusCode);
}
else {
HttpStatus status = HttpStatus.resolve(this.statusCode);
if (status == null) {
throw new IllegalStateException(
"Unresolvable HttpStatus for general ServerHttpResponse: " + this.statusCode);
}
response.setStatusCode(status);
}
response.setRawStatusCode(this.statusCode);
copy(this.headers, response.getHeaders());
copy(this.cookies, response.getCookies());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2020 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 @@ -33,8 +33,6 @@
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.reactive.HandlerResult;
Expand Down Expand Up @@ -141,14 +139,8 @@ else if (returnValue instanceof HttpHeaders) {
}

if (httpEntity instanceof ResponseEntity) {
ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
ServerHttpResponse response = exchange.getResponse();
if (response instanceof AbstractServerHttpResponse) {
((AbstractServerHttpResponse) response).setStatusCodeValue(responseEntity.getStatusCodeValue());
}
else {
response.setStatusCode(responseEntity.getStatusCode());
}
exchange.getResponse().setRawStatusCode(
((ResponseEntity<?>) httpEntity).getStatusCodeValue());
}

HttpHeaders entityHeaders = httpEntity.getHeaders();
Expand Down

0 comments on commit 0d29448

Please sign in to comment.