Skip to content

Support headers with multiple values in ResponseStatusException #24261

Closed
@KanyCTa

Description

@KanyCTa

Affects: Spring-web 5.1.11 & 5.2.1

Spring-web got in version 5.1.11 very useful feature to transfer headers from ResponseExceptions to ResponseStatusExceptionHandler (first mentioned in #23741 and implements in 614c7b0).

Faced with problem: new mechanism can't transfer multivalues headers like

WWW-Authenticate: Negotiate, NTLM

in raw-style (in a Map<String, List<String>> in this case).
ResponseStatusException.getHeaders returns simple java.util.Map.
Nowadays I can transfer multivalues only by converting to delimeted string (for example) and from it in exception handler. Looks lile a bycicle :(

Listing in org.springframework.web.server.ResponseStatusException

/**
 * Return response headers associated with the exception, possibly required
 * for the given status code (e.g. "Allow", "Accept").
 * @since 5.1.11
 */
public Map<String, String> getHeaders() {
    return Collections.emptyMap();
}

Listing in org.springframework.web.server.handler.ResponseStatusExceptionHandler

private boolean updateResponse(ServerHttpResponse response, Throwable ex) {
    boolean result = false;
    HttpStatus status = determineStatus(ex);
    if (status != null) {
        if (response.setStatusCode(status)) {
            if (ex instanceof ResponseStatusException) {
                ((ResponseStatusException) ex).getHeaders()
                        .forEach((name, value) -> response.getHeaders().add(name, value));
            }
            result = true;
        }
    }
    else {
        Throwable cause = ex.getCause();
        if (cause != null) {
            result = updateResponse(response, cause);
        }
    }
    return result;
}

Note, that ServerHttpResponse.getHeaders() returns HttpHeaders that implements MultiValueMap<String, String>

Are any restrictions here for using MultiValueMap in ResponseStatusException.getHeaders() instead of Map?

Seems better to have more flexible mech

Activity

changed the title [-]Fully support of HttpHeaders in ResponseStatusException[/-] [+]Support "honest" HttpHeaders in ResponseStatusException[/+] on Dec 24, 2019
self-assigned this
on Jan 2, 2020
added
in: webIssues in web modules (web, webmvc, webflux, websocket)
and removed on Jan 2, 2020
added this to the 5.2.3 milestone on Jan 2, 2020
changed the title [-]Support "honest" HttpHeaders in ResponseStatusException[/-] [+]Support multiple values response headers via ResponseStatusException[/+] on Jan 2, 2020
changed the title [-]Support multiple values response headers via ResponseStatusException[/-] [+]Support headers with multiple values in ResponseStatusException[/+] on Jan 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @rstoyanchev@KanyCTa@spring-projects-issues

      Issue actions

        Support headers with multiple values in ResponseStatusException · Issue #24261 · spring-projects/spring-framework