Class GrpcServer

java.lang.Object
com.codename1.backend.grpc.GrpcServer
All Implemented Interfaces:
HttpServer.Handler

public final class GrpcServer extends Object implements HttpServer.Handler

Serves gRPC: native gRPC over HTTP/2, and gRPC-Web over HTTP/1.1 or HTTP/2 for browsers and for the Codename One client's GrpcWeb.

The build creates one from the module's @GrpcService beans and the HTTP server routes every application/grpc* request on an HTTP/2 stream to it. It is also an ordinary HttpServer.Handler, which is how gRPC-Web reaches it over HTTP/1.1.

Rules it keeps, each of which a real client depends on:

  • The answer is always HTTP 200 with the outcome in grpc-status; a call that fails before its first message is a "trailers-only" response.
  • An unknown method is UNIMPLEMENTED, a grpc-encoding other than identity or gzip is UNIMPLEMENTED with the accepted encodings named.
  • A unary or server streaming call that sends no request, or more than one, is INTERNAL, as in grpc-java.
  • The grpc-timeout deadline is checked before the handler runs, before each response message and after the handler returns. A handler blocked in its own code is not interrupted; GrpcContext.isDeadlineExceeded() lets it stop.
  • An exception that is not a GrpcException is logged here and reported as UNKNOWN with no message, so an internal error text never reaches a peer.

Not supported: application/grpc-web-text (base64 framing), and server reflection. Calls on one HTTP/2 connection are served one at a time, in the connection's turn, as the server serves every HTTP/2 request; a client that wants concurrent streaming calls opens more connections.

  • Constructor Details

    • GrpcServer

      public GrpcServer()
  • Method Details

    • add

      public GrpcServer add(GrpcMethod method)
      Adds a method. A second method on the same path replaces the first.
      Parameters:
      method - the method
      Returns:
      this
    • setMaxMessageSize

      public GrpcServer setMaxMessageSize(int bytes)
      The largest request message accepted, compressed or not.
      Parameters:
      bytes - the limit; 4 MB by default
      Returns:
      this
    • has

      public boolean has(String path)
      Parameters:
      path - /package.Service/Method
      Returns:
      whether this server has the method
    • isGrpc

      public static boolean isGrpc(String contentType)
      Whether a content type is one this server answers.
      Parameters:
      contentType - the request's content type, may be null
      Returns:
      true for application/grpc and its variants
    • handle

      public HttpServer.Response handle(HttpServer.Request request) throws Exception
      Answers gRPC-Web requests arriving as ordinary HTTP requests; anything else is left to the next handler.
      Specified by:
      handle in interface HttpServer.Handler
      Throws:
      Exception
    • serve

      public void serve(GrpcServerTransport transport, String target, Map<String,String> headers)
      Serves one call over a transport. Never throws: every outcome becomes a gRPC status, and a transport that fails ends the call silently.
      Parameters:
      transport - the HTTP side
      target - the request target, /package.Service/Method
      headers - the request headers, lower-case names