Skip to content

Commit

Permalink
briandilley#306: refactor JsonRpcServer.handle() and remove unused gz…
Browse files Browse the repository at this point in the history
…ip encoding parts

Signed-off-by: cyb3r4nt <[email protected]>
  • Loading branch information
cyb3r4nt committed Jan 19, 2023
1 parent 3706d07 commit 6ec2477
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/main/java/com/googlecode/jsonrpc4j/JsonRpcServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand All @@ -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);
Expand Down

0 comments on commit 6ec2477

Please sign in to comment.