Skip to content

Commit 807cf5e

Browse files
committed
Add Consumer methods to HttpRequestValues.Builder
Closes: gh-34870
1 parent 3b5acc2 commit 807cf5e

File tree

1 file changed

+60
-7
lines changed

1 file changed

+60
-7
lines changed

spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.LinkedHashMap;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.function.Consumer;
2627

2728
import org.jspecify.annotations.Nullable;
2829

@@ -69,7 +70,7 @@ public class HttpRequestValues {
6970

7071
private final MultiValueMap<String, String> cookies;
7172

72-
private @Nullable Object version;
73+
private final @Nullable Object version;
7374

7475
private final Map<String, Object> attributes;
7576

@@ -353,6 +354,16 @@ public Builder addHeader(String headerName, String... headerValues) {
353354
return this;
354355
}
355356

357+
/**
358+
* Provide access to every header configured so far with the option to
359+
* add, replace, or remove values.
360+
* @since 7.0
361+
*/
362+
public Builder configureHeaders(Consumer<HttpHeaders> consumer) {
363+
consumer.accept(initHeaders());
364+
return this;
365+
}
366+
356367
private HttpHeaders initHeaders() {
357368
this.headers = (this.headers != null ? this.headers : new HttpHeaders());
358369
return this.headers;
@@ -362,13 +373,27 @@ private HttpHeaders initHeaders() {
362373
* Add the given cookie name and values.
363374
*/
364375
public Builder addCookie(String name, String... values) {
365-
this.cookies = (this.cookies != null ? this.cookies : new LinkedMultiValueMap<>());
366376
for (String value : values) {
367-
this.cookies.add(name, value);
377+
initCookies().add(name, value);
368378
}
369379
return this;
370380
}
371381

382+
/**
383+
* Provide access to every cookie configured so far with the option to
384+
* add, replace, or remove values.
385+
* @since 7.0
386+
*/
387+
public Builder configureCookies(Consumer<MultiValueMap<String, String>> consumer) {
388+
consumer.accept(initCookies());
389+
return this;
390+
}
391+
392+
private MultiValueMap<String, String> initCookies() {
393+
this.cookies = (this.cookies != null ? this.cookies : new LinkedMultiValueMap<>());
394+
return this.cookies;
395+
}
396+
372397
/**
373398
* Add the given request parameter name and values.
374399
* <p>When {@code "content-type"} is set to
@@ -377,13 +402,27 @@ public Builder addCookie(String name, String... values) {
377402
* parameters.
378403
*/
379404
public Builder addRequestParameter(String name, String... values) {
380-
this.requestParams = (this.requestParams != null ? this.requestParams : new LinkedMultiValueMap<>());
381405
for (String value : values) {
382-
this.requestParams.add(name, value);
406+
initRequestParams().add(name, value);
383407
}
384408
return this;
385409
}
386410

411+
/**
412+
* Provide access to every request parameter configured so far with the
413+
* option to add, replace, or remove values.
414+
* @since 7.0
415+
*/
416+
public Builder configureRequestParams(Consumer<MultiValueMap<String, String>> consumer) {
417+
consumer.accept(initRequestParams());
418+
return this;
419+
}
420+
421+
private MultiValueMap<String, String> initRequestParams() {
422+
this.requestParams = (this.requestParams != null ? this.requestParams : new LinkedMultiValueMap<>());
423+
return this.requestParams;
424+
}
425+
387426
/**
388427
* Add a part for a multipart request. The part may be:
389428
* <ul>
@@ -420,11 +459,25 @@ public Builder setApiVersion(Object version) {
420459
* @param value the attribute value
421460
*/
422461
public Builder addAttribute(String name, Object value) {
423-
this.attributes = (this.attributes != null ? this.attributes : new HashMap<>());
424-
this.attributes.put(name, value);
462+
initAttributes().put(name, value);
425463
return this;
426464
}
427465

466+
/**
467+
* Provide access to every attribute configured so far with the option
468+
* to add, replace, or remove values.
469+
* @since 7.0
470+
*/
471+
public Builder configureAttributes(Consumer<Map<String, Object>> consumer) {
472+
consumer.accept(initAttributes());
473+
return this;
474+
}
475+
476+
private Map<String, Object> initAttributes() {
477+
this.attributes = (this.attributes != null ? this.attributes : new HashMap<>());
478+
return this.attributes;
479+
}
480+
428481
/**
429482
* Set the request body as an Object to be serialized.
430483
*/

0 commit comments

Comments
 (0)