TomcatHeadersAdapter.java 5.4 KB
Newer Older
B
Brian Clozel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * Copyright 2002-2018 the original author or authors.
 *
 * 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 org.springframework.http.server.reactive;

import java.util.AbstractSet;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
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 org.apache.tomcat.util.buf.MessageBytes;
import org.apache.tomcat.util.http.MimeHeaders;

import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;

/**
 * {@code MultiValueMap} implementation for wrapping Tomcat HTTP headers.
 *
 * @author Brian Clozel
41
 * @since 5.1.1
B
Brian Clozel 已提交
42 43 44 45 46
 */
class TomcatHeadersAdapter implements MultiValueMap<String, String> {

	private final MimeHeaders headers;

47

B
Brian Clozel 已提交
48 49 50 51
	TomcatHeadersAdapter(MimeHeaders headers) {
		this.headers = headers;
	}

52

B
Brian Clozel 已提交
53 54 55 56 57 58
	@Override
	public String getFirst(String key) {
		return this.headers.getHeader(key);
	}

	@Override
59
	public void add(String key, @Nullable String value) {
B
Brian Clozel 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73
		this.headers.addValue(key).setString(value);
	}

	@Override
	public void addAll(String key, List<? extends String> values) {
		values.forEach(value -> add(key, value));
	}

	@Override
	public void addAll(MultiValueMap<String, String> values) {
		values.forEach(this::addAll);
	}

	@Override
74
	public void set(String key, @Nullable String value) {
B
Brian Clozel 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		this.headers.setValue(key).setString(value);
	}

	@Override
	public void setAll(Map<String, String> values) {
		values.forEach(this::set);
	}

	@Override
	public Map<String, String> toSingleValueMap() {
		Map<String, String> singleValueMap = new LinkedHashMap<>(this.headers.size());
		this.keySet().forEach(key -> singleValueMap.put(key, getFirst(key)));
		return singleValueMap;
	}

	@Override
	public int size() {
		Enumeration<String> names = this.headers.names();
		int size = 0;
		while (names.hasMoreElements()) {
			size++;
			names.nextElement();
		}
		return size;
	}

	@Override
	public boolean isEmpty() {
103
		return (this.headers.size() == 0);
B
Brian Clozel 已提交
104 105 106 107 108
	}

	@Override
	public boolean containsKey(Object key) {
		if (key instanceof String) {
109
			return (this.headers.findHeader((String) key, 0) != -1);
B
Brian Clozel 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		}
		return false;
	}

	@Override
	public boolean containsValue(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> get(Object key) {
B
Brian Clozel 已提交
131
		if (containsKey(key)) {
B
Brian Clozel 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
			return Collections.list(this.headers.values((String) key));
		}
		return null;
	}

	@Override
	@Nullable
	public List<String> put(String key, List<String> value) {
		List<String> previousValues = get(key);
		value.forEach(v -> this.headers.addValue(key).setString(v));
		return previousValues;
	}

	@Override
	@Nullable
	public List<String> remove(Object key) {
		if (key instanceof String) {
			List<String> previousValues = get(key);
			this.headers.removeHeader((String) key);
			return previousValues;
		}
		return null;
	}

	@Override
157 158
	public void putAll(Map<? extends String, ? extends List<String>> map) {
		map.forEach(this::put);
B
Brian Clozel 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	}

	@Override
	public void clear() {
		this.headers.clear();
	}

	@Override
	public Set<String> keySet() {
		Set<String> result = new HashSet<>(8);
		Enumeration<String> names = this.headers.names();
		while (names.hasMoreElements()) {
			result.add(names.nextElement());
		}
		return result;
	}

	@Override
	public Collection<List<String>> values() {
		return keySet().stream().map(this::get).collect(Collectors.toList());
	}

	@Override
	public Set<Entry<String, List<String>>> entrySet() {
		return new AbstractSet<Entry<String, List<String>>>() {
			@Override
			public Iterator<Entry<String, List<String>>> iterator() {
				return new EntryIterator();
			}

			@Override
			public int size() {
				return headers.size();
			}
		};
	}

196

B
Brian Clozel 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	private class EntryIterator implements Iterator<Entry<String, List<String>>> {

		private Enumeration<String> names = headers.names();

		@Override
		public boolean hasNext() {
			return this.names.hasMoreElements();
		}

		@Override
		public Entry<String, List<String>> next() {
			return new HeaderEntry(this.names.nextElement());
		}
	}

212

B
Brian Clozel 已提交
213 214 215 216
	private final class HeaderEntry implements Entry<String, List<String>> {

		private final String key;

217
		HeaderEntry(String key) {
B
Brian Clozel 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
			this.key = key;
		}

		@Override
		public String getKey() {
			return this.key;
		}

		@Nullable
		@Override
		public List<String> getValue() {
			return get(this.key);
		}

		@Nullable
		@Override
		public List<String> setValue(List<String> value) {
			List<String> previous = getValue();
			headers.removeHeader(this.key);
			addAll(this.key, value);
			return previous;
		}
	}
241

B
Brian Clozel 已提交
242
}