diff --git a/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java b/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java index 16fdd49..4e03106 100644 --- a/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java +++ b/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java @@ -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(); } @@ -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); @@ -144,11 +150,11 @@ 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"); @@ -156,7 +162,7 @@ private InputStream getRequestStream(HttpServletRequest request) throws IOExcept 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); @@ -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(); + } + } }