|
| 1 | +// |
| 2 | +// ======================================================================== |
| 3 | +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 | +// |
| 5 | +// This program and the accompanying materials are made available under the |
| 6 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 | +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 | +// ======================================================================== |
| 12 | +// |
| 13 | + |
| 14 | +package org.eclipse.jetty.http; |
| 15 | + |
| 16 | +import java.util.AbstractMap; |
| 17 | +import java.util.AbstractSet; |
| 18 | +import java.util.Iterator; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Objects; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.eclipse.jetty.util.StringUtil; |
| 24 | + |
| 25 | +/** |
| 26 | + * <p>A {@link java.util.Map} which is backed by an instance of {@link HttpFields.Mutable}.</p> |
| 27 | + * @see HttpFieldsMap.Mutable |
| 28 | + * @see HttpFieldsMap.Immutable |
| 29 | + */ |
| 30 | +abstract class HttpFieldsMap extends AbstractMap<String, List<String>> |
| 31 | +{ |
| 32 | + /** |
| 33 | + * <p>A {@link java.util.Map} which is backed by an instance of {@link HttpFields.Mutable}.</p> |
| 34 | + * <p>Any changes to the {@link java.util.Map} will be reflected in the underlying instance of {@link HttpFields.Mutable}.</p> |
| 35 | + */ |
| 36 | + public static class Mutable extends HttpFieldsMap |
| 37 | + { |
| 38 | + private final HttpFields.Mutable httpFields; |
| 39 | + |
| 40 | + public Mutable(HttpFields.Mutable httpFields) |
| 41 | + { |
| 42 | + this.httpFields = httpFields; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public List<String> get(Object key) |
| 47 | + { |
| 48 | + if (key instanceof String s) |
| 49 | + return httpFields.getValuesList(s); |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public List<String> put(String key, List<String> value) |
| 55 | + { |
| 56 | + List<String> oldValue = get(key); |
| 57 | + httpFields.put(key, value); |
| 58 | + return oldValue; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public List<String> remove(Object key) |
| 63 | + { |
| 64 | + if (key instanceof String s) |
| 65 | + { |
| 66 | + List<String> oldValue = get(s); |
| 67 | + httpFields.remove(s); |
| 68 | + return oldValue; |
| 69 | + } |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Set<Entry<String, List<String>>> entrySet() |
| 75 | + { |
| 76 | + return new AbstractSet<>() |
| 77 | + { |
| 78 | + @Override |
| 79 | + public Iterator<Entry<String, List<String>>> iterator() |
| 80 | + { |
| 81 | + return new Iterator<>() |
| 82 | + { |
| 83 | + private final Iterator<String> iterator = httpFields.getFieldNamesCollection().iterator(); |
| 84 | + private String name = null; |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean hasNext() |
| 88 | + { |
| 89 | + return iterator.hasNext(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Entry<String, List<String>> next() |
| 94 | + { |
| 95 | + name = iterator.next(); |
| 96 | + return new HttpFieldsEntry(name); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void remove() |
| 101 | + { |
| 102 | + if (name != null) |
| 103 | + { |
| 104 | + Mutable.this.remove(name); |
| 105 | + name = null; |
| 106 | + } |
| 107 | + } |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public int size() |
| 113 | + { |
| 114 | + return httpFields.getFieldNamesCollection().size(); |
| 115 | + } |
| 116 | + }; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * <p>A {@link java.util.Map} which is backed by an instance of {@link HttpFields.Mutable}.</p> |
| 122 | + * <p>Any attempt to modify the map will throw {@link UnsupportedOperationException}.</p> |
| 123 | + */ |
| 124 | + public static class Immutable extends HttpFieldsMap |
| 125 | + { |
| 126 | + private final HttpFields httpFields; |
| 127 | + |
| 128 | + public Immutable(HttpFields httpFields) |
| 129 | + { |
| 130 | + this.httpFields = httpFields; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public List<String> get(Object key) |
| 135 | + { |
| 136 | + if (key instanceof String s) |
| 137 | + return httpFields.getValuesList(s); |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public List<String> put(String key, List<String> value) |
| 143 | + { |
| 144 | + throw new UnsupportedOperationException(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public List<String> remove(Object key) |
| 149 | + { |
| 150 | + throw new UnsupportedOperationException(); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public Set<Entry<String, List<String>>> entrySet() |
| 155 | + { |
| 156 | + return new AbstractSet<>() |
| 157 | + { |
| 158 | + @Override |
| 159 | + public Iterator<Entry<String, List<String>>> iterator() |
| 160 | + { |
| 161 | + return new Iterator<>() |
| 162 | + { |
| 163 | + private final Iterator<String> iterator = httpFields.getFieldNamesCollection().iterator(); |
| 164 | + |
| 165 | + @Override |
| 166 | + public boolean hasNext() |
| 167 | + { |
| 168 | + return iterator.hasNext(); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public Entry<String, List<String>> next() |
| 173 | + { |
| 174 | + return new HttpFieldsEntry(iterator.next()); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void remove() |
| 179 | + { |
| 180 | + throw new UnsupportedOperationException(); |
| 181 | + } |
| 182 | + }; |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public int size() |
| 187 | + { |
| 188 | + return httpFields.getFieldNamesCollection().size(); |
| 189 | + } |
| 190 | + }; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private class HttpFieldsEntry implements Entry<String, List<String>> |
| 195 | + { |
| 196 | + private final String _name; |
| 197 | + |
| 198 | + public HttpFieldsEntry(String name) |
| 199 | + { |
| 200 | + _name = name; |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public String getKey() |
| 205 | + { |
| 206 | + return _name; |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public List<String> getValue() |
| 211 | + { |
| 212 | + return HttpFieldsMap.this.get(_name); |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public List<String> setValue(List<String> value) |
| 217 | + { |
| 218 | + return HttpFieldsMap.this.put(_name, value); |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public boolean equals(Object o) |
| 223 | + { |
| 224 | + if (this == o) |
| 225 | + return true; |
| 226 | + if (o instanceof HttpFieldsEntry other) |
| 227 | + return StringUtil.asciiEqualsIgnoreCase(_name, other.getKey()); |
| 228 | + return false; |
| 229 | + } |
| 230 | + |
| 231 | + @Override |
| 232 | + public int hashCode() |
| 233 | + { |
| 234 | + return Objects.hash(StringUtil.asciiToLowerCase(_name)); |
| 235 | + } |
| 236 | + } |
| 237 | +} |
0 commit comments