Class ServerInterceptors

java.lang.Object
io.grpc.ServerInterceptors

public final class ServerInterceptors extends Object
Utility methods for working with ServerInterceptors.
  • Constructor Details

    • ServerInterceptors

      private ServerInterceptors()
  • Method Details

    • interceptForward

      public static ServerServiceDefinition interceptForward(ServerServiceDefinition serviceDef, ServerInterceptor... interceptors)
      Create a new ServerServiceDefinition whose ServerCallHandlers will call interceptors before calling the pre-existing ServerCallHandler. The first interceptor will have its ServerInterceptor.interceptCall(ServerCall, Metadata, ServerCallHandler) called first.
      Parameters:
      serviceDef - the service definition for which to intercept all its methods.
      interceptors - array of interceptors to apply to the service.
      Returns:
      a wrapped version of serviceDef with the interceptors applied.
    • interceptForward

      public static ServerServiceDefinition interceptForward(BindableService bindableService, ServerInterceptor... interceptors)
    • interceptForward

      public static ServerServiceDefinition interceptForward(ServerServiceDefinition serviceDef, List<? extends ServerInterceptor> interceptors)
      Create a new ServerServiceDefinition whose ServerCallHandlers will call interceptors before calling the pre-existing ServerCallHandler. The first interceptor will have its ServerInterceptor.interceptCall(ServerCall, Metadata, ServerCallHandler) called first.
      Parameters:
      serviceDef - the service definition for which to intercept all its methods.
      interceptors - list of interceptors to apply to the service.
      Returns:
      a wrapped version of serviceDef with the interceptors applied.
    • interceptForward

      public static ServerServiceDefinition interceptForward(BindableService bindableService, List<? extends ServerInterceptor> interceptors)
    • intercept

      public static ServerServiceDefinition intercept(ServerServiceDefinition serviceDef, ServerInterceptor... interceptors)
      Create a new ServerServiceDefinition whose ServerCallHandlers will call interceptors before calling the pre-existing ServerCallHandler. The last interceptor will have its ServerInterceptor.interceptCall(ServerCall, Metadata, ServerCallHandler) called first.
      Parameters:
      serviceDef - the service definition for which to intercept all its methods.
      interceptors - array of interceptors to apply to the service.
      Returns:
      a wrapped version of serviceDef with the interceptors applied.
    • intercept

      public static ServerServiceDefinition intercept(BindableService bindableService, ServerInterceptor... interceptors)
    • intercept

      public static ServerServiceDefinition intercept(ServerServiceDefinition serviceDef, List<? extends ServerInterceptor> interceptors)
      Create a new ServerServiceDefinition whose ServerCallHandlers will call interceptors before calling the pre-existing ServerCallHandler. The last interceptor will have its ServerInterceptor.interceptCall(ServerCall, Metadata, ServerCallHandler) called first.
      Parameters:
      serviceDef - the service definition for which to intercept all its methods.
      interceptors - list of interceptors to apply to the service.
      Returns:
      a wrapped version of serviceDef with the interceptors applied.
    • intercept

      public static ServerServiceDefinition intercept(BindableService bindableService, List<? extends ServerInterceptor> interceptors)
    • useInputStreamMessages

      @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1712") public static ServerServiceDefinition useInputStreamMessages(ServerServiceDefinition serviceDef)
      Create a new ServerServiceDefinition whose MethodDescriptor serializes to and from InputStream for all methods. The InputStream is guaranteed return true for markSupported(). The ServerCallHandler created will automatically convert back to the original types for request and response before calling the existing ServerCallHandler. Calling this method combined with the intercept methods will allow the developer to choose whether to intercept messages of InputStream, or the modeled types of their application.
      Parameters:
      serviceDef - the service definition to convert messages to InputStream
      Returns:
      a wrapped version of serviceDef with the InputStream conversion applied.
    • useMarshalledMessages

      @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1712") public static <T> ServerServiceDefinition useMarshalledMessages(ServerServiceDefinition serviceDef, MethodDescriptor.Marshaller<T> marshaller)
      Create a new ServerServiceDefinition whose MethodDescriptor serializes to and from T for all methods. The ServerCallHandler created will automatically convert back to the original types for request and response before calling the existing ServerCallHandler. Calling this method combined with the intercept methods will allow the developer to choose whether to intercept messages of T, or the modeled types of their application. This can also be chained to allow for interceptors to handle messages as multiple different T types within the chain if the added cost of serialization is not a concern.
      Parameters:
      serviceDef - the service definition to convert messages to T
      Returns:
      a wrapped version of serviceDef with the T conversion applied.
    • useMarshalledMessages

      @ExperimentalApi("https://github.com/grpc/grpc-java/issues/9870") public static <ReqT,RespT> ServerServiceDefinition useMarshalledMessages(ServerServiceDefinition serviceDef, MethodDescriptor.Marshaller<ReqT> requestMarshaller, MethodDescriptor.Marshaller<RespT> responseMarshaller)
      Create a new ServerServiceDefinition with MethodDescriptor for deserializing requests and separate MethodDescriptor for serializing responses. The ServerCallHandler created will automatically convert back to the original types for request and response before calling the existing ServerCallHandler. Calling this method combined with the intercept methods will allow the developer to choose whether to intercept messages of ReqT/RespT, or the modeled types of their application. This can also be chained to allow for interceptors to handle messages as multiple different ReqT/RespT types within the chain if the added cost of serialization is not a concern.
      Type Parameters:
      ReqT - the request payload type
      RespT - the response payload type.
      Parameters:
      serviceDef - the service definition to add request and response marshallers to.
      requestMarshaller - request marshaller
      responseMarshaller - response marshaller
      Returns:
      a wrapped version of serviceDef with the ReqT and RespT conversion applied.
    • wrapAndAddMethod

      private static <ReqT,RespT> void wrapAndAddMethod(ServerServiceDefinition.Builder serviceDefBuilder, ServerMethodDefinition<ReqT,RespT> method, List<? extends ServerInterceptor> interceptors)
    • wrapMethod

      static <OReqT,ORespT,WReqT,WRespT> ServerMethodDefinition<WReqT,WRespT> wrapMethod(ServerMethodDefinition<OReqT,ORespT> definition, MethodDescriptor<WReqT,WRespT> wrappedMethod)
    • wrapHandler

      private static <OReqT,ORespT,WReqT,WRespT> ServerCallHandler<WReqT,WRespT> wrapHandler(ServerCallHandler<OReqT,ORespT> originalHandler, MethodDescriptor<OReqT,ORespT> originalMethod, MethodDescriptor<WReqT,WRespT> wrappedMethod)