Add ServerHttpRequest.Builder#header(String, String)
This method allows to set or override easily a specific header value.
This commit is contained in:
parent
712a63205c
commit
a1ae9ac1bd
|
|
@ -17,6 +17,7 @@ package org.springframework.http.server.reactive;
|
|||
|
||||
import java.net.URI;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
|
@ -25,6 +26,7 @@ import org.springframework.web.util.UriComponentsBuilder;
|
|||
* Package private default implementation of {@link ServerHttpRequest.Builder}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
||||
|
|
@ -37,6 +39,8 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
|||
|
||||
private String contextPath;
|
||||
|
||||
private HttpHeaders httpHeaders;
|
||||
|
||||
|
||||
public DefaultServerHttpRequestBuilder(ServerHttpRequest delegate) {
|
||||
Assert.notNull(delegate, "ServerHttpRequest delegate is required.");
|
||||
|
|
@ -62,6 +66,15 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
|||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerHttpRequest.Builder header(String key, String value) {
|
||||
if (this.httpHeaders == null) {
|
||||
this.httpHeaders = new HttpHeaders();
|
||||
}
|
||||
this.httpHeaders.add(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerHttpRequest build() {
|
||||
URI uri = null;
|
||||
|
|
@ -69,7 +82,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
|||
uri = this.delegate.getURI();
|
||||
uri = UriComponentsBuilder.fromUri(uri).replacePath(this.path).build(true).toUri();
|
||||
}
|
||||
return new MutativeDecorator(this.delegate, this.httpMethod, uri, this.contextPath);
|
||||
return new MutativeDecorator(this.delegate, this.httpMethod, uri, this.contextPath, this.httpHeaders);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -85,14 +98,24 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
|||
|
||||
private final String contextPath;
|
||||
|
||||
private final HttpHeaders httpHeaders;
|
||||
|
||||
|
||||
public MutativeDecorator(ServerHttpRequest delegate, HttpMethod httpMethod,
|
||||
URI uri, String contextPath) {
|
||||
URI uri, String contextPath, HttpHeaders httpHeaders) {
|
||||
|
||||
super(delegate);
|
||||
this.httpMethod = httpMethod;
|
||||
this.uri = uri;
|
||||
this.contextPath = contextPath;
|
||||
if (httpHeaders != null) {
|
||||
this.httpHeaders = new HttpHeaders();
|
||||
this.httpHeaders.putAll(super.getHeaders());
|
||||
this.httpHeaders.putAll(httpHeaders);
|
||||
}
|
||||
else {
|
||||
this.httpHeaders = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -110,6 +133,10 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
|||
return (this.contextPath != null ? this.contextPath : super.getContextPath());
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return (this.httpHeaders != null ? this.httpHeaders : super.getHeaders());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,13 +16,7 @@
|
|||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpCookie;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.ReactiveHttpInputMessage;
|
||||
|
|
@ -90,6 +84,11 @@ public interface ServerHttpRequest extends HttpRequest, ReactiveHttpInputMessage
|
|||
*/
|
||||
Builder contextPath(String contextPath);
|
||||
|
||||
/**
|
||||
* Set or override the specified header.
|
||||
*/
|
||||
Builder header(String key, String value);
|
||||
|
||||
/**
|
||||
* Build a {@link ServerHttpRequest} decorator with the mutated properties.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue