You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/01/28 10:04:45 UTC

[isis] branch master updated: ISIS-2158: fixes REST debug logging (RestfulLoggingFilter)

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new 09d54c2  ISIS-2158: fixes REST debug logging (RestfulLoggingFilter)
09d54c2 is described below

commit 09d54c298bc2b51418ab46d0325864414cf6028b
Author: Andi Huber <ah...@apache.org>
AuthorDate: Tue Jan 28 11:04:35 2020 +0100

    ISIS-2158: fixes REST debug logging (RestfulLoggingFilter)
    
    - keep the response entity input stream open, to not close the request
---
 .../isis/core/commons/internal/base/_Bytes.java    | 44 +++++++++++++++++-----
 .../restclient/log/RestfulLoggingFilter.java       | 15 ++++----
 2 files changed, 41 insertions(+), 18 deletions(-)

diff --git a/core/commons/src/main/java/org/apache/isis/core/commons/internal/base/_Bytes.java b/core/commons/src/main/java/org/apache/isis/core/commons/internal/base/_Bytes.java
index 13331d0..f2cfed5 100644
--- a/core/commons/src/main/java/org/apache/isis/core/commons/internal/base/_Bytes.java
+++ b/core/commons/src/main/java/org/apache/isis/core/commons/internal/base/_Bytes.java
@@ -50,7 +50,7 @@ public final class _Bytes {
     private final static int BUFFER_SIZE = 16 * 1024; // 16k
 
     /**
-     * Reads the input stream into an array of byte.
+     * Reads the input stream into an array of byte, then closes the input (-stream). 
      * @param input
      * @return null if {@code input} is null
      * @throws IOException
@@ -60,17 +60,41 @@ public final class _Bytes {
             return null;
         }
 
-        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        final byte[] buffer = new byte[BUFFER_SIZE];
-
-        int nRead;
-        while ((nRead = input.read(buffer, 0, buffer.length)) != -1) {
-            bos.write(buffer, 0, nRead);
+        try(final ByteArrayOutputStream bos = new ByteArrayOutputStream()){
+            final byte[] buffer = new byte[BUFFER_SIZE];
+    
+            int nRead;
+            while ((nRead = input.read(buffer, 0, buffer.length)) != -1) {
+                bos.write(buffer, 0, nRead);
+            }
+            bos.flush();
+            return bos.toByteArray();
+        } finally {
+            input.close();
+        }
+    }
+    
+    /**
+     * Reads the input stream into an array of byte, but does not close the input (-stream).
+     * @param input
+     * @return null if {@code input} is null
+     * @throws IOException
+     */
+    public static byte[] ofKeepOpen(@Nullable final InputStream input) throws IOException {
+        if(input==null) {
+            return null;
         }
-        bos.flush();
 
-        input.close();
-        return bos.toByteArray();
+        try(final ByteArrayOutputStream bos = new ByteArrayOutputStream()){
+            final byte[] buffer = new byte[BUFFER_SIZE];
+    
+            int nRead;
+            while ((nRead = input.read(buffer, 0, buffer.length)) != -1) {
+                bos.write(buffer, 0, nRead);
+            }
+            bos.flush();
+            return bos.toByteArray();
+        }
     }
 
     // -- PREPEND/APPEND
diff --git a/mappings/restclient/api/src/main/java/org/apache/isis/extensions/restclient/log/RestfulLoggingFilter.java b/mappings/restclient/api/src/main/java/org/apache/isis/extensions/restclient/log/RestfulLoggingFilter.java
index bc9f3dd..7388522 100644
--- a/mappings/restclient/api/src/main/java/org/apache/isis/extensions/restclient/log/RestfulLoggingFilter.java
+++ b/mappings/restclient/api/src/main/java/org/apache/isis/extensions/restclient/log/RestfulLoggingFilter.java
@@ -31,8 +31,10 @@ import javax.ws.rs.client.ClientRequestFilter;
 import javax.ws.rs.client.ClientResponseContext;
 import javax.ws.rs.client.ClientResponseFilter;
 
+import org.apache.isis.core.commons.internal.base._Bytes;
 import org.apache.isis.core.commons.internal.base._Strings;
 
+import lombok.val;
 import lombok.extern.log4j.Log4j2;
 
 /**
@@ -40,8 +42,8 @@ import lombok.extern.log4j.Log4j2;
  * @since 2.0
  */
 @Priority(999) @Log4j2
-public class RestfulLoggingFilter implements ClientRequestFilter, ClientResponseFilter{
-
+public class RestfulLoggingFilter implements ClientRequestFilter, ClientResponseFilter {
+    
     @Override
     public void filter(ClientRequestContext requestContext) throws IOException {
         final String endpoint = requestContext.getUri().toString();
@@ -86,8 +88,9 @@ public class RestfulLoggingFilter implements ClientRequestFilter, ClientResponse
         final InputStream inputStream = responseContext.getEntityStream();
         final String responseBody;
         if(inputStream!=null) {
-            responseBody = _Strings.read(responseContext.getEntityStream(), StandardCharsets.UTF_8);
-            responseContext.setEntityStream(new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)));    
+            val bytes = _Bytes.ofKeepOpen(inputStream);
+            responseBody = new String(bytes, StandardCharsets.UTF_8);
+            responseContext.setEntityStream(new ByteArrayInputStream(bytes));
         } else {
             responseBody = "null";
         }
@@ -125,8 +128,4 @@ public class RestfulLoggingFilter implements ClientRequestFilter, ClientResponse
     }
 
 
-
-
-
-
 }
\ No newline at end of file