Class GrpcWebTranslator

java.lang.Object
org.mockserver.grpc.GrpcWebTranslator

public class GrpcWebTranslator extends Object
Translates between gRPC-Web framing and standard gRPC framing.

gRPC-Web uses the same length-prefixed message framing as gRPC but differs in two ways:

  • Trailers-in-body: gRPC status trailers are sent as a special frame with flag byte 0x80 followed by ASCII-encoded trailer lines (grpc-status: N\r\ngrpc-message: ...\r\n)
  • Base64 encoding (-text variant): when the content-type is application/grpc-web-text, the entire body (message frames + trailer frame) is base64-encoded

This class provides the conversion layer so gRPC-Web requests can be fed into the existing gRPC pipeline unchanged and gRPC responses can be re-framed as gRPC-Web.

  • Field Details

  • Constructor Details

    • GrpcWebTranslator

      public GrpcWebTranslator()
  • Method Details

    • isGrpcWebContentType

      public static boolean isGrpcWebContentType(String contentType)
      Returns true if the content-type indicates a gRPC-Web request (binary or text variant).
    • isGrpcWebTextContentType

      public static boolean isGrpcWebTextContentType(String contentType)
      Returns true if the content-type is the base64-encoded text variant.
    • decodeRequestBody

      public static byte[] decodeRequestBody(byte[] body, String contentType)
      Decodes the request body from gRPC-Web framing to standard gRPC framing. For the -text variant, the body is base64-decoded first. The returned bytes are standard gRPC length-prefixed message frames that can be fed directly into GrpcFrameCodec.decode(byte[]).
      Parameters:
      body - the raw request body
      contentType - the content-type header value
      Returns:
      standard gRPC-framed message bytes
    • encodeResponseBody

      public static byte[] encodeResponseBody(byte[] messageFrameBody, String grpcStatus, String grpcMessage, boolean isTextVariant)
      Builds a gRPC-Web response body from a standard gRPC response.

      The response consists of message frame(s) followed by a trailer frame (flag 0x80) containing the grpc-status and optional grpc-message as ASCII lines.

      Parameters:
      messageFrameBody - the gRPC-framed message body (may be empty/null)
      grpcStatus - the gRPC status code as a string (e.g. "0")
      grpcMessage - the gRPC status message (may be null)
      isTextVariant - if true, the entire body is base64-encoded
      Returns:
      the gRPC-Web response body bytes
    • buildTrailerFrame

      public static byte[] buildTrailerFrame(String grpcStatus, String grpcMessage)
      Builds a gRPC-Web trailer frame: flag byte 0x80, 4-byte length, then ASCII-encoded trailer lines.
    • parseTrailerFrame

      public static String parseTrailerFrame(byte[] trailerBody)
      Parses trailers from a gRPC-Web trailer frame body. The trailer frame body is the ASCII text after the 5-byte header.
      Parameters:
      trailerBody - the raw trailer body bytes (without the 5-byte frame header)
      Returns:
      parsed trailer lines as a string
    • responseContentType

      public static String responseContentType(String requestContentType)
      Returns the appropriate gRPC-Web response content-type based on the original request content-type.
      Parameters:
      requestContentType - the original gRPC-Web request content-type
      Returns:
      the matching gRPC-Web response content-type