io.gravitee.gateway.api.http.DefaultHttpHeaders Maven / Gradle / Ivy
/*
* Copyright © 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.gateway.api.http;
import io.gravitee.common.util.LinkedCaseInsensitiveMap;
import io.gravitee.common.util.MultiValueMap;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Implements {@link MultiValueMap} for backward compatibility.
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class DefaultHttpHeaders implements io.gravitee.gateway.api.http.HttpHeaders, MultiValueMap {
private final Map> headers;
/**@
* Constructs a new, empty instance of the {@code HttpHeaders} object.
*/
DefaultHttpHeaders() {
this.headers = new LinkedCaseInsensitiveMap<>(8);
}
private DefaultHttpHeaders(int initialCapacity) {
this.headers = new LinkedCaseInsensitiveMap<>(initialCapacity);
}
DefaultHttpHeaders(io.gravitee.gateway.api.http.HttpHeaders headers) {
this(headers.size());
headers.toListValuesMap().forEach((key, values) -> values.forEach(value -> add(key, value)));
}
@Override
public int size() {
return headers.size();
}
@Override
public boolean isEmpty() {
return headers.isEmpty();
}
@Override
public String get(CharSequence name) {
List headerValues = this.headers.get(name.toString());
return (headerValues != null ? headerValues.get(0) : null);
}
@Override
public List getAll(CharSequence name) {
return headers.get(name.toString());
}
@Override
public boolean contains(CharSequence name) {
return headers.containsKey(name.toString());
}
@Override
public Set names() {
return headers.keySet();
}
@Override
public io.gravitee.gateway.api.http.HttpHeaders add(CharSequence name, CharSequence value) {
List headerValues = this.headers.computeIfAbsent(name.toString(), s -> new LinkedList<>());
headerValues.add(value.toString());
return this;
}
@Override
public io.gravitee.gateway.api.http.HttpHeaders add(CharSequence name, Iterable values) {
if (values != null) {
final LinkedList list = new LinkedList<>();
values.forEach(charSequence -> list.add(charSequence.toString()));
headers.put(name.toString(), list);
}
return this;
}
@Override
public io.gravitee.gateway.api.http.HttpHeaders set(CharSequence name, CharSequence value) {
List headerValues = new LinkedList<>();
headerValues.add(value.toString());
this.headers.put(name.toString(), headerValues);
return this;
}
@Override
public io.gravitee.gateway.api.http.HttpHeaders set(CharSequence name, Iterable values) {
if (values != null) {
final LinkedList list = new LinkedList<>();
values.forEach(charSequence -> list.add(charSequence.toString()));
headers.put(name.toString(), list);
}
return this;
}
@Override
public io.gravitee.gateway.api.http.HttpHeaders remove(CharSequence name) {
headers.remove(name.toString());
return this;
}
@Override
public void clear() {
headers.clear();
}
@Override
public boolean equals(Object o) {
return headers.equals(o);
}
@Override
public int hashCode() {
return headers.hashCode();
}
@Override
public Iterator> iterator() {
final Iterator>> entries = headers.entrySet().iterator();
return new Iterator<>() {
@Override
public boolean hasNext() {
return entries.hasNext();
}
@Override
public Map.Entry next() {
Map.Entry> next = entries.next();
return new Map.Entry<>() {
@Override
public String getKey() {
return next.getKey();
}
@Override
public String getValue() {
return next.getValue().get(0);
}
@Override
public String setValue(String value) {
throw new IllegalStateException();
}
};
}
};
}
@Override
public Map toSingleValueMap() {
return HttpHeaders.super.toSingleValueMap();
}
@Override
public boolean containsAllKeys(Collection names) {
return HttpHeaders.super.containsAllKeys(names);
}
@Override
public boolean containsKey(Object key) {
return headers.containsKey(key.toString());
}
@Override
public boolean containsValue(Object value) {
return headers.containsValue(value);
}
/**
* @see LinkedCaseInsensitiveMap#get(Object)
* Contrary to {@link DefaultHttpHeaders#get(CharSequence)}, the list of values is returned, not only the first element
*/
@Override
public List get(Object key) {
return headers.get(key.toString());
}
/**
* @see HashMap#putVal(int, Object, Object, boolean, boolean), returns the previous value if present, else null.
*/
@Override
public List put(String key, List value) {
return headers.put(key, value);
}
/**
* @see HashMap#remove(Object), returns the previous value (can bee {@code null}) associated with {@code key} or null if none.
*/
@Override
public List remove(Object key) {
return headers.remove(key.toString());
}
@Override
public void putAll(Map extends String, ? extends List> map) {
headers.putAll(map);
}
@Override
public Set keySet() {
return headers.keySet();
}
@Override
public Collection> values() {
return headers.values();
}
@Override
public Set>> entrySet() {
return headers.entrySet();
}
@Override
public String getFirst(String header) {
List headerValues = this.headers.get(header);
return (headerValues != null ? headerValues.get(0) : null);
}
@Override
public void add(String name, String value) {
List headerValues = this.headers.computeIfAbsent(name, s -> new LinkedList<>());
headerValues.add(value);
}
@Override
public void set(String name, String value) {
List headerValues = new LinkedList<>();
headerValues.add(value);
this.headers.put(name, headerValues);
}
@Override
public void setAll(Map values) {
for (Entry entry : values.entrySet()) {
set(entry.getKey(), entry.getValue());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy