Skip to content

Commit bd6fc4d

Browse files
committed
GH-5723: improve support for setting headers of HttpRequest
1 parent 1585ffa commit bd6fc4d

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

core/http/client-api/src/main/java/org/eclipse/rdf4j/http/client/spi/HttpRequest.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.net.URI;
1414
import java.util.ArrayList;
1515
import java.util.List;
16+
import java.util.Map;
1617
import java.util.Optional;
1718

1819
/**
@@ -23,8 +24,9 @@
2324
* request patterns, consider using the convenience factory methods in {@link HttpRequests}.
2425
*
2526
* <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.
2830
*
2931
* <p>
3032
* Example usage:
@@ -91,6 +93,45 @@ public void addHeader(String name, String value) {
9193
headers.add(HttpHeader.of(name, value));
9294
}
9395

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+
94135
/**
95136
* Returns the optional request body. For methods that do not carry a body (e.g. {@code GET}, {@code DELETE}) this
96137
* will be empty.
@@ -170,6 +211,19 @@ public Builder headers(List<HttpHeader> hdrs) {
170211
return this;
171212
}
172213

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+
173227
/**
174228
* Sets the request body. Pass {@code null} or omit this call for bodyless requests (e.g. {@code GET},
175229
* {@code DELETE}).

core/http/client-api/src/main/java/org/eclipse/rdf4j/http/client/spi/HttpUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.io.IOException;
1414
import java.nio.charset.Charset;
1515
import java.nio.charset.StandardCharsets;
16-
import java.nio.charset.UnsupportedCharsetException;
1716
import java.util.Locale;
1817

1918
/**

0 commit comments

Comments
 (0)