diff --git a/build.gradle b/build.gradle index 186e388..c9eed1c 100644 --- a/build.gradle +++ b/build.gradle @@ -96,6 +96,9 @@ dependencies { servletSupportImplementation 'javax.portlet:portlet-api:3.0.1' servletSupportImplementation 'javax.servlet:javax.servlet-api:4.0.1' + // TODO: Jakarta EE 9 and jakarta.servlet-api 5.x are still compatible with Java SE 8, + // update jakarta.servlet-api to version 6+ when JDK baseline is increased to 11+ + servletSupportImplementation 'jakarta.servlet:jakarta.servlet-api:5.0.0' implementation "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}" implementation "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}" diff --git a/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java b/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java index 4e03106..d58ba39 100644 --- a/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java +++ b/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java @@ -114,7 +114,27 @@ private static InputStream createInputStream(ResourceRequest request) throws IOE * @throws IOException on error */ public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException { - handleCommon(new JavaxHttpServletRequest(request), new JavaxHttpServletResponse(response)); + handleCommon( + new JavaxHttpServletRequest(request), + new JavaxHttpServletResponse(response) + ); + } + + /** + * Handles a servlet request. + * + * @param request the {@link jakarta.servlet.http.HttpServletRequest} + * @param response the {@link jakarta.servlet.http.HttpServletResponse} + * @throws IOException on error + */ + public void handle( + jakarta.servlet.http.HttpServletRequest request, + jakarta.servlet.http.HttpServletResponse response + ) throws IOException { + handleCommon( + new JakartaHttpServletRequest(request), + new JakartaHttpServletResponse(response) + ); } private void handleCommon(CommonHttpServletRequest request, CommonHttpServletResponse response) throws IOException { @@ -193,7 +213,36 @@ private JavaxHttpServletRequest(HttpServletRequest request) { } @Override - public HttpServletRequest unwrap() { + public Object unwrap() { + return this.request; + } + + @Override + public InputStream getInputStream() throws IOException { + return this.request.getInputStream(); + } + + @Override + public String getMethod() { + return this.request.getMethod(); + } + + @Override + public String getParameter(String name) { + return this.request.getParameter(name); + } + } + + private static class JakartaHttpServletRequest implements CommonHttpServletRequest { + + private final jakarta.servlet.http.HttpServletRequest request; + + private JakartaHttpServletRequest(jakarta.servlet.http.HttpServletRequest request) { + this.request = request; + } + + @Override + public Object unwrap() { return this.request; } @@ -248,4 +297,33 @@ public OutputStream getOutputStream() throws IOException { return this.response.getOutputStream(); } } + + private static class JakartaHttpServletResponse implements CommonHttpServletResponse { + + private final jakarta.servlet.http.HttpServletResponse response; + + private JakartaHttpServletResponse(jakarta.servlet.http.HttpServletResponse response) { + this.response = response; + } + + @Override + public void setContentType(String type) { + this.response.setContentType(type); + } + + @Override + public void setStatus(int sc) { + this.response.setStatus(sc); + } + + @Override + public void setContentLength(int len) { + this.response.setContentLength(len); + } + + @Override + public OutputStream getOutputStream() throws IOException { + return this.response.getOutputStream(); + } + } }