package org.springframework.http.client.reactive;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
class implements MultiValueMap<String, String> {
private final HttpResponse response;
HttpComponentsHeadersAdapter(HttpResponse response) {
this.response = response;
}
@Override
public String (String key) {
Header header = this.response.getFirstHeader(key);
return (header != null ? header.getValue() : null);
}
@Override
public void (String key, @Nullable String value) {
this.response.addHeader(key, value);
}
@Override
public void (String key, List<? extends String> values) {
values.forEach(value -> add(key, value));
}
@Override
public void (MultiValueMap<String, String> values) {
values.forEach(this::addAll);
}
@Override
public void (String key, @Nullable String value) {
this.response.setHeader(key, value);
}
@Override
public void (Map<String, String> values) {
values.forEach(this::set);
}
@Override
public Map<String, String> () {
Map<String, String> map = CollectionUtils.newLinkedHashMap(size());
this.response.headerIterator().forEachRemaining(h -> map.putIfAbsent(h.getName(), h.getValue()));
return map;
}
@Override
public int () {
return this.response.getHeaders().length;
}
@Override
public boolean () {
return (this.response.getHeaders().length == 0);
}
@Override
public boolean (Object key) {
return (key instanceof String && this.response.containsHeader((String) key));
}
@Override
public boolean (Object value) {
return (value instanceof String &&
Arrays.stream(this.response.getHeaders()).anyMatch(h -> h.getValue().equals(value)));
}
@Nullable
@Override
public List<String> (Object key) {
List<String> values = null;
if (containsKey(key)) {
Header[] headers = this.response.getHeaders((String) key);
values = new ArrayList<>(headers.length);
for (Header header : headers) {
values.add(header.getValue());
}
}
return values;
}
@Nullable
@Override
public List<String> (String key, List<String> values) {
List<String> oldValues = remove(key);
values.forEach(value -> add(key, value));
return oldValues;
}
@Nullable
@Override
public List<String> (Object key) {
if (key instanceof String) {
List<String> oldValues = get(key);
this.response.removeHeaders((String) key);
return oldValues;
}
return null;
}
@Override
public void (Map<? extends String, ? extends List<String>> map) {
map.forEach(this::put);
}
@Override
public void () {
this.response.setHeaders();
}
@Override
public Set<String> () {
Set<String> keys = new LinkedHashSet<>(size());
for (Header header : this.response.getHeaders()) {
keys.add(header.getName());
}
return keys;
}
@Override
public Collection<List<String>> () {
Collection<List<String>> values = new ArrayList<>(size());
for (Header header : this.response.getHeaders()) {
values.add(get(header.getName()));
}
return values;
}
@Override
public Set<Entry<String, List<String>>> () {
return new AbstractSet<Entry<String, List<String>>>() {
@Override
public Iterator<Entry<String, List<String>>> () {
return new EntryIterator();
}
@Override
public int () {
return HttpComponentsHeadersAdapter.this.size();
}
};
}
@Override
public String () {
return HttpHeaders.formatHeaders(this);
}
private class implements Iterator<Entry<String, List<String>>> {
private Iterator<Header> iterator = response.headerIterator();
@Override
public boolean () {
return this.iterator.hasNext();
}
@Override
public Entry<String, List<String>> () {
return new HeaderEntry(this.iterator.next().getName());
}
}
private class implements Entry<String, List<String>> {
private final String ;
(String key) {
this.key = key;
}
@Override
public String () {
return this.key;
}
@Override
public List<String> () {
List<String> values = HttpComponentsHeadersAdapter.this.get(this.key);
return values != null ? values : Collections.emptyList();
}
@Override
public List<String> (List<String> value) {
List<String> previousValues = getValue();
HttpComponentsHeadersAdapter.this.put(this.key, value);
return previousValues;
}
}
}