From 6ec2477ac7d694be9f95f76be4c8a651a018578f Mon Sep 17 00:00:00 2001 From: cyb3r4nt <104218001+cyb3r4nt@users.noreply.github.com> Date: Thu, 19 Jan 2023 16:28:20 +0200 Subject: [PATCH] #306: refactor JsonRpcServer.handle() and remove unused gzip encoding parts Signed-off-by: cyb3r4nt <104218001+cyb3r4nt@users.noreply.github.com> --- .../googlecode/jsonrpc4j/JsonRpcServer.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java b/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java index 1a45d20..16fdd49 100644 --- a/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java +++ b/src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java @@ -117,28 +117,33 @@ public void handle(HttpServletRequest request, HttpServletResponse response) thr OutputStream output = response.getOutputStream(); InputStream input = getRequestStream(request); int result = ErrorResolver.JsonError.PARSE_ERROR.code; - int contentLength = 0; + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); try { - String acceptEncoding = request.getHeader(ACCEPT_ENCODING); - result = handleRequest0(input, output, acceptEncoding, response, byteOutput); - - contentLength = byteOutput.size(); + result = handleRequest(input, byteOutput); } catch (Throwable t) { - if (StreamEndedException.class.isInstance(t)) { + if (t instanceof StreamEndedException) { logger.debug("Bad request: empty contents!"); } else { logger.error(t.getMessage(), t); } } - int httpStatusCode = httpStatusCodeProvider == null ? DefaultHttpStatusCodeProvider.INSTANCE.getHttpStatusCode(result) - : httpStatusCodeProvider.getHttpStatusCode(result); - response.setStatus(httpStatusCode); - response.setContentLength(contentLength); + + response.setStatus(resolveHttpStatusCode(result)); + response.setContentLength(byteOutput.size()); byteOutput.writeTo(output); output.flush(); } + private int resolveHttpStatusCode(int result) { + int httpStatusCode; + if (this.httpStatusCodeProvider != null) { + return this.httpStatusCodeProvider.getHttpStatusCode(result); + } else { + return DefaultHttpStatusCodeProvider.INSTANCE.getHttpStatusCode(result); + } + } + private InputStream getRequestStream(HttpServletRequest request) throws IOException { InputStream input; if (request.getMethod().equals("POST")) { @@ -151,10 +156,6 @@ private InputStream getRequestStream(HttpServletRequest request) throws IOExcept return input; } - private int handleRequest0(InputStream input, OutputStream output, String contentEncoding, HttpServletResponse response, ByteArrayOutputStream byteOutput) throws IOException { - return handleRequest(input, byteOutput); - } - private static InputStream createInputStream(HttpServletRequest request) throws IOException { String method = request.getParameter(METHOD); String id = request.getParameter(ID);