Package org.mockserver.grpc
Class GrpcForwardTranslator
java.lang.Object
org.mockserver.grpc.GrpcForwardTranslator
Translates a MockServer-internal gRPC exchange (the JSON representation that
GrpcToHttpRequestHandler produces after decoding an inbound protobuf request) to and from
the wire form a real upstream gRPC server expects, so that a matched FORWARD-class
expectation — or the anonymous proxy no-match path — can relay a gRPC call to an upstream service
and record the decoded exchange.
Two symmetric transforms:
encodeRequestForUpstream(HttpRequest, GrpcProtoDescriptorStore)— turns the JSON request body (single message, or a JSON array for client-streaming) back into gRPC length-prefixed protobuf frames, setscontent-type: application/grpc, forces HTTP/2, addste: trailers, and strips the internalx-grpc-*helper headers so they do not leak upstream.decodeResponseFromUpstream(HttpResponse, String, String, GrpcProtoDescriptorStore)— decodes the upstream's gRPC-framed protobuf response back to JSON and re-stampsx-grpc-service/x-grpc-method(andgrpc-status-name) onto the response. The JSON body makes the loggedFORWARDED_REQUESTentry replayable as a mock, and the stamped headers letGrpcToHttpResponseHandlerre-frame the response to protobuf for the calling gRPC client.
Both transforms are fail-safe: when the request is not a decoded gRPC exchange, the descriptor store has no matching method, or any conversion error occurs, the original message is returned unchanged so ordinary HTTP forwarding is never disrupted.
Boundary: unary and client-streaming request bodies (single JSON object / JSON array) and unary or server-streaming responses (one or more frames) are handled. Full bidirectional streaming forward is out of scope here — it is driven by the multiplex bidi pipeline, not the request/response forward path.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpResponsedecodeResponseFromUpstream(HttpResponse response, String service, String method, GrpcProtoDescriptorStore store) Decodes an upstream gRPC-framed protobuf response back to JSON and stamps thex-grpc-service/x-grpc-methodheaders (andgrpc-status-name) so the response is both replayable (JSON body in the recorded exchange) and re-framable to protobuf byGrpcToHttpResponseHandlerfor the calling client.static HttpRequestencodeRequestForUpstream(HttpRequest request, GrpcProtoDescriptorStore store) Re-encodes a decoded gRPC request's JSON body back to gRPC-framed protobuf for an upstream call.static booleanisGrpcForwardRequest(HttpRequest request) Whetherrequestis a decoded gRPC exchange eligible for upstream re-encoding — it must carry a non-emptyx-grpc-serviceandx-grpc-methodand anapplication/grpccontent-type (the shapeGrpcToHttpRequestHandlerproduces).
-
Field Details
-
SERVICE_HEADER
- See Also:
-
METHOD_HEADER
- See Also:
-
ORIGINAL_CONTENT_TYPE_HEADER
- See Also:
-
CLIENT_STREAMING_HEADER
- See Also:
-
-
Method Details
-
isGrpcForwardRequest
Whetherrequestis a decoded gRPC exchange eligible for upstream re-encoding — it must carry a non-emptyx-grpc-serviceandx-grpc-methodand anapplication/grpccontent-type (the shapeGrpcToHttpRequestHandlerproduces). -
encodeRequestForUpstream
public static HttpRequest encodeRequestForUpstream(HttpRequest request, GrpcProtoDescriptorStore store) Re-encodes a decoded gRPC request's JSON body back to gRPC-framed protobuf for an upstream call. Returns the original request unchanged when it is not an eligible gRPC exchange, when the store has no matching method descriptor, or when conversion fails. -
decodeResponseFromUpstream
public static HttpResponse decodeResponseFromUpstream(HttpResponse response, String service, String method, GrpcProtoDescriptorStore store) Decodes an upstream gRPC-framed protobuf response back to JSON and stamps thex-grpc-service/x-grpc-methodheaders (andgrpc-status-name) so the response is both replayable (JSON body in the recorded exchange) and re-framable to protobuf byGrpcToHttpResponseHandlerfor the calling client. Returns the original response when the store has no matching method descriptor or when conversion fails.
-