Class GrpcWeb

java.lang.Object
com.codename1.io.grpc.GrpcWeb

public final class GrpcWeb extends Object

High-level gRPC-Web invoker used by generated @GrpcClient implementations. Handles HTTP-level transport, gRPC-Web payload framing (flags + length + payload), and trailer parsing (grpc-status, grpc-message).

Wire details (from the upstream gRPC-Web spec):

  • URL: <baseUrl>/<service-fqn>/<method-name> (no trailing slash).
  • HTTP method: POST.
  • Request Content-Type: application/grpc-web+proto.
  • Request headers: X-Grpc-Web: 1, X-User-Agent: grpc-web-cn1/<ver>.
  • Request body: 0x00 <length-be32> <payload> -- one data frame.
  • Response body: zero or more data frames (flags low bit 0) followed by one trailer frame (flags high bit 0x80) carrying grpc-status:<n>\r\ngrpc-message:<text>\r\n.

All methods are static. Generated impls call invokeUnary(String, String, String, String, Req, ProtoCodec, ProtoCodec, OnComplete) and receive the parsed GrpcResponse on the supplied callback.

  • Field Details

    • CONTENT_TYPE

      public static final String CONTENT_TYPE
      Content type for binary gRPC-Web payloads. The text variant (application/grpc-web-text, base64-encoded) is not supported -- modern Envoy/gRPC-Web proxies all speak binary.
      See Also:
    • FLAG_TRAILER

      public static final int FLAG_TRAILER
      gRPC-Web frame flag set in the trailer frame's first byte.
      See Also:
  • Method Details

    • invokeUnary

      public static <Req,Resp> void invokeUnary(String baseUrl, String service, String method, String bearerToken, Req request, ProtoCodec<Req> reqCodec, ProtoCodec<Resp> respCodec, OnComplete<GrpcResponse<Resp>> callback)

      Sends a unary gRPC-Web request and invokes callback with the decoded response (or a transport-failure marker).

      The baseUrl should not include a trailing slash; the service is the fully qualified service path (e.g. helloworld.Greeter) and method is the gRPC method name (e.g. SayHello). The resulting URL is <baseUrl>/<service>/<method>.

    • frame

      public static <T> byte[] frame(T request, ProtoCodec<T> codec) throws IOException
      Wraps a serialised request message in a single gRPC-Web data frame: [0x00][length-be32][payload]. Public so unit tests can verify framing independently of the network path.
      Throws:
      IOException
    • decode

      public static <Resp> GrpcResponse<Resp> decode(byte[] response, int httpCode, ProtoCodec<Resp> respCodec)
      Decodes a complete gRPC-Web response body. Iterates frames, concatenates data payloads, and parses the trailer frame for grpc-status / grpc-message. Public so callers can replay canned responses in unit tests.