Add Consumer methods to HttpRequestValues.Builder

Closes: gh-34870
This commit is contained in:
rstoyanchev
2025-05-14 15:15:29 +01:00
parent 3b5acc270f
commit 807cf5e254

View File

@@ -23,6 +23,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.jspecify.annotations.Nullable;
@@ -69,7 +70,7 @@ public class HttpRequestValues {
private final MultiValueMap<String, String> cookies;
private @Nullable Object version;
private final @Nullable Object version;
private final Map<String, Object> attributes;
@@ -353,6 +354,16 @@ public class HttpRequestValues {
return this;
}
/**
* Provide access to every header configured so far with the option to
* add, replace, or remove values.
* @since 7.0
*/
public Builder configureHeaders(Consumer<HttpHeaders> consumer) {
consumer.accept(initHeaders());
return this;
}
private HttpHeaders initHeaders() {
this.headers = (this.headers != null ? this.headers : new HttpHeaders());
return this.headers;
@@ -362,13 +373,27 @@ public class HttpRequestValues {
* Add the given cookie name and values.
*/
public Builder addCookie(String name, String... values) {
this.cookies = (this.cookies != null ? this.cookies : new LinkedMultiValueMap<>());
for (String value : values) {
this.cookies.add(name, value);
initCookies().add(name, value);
}
return this;
}
/**
* Provide access to every cookie configured so far with the option to
* add, replace, or remove values.
* @since 7.0
*/
public Builder configureCookies(Consumer<MultiValueMap<String, String>> consumer) {
consumer.accept(initCookies());
return this;
}
private MultiValueMap<String, String> initCookies() {
this.cookies = (this.cookies != null ? this.cookies : new LinkedMultiValueMap<>());
return this.cookies;
}
/**
* Add the given request parameter name and values.
* <p>When {@code "content-type"} is set to
@@ -377,13 +402,27 @@ public class HttpRequestValues {
* parameters.
*/
public Builder addRequestParameter(String name, String... values) {
this.requestParams = (this.requestParams != null ? this.requestParams : new LinkedMultiValueMap<>());
for (String value : values) {
this.requestParams.add(name, value);
initRequestParams().add(name, value);
}
return this;
}
/**
* Provide access to every request parameter configured so far with the
* option to add, replace, or remove values.
* @since 7.0
*/
public Builder configureRequestParams(Consumer<MultiValueMap<String, String>> consumer) {
consumer.accept(initRequestParams());
return this;
}
private MultiValueMap<String, String> initRequestParams() {
this.requestParams = (this.requestParams != null ? this.requestParams : new LinkedMultiValueMap<>());
return this.requestParams;
}
/**
* Add a part for a multipart request. The part may be:
* <ul>
@@ -420,11 +459,25 @@ public class HttpRequestValues {
* @param value the attribute value
*/
public Builder addAttribute(String name, Object value) {
this.attributes = (this.attributes != null ? this.attributes : new HashMap<>());
this.attributes.put(name, value);
initAttributes().put(name, value);
return this;
}
/**
* Provide access to every attribute configured so far with the option
* to add, replace, or remove values.
* @since 7.0
*/
public Builder configureAttributes(Consumer<Map<String, Object>> consumer) {
consumer.accept(initAttributes());
return this;
}
private Map<String, Object> initAttributes() {
this.attributes = (this.attributes != null ? this.attributes : new HashMap<>());
return this.attributes;
}
/**
* Set the request body as an Object to be serialized.
*/