Skip to content

Commit 86c07cc

Browse files
Spikhalskiyslandelle
authored andcommitted
Expose additional set headers method for headers with single and multiple values (AsyncHttpClient#1296)
Thanks!
1 parent 8988eca commit 86c07cc

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

client/src/main/java/org/asynchttpclient/RequestBuilderBase.java

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,48 @@ public T setVirtualHost(String virtualHost) {
168168
return asDerivedType();
169169
}
170170

171+
/**
172+
* Remove all added headers
173+
*
174+
* @return {@code this}
175+
*/
176+
public T clearHeaders() {
177+
this.headers.clear();
178+
return asDerivedType();
179+
}
180+
181+
/**
182+
* Set uni-value header for the request
183+
*
184+
* @param name header name
185+
* @param value header value to set
186+
* @return {@code this}
187+
*/
171188
public T setHeader(CharSequence name, String value) {
172189
this.headers.set(name, value);
173190
return asDerivedType();
174191
}
175192

193+
/**
194+
* Set multi-values header for the request
195+
*
196+
* @param name header name
197+
* @param values {@code Iterable} with multiple header values to set
198+
* @return {@code this}
199+
*/
200+
public T setHeader(CharSequence name, Iterable<String> values) {
201+
this.headers.set(name, values);
202+
return asDerivedType();
203+
}
204+
205+
/**
206+
* Add a header value for the request. If a header with {@code name} was setup for this request already -
207+
* call will add one more header value and convert it to multi-value header
208+
*
209+
* @param name header name
210+
* @param value header value to add
211+
* @return {@code this}
212+
*/
176213
public T addHeader(CharSequence name, String value) {
177214
if (value == null) {
178215
LOGGER.warn("Value was null, set to \"\"");
@@ -183,6 +220,19 @@ public T addHeader(CharSequence name, String value) {
183220
return asDerivedType();
184221
}
185222

223+
/**
224+
* Add header values for the request. If a header with {@code name} was setup for this request already -
225+
* call will add more header values and convert it to multi-value header
226+
*
227+
* @param name header name
228+
* @param values {@code Iterable} with multiple header values to add
229+
* @return {@code}
230+
*/
231+
public T addHeader(CharSequence name, Iterable<String> values) {
232+
this.headers.add(name, values);
233+
return asDerivedType();
234+
}
235+
186236
public T setHeaders(HttpHeaders headers) {
187237
if (headers == null)
188238
this.headers.clear();
@@ -191,13 +241,32 @@ public T setHeaders(HttpHeaders headers) {
191241
return asDerivedType();
192242
}
193243

194-
public T setHeaders(Map<String, Collection<String>> headers) {
195-
this.headers.clear();
244+
/**
245+
* Set request headers using a map {@code headers} of pair (Header name, Header values)
246+
* This method could be used to setup multi-valued headers
247+
*
248+
* @param headers map of header names as the map keys and header values {@link Iterable} as the map values
249+
* @return {@code this}
250+
*/
251+
public T setHeaders(Map<String, ? extends Iterable<String>> headers) {
252+
clearHeaders();
196253
if (headers != null) {
197-
for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
198-
String headerName = entry.getKey();
199-
this.headers.add(headerName, entry.getValue());
200-
}
254+
headers.forEach((name, values) -> this.headers.add(name, values));
255+
}
256+
return asDerivedType();
257+
}
258+
259+
/**
260+
* Set single-value request headers using a map {@code headers} of pairs (Header name, Header value).
261+
* To set headers with multiple values use {@link #setHeaders(Map)}
262+
*
263+
* @param headers map of header names as the map keys and header values as the map values
264+
* @return {@code this}
265+
*/
266+
public T setSingleHeaders(Map<String, String> headers) {
267+
clearHeaders();
268+
if (headers != null) {
269+
headers.forEach((name, value) -> this.headers.add(name, value));
201270
}
202271
return asDerivedType();
203272
}

0 commit comments

Comments
 (0)