Class ServletAdapter


  • @ExperimentalApi("https://github.com/grpc/grpc-java/issues/5066")
    public final class ServletAdapter
    extends java.lang.Object
    An adapter that transforms HttpServletRequest into gRPC request and lets a gRPC server process it, and transforms the gRPC response into HttpServletResponse. An adapter can be instantiated by ServletServerBuilder.buildServletAdapter().

    In a servlet, calling doPost(HttpServletRequest, HttpServletResponse) inside HttpServlet.doPost(HttpServletRequest, HttpServletResponse) makes the servlet backed by the gRPC server associated with the adapter. The servlet must support Asynchronous Processing and must be deployed to a container that supports servlet 4.0 and enables HTTP/2.

    The API is experimental. The authors would like to know more about the real usecases. Users are welcome to provide feedback by commenting on the tracking issue.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void destroy()
      Call this method when the adapter is no longer needed.
      void doGet​(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp)
      Call this method inside HttpServlet.doGet(HttpServletRequest, HttpServletResponse) to serve gRPC GET request.
      void doPost​(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp)
      Call this method inside HttpServlet.doPost(HttpServletRequest, HttpServletResponse) to serve gRPC POST request.
      private static java.lang.String getAuthority​(jakarta.servlet.http.HttpServletRequest req)  
      private static Metadata getHeaders​(jakarta.servlet.http.HttpServletRequest req)  
      static boolean isGrpc​(jakarta.servlet.http.HttpServletRequest request)
      Checks whether an incoming HttpServletRequest may come from a gRPC client.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • logger

        static final java.util.logging.Logger logger
      • maxInboundMessageSize

        private final int maxInboundMessageSize
    • Method Detail

      • doGet

        public void doGet​(jakarta.servlet.http.HttpServletRequest req,
                          jakarta.servlet.http.HttpServletResponse resp)
                   throws java.io.IOException
        Call this method inside HttpServlet.doGet(HttpServletRequest, HttpServletResponse) to serve gRPC GET request.

        This method is currently not implemented.

        Note that in rare case gRPC client sends GET requests.

        Do not modify req and resp before or after calling this method. However, calling resp.setBufferSize() before invocation is allowed.

        Throws:
        java.io.IOException
      • doPost

        public void doPost​(jakarta.servlet.http.HttpServletRequest req,
                           jakarta.servlet.http.HttpServletResponse resp)
                    throws java.io.IOException
        Call this method inside HttpServlet.doPost(HttpServletRequest, HttpServletResponse) to serve gRPC POST request.

        Do not modify req and resp before or after calling this method. However, calling resp.setBufferSize() before invocation is allowed.

        Throws:
        java.io.IOException
      • getHeaders

        private static Metadata getHeaders​(jakarta.servlet.http.HttpServletRequest req)
      • getAuthority

        private static java.lang.String getAuthority​(jakarta.servlet.http.HttpServletRequest req)
      • destroy

        public void destroy()
        Call this method when the adapter is no longer needed. The gRPC server will be terminated.
      • isGrpc

        public static boolean isGrpc​(jakarta.servlet.http.HttpServletRequest request)
        Checks whether an incoming HttpServletRequest may come from a gRPC client.
        Returns:
        true if the request comes from a gRPC client