Skip to content

Commit 34cfbe5

Browse files
committed
ResponseStatusException associated headers
A ResponseStatus exception now exposes extra method to return headers for the response. This is used in ResponseStatusExceptionHandler to apply the headers to the response. Closes spring-projectsgh-23741
1 parent 0e08e19 commit 34cfbe5

File tree

5 files changed

+82
-11
lines changed

5 files changed

+82
-11
lines changed

spring-web/src/main/java/org/springframework/web/server/MethodNotAllowedException.java

Lines changed: 14 additions & 1 deletion
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-2019 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.
@@ -19,12 +19,14 @@
1919
import java.util.Collection;
2020
import java.util.Collections;
2121
import java.util.HashSet;
22+
import java.util.Map;
2223
import java.util.Set;
2324

2425
import org.springframework.http.HttpMethod;
2526
import org.springframework.http.HttpStatus;
2627
import org.springframework.lang.Nullable;
2728
import org.springframework.util.Assert;
29+
import org.springframework.util.StringUtils;
2830

2931
/**
3032
* Exception for errors that fit response status 405 (method not allowed).
@@ -55,6 +57,16 @@ public MethodNotAllowedException(String method, @Nullable Collection<HttpMethod>
5557
}
5658

5759

60+
/**
61+
* Return a Map with an "Allow" header.
62+
* @since 5.1.11
63+
*/
64+
@Override
65+
public Map<String, String> getHeaders() {
66+
return Collections.singletonMap("Allow",
67+
StringUtils.collectionToDelimitedString(this.supportedMethods, ", "));
68+
}
69+
5870
/**
5971
* Return the HTTP method for the failed request.
6072
*/
@@ -68,4 +80,5 @@ public String getHttpMethod() {
6880
public Set<HttpMethod> getSupportedMethods() {
6981
return this.supportedMethods;
7082
}
83+
7184
}

spring-web/src/main/java/org/springframework/web/server/NotAcceptableStatusException.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Collections;
2020
import java.util.List;
21+
import java.util.Map;
2122

2223
import org.springframework.http.HttpStatus;
2324
import org.springframework.http.MediaType;
@@ -51,6 +52,15 @@ public NotAcceptableStatusException(List<MediaType> supportedMediaTypes) {
5152
}
5253

5354

55+
/**
56+
* Return a Map with an "Accept" header.
57+
* @since 5.1.11
58+
*/
59+
@Override
60+
public Map<String, String> getHeaders() {
61+
return Collections.singletonMap("Accept", MediaType.toString(this.supportedMediaTypes));
62+
}
63+
5464
/**
5565
* Return the list of supported content types in cases when the Accept
5666
* header is parsed but not supported, or an empty list otherwise.

spring-web/src/main/java/org/springframework/web/server/ResponseStatusException.java

Lines changed: 15 additions & 2 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-2019 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.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.web.server;
1818

19+
import java.util.Collections;
20+
import java.util.Map;
21+
1922
import org.springframework.core.NestedExceptionUtils;
2023
import org.springframework.core.NestedRuntimeException;
2124
import org.springframework.http.HttpStatus;
@@ -72,12 +75,21 @@ public ResponseStatusException(HttpStatus status, @Nullable String reason, @Null
7275

7376

7477
/**
75-
* The HTTP status that fits the exception (never {@code null}).
78+
* Return the HTTP status associated with this exception.
7679
*/
7780
public HttpStatus getStatus() {
7881
return this.status;
7982
}
8083

84+
/**
85+
* Return response headers associated with the exception, possibly required
86+
* for the given status code (e.g. "Allow", "Accept").
87+
* @since 5.1.11
88+
*/
89+
public Map<String, String> getHeaders() {
90+
return Collections.emptyMap();
91+
}
92+
8193
/**
8294
* The reason explaining the exception (potentially {@code null} or empty).
8395
*/
@@ -86,6 +98,7 @@ public String getReason() {
8698
return this.reason;
8799
}
88100

101+
89102
@Override
90103
public String getMessage() {
91104
String msg = this.status + (this.reason != null ? " \"" + this.reason + "\"" : "");

spring-web/src/main/java/org/springframework/web/server/handler/ResponseStatusExceptionHandler.java

Lines changed: 17 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-2019 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.
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.http.HttpStatus;
2424
import org.springframework.http.server.reactive.ServerHttpRequest;
25+
import org.springframework.http.server.reactive.ServerHttpResponse;
2526
import org.springframework.lang.Nullable;
2627
import org.springframework.web.server.ResponseStatusException;
2728
import org.springframework.web.server.ServerWebExchange;
@@ -62,8 +63,7 @@ public void setWarnLogCategory(String loggerName) {
6263

6364
@Override
6465
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
65-
HttpStatus status = resolveStatus(ex);
66-
if (status == null || !exchange.getResponse().setStatusCode(status)) {
66+
if (!updateResponse(exchange.getResponse(), ex)) {
6767
return Mono.error(ex);
6868
}
6969

@@ -86,16 +86,25 @@ private String formatError(Throwable ex, ServerHttpRequest request) {
8686
return "Resolved [" + reason + "] for HTTP " + request.getMethod() + " " + path;
8787
}
8888

89-
@Nullable
90-
private HttpStatus resolveStatus(Throwable ex) {
89+
private boolean updateResponse(ServerHttpResponse response, Throwable ex) {
90+
boolean result = false;
9191
HttpStatus status = determineStatus(ex);
92-
if (status == null) {
92+
if (status != null) {
93+
if (response.setStatusCode(status)) {
94+
if (ex instanceof ResponseStatusException) {
95+
((ResponseStatusException) ex).getHeaders()
96+
.forEach((name, value) -> response.getHeaders().add(name, value));
97+
}
98+
result = true;
99+
}
100+
}
101+
else {
93102
Throwable cause = ex.getCause();
94103
if (cause != null) {
95-
status = resolveStatus(cause);
104+
result = updateResponse(response, cause);
96105
}
97106
}
98-
return status;
107+
return result;
99108
}
100109

101110
/**

spring-web/src/test/java/org/springframework/web/server/handler/ResponseStatusExceptionHandlerTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@
1717
package org.springframework.web.server.handler;
1818

1919
import java.time.Duration;
20+
import java.util.Arrays;
2021

2122
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Test;
2324
import reactor.core.publisher.Mono;
2425
import reactor.test.StepVerifier;
2526

27+
import org.springframework.http.HttpMethod;
2628
import org.springframework.http.HttpStatus;
29+
import org.springframework.http.MediaType;
2730
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
31+
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
2832
import org.springframework.mock.web.test.server.MockServerWebExchange;
33+
import org.springframework.web.server.MethodNotAllowedException;
34+
import org.springframework.web.server.NotAcceptableStatusException;
2935
import org.springframework.web.server.ResponseStatusException;
3036

3137
import static org.assertj.core.api.Assertions.assertThat;
@@ -67,6 +73,26 @@ public void handleNestedResponseStatusException() {
6773
assertThat(this.exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
6874
}
6975

76+
@Test // gh-23741
77+
public void handleMethodNotAllowed() {
78+
Throwable ex = new MethodNotAllowedException(HttpMethod.PATCH, Arrays.asList(HttpMethod.POST, HttpMethod.PUT));
79+
this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5));
80+
81+
MockServerHttpResponse response = this.exchange.getResponse();
82+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.METHOD_NOT_ALLOWED);
83+
assertThat(response.getHeaders().getAllow()).containsOnly(HttpMethod.POST, HttpMethod.PUT);
84+
}
85+
86+
@Test // gh-23741
87+
public void handleResponseStatusExceptionWithHeaders() {
88+
Throwable ex = new NotAcceptableStatusException(Arrays.asList(MediaType.TEXT_PLAIN, MediaType.TEXT_HTML));
89+
this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5));
90+
91+
MockServerHttpResponse response = this.exchange.getResponse();
92+
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE);
93+
assertThat(response.getHeaders().getAccept()).containsOnly(MediaType.TEXT_PLAIN, MediaType.TEXT_HTML);
94+
}
95+
7096
@Test
7197
public void unresolvedException() {
7298
Throwable expected = new IllegalStateException();

0 commit comments

Comments
 (0)