1 /* 2 * Copyright 2021 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; 18 19 import com.google.common.base.MoreObjects; 20 import com.google.protobuf.Message; 21 import io.grpc.ClientInterceptor; 22 import io.grpc.LoadBalancer.PickSubchannelArgs; 23 import io.grpc.ServerInterceptor; 24 import java.util.Objects; 25 import java.util.concurrent.ScheduledExecutorService; 26 import javax.annotation.Nullable; 27 28 /** 29 * Defines the parsing functionality of an HTTP filter. A Filter may optionally implement either 30 * {@link ClientInterceptorBuilder} or {@link ServerInterceptorBuilder} or both, indicating it is 31 * capable of working on the client side or server side or both, respectively. 32 */ 33 interface Filter { 34 35 /** 36 * The proto message types supported by this filter. A filter will be registered by each of its 37 * supported message types. 38 */ typeUrls()39 String[] typeUrls(); 40 41 /** 42 * Parses the top-level filter config from raw proto message. The message may be either a {@link 43 * com.google.protobuf.Any} or a {@link com.google.protobuf.Struct}. 44 */ parseFilterConfig(Message rawProtoMessage)45 ConfigOrError<? extends FilterConfig> parseFilterConfig(Message rawProtoMessage); 46 47 /** 48 * Parses the per-filter override filter config from raw proto message. The message may be either 49 * a {@link com.google.protobuf.Any} or a {@link com.google.protobuf.Struct}. 50 */ parseFilterConfigOverride(Message rawProtoMessage)51 ConfigOrError<? extends FilterConfig> parseFilterConfigOverride(Message rawProtoMessage); 52 53 /** Represents an opaque data structure holding configuration for a filter. */ 54 interface FilterConfig { typeUrl()55 String typeUrl(); 56 } 57 58 /** Uses the FilterConfigs produced above to produce an HTTP filter interceptor for clients. */ 59 interface ClientInterceptorBuilder { 60 @Nullable buildClientInterceptor( FilterConfig config, @Nullable FilterConfig overrideConfig, PickSubchannelArgs args, ScheduledExecutorService scheduler)61 ClientInterceptor buildClientInterceptor( 62 FilterConfig config, @Nullable FilterConfig overrideConfig, PickSubchannelArgs args, 63 ScheduledExecutorService scheduler); 64 } 65 66 /** Uses the FilterConfigs produced above to produce an HTTP filter interceptor for the server. */ 67 interface ServerInterceptorBuilder { 68 @Nullable buildServerInterceptor( FilterConfig config, @Nullable FilterConfig overrideConfig)69 ServerInterceptor buildServerInterceptor( 70 FilterConfig config, @Nullable FilterConfig overrideConfig); 71 } 72 73 /** Filter config with instance name. */ 74 final class NamedFilterConfig { 75 // filter instance name 76 final String name; 77 final FilterConfig filterConfig; 78 NamedFilterConfig(String name, FilterConfig filterConfig)79 NamedFilterConfig(String name, FilterConfig filterConfig) { 80 this.name = name; 81 this.filterConfig = filterConfig; 82 } 83 84 @Override equals(Object o)85 public boolean equals(Object o) { 86 if (this == o) { 87 return true; 88 } 89 if (o == null || getClass() != o.getClass()) { 90 return false; 91 } 92 NamedFilterConfig that = (NamedFilterConfig) o; 93 return Objects.equals(name, that.name) 94 && Objects.equals(filterConfig, that.filterConfig); 95 } 96 97 @Override hashCode()98 public int hashCode() { 99 return Objects.hash(name, filterConfig); 100 } 101 102 @Override toString()103 public String toString() { 104 return MoreObjects.toStringHelper(this) 105 .add("name", name) 106 .add("filterConfig", filterConfig) 107 .toString(); 108 } 109 } 110 } 111