package org.springframework.http.server.reactive;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import io.undertow.util.HeaderMap;
import io.undertow.util.HeaderValues;
import io.undertow.util.HttpString;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;
class implements MultiValueMap<String, String> {
private final HeaderMap ;
(HeaderMap headers) {
this.headers = headers;
}
@Override
public String (String key) {
return this.headers.getFirst(key);
}
@Override
public void (String key, @Nullable String value) {
this.headers.add(HttpString.tryFromString(key), value);
}
@Override
@SuppressWarnings("unchecked")
public void (String key, List<? extends String> values) {
this.headers.addAll(HttpString.tryFromString(key), (List<String>) values);
}
@Override
public void (MultiValueMap<String, String> values) {
values.forEach((key, list) -> this.headers.addAll(HttpString.tryFromString(key), list));
}
@Override
public void (String key, @Nullable String value) {
this.headers.put(HttpString.tryFromString(key), value);
}
@Override
public void (Map<String, String> values) {
values.forEach((key, list) -> this.headers.put(HttpString.tryFromString(key), list));
}
@Override
public Map<String, String> () {
Map<String, String> singleValueMap = new LinkedHashMap<>(this.headers.size());
this.headers.forEach(values ->
singleValueMap.put(values.getHeaderName().toString(), values.getFirst()));
return singleValueMap;
}
@Override
public int () {
return this.headers.size();
}
@Override
public boolean () {
return (this.headers.size() == 0);
}
@Override
public boolean (Object key) {
return (key instanceof String && this.headers.contains((String) key));
}
@Override
public boolean (Object value) {
return (value instanceof String &&
this.headers.getHeaderNames().stream()
.map(this.headers::get)
.anyMatch(values -> values.contains(value)));
}
@Override
@Nullable
public List<String> (Object key) {
if (key instanceof String) {
return this.headers.get((String) key);
}
return null;
}
@Override
@Nullable
public List<String> (String key, List<String> value) {
HeaderValues previousValues = this.headers.get(key);
this.headers.putAll(HttpString.tryFromString(key), value);
return previousValues;
}
@Override
@Nullable
public List<String> (Object key) {
if (key instanceof String) {
this.headers.remove((String) key);
}
return null;
}
@Override
public void (Map<? extends String, ? extends List<String>> map) {
map.forEach((key, values) ->
this.headers.putAll(HttpString.tryFromString(key), values));
}
@Override
public void () {
this.headers.clear();
}
@Override
public Set<String> () {
return this.headers.getHeaderNames().stream()
.map(HttpString::toString)
.collect(Collectors.toSet());
}
@Override
public Collection<List<String>> () {
return this.headers.getHeaderNames().stream()
.map(this.headers::get)
.collect(Collectors.toList());
}
@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 headers.size();
}
};
}
@Override
public String () {
return org.springframework.http.HttpHeaders.formatHeaders(this);
}
private class implements Iterator<Entry<String, List<String>>> {
private Iterator<HttpString> = headers.getHeaderNames().iterator();
@Override
public boolean () {
return this.names.hasNext();
}
@Override
public Entry<String, List<String>> () {
return new HeaderEntry(this.names.next());
}
}
private class implements Entry<String, List<String>> {
private final HttpString ;
(HttpString key) {
this.key = key;
}
@Override
public String () {
return this.key.toString();
}
@Override
public List<String> () {
return headers.get(this.key);
}
@Override
public List<String> (List<String> value) {
List<String> previousValues = headers.get(this.key);
headers.putAll(this.key, value);
return previousValues;
}
}
}