Skip to content

Commit

Permalink
briandilley#306: add support for jakarta.servlet-api request objects …
Browse files Browse the repository at this point in the history
…into JsonRpcServer

Both javax and jakarta objects/APIs have similar method signatures and differ only with types.
Two new classes were added to adapt to the common interface.

Not very last version of jakarta.servlet-api v5.0.0 was added,
because project JDK target is Java 8.
jakarta.servlet-api v5.0.0 comes from the Jakarta EE 9,
which is still compatible with JDK 8.


Signed-off-by: cyb3r4nt <[email protected]>
  • Loading branch information
cyb3r4nt committed Jan 19, 2023
1 parent 08fc462 commit 59107e8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
82 changes: 80 additions & 2 deletions src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit 59107e8

Please sign in to comment.