Skip to content

Expose additional set headers method for headers with single and multiple values #1296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 75 additions & 6 deletions client/src/main/java/org/asynchttpclient/RequestBuilderBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,48 @@ public T setVirtualHost(String virtualHost) {
return asDerivedType();
}

/**
* Remove all added headers
*
* @return {@code this}
*/
public T clearHeaders() {
this.headers.clear();
return asDerivedType();
}

/**
* Set uni-value header for the request
*
* @param name header name
* @param value header value to set
* @return {@code this}
*/
public T setHeader(CharSequence name, String value) {
this.headers.set(name, value);
return asDerivedType();
}

/**
* Set multi-values header for the request
*
* @param name header name
* @param values {@code Iterable} with multiple header values to set
* @return {@code this}
*/
public T setHeader(CharSequence name, Iterable<String> values) {
this.headers.set(name, values);
return asDerivedType();
}

/**
* Add a header value for the request. If a header with {@code name} was setup for this request already -
* call will add one more header value and convert it to multi-value header
*
* @param name header name
* @param value header value to add
* @return {@code this}
*/
public T addHeader(CharSequence name, String value) {
if (value == null) {
LOGGER.warn("Value was null, set to \"\"");
Expand All @@ -183,6 +220,19 @@ public T addHeader(CharSequence name, String value) {
return asDerivedType();
}

/**
* Add header values for the request. If a header with {@code name} was setup for this request already -
* call will add more header values and convert it to multi-value header
*
* @param name header name
* @param values {@code Iterable} with multiple header values to add
* @return {@code}
*/
public T addHeader(CharSequence name, Iterable<String> values) {
this.headers.add(name, values);
return asDerivedType();
}

public T setHeaders(HttpHeaders headers) {
if (headers == null)
this.headers.clear();
Expand All @@ -191,13 +241,32 @@ public T setHeaders(HttpHeaders headers) {
return asDerivedType();
}

public T setHeaders(Map<String, Collection<String>> headers) {
this.headers.clear();
/**
* Set request headers using a map {@code headers} of pair (Header name, Header values)
* This method could be used to setup multi-valued headers
*
* @param headers map of header names as the map keys and header values {@link Iterable} as the map values
* @return {@code this}
*/
public T setHeaders(Map<String, ? extends Iterable<String>> headers) {
clearHeaders();
if (headers != null) {
for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
this.headers.add(headerName, entry.getValue());
}
headers.forEach((name, values) -> this.headers.add(name, values));
}
return asDerivedType();
}

/**
* Set single-value request headers using a map {@code headers} of pairs (Header name, Header value).
* To set headers with multiple values use {@link #setHeaders(Map)}
*
* @param headers map of header names as the map keys and header values as the map values
* @return {@code this}
*/
public T setSingleHeaders(Map<String, String> headers) {
clearHeaders();
if (headers != null) {
headers.forEach((name, value) -> this.headers.add(name, value));
}
return asDerivedType();
}
Expand Down