Class GrpcHttp3Adapter

java.lang.Object
org.mockserver.netty.http3.GrpcHttp3Adapter

public final class GrpcHttp3Adapter extends Object
Adapter that bridges gRPC request/response framing for the HTTP/3 path.

Reuses the existing GrpcFrameCodec, GrpcProtoDescriptorStore, and GrpcJsonMessageConverter from mockserver-core -- no logic is duplicated. The adapter handles:

  • Inbound: detecting gRPC content-type, decoding the 5-byte length-prefixed gRPC frame(s) from the request body, converting protobuf to JSON via the descriptor store, and tagging the request with x-grpc-service / x-grpc-method so the response path can re-encode.
  • Outbound: converting the matched response's JSON body back to gRPC-framed protobuf, building the initial HTTP/3 HEADERS frame (without grpc-status), the DATA frame (gRPC framed body), and a separate trailing HEADERS frame with grpc-status/grpc-message -- the correct wire framing that gRPC clients expect over HTTP/3.

This class is stateless and thread-safe; all state is passed via method parameters.

  • Method Details

    • isGrpcRequest

      public static boolean isGrpcRequest(String contentType)
      Detect whether the given content-type indicates a gRPC request. Delegates to GrpcStatusMapper.isGrpcContentType(String).
    • transformGrpcRequest

      public static HttpRequest transformGrpcRequest(HttpRequest request, GrpcProtoDescriptorStore descriptorStore)
      Transform a gRPC request for the MockServer matching pipeline: decode the gRPC length-prefixed message(s), convert protobuf to JSON via the descriptor store, and tag the request with service/method markers.
      Parameters:
      request - the raw HttpRequest with gRPC-framed binary body
      descriptorStore - the gRPC proto descriptor store
      Returns:
      a transformed HttpRequest with a JSON body suitable for matching
      Throws:
      GrpcException - if the gRPC path or framing is invalid
    • transformGrpcResponse

      public static GrpcHttp3Adapter.GrpcResponseParts transformGrpcResponse(HttpResponse response, String serviceName, String methodName, GrpcProtoDescriptorStore descriptorStore)
      Convert the response from the matching pipeline back to a gRPC-framed response. Encodes the JSON body to protobuf, wraps it in a gRPC length-prefixed frame, and determines the grpc-status.
      Parameters:
      response - the matched HttpResponse (JSON body, grpc-status-name header)
      serviceName - the gRPC service name from the original request
      methodName - the gRPC method name from the original request
      descriptorStore - the gRPC proto descriptor store
      Returns:
      a GrpcHttp3Adapter.GrpcResponseParts with the initial headers, body bytes, and trailing headers
    • errorResponse

      public static GrpcHttp3Adapter.GrpcResponseParts errorResponse(GrpcStatusMapper.GrpcStatusCode statusCode, String message)
      Build an error response (trailers-only pattern) for gRPC over HTTP/3. Used when gRPC request processing fails before matching.
      Parameters:
      statusCode - the gRPC status code
      message - the error message
      Returns:
      a GrpcHttp3Adapter.GrpcResponseParts with no body and the error status
    • buildInitialHeadersFrame

      public static io.netty.handler.codec.http3.DefaultHttp3HeadersFrame buildInitialHeadersFrame()
      Build the initial HTTP/3 HEADERS frame for a gRPC response. Contains :status=200 and content-type=application/grpc but NOT grpc-status (which belongs in the trailing HEADERS frame).
    • buildTrailingHeadersFrame

      public static io.netty.handler.codec.http3.DefaultHttp3HeadersFrame buildTrailingHeadersFrame(String grpcStatus, String grpcMessage)
      Build the trailing HTTP/3 HEADERS frame for a gRPC response. Contains grpc-status and optionally grpc-message. No pseudo-headers (no :status) because this is a trailing HEADERS frame.
    • buildDataFrame

      public static io.netty.handler.codec.http3.DefaultHttp3DataFrame buildDataFrame(byte[] grpcFrameBytes)
      Build an HTTP/3 DATA frame from gRPC-framed bytes. Returns null if the frame bytes are null or empty.
    • buildTrailersOnlyFrame

      public static io.netty.handler.codec.http3.DefaultHttp3HeadersFrame buildTrailersOnlyFrame(String grpcStatus, String grpcMessage)
      Build a "trailers-only" HTTP/3 HEADERS frame that combines :status, content-type, and grpc-status/grpc-message in a single HEADERS frame (no DATA frame follows). This is used for error responses where there is no message body.