Skip to content

Commit 547342b

Browse files
committed
Status code as Integer methods in ServerHttpResponse
Closes gh-24400
1 parent cc4261c commit 547342b

File tree

8 files changed

+108
-53
lines changed

8 files changed

+108
-53
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/HttpHandlerConnector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -135,7 +135,7 @@ private ServerHttpResponse prepareResponse(ServerHttpResponse response, ServerHt
135135
}
136136

137137
private ClientHttpResponse adaptResponse(MockServerHttpResponse response, Flux<DataBuffer> body) {
138-
Integer status = response.getStatusCodeValue();
138+
Integer status = response.getRawStatusCode();
139139
MockClientHttpResponse clientResponse = new MockClientHttpResponse((status != null) ? status : 200);
140140
clientResponse.getHeaders().putAll(response.getHeaders());
141141
clientResponse.getCookies().putAll(response.getCookies());

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,44 @@ public HttpStatus getStatusCode() {
111111
return (this.statusCode != null ? HttpStatus.resolve(this.statusCode) : null);
112112
}
113113

114+
@Override
115+
public boolean setRawStatusCode(@Nullable Integer statusCode) {
116+
if (this.state.get() == State.COMMITTED) {
117+
return false;
118+
}
119+
else {
120+
this.statusCode = statusCode;
121+
return true;
122+
}
123+
}
124+
125+
@Override
126+
@Nullable
127+
public Integer getRawStatusCode() {
128+
return this.statusCode;
129+
}
130+
114131
/**
115132
* Set the HTTP status code of the response.
116133
* @param statusCode the HTTP status as an integer value
117134
* @since 5.0.1
135+
* @deprecated as of 5.2.4 in favor of {@link ServerHttpResponse#setRawStatusCode(Integer)}.
118136
*/
137+
@Deprecated
119138
public void setStatusCodeValue(@Nullable Integer statusCode) {
120-
this.statusCode = statusCode;
139+
if (this.state.get() != State.COMMITTED) {
140+
this.statusCode = statusCode;
141+
}
121142
}
122143

123144
/**
124145
* Return the HTTP status code of the response.
125146
* @return the HTTP status as an integer value
126147
* @since 5.0.1
148+
* @deprecated as of 5.2.4 in favor of {@link ServerHttpResponse#getRawStatusCode()}.
127149
*/
128150
@Nullable
151+
@Deprecated
129152
public Integer getStatusCodeValue() {
130153
return this.statusCode;
131154
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,16 +61,21 @@ public <T> T getNativeResponse() {
6161

6262
@Override
6363
public HttpStatus getStatusCode() {
64-
HttpStatus httpStatus = super.getStatusCode();
65-
return (httpStatus != null ? httpStatus : HttpStatus.resolve(this.response.status().code()));
64+
HttpStatus status = super.getStatusCode();
65+
return (status != null ? status : HttpStatus.resolve(this.response.status().code()));
6666
}
6767

68+
@Override
69+
public Integer getRawStatusCode() {
70+
Integer status = super.getRawStatusCode();
71+
return (status != null ? status : this.response.status().code());
72+
}
6873

6974
@Override
7075
protected void applyStatusCode() {
71-
Integer statusCode = getStatusCodeValue();
72-
if (statusCode != null) {
73-
this.response.status(statusCode);
76+
Integer status = super.getRawStatusCode();
77+
if (status != null) {
78+
this.response.status(status);
7479
}
7580
}
7681

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

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,28 +27,63 @@
2727
*
2828
* @author Arjen Poutsma
2929
* @author Sebastien Deleuze
30+
* @author Rossen Stoyanchev
3031
* @since 5.0
3132
*/
3233
public interface ServerHttpResponse extends ReactiveHttpOutputMessage {
3334

3435
/**
3536
* Set the HTTP status code of the response.
3637
* @param status the HTTP status as an {@link HttpStatus} enum value
37-
* @return {@code false} if the status code has not been set because the
38-
* HTTP response is already committed, {@code true} if successfully set.
38+
* @return {@code false} if the status code change wasn't processed because
39+
* the HTTP response is committed, {@code true} if successfully set.
3940
*/
4041
boolean setStatusCode(@Nullable HttpStatus status);
4142

4243
/**
43-
* Return the status code set via {@link #setStatusCode}, or if the status
44-
* has not been set, return the default status code from the underlying
45-
* server response. The return value may be {@code null} if the status code
46-
* value is outside the {@link HttpStatus} enum range, or if the underlying
47-
* server response does not have a default value.
44+
* Return the status code that has been set, or otherwise fall back on the
45+
* status of the response from the underlying server. The return value may
46+
* be {@code null} if the status code value is outside the
47+
* {@link HttpStatus} enum range, or if there is no default value from the
48+
* underlying server.
4849
*/
4950
@Nullable
5051
HttpStatus getStatusCode();
5152

53+
/**
54+
* Set the HTTP status code to the given value (potentially non-standard and
55+
* not resolvable through the {@link HttpStatus} enum) as an integer.
56+
* @param value the status code value
57+
* @return {@code false} if the status code change wasn't processed because
58+
* the HTTP response is committed, {@code true} if successfully set.
59+
* @since 5.2.4
60+
*/
61+
default boolean setRawStatusCode(@Nullable Integer value) {
62+
if (value == null) {
63+
return setStatusCode(null);
64+
}
65+
else {
66+
HttpStatus httpStatus = HttpStatus.resolve(value);
67+
if (httpStatus == null) {
68+
throw new IllegalStateException(
69+
"Unresolvable HttpStatus for general ServerHttpResponse: " + value);
70+
}
71+
return setStatusCode(httpStatus);
72+
}
73+
}
74+
75+
/**
76+
* Return the status code that has been set, or otherwise fall back on the
77+
* status of the response from the underlying server. The return value may
78+
* be {@code null} if there is no default value from the underlying server.
79+
* @since 5.2.4
80+
*/
81+
@Nullable
82+
default Integer getRawStatusCode() {
83+
HttpStatus httpStatus = getStatusCode();
84+
return (httpStatus != null ? httpStatus.value() : null);
85+
}
86+
5287
/**
5388
* Return a mutable map with the cookies to send to the server.
5489
*/

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -100,15 +100,21 @@ public <T> T getNativeResponse() {
100100

101101
@Override
102102
public HttpStatus getStatusCode() {
103-
HttpStatus httpStatus = super.getStatusCode();
104-
return (httpStatus != null ? httpStatus : HttpStatus.resolve(this.response.getStatus()));
103+
HttpStatus status = super.getStatusCode();
104+
return (status != null ? status : HttpStatus.resolve(this.response.getStatus()));
105+
}
106+
107+
@Override
108+
public Integer getRawStatusCode() {
109+
Integer status = super.getRawStatusCode();
110+
return (status != null ? status : this.response.getStatus());
105111
}
106112

107113
@Override
108114
protected void applyStatusCode() {
109-
Integer statusCode = getStatusCodeValue();
110-
if (statusCode != null) {
111-
this.response.setStatus(statusCode);
115+
Integer status = super.getRawStatusCode();
116+
if (status != null) {
117+
this.response.setStatus(status);
112118
}
113119
}
114120

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -82,16 +82,21 @@ public <T> T getNativeResponse() {
8282

8383
@Override
8484
public HttpStatus getStatusCode() {
85-
HttpStatus httpStatus = super.getStatusCode();
86-
return (httpStatus != null ? httpStatus : HttpStatus.resolve(this.exchange.getStatusCode()));
85+
HttpStatus status = super.getStatusCode();
86+
return (status != null ? status : HttpStatus.resolve(this.exchange.getStatusCode()));
8787
}
8888

89+
@Override
90+
public Integer getRawStatusCode() {
91+
Integer status = super.getRawStatusCode();
92+
return (status != null ? status : this.exchange.getStatusCode());
93+
}
8994

9095
@Override
9196
protected void applyStatusCode() {
92-
Integer statusCode = getStatusCodeValue();
93-
if (statusCode != null) {
94-
this.exchange.setStatusCode(statusCode);
97+
Integer status = super.getRawStatusCode();
98+
if (status != null) {
99+
this.exchange.setStatusCode(status);
95100
}
96101
}
97102

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,7 +45,6 @@
4545
import org.springframework.http.ReactiveHttpOutputMessage;
4646
import org.springframework.http.ResponseCookie;
4747
import org.springframework.http.codec.HttpMessageWriter;
48-
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
4948
import org.springframework.http.server.reactive.ServerHttpRequest;
5049
import org.springframework.http.server.reactive.ServerHttpResponse;
5150
import org.springframework.util.Assert;
@@ -354,17 +353,7 @@ public final Mono<Void> writeTo(ServerWebExchange exchange, Context context) {
354353
}
355354

356355
private void writeStatusAndHeaders(ServerHttpResponse response) {
357-
if (response instanceof AbstractServerHttpResponse) {
358-
((AbstractServerHttpResponse) response).setStatusCodeValue(this.statusCode);
359-
}
360-
else {
361-
HttpStatus status = HttpStatus.resolve(this.statusCode);
362-
if (status == null) {
363-
throw new IllegalStateException(
364-
"Unresolvable HttpStatus for general ServerHttpResponse: " + this.statusCode);
365-
}
366-
response.setStatusCode(status);
367-
}
356+
response.setRawStatusCode(this.statusCode);
368357
copy(this.headers, response.getHeaders());
369358
copy(this.cookies, response.getCookies());
370359
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,8 +33,6 @@
3333
import org.springframework.http.RequestEntity;
3434
import org.springframework.http.ResponseEntity;
3535
import org.springframework.http.codec.HttpMessageWriter;
36-
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
37-
import org.springframework.http.server.reactive.ServerHttpResponse;
3836
import org.springframework.lang.Nullable;
3937
import org.springframework.util.Assert;
4038
import org.springframework.web.reactive.HandlerResult;
@@ -141,14 +139,8 @@ else if (returnValue instanceof HttpHeaders) {
141139
}
142140

143141
if (httpEntity instanceof ResponseEntity) {
144-
ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
145-
ServerHttpResponse response = exchange.getResponse();
146-
if (response instanceof AbstractServerHttpResponse) {
147-
((AbstractServerHttpResponse) response).setStatusCodeValue(responseEntity.getStatusCodeValue());
148-
}
149-
else {
150-
response.setStatusCode(responseEntity.getStatusCode());
151-
}
142+
exchange.getResponse().setRawStatusCode(
143+
((ResponseEntity<?>) httpEntity).getStatusCodeValue());
152144
}
153145

154146
HttpHeaders entityHeaders = httpEntity.getHeaders();

0 commit comments

Comments
 (0)