Skip to content

Commit

Permalink
Headers support in ResponseStatusExceptionResolver
Browse files Browse the repository at this point in the history
Closes gh-24944
  • Loading branch information
rstoyanchev committed Apr 24, 2020
1 parent 8265db0 commit e18901d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
@@ -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 @@ -115,8 +115,10 @@ protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, Http

/**
* Template method that handles an {@link ResponseStatusException}.
* <p>The default implementation delegates to {@link #applyStatusAndReason}
* with the status code and reason from the exception.
* <p>The default implementation applies the headers from
* {@link ResponseStatusException#getResponseHeaders()} and delegates to
* {@link #applyStatusAndReason} with the status code and reason from the
* exception.
* @param ex the exception
* @param request current HTTP request
* @param response current HTTP response
Expand All @@ -128,6 +130,12 @@ protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, Http
protected ModelAndView resolveResponseStatusException(ResponseStatusException ex,
HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws Exception {

ex.getResponseHeaders().forEach((name, values) -> {
if (response.getHeader(name) == null) {
values.forEach(value -> response.addHeader(name, value));
}
});

int statusCode = ex.getStatus().value();
String reason = ex.getReason();
return applyStatusAndReason(statusCode, reason, response);
Expand Down
@@ -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 All @@ -18,6 +18,7 @@

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.Locale;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -28,8 +29,11 @@
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.core.annotation.AliasFor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.server.MethodNotAllowedException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
Expand Down Expand Up @@ -128,6 +132,16 @@ public void responseStatusExceptionWithReason() throws Exception {
assertResolved(mav, 400, "The reason");
}

@Test
void responseStatusExceptionWithHeaders() {
Exception ex = new MethodNotAllowedException(
HttpMethod.GET, Arrays.asList(HttpMethod.POST, HttpMethod.PUT));

ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);

assertResolved(mav, 405, "Request method 'GET' not supported");
assertThat(response.getHeader(HttpHeaders.ALLOW)).isEqualTo("POST,PUT");
}

private void assertResolved(ModelAndView mav, int status, String reason) {
assertThat(mav != null && mav.isEmpty()).as("No Empty ModelAndView returned").isTrue();
Expand Down

0 comments on commit e18901d

Please sign in to comment.