Class GrpcForwardTranslator

java.lang.Object
org.mockserver.grpc.GrpcForwardTranslator

public class GrpcForwardTranslator extends Object
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, sets content-type: application/grpc, forces HTTP/2, adds te: trailers, and strips the internal x-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-stamps x-grpc-service/x-grpc-method (and grpc-status-name) onto the response. The JSON body makes the logged FORWARDED_REQUEST entry replayable as a mock, and the stamped headers let GrpcToHttpResponseHandler re-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 Details

  • Method Details

    • isGrpcForwardRequest

      public static boolean isGrpcForwardRequest(HttpRequest request)
      Whether request is a decoded gRPC exchange eligible for upstream re-encoding — it must carry a non-empty x-grpc-service and x-grpc-method and an application/grpc content-type (the shape GrpcToHttpRequestHandler produces).
    • 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 the x-grpc-service/x-grpc-method headers (and grpc-status-name) so the response is both replayable (JSON body in the recorded exchange) and re-framable to protobuf by GrpcToHttpResponseHandler for the calling client. Returns the original response when the store has no matching method descriptor or when conversion fails.