|
| 1 | +/* |
| 2 | + * Copyright 2024 The gRPC Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.grpc.xds.internal.matchers; |
| 18 | + |
| 19 | +import com.google.common.base.Objects; |
| 20 | +import com.google.common.collect.ImmutableSet; |
| 21 | +import com.google.errorprone.annotations.DoNotCall; |
| 22 | +import io.grpc.xds.internal.MetadataHelper; |
| 23 | +import java.util.AbstractMap; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.function.BiFunction; |
| 27 | +import java.util.function.Function; |
| 28 | +import javax.annotation.Nullable; |
| 29 | + |
| 30 | +public final class HeadersWrapper extends AbstractMap<String, String> { |
| 31 | + private static final ImmutableSet<String> PSEUDO_HEADERS = |
| 32 | + ImmutableSet.of(":method", ":authority", ":path"); |
| 33 | + private final HttpMatchInput httpMatchInput; |
| 34 | + |
| 35 | + HeadersWrapper(HttpMatchInput httpMatchInput) { |
| 36 | + this.httpMatchInput = httpMatchInput; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + @Nullable |
| 41 | + public String get(Object key) { |
| 42 | + String headerName = (String) key; |
| 43 | + // Pseudo-headers. |
| 44 | + switch (headerName) { |
| 45 | + case ":method": |
| 46 | + return httpMatchInput.getMethod(); |
| 47 | + case ":authority": |
| 48 | + return httpMatchInput.getHost(); |
| 49 | + case ":path": |
| 50 | + return httpMatchInput.getPath(); |
| 51 | + default: |
| 52 | + return MetadataHelper.deserializeHeader(httpMatchInput.metadata(), headerName); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String getOrDefault(Object key, String defaultValue) { |
| 58 | + String value = get(key); |
| 59 | + return value != null ? value : defaultValue; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean containsKey(Object key) { |
| 64 | + String headerName = (String) key; |
| 65 | + if (PSEUDO_HEADERS.contains(headerName)) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + return MetadataHelper.containsHeader(httpMatchInput.metadata(), headerName); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Set<String> keySet() { |
| 73 | + return ImmutableSet.<String>builder() |
| 74 | + .addAll(httpMatchInput.metadata().keys()) |
| 75 | + .addAll(PSEUDO_HEADERS).build(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + @DoNotCall("Always throws UnsupportedOperationException") |
| 80 | + public Set<Entry<String, String>> entrySet() { |
| 81 | + throw new UnsupportedOperationException( |
| 82 | + "Should not be called to prevent resolving header values."); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + @DoNotCall("Always throws UnsupportedOperationException") |
| 87 | + public Collection<String> values() { |
| 88 | + throw new UnsupportedOperationException( |
| 89 | + "Should not be called to prevent resolving header values."); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String toString() { |
| 94 | + // Prevent iterating to avoid resolving all values on "key not found". |
| 95 | + return getClass().getName() + "@" + Integer.toHexString(hashCode()); |
| 96 | + } |
| 97 | + |
| 98 | + @Override public int hashCode() { |
| 99 | + return Objects.hashCode(httpMatchInput.serverCall(), httpMatchInput.metadata()); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + @DoNotCall("Always throws UnsupportedOperationException") |
| 104 | + public void replaceAll(BiFunction<? super String, ? super String, ? extends String> function) { |
| 105 | + throw new UnsupportedOperationException(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + @DoNotCall("Always throws UnsupportedOperationException") |
| 110 | + public String putIfAbsent(String key, String value) { |
| 111 | + throw new UnsupportedOperationException(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + @DoNotCall("Always throws UnsupportedOperationException") |
| 116 | + public boolean remove(Object key, Object value) { |
| 117 | + throw new UnsupportedOperationException(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + @DoNotCall("Always throws UnsupportedOperationException") |
| 122 | + public boolean replace(String key, String oldValue, String newValue) { |
| 123 | + throw new UnsupportedOperationException(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + @DoNotCall("Always throws UnsupportedOperationException") |
| 128 | + public String replace(String key, String value) { |
| 129 | + throw new UnsupportedOperationException(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + @DoNotCall("Always throws UnsupportedOperationException") |
| 134 | + public String computeIfAbsent( |
| 135 | + String key, Function<? super String, ? extends String> mappingFunction) { |
| 136 | + throw new UnsupportedOperationException(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + @DoNotCall("Always throws UnsupportedOperationException") |
| 141 | + public String computeIfPresent( |
| 142 | + String key, BiFunction<? super String, ? super String, ? extends String> remappingFunction) { |
| 143 | + throw new UnsupportedOperationException(); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + @DoNotCall("Always throws UnsupportedOperationException") |
| 148 | + public String compute( |
| 149 | + String key, BiFunction<? super String, ? super String, ? extends String> remappingFunction) { |
| 150 | + throw new UnsupportedOperationException(); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + @DoNotCall("Always throws UnsupportedOperationException") |
| 155 | + public String merge( |
| 156 | + String key, String value, |
| 157 | + BiFunction<? super String, ? super String, ? extends String> remappingFunction) { |
| 158 | + throw new UnsupportedOperationException(); |
| 159 | + } |
| 160 | +} |
0 commit comments