|
| 1 | +/* |
| 2 | + * Copyright 2026 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.matcher; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableSet; |
| 20 | +import io.grpc.Metadata; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.concurrent.ConcurrentHashMap; |
| 25 | +import javax.annotation.Nullable; |
| 26 | + |
| 27 | +/** |
| 28 | + * Registry for {@link AttributeProvider}s. |
| 29 | + */ |
| 30 | +public final class AttributeProviderRegistry { |
| 31 | + private static final AttributeProviderRegistry DEFAULT_INSTANCE = new AttributeProviderRegistry(); |
| 32 | + |
| 33 | + private final Map<String, AttributeProvider> providers = new ConcurrentHashMap<>(); |
| 34 | + |
| 35 | + private AttributeProviderRegistry() { |
| 36 | + register(new HeadersProvider()); |
| 37 | + register(new HostProvider()); |
| 38 | + register(new IdProvider()); |
| 39 | + register(new MethodProvider()); |
| 40 | + register(new PathProvider()); |
| 41 | + register(new QueryProvider()); |
| 42 | + register(new SchemeProvider()); |
| 43 | + register(new ProtocolProvider()); |
| 44 | + register(new TimeProvider()); |
| 45 | + register(new RefererProvider()); |
| 46 | + register(new UserAgentProvider()); |
| 47 | + } |
| 48 | + |
| 49 | + public static AttributeProviderRegistry getDefaultRegistry() { |
| 50 | + return DEFAULT_INSTANCE; |
| 51 | + } |
| 52 | + |
| 53 | + public void register(AttributeProvider provider) { |
| 54 | + for (String name : provider.names()) { |
| 55 | + providers.put(name, provider); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Nullable |
| 60 | + public AttributeProvider get(String name) { |
| 61 | + return providers.get(name); |
| 62 | + } |
| 63 | + |
| 64 | + public Set<String> getRegisteredNames() { |
| 65 | + return Collections.unmodifiableSet(providers.keySet()); |
| 66 | + } |
| 67 | + |
| 68 | + private static String orEmpty(@Nullable Object s) { |
| 69 | + return s == null ? "" : s.toString(); |
| 70 | + } |
| 71 | + |
| 72 | + private static String or(@Nullable Object s, String def) { |
| 73 | + return s == null ? def : s.toString(); |
| 74 | + } |
| 75 | + |
| 76 | + private static String getHeader(MatchContext context, String key) { |
| 77 | + Metadata metadata = context.getMetadata(); |
| 78 | + Iterable<String> values = metadata.getAll( |
| 79 | + Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)); |
| 80 | + if (values == null) { |
| 81 | + return ""; |
| 82 | + } |
| 83 | + return String.join(",", values); |
| 84 | + } |
| 85 | + |
| 86 | + private static final class HeadersProvider implements AttributeProvider { |
| 87 | + private static final Set<String> NAMES = Collections.singleton("headers"); |
| 88 | + |
| 89 | + @Override |
| 90 | + public Iterable<String> names() { |
| 91 | + return NAMES; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Object get(MatchContext context) { |
| 96 | + return new HeadersWrapper(context); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static final class HostProvider implements AttributeProvider { |
| 101 | + private static final Set<String> NAMES = Collections.singleton("host"); |
| 102 | + |
| 103 | + @Override |
| 104 | + public Iterable<String> names() { |
| 105 | + return NAMES; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Object get(MatchContext context) { |
| 110 | + return orEmpty(context.getRawAttribute("host")); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static final class IdProvider implements AttributeProvider { |
| 115 | + private static final Set<String> NAMES = Collections.singleton("id"); |
| 116 | + |
| 117 | + @Override |
| 118 | + public Iterable<String> names() { |
| 119 | + return NAMES; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public Object get(MatchContext context) { |
| 124 | + return orEmpty(context.getRawAttribute("id")); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static final class MethodProvider implements AttributeProvider { |
| 129 | + private static final Set<String> NAMES = Collections.singleton("method"); |
| 130 | + |
| 131 | + @Override |
| 132 | + public Iterable<String> names() { |
| 133 | + return NAMES; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public Object get(MatchContext context) { |
| 138 | + return or(context.getRawAttribute("method"), "POST"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static final class PathProvider implements AttributeProvider { |
| 143 | + private static final Set<String> NAMES = ImmutableSet.of("path", "url_path"); |
| 144 | + |
| 145 | + @Override |
| 146 | + public Iterable<String> names() { |
| 147 | + return NAMES; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public Object get(MatchContext context) { |
| 152 | + return orEmpty(context.getRawAttribute("path")); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private static final class QueryProvider implements AttributeProvider { |
| 157 | + private static final Set<String> NAMES = Collections.singleton("query"); |
| 158 | + |
| 159 | + @Override |
| 160 | + public Iterable<String> names() { |
| 161 | + return NAMES; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public Object get(MatchContext context) { |
| 166 | + return ""; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private static final class SchemeProvider implements AttributeProvider { |
| 171 | + private static final Set<String> NAMES = Collections.singleton("scheme"); |
| 172 | + |
| 173 | + @Override |
| 174 | + public Iterable<String> names() { |
| 175 | + return NAMES; |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public Object get(MatchContext context) { |
| 180 | + return ""; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private static final class ProtocolProvider implements AttributeProvider { |
| 185 | + private static final Set<String> NAMES = Collections.singleton("protocol"); |
| 186 | + |
| 187 | + @Override |
| 188 | + public Iterable<String> names() { |
| 189 | + return NAMES; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public Object get(MatchContext context) { |
| 194 | + return ""; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + private static final class TimeProvider implements AttributeProvider { |
| 199 | + private static final Set<String> NAMES = Collections.singleton("time"); |
| 200 | + |
| 201 | + @Override |
| 202 | + public Iterable<String> names() { |
| 203 | + return NAMES; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + @Nullable |
| 208 | + public Object get(MatchContext context) { |
| 209 | + return null; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + private static final class RefererProvider implements AttributeProvider { |
| 214 | + private static final Set<String> NAMES = Collections.singleton("referer"); |
| 215 | + |
| 216 | + @Override |
| 217 | + public Iterable<String> names() { |
| 218 | + return NAMES; |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public Object get(MatchContext context) { |
| 223 | + return getHeader(context, "referer"); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + private static final class UserAgentProvider implements AttributeProvider { |
| 228 | + private static final Set<String> NAMES = Collections.singleton("useragent"); |
| 229 | + |
| 230 | + @Override |
| 231 | + public Iterable<String> names() { |
| 232 | + return NAMES; |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public Object get(MatchContext context) { |
| 237 | + return getHeader(context, "user-agent"); |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments