chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1227089 - in /chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter: JsonPrettyPrinter.java LoggingFilter.java
Date Wed, 04 Jan 2012 09:26:48 GMT
Author: jens
Date: Wed Jan  4 09:26:48 2012
New Revision: 1227089

URL: http://svn.apache.org/viewvc?rev=1227089&view=rev
Log:
add pretty printing for JSON, add writer support for LoggingFilter

Added:
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/JsonPrettyPrinter.java
Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/LoggingFilter.java

Added: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/JsonPrettyPrinter.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/JsonPrettyPrinter.java?rev=1227089&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/JsonPrettyPrinter.java
(added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/JsonPrettyPrinter.java
Wed Jan  4 09:26:48 2012
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.server.support.filter;
+
+
+/* 
+ * A super simple JSON pretty printer
+ */
+
+public class JsonPrettyPrinter  {
+
+    private int indent = 0;
+    private String indentStr;
+    private StringBuffer sb = new StringBuffer();
+    
+    public JsonPrettyPrinter () {
+        init(3);
+    }
+
+    public JsonPrettyPrinter (int indent) {
+        init(indent);
+    }
+    
+    private void init(int indent) {
+        StringBuffer sb = new StringBuffer();
+        for (int i=0; i<indent; i++)
+            sb.append(" ");
+        indentStr = sb.toString();
+    }
+        
+    public String prettyPrint(String jsonStr) {
+        for (int i=0; i<jsonStr.length(); i++)  {
+            char c = jsonStr.charAt(i);
+            writeChar(c);
+        }
+        return sb.toString();
+    }
+    
+    private void writeChar(char c) {
+        if (((char) c) == '[' || ((char) c) == '{') {
+            sb.append(c);
+            sb.append('\n');
+            indent++;
+            addIndent();
+        } else if (((char) c) == ',') {
+            sb.append(c);
+            sb.append('\n');
+            addIndent();
+        } else if (((char) c) == ']' || ((char) c) == '}') {
+            sb.append('\n');
+            indent--;
+            addIndent();
+            sb.append(c);
+        } else {
+            sb.append(c);
+        }
+
+    }
+
+    private void addIndent() {
+        for (int i = 0; i < indent; i++) {
+            sb.append(indentStr);
+        }
+    }
+    
+    public static void main(String[] args) {
+        args = new String[2];
+        args[0] = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
+        args[1] = "{\"abc\":{\"def\":{\"ghi\":{\"jkl\":[5,{\"mno\":7}]}}}}";
+        for (String s : args) {
+            JsonPrettyPrinter pp = new JsonPrettyPrinter();
+            System.out.println("Pretty Printing JSON String: " + s);
+            String result = pp.prettyPrint(s);
+            System.out.println("Pretty Printed JSON: " + result);
+        }
+    }
+}

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/LoggingFilter.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/LoggingFilter.java?rev=1227089&r1=1227088&r2=1227089&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/LoggingFilter.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-support/src/main/java/org/apache/chemistry/opencmis/server/support/filter/LoggingFilter.java
Wed Jan  4 09:26:48 2012
@@ -27,6 +27,7 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.io.Writer;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Enumeration;
@@ -124,7 +125,7 @@ public class LoggingFilter implements Fi
                 if (cType.startsWith("multipart")) {
                     xmlRequest = processMultipart(cType, xmlRequest);
                 } else if (cType.contains("xml")) { 
-                    xmlRequest = prettyPrint(xmlRequest, indent);
+                    xmlRequest = prettyPrintXml(xmlRequest, indent);
                 }
             }
             
@@ -149,7 +150,9 @@ public class LoggingFilter implements Fi
                 if (cType.startsWith("multipart")) {
                     xmlResponse = processMultipart(cType, xmlResponse);
                 } else if (cType.contains("xml")) { 
-                    xmlResponse = prettyPrint(xmlResponse, indent);
+                    xmlResponse = prettyPrintXml(xmlResponse, indent);
+                } else if (cType.contains("json")) { 
+                    xmlResponse = prettyPrintJson(xmlResponse, indent);
                 }
             }
                 
@@ -189,7 +192,7 @@ public class LoggingFilter implements Fi
         }
     }
     
-    private static String prettyPrint(String input, int indent) {
+    private static String prettyPrintXml(String input, int indent) {
         try {
             Source xmlInput = new StreamSource(new StringReader(input));
             StringWriter stringWriter = new StringWriter();
@@ -205,6 +208,11 @@ public class LoggingFilter implements Fi
         }
     }
     
+    private static String prettyPrintJson(String input, int indent) {
+        JsonPrettyPrinter pp = new JsonPrettyPrinter(indent);
+        return pp.prettyPrint(input);
+    }
+    
     private String processMultipart(String cType, String messageBody) throws IOException
{
         int beginIndex = cType.indexOf("boundary=\"") + 10;
         int endIndex = cType.indexOf("\"", beginIndex);
@@ -216,21 +224,26 @@ public class LoggingFilter implements Fi
         ByteArrayOutputStream xmlBodyBuffer = new ByteArrayOutputStream();
         boolean boundaryFound;
         
-        boolean inXmlBody = false;
-        boolean inXmlPart = false;
+        boolean inXmlOrJsonBody = false;
+        boolean inXmlOrJsonPart = false;
+        boolean isXml;
         while ((line = in.readLine()) != null) {
-            if (inXmlPart) {
-                if (line.startsWith("<?xml")) {
-                    inXmlBody = true;
+            if (inXmlOrJsonPart) {
+                if (line.startsWith("<?xml") || line.startsWith("{")) {
+                    inXmlOrJsonBody = true;
+                    isXml = line.startsWith("<?xml");
                     xmlBodyBuffer.write(line.getBytes(), 0, line.length());             
     
-                    while (inXmlBody)  {
+                    while (inXmlOrJsonBody)  {
                         line = in.readLine();
                         boundaryFound = line.startsWith(boundary);
                         if (boundaryFound) {
                             log.debug("Leaving XML body: " + line);
-                            inXmlBody = false;
-                            inXmlPart = false;
-                            out.append(prettyPrint(xmlBodyBuffer.toString(), indent));
+                            inXmlOrJsonBody = false;
+                            inXmlOrJsonPart = false;
+                            if (isXml)
+                                out.append(prettyPrintXml(xmlBodyBuffer.toString(), indent));
+                            else
+                                out.append(prettyPrintJson(xmlBodyBuffer.toString(), indent));
                             out.append(line).append("\n");
                         } else
                             xmlBodyBuffer.write(line.getBytes(), 0, line.length());
@@ -246,7 +259,7 @@ public class LoggingFilter implements Fi
                 boundaryFound = line.startsWith(boundary);
                 if (boundaryFound) {
                     log.debug("Boundardy found!");
-                    inXmlPart = true;
+                    inXmlOrJsonPart = true;
                 }
             }
         }
@@ -381,6 +394,7 @@ public class LoggingFilter implements Fi
      private class LoggingResponseWrapper extends HttpServletResponseWrapper {
          
          private LoggingOutputStream os;
+         private PrintWriter writer;
          private int statusCode;
          private Map<String, String> headers = new HashMap<String, String>();
          String encoding;
@@ -391,6 +405,19 @@ public class LoggingFilter implements Fi
          }
     
          @Override
+         public PrintWriter getWriter() {
+            try {
+                if (null == writer)
+                    writer = new PrintWriter(this.getOutputStream());
+                return writer;
+            } catch (IOException e) {
+                log.error("Failed to get PrintWriter in LoggingFilter: "+ e);
+                e.printStackTrace();
+                return null;             
+            }
+         }
+         
+         @Override
          public ServletOutputStream getOutputStream() throws IOException {
             return os;
          }



Mime
View raw message