|
13 | 13 | import java.net.URI; |
14 | 14 | import java.util.ArrayList; |
15 | 15 | import java.util.List; |
| 16 | +import java.util.Map; |
16 | 17 | import java.util.Optional; |
17 | 18 |
|
18 | 19 | /** |
|
23 | 24 | * request patterns, consider using the convenience factory methods in {@link HttpRequests}. |
24 | 25 | * |
25 | 26 | * <p> |
26 | | - * Headers can be added after construction via {@link #addHeader(String, String)}, which allows |
27 | | - * {@link AuthenticationHandler} implementations to modify requests in-place. |
| 27 | + * Headers can be manipulated after construction via {@link #addHeader(String, String)}, |
| 28 | + * {@link #setHeader(String, String)}, {@link #setHeaders(Map)}, {@link #unsetHeader(String)}, and |
| 29 | + * {@link #unsetHeaders()}, which allows {@link AuthenticationHandler} implementations to modify requests in-place. |
28 | 30 | * |
29 | 31 | * <p> |
30 | 32 | * Example usage: |
@@ -91,6 +93,45 @@ public void addHeader(String name, String value) { |
91 | 93 | headers.add(HttpHeader.of(name, value)); |
92 | 94 | } |
93 | 95 |
|
| 96 | + /** |
| 97 | + * Replaces all existing values for the given header name with a single new value. The comparison is |
| 98 | + * case-insensitive. |
| 99 | + * |
| 100 | + * @param name the header name; must not be {@code null} |
| 101 | + * @param value the new header value; must not be {@code null} |
| 102 | + */ |
| 103 | + public void setHeader(String name, String value) { |
| 104 | + headers.removeIf(h -> h.getName().equalsIgnoreCase(name)); |
| 105 | + headers.add(HttpHeader.of(name, value)); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Replaces all existing headers with the entries from the given map. Each map entry sets a single value for its |
| 110 | + * header name, replacing any previously set values for that name. Header names that are not present in the map are |
| 111 | + * left untouched. |
| 112 | + * |
| 113 | + * @param headerMap a map of header names to values; must not be {@code null} |
| 114 | + */ |
| 115 | + public void setHeaders(Map<String, String> headerMap) { |
| 116 | + headerMap.forEach(this::setHeader); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Removes all headers with the given name. The comparison is case-insensitive. |
| 121 | + * |
| 122 | + * @param name the header name to remove; must not be {@code null} |
| 123 | + */ |
| 124 | + public void unsetHeader(String name) { |
| 125 | + headers.removeIf(h -> h.getName().equalsIgnoreCase(name)); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Removes all headers from this request. |
| 130 | + */ |
| 131 | + public void unsetHeaders() { |
| 132 | + headers.clear(); |
| 133 | + } |
| 134 | + |
94 | 135 | /** |
95 | 136 | * Returns the optional request body. For methods that do not carry a body (e.g. {@code GET}, {@code DELETE}) this |
96 | 137 | * will be empty. |
@@ -170,6 +211,19 @@ public Builder headers(List<HttpHeader> hdrs) { |
170 | 211 | return this; |
171 | 212 | } |
172 | 213 |
|
| 214 | + /** |
| 215 | + * Appends one or more headers to the request. Multiple calls are additive. |
| 216 | + * |
| 217 | + * @param hdrs the {@link HttpHeader}s to add; must not be {@code null} |
| 218 | + * @return this builder |
| 219 | + */ |
| 220 | + public Builder headers(HttpHeader... hdrs) { |
| 221 | + for (HttpHeader h : hdrs) { |
| 222 | + headers.add(h); |
| 223 | + } |
| 224 | + return this; |
| 225 | + } |
| 226 | + |
173 | 227 | /** |
174 | 228 | * Sets the request body. Pass {@code null} or omit this call for bodyless requests (e.g. {@code GET}, |
175 | 229 | * {@code DELETE}). |
|
0 commit comments