JAL-3851 Fix getRequestBody()
authorBen Soares <b.soares@dundee.ac.uk>
Wed, 23 Mar 2022 21:38:16 +0000 (21:38 +0000)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 23 Mar 2022 21:38:16 +0000 (21:38 +0000)
src/jalview/rest/AbstractEndpoint.java
src/jalview/rest/InputAlignmentEndpoint.java
src/jalview/rest/RestHandler.java

index deb4683..c84b7b7 100644 (file)
@@ -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)
   {
index 095fd24..caad39f 100644 (file)
@@ -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");
index caec830..c4ada0e 100644 (file)
@@ -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