Skip to content

Commit 1e03b30

Browse files
committed
Use existing context path in DefaultServerRequestBuilder
Closes gh-28820
1 parent 7df149c commit 1e03b30

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -75,6 +75,9 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
7575

7676
private URI uri;
7777

78+
@Nullable
79+
private String contextPath;
80+
7881
private final HttpHeaders headers = new HttpHeaders();
7982

8083
private final MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
@@ -90,6 +93,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
9093
this.exchange = other.exchange();
9194
this.methodName = other.methodName();
9295
this.uri = other.uri();
96+
this.contextPath = other.requestPath().contextPath().value();
9397
this.headers.addAll(other.headers().asHttpHeaders());
9498
this.cookies.addAll(other.cookies());
9599
this.attributes.putAll(other.attributes());
@@ -110,6 +114,12 @@ public ServerRequest.Builder uri(URI uri) {
110114
return this;
111115
}
112116

117+
@Override
118+
public ServerRequest.Builder contextPath(@Nullable String contextPath) {
119+
this.contextPath = contextPath;
120+
return this;
121+
}
122+
113123
@Override
114124
public ServerRequest.Builder header(String headerName, String... headerValues) {
115125
for (String headerValue : headerValues) {
@@ -177,7 +187,7 @@ public ServerRequest.Builder attributes(Consumer<Map<String, Object>> attributes
177187
@Override
178188
public ServerRequest build() {
179189
ServerHttpRequest serverHttpRequest = new BuiltServerHttpRequest(this.exchange.getRequest().getId(),
180-
this.methodName, this.uri, this.headers, this.cookies, this.body);
190+
this.methodName, this.uri, this.contextPath, this.headers, this.cookies, this.body);
181191
ServerWebExchange exchange = new DelegatingServerWebExchange(
182192
serverHttpRequest, this.attributes, this.exchange, this.messageReaders);
183193
return new DefaultServerRequest(exchange, this.messageReaders);
@@ -204,13 +214,13 @@ private static class BuiltServerHttpRequest implements ServerHttpRequest {
204214

205215
private final Flux<DataBuffer> body;
206216

207-
public BuiltServerHttpRequest(String id, String method, URI uri, HttpHeaders headers,
208-
MultiValueMap<String, HttpCookie> cookies, Flux<DataBuffer> body) {
217+
public BuiltServerHttpRequest(String id, String method, URI uri, @Nullable String contextPath,
218+
HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies, Flux<DataBuffer> body) {
209219

210220
this.id = id;
211221
this.method = method;
212222
this.uri = uri;
213-
this.path = RequestPath.parse(uri, null);
223+
this.path = RequestPath.parse(uri, contextPath);
214224
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
215225
this.cookies = unmodifiableCopy(cookies);
216226
this.queryParams = parseQueryParams(uri);

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerRequest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -527,6 +527,14 @@ interface Builder {
527527
*/
528528
Builder uri(URI uri);
529529

530+
/**
531+
* Set the context path of the request.
532+
* @param contextPath the new context path
533+
* @return this builder
534+
* @since 5.3.23
535+
*/
536+
Builder contextPath(@Nullable String contextPath);
537+
530538
/**
531539
* Add the given header value(s) under the given name.
532540
* @param headerName the header name

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilderTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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,7 @@
1616

1717
package org.springframework.web.reactive.function.server;
1818

19+
import java.net.URI;
1920
import java.nio.charset.StandardCharsets;
2021

2122
import org.junit.jupiter.api.Test;
@@ -56,8 +57,11 @@ public void from() {
5657
.map(s -> s.getBytes(StandardCharsets.UTF_8))
5758
.map(DefaultDataBufferFactory.sharedInstance::wrap);
5859

60+
URI uri = URI.create("https://example2.com/foo/bar");
5961
ServerRequest result = ServerRequest.from(other)
6062
.method(HttpMethod.HEAD)
63+
.uri(uri)
64+
.contextPath("/foo")
6165
.headers(httpHeaders -> httpHeaders.set("foo", "baar"))
6266
.cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build()))
6367
.attribute("attr2", "value2")
@@ -66,6 +70,9 @@ public void from() {
6670
.build();
6771

6872
assertThat(result.method()).isEqualTo(HttpMethod.HEAD);
73+
assertThat(result.uri()).isEqualTo(uri);
74+
assertThat(result.requestPath().pathWithinApplication().value()).isEqualTo("/bar");
75+
assertThat(result.requestPath().contextPath().value()).isEqualTo("/foo");
6976
assertThat(result.headers().asHttpHeaders()).hasSize(1);
7077
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
7178
assertThat(result.cookies()).hasSize(1);

0 commit comments

Comments
 (0)