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);