Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2495095
feat[ServletAdapter]: Ability remove context path when get method name
long76 Dec 9, 2024
4bb9d65
fix: getMethod must be not static
long76 Dec 9, 2024
2f4aea5
fix: wrong realisation
long76 Dec 9, 2024
33ed4c6
fix[ServletAdapter]: java doc
long76 Dec 9, 2024
9ffaeb7
fix: code style
long76 Dec 9, 2024
bb7a6d4
fix[ServletServerBuilderTest]: compile test
long76 Dec 9, 2024
2c1c2f1
fix[bb7a6d4]: var name
long76 Dec 9, 2024
27a99bc
fix[HelloWorldServlet]: example
long76 Dec 9, 2024
21e48be
fix[ServletServerBuilderTest]: use verify
long76 Dec 9, 2024
ed0fdbd
fix: use hardcoded method
long76 Dec 9, 2024
2609bca
Merge branch 'grpc:master' into task-5066-context-path
long76 Jan 14, 2025
e20b3df
refactor: tests and realization
long76 Jan 14, 2025
6c0c161
Merge branch 'grpc:master' into task-5066-context-path
long76 Jan 29, 2025
ecb51d4
fix(GrpcServlet): check getServletConfig on null before getInitParam…
long76 Jan 29, 2025
8956729
fix(GrpcServlet): getInitParameter in init
long76 Jan 29, 2025
68fe5ac
tests: add GrpcServletTest for getMethod
long76 Jan 29, 2025
a216eb9
fix(GrpcServlet): import ServletException
long76 Jan 29, 2025
76b49cf
style(GrpcServletTest): remove empty line
long76 Jan 29, 2025
462b92d
refactor(GrpcServlet): removeContextPath move this assignment to the …
long76 Mar 11, 2025
53018ca
docs(GrpcServlet): add comment for REMOVE_CONTEXT_PATH
long76 Mar 12, 2025
41a3f02
docs(GrpcServlet): fix typo
long76 Mar 12, 2025
fc9a782
refactor: remove init param, just override getMethod
long76 Sep 4, 2025
11ba23c
docs: fix javadoc link
long76 Sep 4, 2025
be20172
fix: remove unused import
long76 Sep 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion servlet/src/main/java/io/grpc/servlet/GrpcServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
* A simple servlet backed by a gRPC server. Must set {@code asyncSupported} to true. The {@code
* /contextRoot/urlPattern} must match the gRPC services' path, which is
* "/full-service-name/short-method-name".
* If you use application server and want to get access to grpc from non root path
* for example {@code /deployment-name/full-service-name/short-method-name}
* you must override {@link #getMethod(HttpServletRequest)}.
* <a href=https://github.com/grpc/grpc-java/pull/11825>More info</a>.
*
* <p>The API is experimental. The authors would like to know more about the real usecases. Users
* are welcome to provide feedback by commenting on
Expand Down Expand Up @@ -58,6 +62,10 @@ private static ServletAdapter loadServices(List<? extends BindableService> binda
return serverBuilder.buildServletAdapter();
}

protected String getMethod(HttpServletRequest req) {
return req.getRequestURI().substring(1); // remove the leading "/"
}

@Override
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
Expand All @@ -67,7 +75,7 @@ protected final void doGet(HttpServletRequest request, HttpServletResponse respo
@Override
protected final void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
servletAdapter.doPost(request, response);
servletAdapter.doPost(getMethod(request), request, response);
}

@Override
Expand Down
17 changes: 11 additions & 6 deletions servlet/src/main/java/io/grpc/servlet/ServletAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@
* process it, and transforms the gRPC response into {@link HttpServletResponse}. An adapter can be
* instantiated by {@link ServletServerBuilder#buildServletAdapter()}.
*
* <p>In a servlet, calling {@link #doPost(HttpServletRequest, HttpServletResponse)} inside {@link
* javax.servlet.http.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.
* <p>In a servlet, calling {@link #doPost(String, HttpServletRequest, HttpServletResponse)} inside
* {@link javax.servlet.http.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.
*
* <p>The API is experimental. The authors would like to know more about the real usecases. Users
* are welcome to provide feedback by commenting on
Expand Down Expand Up @@ -103,14 +104,19 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "GET method not supported");
}

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
doPost(req.getRequestURI().substring(1), req, resp);
}

/**
* Call this method inside {@link javax.servlet.http.HttpServlet#doPost(HttpServletRequest,
* HttpServletResponse)} to serve gRPC POST request.
*
* <p>Do not modify {@code req} and {@code resp} before or after calling this method. However,
* calling {@code resp.setBufferSize()} before invocation is allowed.
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
public void doPost(String method, HttpServletRequest req, HttpServletResponse resp)
throws IOException {
checkArgument(req.isAsyncSupported(), "servlet does not support asynchronous operation");
checkArgument(ServletAdapter.isGrpc(req), "the request is not a gRPC request");

Expand All @@ -119,7 +125,6 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx

AsyncContext asyncCtx = req.startAsync(req, resp);

String method = req.getRequestURI().substring(1); // remove the leading "/"
Metadata headers = getHeaders(req);

if (logger.isLoggable(FINEST)) {
Expand Down