Use existing context path in DefaultServerRequestBuilder
Closes gh-28820
This commit is contained in:
parent
7df149c8b8
commit
1e03b30d33
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -75,6 +75,9 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
|
||||||
|
|
||||||
private URI uri;
|
private URI uri;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private String contextPath;
|
||||||
|
|
||||||
private final HttpHeaders headers = new HttpHeaders();
|
private final HttpHeaders headers = new HttpHeaders();
|
||||||
|
|
||||||
private final MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
|
private final MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
|
||||||
|
@ -90,6 +93,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
|
||||||
this.exchange = other.exchange();
|
this.exchange = other.exchange();
|
||||||
this.methodName = other.methodName();
|
this.methodName = other.methodName();
|
||||||
this.uri = other.uri();
|
this.uri = other.uri();
|
||||||
|
this.contextPath = other.requestPath().contextPath().value();
|
||||||
this.headers.addAll(other.headers().asHttpHeaders());
|
this.headers.addAll(other.headers().asHttpHeaders());
|
||||||
this.cookies.addAll(other.cookies());
|
this.cookies.addAll(other.cookies());
|
||||||
this.attributes.putAll(other.attributes());
|
this.attributes.putAll(other.attributes());
|
||||||
|
@ -110,6 +114,12 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServerRequest.Builder contextPath(@Nullable String contextPath) {
|
||||||
|
this.contextPath = contextPath;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServerRequest.Builder header(String headerName, String... headerValues) {
|
public ServerRequest.Builder header(String headerName, String... headerValues) {
|
||||||
for (String headerValue : headerValues) {
|
for (String headerValue : headerValues) {
|
||||||
|
@ -177,7 +187,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
|
||||||
@Override
|
@Override
|
||||||
public ServerRequest build() {
|
public ServerRequest build() {
|
||||||
ServerHttpRequest serverHttpRequest = new BuiltServerHttpRequest(this.exchange.getRequest().getId(),
|
ServerHttpRequest serverHttpRequest = new BuiltServerHttpRequest(this.exchange.getRequest().getId(),
|
||||||
this.methodName, this.uri, this.headers, this.cookies, this.body);
|
this.methodName, this.uri, this.contextPath, this.headers, this.cookies, this.body);
|
||||||
ServerWebExchange exchange = new DelegatingServerWebExchange(
|
ServerWebExchange exchange = new DelegatingServerWebExchange(
|
||||||
serverHttpRequest, this.attributes, this.exchange, this.messageReaders);
|
serverHttpRequest, this.attributes, this.exchange, this.messageReaders);
|
||||||
return new DefaultServerRequest(exchange, this.messageReaders);
|
return new DefaultServerRequest(exchange, this.messageReaders);
|
||||||
|
@ -204,13 +214,13 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
|
||||||
|
|
||||||
private final Flux<DataBuffer> body;
|
private final Flux<DataBuffer> body;
|
||||||
|
|
||||||
public BuiltServerHttpRequest(String id, String method, URI uri, HttpHeaders headers,
|
public BuiltServerHttpRequest(String id, String method, URI uri, @Nullable String contextPath,
|
||||||
MultiValueMap<String, HttpCookie> cookies, Flux<DataBuffer> body) {
|
HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies, Flux<DataBuffer> body) {
|
||||||
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.uri = uri;
|
this.uri = uri;
|
||||||
this.path = RequestPath.parse(uri, null);
|
this.path = RequestPath.parse(uri, contextPath);
|
||||||
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
|
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
|
||||||
this.cookies = unmodifiableCopy(cookies);
|
this.cookies = unmodifiableCopy(cookies);
|
||||||
this.queryParams = parseQueryParams(uri);
|
this.queryParams = parseQueryParams(uri);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -527,6 +527,14 @@ public interface ServerRequest {
|
||||||
*/
|
*/
|
||||||
Builder uri(URI uri);
|
Builder uri(URI uri);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the context path of the request.
|
||||||
|
* @param contextPath the new context path
|
||||||
|
* @return this builder
|
||||||
|
* @since 5.3.23
|
||||||
|
*/
|
||||||
|
Builder contextPath(@Nullable String contextPath);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the given header value(s) under the given name.
|
* Add the given header value(s) under the given name.
|
||||||
* @param headerName the header name
|
* @param headerName the header name
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.web.reactive.function.server;
|
package org.springframework.web.reactive.function.server;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -56,8 +57,11 @@ public class DefaultServerRequestBuilderTests {
|
||||||
.map(s -> s.getBytes(StandardCharsets.UTF_8))
|
.map(s -> s.getBytes(StandardCharsets.UTF_8))
|
||||||
.map(DefaultDataBufferFactory.sharedInstance::wrap);
|
.map(DefaultDataBufferFactory.sharedInstance::wrap);
|
||||||
|
|
||||||
|
URI uri = URI.create("https://example2.com/foo/bar");
|
||||||
ServerRequest result = ServerRequest.from(other)
|
ServerRequest result = ServerRequest.from(other)
|
||||||
.method(HttpMethod.HEAD)
|
.method(HttpMethod.HEAD)
|
||||||
|
.uri(uri)
|
||||||
|
.contextPath("/foo")
|
||||||
.headers(httpHeaders -> httpHeaders.set("foo", "baar"))
|
.headers(httpHeaders -> httpHeaders.set("foo", "baar"))
|
||||||
.cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build()))
|
.cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build()))
|
||||||
.attribute("attr2", "value2")
|
.attribute("attr2", "value2")
|
||||||
|
@ -66,6 +70,9 @@ public class DefaultServerRequestBuilderTests {
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
assertThat(result.method()).isEqualTo(HttpMethod.HEAD);
|
assertThat(result.method()).isEqualTo(HttpMethod.HEAD);
|
||||||
|
assertThat(result.uri()).isEqualTo(uri);
|
||||||
|
assertThat(result.requestPath().pathWithinApplication().value()).isEqualTo("/bar");
|
||||||
|
assertThat(result.requestPath().contextPath().value()).isEqualTo("/foo");
|
||||||
assertThat(result.headers().asHttpHeaders()).hasSize(1);
|
assertThat(result.headers().asHttpHeaders()).hasSize(1);
|
||||||
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
|
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
|
||||||
assertThat(result.cookies()).hasSize(1);
|
assertThat(result.cookies()).hasSize(1);
|
||||||
|
|
Loading…
Reference in New Issue