You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:24:07 UTC

[sling-org-apache-sling-tracer] 07/30: SLING-5459 - Recording of tracer logs

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

rombert pushed a commit to annotated tag org.apache.sling.tracer-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-tracer.git

commit 0f4f997095730885a9eae4d09dbc2f7c0746dbf3
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Tue Feb 9 10:39:12 2016 +0000

    SLING-5459 - Recording of tracer logs
    
    Move JSON rendering logic to recording
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/tracer@1729352 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/tracer/internal/JSONRecording.java       | 10 ++++++--
 .../sling/tracer/internal/TracerLogServlet.java    | 27 +++++++++++-----------
 .../sling/tracer/internal/JSONRecordingTest.java   |  9 ++------
 3 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java b/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
index 7682e8e..391684f 100644
--- a/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
+++ b/src/main/java/org/apache/sling/tracer/internal/JSONRecording.java
@@ -19,6 +19,7 @@
 
 package org.apache.sling.tracer.internal;
 
+import java.io.Writer;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -38,14 +39,18 @@ class JSONRecording implements Recording {
         this.method = r.getMethod();
     }
 
-    public void render(JSONWriter jw) throws JSONException {
+    public void render(Writer pw) throws JSONException {
+        JSONWriter jw = new JSONWriter(pw);
+        jw.setTidy(true);
+        jw.object();
         jw.key("method").value(method);
 
         if (tracker != null) {
             jw.key("logs");
             jw.array();
             Iterator<String> it = tracker.getMessages();
-            while (it.hasNext()) {
+            //Per docs iterator can be null
+            while (it != null && it.hasNext()) {
                 jw.value(it.next());
             }
             jw.endArray();
@@ -57,6 +62,7 @@ class JSONRecording implements Recording {
             jw.value(q);
         }
         jw.endArray();
+        jw.endObject();
     }
 
     //~---------------------------------------< Recording >
diff --git a/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java b/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
index 479f6dc..7af8dfc 100644
--- a/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
+++ b/src/main/java/org/apache/sling/tracer/internal/TracerLogServlet.java
@@ -74,14 +74,22 @@ class TracerLogServlet extends SimpleWebConsolePlugin implements TraceLogRecorde
         } else {
             String requestId = getRequestId(request);
             prepareJSONResponse(response);
-            JSONWriter jw = new JSONWriter(pw);
             try {
-                jw.setTidy(true);
-                jw.object();
+                boolean responseDone = false;
                 if (requestId != null) {
-                    renderRequestData(requestId, jw);
+                    JSONRecording recording = cache.getIfPresent(requestId);
+                    if (recording != null){
+                        recording.render(pw);
+                        responseDone = true;
+                    }
+                }
+
+                if (!responseDone) {
+                    JSONWriter jw = new JSONWriter(pw);
+                    jw.object();
+                    jw.key("error").value("Not found");
+                    jw.endObject();
                 }
-                jw.endObject();
             } catch (JSONException e) {
                 throw new ServletException(e);
             }
@@ -107,15 +115,6 @@ class TracerLogServlet extends SimpleWebConsolePlugin implements TraceLogRecorde
         response.setCharacterEncoding("UTF-8");
     }
 
-    private void renderRequestData(String requestId, JSONWriter jw) throws JSONException {
-        JSONRecording recording = cache.getIfPresent(requestId);
-        if (recording == null){
-            jw.key("error").value("Not found");
-            return;
-        }
-        recording.render(jw);
-    }
-
     private void renderStatus(PrintWriter pw) {
         pw.printf("<p class='statline'>Log Tracer Recordings: %d recordings</p>%n", cache.size());
 
diff --git a/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java b/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java
index 5c5ae4e..560dc2d 100644
--- a/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java
+++ b/src/test/java/org/apache/sling/tracer/internal/JSONRecordingTest.java
@@ -24,7 +24,6 @@ import java.io.StringWriter;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.sling.commons.json.JSONObject;
-import org.apache.sling.commons.json.io.JSONWriter;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -44,9 +43,7 @@ public class JSONRecordingTest {
         r.log(TracerContext.QUERY_LOGGER, "foo bar", new Object[]{"x" , "y"});
         r.log(TracerContext.QUERY_LOGGER, "foo bar", new Object[]{"x" , "z"});
 
-        JSONWriter jw = new JSONWriter(sw).object();
-        r.render(jw);
-        jw.endObject();
+        r.render(sw);
 
         JSONObject json = new JSONObject(sw.toString());
         assertEquals("GET", json.get("method"));
@@ -60,9 +57,7 @@ public class JSONRecordingTest {
 
         r.registerTracker(TestUtil.createTracker("x", "y"));
 
-        JSONWriter jw = new JSONWriter(sw).object();
-        r.render(jw);
-        jw.endObject();
+        r.render(sw);
 
         JSONObject json = new JSONObject(sw.toString());
         assertEquals(2, json.getJSONArray("logs").length());

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.