From 4df32a4404d47a31513d4a9e92bf9b0b6157e34e Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Wed, 23 Mar 2022 21:38:16 +0000 Subject: [PATCH] JAL-3851 Fix getRequestBody() --- src/jalview/rest/AbstractEndpoint.java | 20 ------------ src/jalview/rest/InputAlignmentEndpoint.java | 3 +- src/jalview/rest/RestHandler.java | 44 ++++++++++++++++++++------ 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/jalview/rest/AbstractEndpoint.java b/src/jalview/rest/AbstractEndpoint.java index deb4683..c84b7b7 100644 --- a/src/jalview/rest/AbstractEndpoint.java +++ b/src/jalview/rest/AbstractEndpoint.java @@ -1,6 +1,5 @@ package jalview.rest; -import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; @@ -102,25 +101,6 @@ public abstract class AbstractEndpoint implements EndpointI } } - protected String getRequestBody(HttpServletRequest request) - throws IOException - { - StringBuilder sb = new StringBuilder(); - BufferedReader reader = request.getReader(); - try - { - String line; - while ((line = reader.readLine()) != null) - { - sb.append(line).append('\n'); - } - } finally - { - reader.close(); - } - return sb.toString(); - } - protected boolean checkParameters(HttpServletRequest request, HttpServletResponse response, int i) { diff --git a/src/jalview/rest/InputAlignmentEndpoint.java b/src/jalview/rest/InputAlignmentEndpoint.java index 095fd24..caad39f 100644 --- a/src/jalview/rest/InputAlignmentEndpoint.java +++ b/src/jalview/rest/InputAlignmentEndpoint.java @@ -67,7 +67,8 @@ public class InputAlignmentEndpoint extends AbstractEndpointAsync access = "post"; try { - body = getRequestBody(request); + body = RestHandler.getInstance().getRequestBody(request, response); + Console.debug("BODY='" + body + "'"); } catch (IOException e) { returnError(request, response, "could not read POST body"); diff --git a/src/jalview/rest/RestHandler.java b/src/jalview/rest/RestHandler.java index caec830..c4ada0e 100644 --- a/src/jalview/rest/RestHandler.java +++ b/src/jalview/rest/RestHandler.java @@ -22,11 +22,13 @@ package jalview.rest; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.BindException; import java.util.HashMap; import java.util.Map; +import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -130,7 +132,9 @@ public class RestHandler extends AbstractRequestHandler * Currently just echoes the request; add helper classes as required to * process requests */ - System.out.println(request.toString()); + // This "pointless" call to request.getInputStream() seems to preserve the + // InputStream for use later in getRequestBody. + request.getInputStream(); if (endpoints == null) { final String queryString = request.getQueryString(); @@ -267,23 +271,43 @@ public class RestHandler extends AbstractRequestHandler } } - protected String getRequestBody(HttpServletRequest request) - throws IOException + protected String getRequestBody(HttpServletRequest request, + HttpServletResponse response) throws IOException { StringBuilder sb = new StringBuilder(); - BufferedReader reader = request.getReader(); + BufferedReader reader = null; + Console.debug("REQUEST=" + request.toString()); + Console.debug("REQUEST.Content-Lenggth=" + request.getContentLength()); try { - String line; - while ((line = reader.readLine()) != null) + reader = request.getReader(); + Console.debug("Using getReader()"); + } catch (IllegalStateException e) + { + ServletInputStream is = request.getInputStream(); + Console.debug("INPUTSTREAM " + + (is.isFinished() ? "FINISHED" : "NOT FINISHED")); + reader = new BufferedReader(new InputStreamReader(is)); + Console.debug("Using getInputStream()"); + } + if (reader != null) + { + try { - sb.append(line).append('\n'); + String line; + while ((line = reader.readLine()) != null) + { + sb.append(line).append('\n'); + } + } finally + { + reader.close(); } - } finally + } + else { - reader.close(); + returnError(request, response, "Error reading body of HTTP request"); } return sb.toString(); } - } \ No newline at end of file -- 1.7.10.2