Package org.mockserver.netty.http3
Class GrpcHttp3Adapter
java.lang.Object
org.mockserver.netty.http3.GrpcHttp3Adapter
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-methodso 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classHolds the parts of a gRPC response for HTTP/3 framing: the gRPC-framed body bytes, the grpc-status value, and an optional grpc-message. -
Method Summary
Modifier and TypeMethodDescriptionstatic io.netty.handler.codec.http3.DefaultHttp3DataFramebuildDataFrame(byte[] grpcFrameBytes) Build an HTTP/3 DATA frame from gRPC-framed bytes.static io.netty.handler.codec.http3.DefaultHttp3HeadersFrameBuild the initial HTTP/3 HEADERS frame for a gRPC response.static io.netty.handler.codec.http3.DefaultHttp3HeadersFramebuildTrailersOnlyFrame(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).static io.netty.handler.codec.http3.DefaultHttp3HeadersFramebuildTrailingHeadersFrame(String grpcStatus, String grpcMessage) Build the trailing HTTP/3 HEADERS frame for a gRPC response.errorResponse(GrpcStatusMapper.GrpcStatusCode statusCode, String message) Build an error response (trailers-only pattern) for gRPC over HTTP/3.static booleanisGrpcRequest(String contentType) Detect whether the given content-type indicates a gRPC request.static HttpRequesttransformGrpcRequest(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.transformGrpcResponse(HttpResponse response, String serviceName, String methodName, GrpcProtoDescriptorStore descriptorStore) Convert the response from the matching pipeline back to a gRPC-framed response.
-
Method Details
-
isGrpcRequest
Detect whether the given content-type indicates a gRPC request. Delegates toGrpcStatusMapper.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 bodydescriptorStore- 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 requestmethodName- the gRPC method name from the original requestdescriptorStore- the gRPC proto descriptor store- Returns:
- a
GrpcHttp3Adapter.GrpcResponsePartswith 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 codemessage- the error message- Returns:
- a
GrpcHttp3Adapter.GrpcResponsePartswith 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.
-