package org.springframework.http.server.reactive;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
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.CollectionUtils;
import org.springframework.util.MultiValueMap;
class implements MultiValueMap<String, String> {
private final HeaderMap headers;
UndertowHeadersAdapter(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 = CollectionUtils.newLinkedHashMap(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 new HeaderNames();
}
@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> names = 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 key;
HeaderEntry(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;
}
}
private class extends AbstractSet<String> {
@Override
public Iterator<String> () {
return new HeaderNamesIterator(headers.getHeaderNames().iterator());
}
@Override
public int () {
return headers.getHeaderNames().size();
}
}
private final class implements Iterator<String> {
private final Iterator<HttpString> iterator;
@Nullable
private String ;
private HeaderNamesIterator(Iterator<HttpString> iterator) {
this.iterator = iterator;
}
@Override
public boolean () {
return this.iterator.hasNext();
}
@Override
public String () {
this.currentName = this.iterator.next().toString();
return this.currentName;
}
@Override
public void () {
if (this.currentName == null) {
throw new IllegalStateException("No current Header in iterator");
}
if (!headers.contains(this.currentName)) {
throw new IllegalStateException("Header not present: " + this.currentName);
}
headers.remove(this.currentName);
}
}
}