Search in sources :

Example 1 with ClientInterceptorBuilder

use of io.grpc.xds.Filter.ClientInterceptorBuilder in project grpc-java by grpc.

the class ClientXdsClient method parseHttpFilter.

@VisibleForTesting
// Returns null if the filter is optional but not supported.
@Nullable
static StructOrError<FilterConfig> parseHttpFilter(io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpFilter httpFilter, FilterRegistry filterRegistry, boolean isForClient) {
    String filterName = httpFilter.getName();
    boolean isOptional = httpFilter.getIsOptional();
    if (!httpFilter.hasTypedConfig()) {
        if (isOptional) {
            return null;
        } else {
            return StructOrError.fromError("HttpFilter [" + filterName + "] is not optional and has no typed config");
        }
    }
    Message rawConfig = httpFilter.getTypedConfig();
    String typeUrl = httpFilter.getTypedConfig().getTypeUrl();
    try {
        if (typeUrl.equals(TYPE_URL_TYPED_STRUCT_UDPA)) {
            TypedStruct typedStruct = httpFilter.getTypedConfig().unpack(TypedStruct.class);
            typeUrl = typedStruct.getTypeUrl();
            rawConfig = typedStruct.getValue();
        } else if (typeUrl.equals(TYPE_URL_TYPED_STRUCT)) {
            com.github.xds.type.v3.TypedStruct newTypedStruct = httpFilter.getTypedConfig().unpack(com.github.xds.type.v3.TypedStruct.class);
            typeUrl = newTypedStruct.getTypeUrl();
            rawConfig = newTypedStruct.getValue();
        }
    } catch (InvalidProtocolBufferException e) {
        return StructOrError.fromError("HttpFilter [" + filterName + "] contains invalid proto: " + e);
    }
    Filter filter = filterRegistry.get(typeUrl);
    if ((isForClient && !(filter instanceof ClientInterceptorBuilder)) || (!isForClient && !(filter instanceof ServerInterceptorBuilder))) {
        if (isOptional) {
            return null;
        } else {
            return StructOrError.fromError("HttpFilter [" + filterName + "](" + typeUrl + ") is required but unsupported for " + (isForClient ? "client" : "server"));
        }
    }
    ConfigOrError<? extends FilterConfig> filterConfig = filter.parseFilterConfig(rawConfig);
    if (filterConfig.errorDetail != null) {
        return StructOrError.fromError("Invalid filter config for HttpFilter [" + filterName + "]: " + filterConfig.errorDetail);
    }
    return StructOrError.fromStruct(filterConfig.config);
}
Also used : Message(com.google.protobuf.Message) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) TypedStruct(com.github.udpa.udpa.type.v1.TypedStruct) ServerInterceptorBuilder(io.grpc.xds.Filter.ServerInterceptorBuilder) ClientInterceptorBuilder(io.grpc.xds.Filter.ClientInterceptorBuilder) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Aggregations

TypedStruct (com.github.udpa.udpa.type.v1.TypedStruct)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Message (com.google.protobuf.Message)1 ClientInterceptorBuilder (io.grpc.xds.Filter.ClientInterceptorBuilder)1 ServerInterceptorBuilder (io.grpc.xds.Filter.ServerInterceptorBuilder)1 Nullable (javax.annotation.Nullable)1