Skip to content

Commit

Permalink
briandilley#306: extract common HTTP request and response interfaces …
Browse files Browse the repository at this point in the history
…inside JsonRpcServer

Common logic may be used to handle the HTTP requests.
This is a preparation for using jakarta.servlet.http namespace objects.
There are no much requirements for those interfaces,
only some common methods, which are present in most

Portlet API requests were not changed, because there is a comment,
which assumes that HTTP status code is assigned outside.
Added a TODO comment there with possible solution proposal.


Signed-off-by: cyb3r4nt <[email protected]>
  • Loading branch information
cyb3r4nt committed Jan 19, 2023
1 parent 6ec2477 commit 08fc462
Showing 1 changed file with 82 additions and 5 deletions.
87 changes: 82 additions & 5 deletions src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public void handle(ResourceRequest request, ResourceResponse response) throws IO
OutputStream output = response.getPortletOutputStream();
handleRequest(input, output);
// fix to not flush within handleRequest() but outside so http status code can be set
// TODO: this logic may be changed to use handleCommon() method,
// HTTP status code may be provided by the HttpStatusCodeProvider extension
output.flush();
}

Expand All @@ -112,7 +114,11 @@ private static InputStream createInputStream(ResourceRequest request) throws IOE
* @throws IOException on error
*/
public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
logger.debug("Handling HttpServletRequest {}", request);
handleCommon(new JavaxHttpServletRequest(request), new JavaxHttpServletResponse(response));
}

private void handleCommon(CommonHttpServletRequest request, CommonHttpServletResponse response) throws IOException {
logger.debug("Handling HttpServletRequest {}", request.unwrap());
response.setContentType(contentType);
OutputStream output = response.getOutputStream();
InputStream input = getRequestStream(request);
Expand Down Expand Up @@ -144,19 +150,19 @@ private int resolveHttpStatusCode(int result) {
}
}

private InputStream getRequestStream(HttpServletRequest request) throws IOException {
private InputStream getRequestStream(CommonHttpServletRequest request) throws IOException {
InputStream input;
if (request.getMethod().equals("POST")) {
if ("POST".equals(request.getMethod())) {
input = request.getInputStream();
} else if (request.getMethod().equals("GET")) {
} else if ("GET".equals(request.getMethod())) {
input = createInputStream(request);
} else {
throw new IOException("Invalid request method, only POST and GET is supported");
}
return input;
}

private static InputStream createInputStream(HttpServletRequest request) throws IOException {
private static InputStream createInputStream(CommonHttpServletRequest request) throws IOException {
String method = request.getParameter(METHOD);
String id = request.getParameter(ID);
String params = request.getParameter(PARAMS);
Expand All @@ -171,4 +177,75 @@ public void setContentType(String contentType) {
this.contentType = contentType;
}

private interface CommonHttpServletRequest {
Object unwrap();
InputStream getInputStream() throws IOException;
String getMethod();
String getParameter(String name);
}

private static class JavaxHttpServletRequest implements CommonHttpServletRequest {

private final HttpServletRequest request;

private JavaxHttpServletRequest(HttpServletRequest request) {
this.request = request;
}

@Override
public HttpServletRequest 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 interface CommonHttpServletResponse {
void setContentType(String type);
void setStatus(int sc);
void setContentLength(int len);
OutputStream getOutputStream() throws IOException;
}

private static class JavaxHttpServletResponse implements CommonHttpServletResponse {

private final HttpServletResponse response;

private JavaxHttpServletResponse(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 08fc462

Please sign in to comment.