package org.springframework.http.server.reactive;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.tomcat.util.buf.MessageBytes;
import org.apache.tomcat.util.http.MimeHeaders;
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 MimeHeaders headers;
TomcatHeadersAdapter(MimeHeaders headers) {
this.headers = headers;
}
@Override
public String (String key) {
return this.headers.getHeader(key);
}
@Override
public void (String key, @Nullable String value) {
this.headers.addValue(key).setString(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.headers.setValue(key).setString(value);
}
@Override
public void (Map<String, String> values) {
values.forEach(this::set);
}
@Override
public Map<String, String> () {
Map<String, String> singleValueMap = CollectionUtils.newLinkedHashMap(this.headers.size());
this.keySet().forEach(key -> singleValueMap.put(key, getFirst(key)));
return singleValueMap;
}
@Override
public int () {
Enumeration<String> names = this.headers.names();
int size = 0;
while (names.hasMoreElements()) {
size++;
names.nextElement();
}
return size;
}
@Override
public boolean () {
return (this.headers.size() == 0);
}
@Override
public boolean (Object key) {
if (key instanceof String) {
return (this.headers.findHeader((String) key, 0) != -1);
}
return false;
}
@Override
public boolean (Object value) {
if (value instanceof String) {
MessageBytes needle = MessageBytes.newInstance();
needle.setString((String) value);
for (int i = 0; i < this.headers.size(); i++) {
if (this.headers.getValue(i).equals(needle)) {
return true;
}
}
}
return false;
}
@Override
@Nullable
public List<String> (Object key) {
if (containsKey(key)) {
return Collections.list(this.headers.values((String) key));
}
return null;
}
@Override
@Nullable
public List<String> (String key, List<String> value) {
List<String> previousValues = get(key);
this.headers.removeHeader(key);
value.forEach(v -> this.headers.addValue(key).setString(v));
return previousValues;
}
@Override
@Nullable
public List<String> (Object key) {
if (key instanceof String) {
List<String> previousValues = get(key);
this.headers.removeHeader((String) key);
return previousValues;
}
return null;
}
@Override
public void (Map<? extends String, ? extends List<String>> map) {
map.forEach(this::put);
}
@Override
public void () {
this.headers.clear();
}
@Override
public Set<String> () {
return new HeaderNames();
}
@Override
public Collection<List<String>> () {
return keySet().stream().map(this::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 HttpHeaders.formatHeaders(this);
}
private class implements Iterator<Entry<String, List<String>>> {
private Enumeration<String> = headers.names();
@Override
public boolean () {
return this.names.hasMoreElements();
}
@Override
public Entry<String, List<String>> () {
return new HeaderEntry(this.names.nextElement());
}
}
private final class implements Entry<String, List<String>> {
private final String ;
(String key) {
this.key = key;
}
@Override
public String () {
return this.key;
}
@Nullable
@Override
public List<String> () {
return get(this.key);
}
@Nullable
@Override
public List<String> (List<String> value) {
List<String> previous = getValue();
headers.removeHeader(this.key);
addAll(this.key, value);
return previous;
}
}
private class extends AbstractSet<String> {
@Override
public Iterator<String> () {
return new HeaderNamesIterator(headers.names());
}
@Override
public int () {
Enumeration<String> names = headers.names();
int size = 0;
while (names.hasMoreElements()) {
names.nextElement();
size++;
}
return size;
}
}
private final class implements Iterator<String> {
private final Enumeration<String> ;
@Nullable
private String ;
private (Enumeration<String> enumeration) {
this.enumeration = enumeration;
}
@Override
public boolean () {
return this.enumeration.hasMoreElements();
}
@Override
public String () {
this.currentName = this.enumeration.nextElement();
return this.currentName;
}
@Override
public void () {
if (this.currentName == null) {
throw new IllegalStateException("No current Header in iterator");
}
int index = headers.findHeader(this.currentName, 0);
if (index == -1) {
throw new IllegalStateException("Header not present: " + this.currentName);
}
headers.removeHeader(index);
}
}
}