From d8215e7c79d7734da06f724e56d4e6a3d3d04c0d Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 26 Mar 2019 15:31:15 +0100 Subject: [PATCH] Copy cookies and hints in built ServerResponse Closes gh-22481 --- .../server/DefaultEntityResponseBuilder.java | 6 ++--- .../DefaultRenderingResponseBuilder.java | 4 +-- .../server/DefaultServerResponseBuilder.java | 27 +++++++++++++------ .../DefaultServerResponseBuilderTests.java | 12 ++++++--- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java index e026f5cf5c1..2e187462318 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -202,16 +202,14 @@ class DefaultEntityResponseBuilder implements EntityResponse.Builder { private final BodyInserter inserter; - private final Map hints; public DefaultEntityResponse(int statusCode, HttpHeaders headers, MultiValueMap cookies, T entity, BodyInserter inserter, Map hints) { - super(statusCode, headers, cookies); + super(statusCode, headers, cookies, hints); this.entity = entity; this.inserter = inserter; - this.hints = hints; } @Override diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java index ce92f1f8833..a67ae486532 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -168,7 +168,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder public DefaultRenderingResponse(int statusCode, HttpHeaders headers, MultiValueMap cookies, String name, Map model) { - super(statusCode, headers, cookies); + super(statusCode, headers, cookies, Collections.emptyMap()); this.name = name; this.model = Collections.unmodifiableMap(new LinkedHashMap<>(model)); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java index 4b39fcec572..43143ba9d34 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -20,6 +20,7 @@ import java.net.URI; import java.time.Instant; import java.time.ZonedDateTime; import java.util.Arrays; +import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; import java.util.LinkedHashSet; @@ -73,9 +74,16 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { public DefaultServerResponseBuilder(ServerResponse other) { Assert.notNull(other, "ServerResponse must not be null"); - this.statusCode = (other instanceof AbstractServerResponse ? - ((AbstractServerResponse) other).statusCode : other.statusCode().value()); this.headers.addAll(other.headers()); + this.cookies.addAll(other.cookies()); + if (other instanceof AbstractServerResponse) { + AbstractServerResponse abstractOther = (AbstractServerResponse) other; + this.statusCode = abstractOther.statusCode; + this.hints.putAll(abstractOther.hints); + } + else { + this.statusCode = other.statusCode().value(); + } } public DefaultServerResponseBuilder(HttpStatus status) { @@ -289,12 +297,17 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { private final MultiValueMap cookies; + final Map hints; + + protected AbstractServerResponse( - int statusCode, HttpHeaders headers, MultiValueMap cookies) { + int statusCode, HttpHeaders headers, MultiValueMap cookies, + Map hints) { this.statusCode = statusCode; this.headers = HttpHeaders.readOnlyHttpHeaders(headers); this.cookies = CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>(cookies)); + this.hints = hints; } @Override @@ -361,7 +374,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { MultiValueMap cookies, BiFunction> writeFunction) { - super(statusCode, headers, cookies); + super(statusCode, headers, cookies, Collections.emptyMap()); Assert.notNull(writeFunction, "BiFunction must not be null"); this.writeFunction = writeFunction; } @@ -377,16 +390,14 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { private final BodyInserter inserter; - private final Map hints; public BodyInserterResponse(int statusCode, HttpHeaders headers, MultiValueMap cookies, BodyInserter body, Map hints) { - super(statusCode, headers, cookies); + super(statusCode, headers, cookies, hints); Assert.notNull(body, "BodyInserter must not be null"); this.inserter = body; - this.hints = hints; } @Override diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java index eede025f952..44a5905e268 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -65,11 +65,17 @@ public class DefaultServerResponseBuilderTests { @Test public void from() { - ServerResponse other = ServerResponse.ok().header("foo", "bar").build().block(); + ResponseCookie cookie = ResponseCookie.from("foo", "bar").build(); + ServerResponse other = ServerResponse.ok().header("foo", "bar") + .cookie(cookie) + .hint("foo", "bar") + .build().block(); + Mono result = ServerResponse.from(other).build(); StepVerifier.create(result) .expectNextMatches(response -> HttpStatus.OK.equals(response.statusCode()) && - "bar".equals(response.headers().getFirst("foo"))) + "bar".equals(response.headers().getFirst("foo")) && + cookie.equals(response.cookies().getFirst("foo"))) .expectComplete() .verify(); }