Use HttpStatusCode interface

This commit contains changes made because of the introduction of
HttpStatusCode. In general, methods that used to return a HttpStatus
now return HttpStatusCode instead, and methods that returned raw status
codes are now deprecated.

See gh-28214
This commit is contained in:
Arjen Poutsma
2022-03-17 14:21:04 +01:00
parent ca4b6e86a4
commit 28ac0d3883
143 changed files with 1064 additions and 950 deletions

View File

@@ -31,7 +31,7 @@ import org.eclipse.jetty.http.HttpMethod;
import org.springframework.context.Lifecycle;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -151,7 +151,7 @@ public class JettyXhrTransport extends AbstractXhrTransport implements Lifecycle
catch (Exception ex) {
throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
}
HttpStatus status = HttpStatus.valueOf(response.getStatus());
HttpStatusCode status = HttpStatusCode.valueOf(response.getStatus());
HttpHeaders responseHeaders = toHttpHeaders(response.getHeaders());
return (response.getContent() != null ?
new ResponseEntity<>(response.getContentAsString(), responseHeaders, status) :
@@ -213,7 +213,7 @@ public class JettyXhrTransport extends AbstractXhrTransport implements Lifecycle
@Override
public void onBegin(Response response) {
if (response.getStatus() != 200) {
HttpStatus status = HttpStatus.valueOf(response.getStatus());
HttpStatusCode status = HttpStatusCode.valueOf(response.getStatus());
response.abort(new HttpServerErrorException(status, "Unexpected XHR receive status"));
}
}

View File

@@ -26,6 +26,7 @@ import org.springframework.core.task.TaskExecutor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.http.client.ClientHttpRequest;
@@ -39,7 +40,6 @@ import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.UnknownHttpStatusCodeException;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
@@ -156,7 +156,7 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport {
private static final ResponseExtractor<ResponseEntity<String>> textResponseExtractor =
response -> {
String body = StreamUtils.copyToString(response.getBody(), SockJsFrame.CHARSET);
return ResponseEntity.status(response.getRawStatusCode()).headers(response.getHeaders()).body(body);
return ResponseEntity.status(response.getStatusCode()).headers(response.getHeaders()).body(body);
};
@@ -208,11 +208,7 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport {
@Override
public Object extractData(ClientHttpResponse response) throws IOException {
HttpStatus httpStatus = HttpStatus.resolve(response.getRawStatusCode());
if (httpStatus == null) {
throw new UnknownHttpStatusCodeException(
response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), null, null);
}
HttpStatusCode httpStatus = response.getStatusCode();
if (httpStatus != HttpStatus.OK) {
throw new HttpServerErrorException(
httpStatus, response.getStatusText(), response.getHeaders(), null, null);

View File

@@ -49,7 +49,7 @@ import org.xnio.channels.StreamSinkChannel;
import org.xnio.channels.StreamSourceChannel;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -190,7 +190,7 @@ public class UndertowXhrTransport extends AbstractXhrTransport {
public void completed(ClientExchange result) {
ClientResponse response = result.getResponse();
if (response.getResponseCode() != 200) {
HttpStatus status = HttpStatus.valueOf(response.getResponseCode());
HttpStatusCode status = HttpStatusCode.valueOf(response.getResponseCode());
IoUtils.safeClose(result.getConnection());
onFailure(new HttpServerErrorException(status, "Unexpected XHR receive status"));
}
@@ -286,7 +286,7 @@ public class UndertowXhrTransport extends AbstractXhrTransport {
latch.await();
ClientResponse response = responses.iterator().next();
HttpStatus status = HttpStatus.valueOf(response.getResponseCode());
HttpStatusCode status = HttpStatusCode.valueOf(response.getResponseCode());
HttpHeaders responseHeaders = toHttpHeaders(response.getResponseHeaders());
String responseBody = response.getAttachment(RESPONSE_BODY);
return (responseBody != null ?

View File

@@ -33,6 +33,7 @@ import org.springframework.context.Lifecycle;
import org.springframework.core.log.LogFormatUtils;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpResponse;
@@ -316,9 +317,9 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem
transportHandler.handleRequest(request, response, handler, session);
if (isNewSession && (response instanceof ServletServerHttpResponse)) {
int status = ((ServletServerHttpResponse) response).getServletResponse().getStatus();
if (HttpStatus.valueOf(status).is4xxClientError()) {
if (isNewSession && response instanceof ServletServerHttpResponse servletResponse) {
int status = servletResponse.getServletResponse().getStatus();
if (HttpStatusCode.valueOf(status).is4xxClientError()) {
this.sessions.remove(sessionId);
}
}

View File

@@ -21,6 +21,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
@@ -104,6 +105,6 @@ public abstract class AbstractHttpReceivingTransportHandler extends AbstractTran
@Nullable
protected abstract String[] readMessages(ServerHttpRequest request) throws IOException;
protected abstract HttpStatus getResponseStatus();
protected abstract HttpStatusCode getResponseStatus();
}

View File

@@ -19,6 +19,7 @@ package org.springframework.web.socket.sockjs.transport.handler;
import java.io.IOException;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.web.socket.sockjs.transport.TransportHandler;
@@ -44,7 +45,7 @@ public class XhrReceivingTransportHandler extends AbstractHttpReceivingTransport
}
@Override
protected HttpStatus getResponseStatus() {
protected HttpStatusCode getResponseStatus() {
return HttpStatus.NO_CONTENT;
}

View File

@@ -190,7 +190,7 @@ public class RestTemplateXhrTransportTests {
private ClientHttpResponse response(HttpStatus status, String body) throws IOException {
ClientHttpResponse response = mock(ClientHttpResponse.class);
InputStream inputStream = getInputStream(body);
given(response.getRawStatusCode()).willReturn(status.value());
given(response.getStatusCode()).willReturn(status);
given(response.getBody()).willReturn(inputStream);
return response;
}