jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1394270 - in /jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http: JsonRepresentation.java PostRepresentation.java
Date Thu, 04 Oct 2012 21:13:45 GMT
Author: mduerig
Date: Thu Oct  4 21:13:45 2012
New Revision: 1394270

URL: http://svn.apache.org/viewvc?rev=1394270&view=rev
Log:
OAK-350: Unify PropertyState and CoreValue
- Replace CoreValue.getString with PropertyState.getValue(Type)

Modified:
    jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/JsonRepresentation.java
    jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/PostRepresentation.java

Modified: jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/JsonRepresentation.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/JsonRepresentation.java?rev=1394270&r1=1394269&r2=1394270&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/JsonRepresentation.java
(original)
+++ jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/JsonRepresentation.java
Thu Oct  4 21:13:45 2012
@@ -19,17 +19,29 @@ package org.apache.jackrabbit.oak.http;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.math.BigDecimal;
 
-import javax.jcr.PropertyType;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.jackrabbit.oak.api.CoreValue;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonGenerator;
+import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.Type;
 import org.apache.tika.mime.MediaType;
 
-import com.fasterxml.jackson.core.JsonFactory;
-import com.fasterxml.jackson.core.JsonGenerator;
+import static org.apache.jackrabbit.oak.api.Type.BINARIES;
+import static org.apache.jackrabbit.oak.api.Type.BINARY;
+import static org.apache.jackrabbit.oak.api.Type.BOOLEAN;
+import static org.apache.jackrabbit.oak.api.Type.BOOLEANS;
+import static org.apache.jackrabbit.oak.api.Type.DECIMAL;
+import static org.apache.jackrabbit.oak.api.Type.DECIMALS;
+import static org.apache.jackrabbit.oak.api.Type.DOUBLE;
+import static org.apache.jackrabbit.oak.api.Type.DOUBLES;
+import static org.apache.jackrabbit.oak.api.Type.LONG;
+import static org.apache.jackrabbit.oak.api.Type.LONGS;
+import static org.apache.jackrabbit.oak.api.Type.STRINGS;
 
 class JsonRepresentation implements Representation {
 
@@ -47,6 +59,7 @@ class JsonRepresentation implements Repr
         return type;
     }
 
+    @Override
     public void render(Tree tree, HttpServletResponse response)
             throws IOException {
         JsonGenerator generator = startResponse(response);
@@ -54,6 +67,7 @@ class JsonRepresentation implements Repr
         generator.close();
     }
 
+    @Override
     public void render(PropertyState property, HttpServletResponse response)
             throws IOException {
         JsonGenerator generator = startResponse(response);
@@ -67,7 +81,7 @@ class JsonRepresentation implements Repr
         return factory.createJsonGenerator(response.getOutputStream());
     }
 
-    private void render(Tree tree, JsonGenerator generator)
+    private static void render(Tree tree, JsonGenerator generator)
             throws IOException {
         generator.writeStartObject();
         for (PropertyState property : tree.getProperties()) {
@@ -82,46 +96,57 @@ class JsonRepresentation implements Repr
         generator.writeEndObject();
     }
 
-    private void render(PropertyState property, JsonGenerator generator)
+    private static void render(PropertyState property, JsonGenerator generator)
             throws IOException {
         if (property.isArray()) {
             generator.writeStartArray();
-            for (CoreValue value : property.getValues()) {
-                render(value, generator);
-            }
+            renderValue(property, generator);
             generator.writeEndArray();
         } else {
-            render(property.getValue(), generator);
+            renderValue(property, generator);
         }
     }
 
-    private void render(CoreValue value, JsonGenerator generator)
+    private static void renderValue(PropertyState property, JsonGenerator generator)
             throws IOException {
         // TODO: Type info?
-        if (value.getType() == PropertyType.BOOLEAN) {
-            generator.writeBoolean(value.getBoolean());
-        } else if (value.getType() == PropertyType.DECIMAL) {
-            generator.writeNumber(value.getDecimal());
-        } else if (value.getType() == PropertyType.DOUBLE) {
-            generator.writeNumber(value.getDouble());
-        } else if (value.getType() == PropertyType.LONG) {
-            generator.writeNumber(value.getLong());
-        } else if (value.getType() == PropertyType.BINARY) {
+        Type<?> type = property.getType();
+        if (BOOLEAN.equals(type)) {
+            for (boolean value : property.getValue(BOOLEANS)) {
+                generator.writeBoolean(value);
+            }
+        } else if (DECIMAL.equals(type)) {
+            for (BigDecimal value : property.getValue(DECIMALS)) {
+                generator.writeNumber(value);
+            }
+        } else if (DOUBLE.equals(type)) {
+            for (double value : property.getValue(DOUBLES)) {
+                generator.writeNumber(value);
+            }
+        } else if (LONG.equals(type)) {
+            for (long value : property.getValue(LONGS)) {
+                generator.writeNumber(value);
+            }
+        } else if (BINARY.equals(type)) {
             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-            InputStream stream = value.getNewStream();
-            try {
-                byte[] b = new byte[1024];
-                int n = stream.read(b);
-                while (n != -1) {
-                    buffer.write(b, 0, n);
-                    n = stream.read(b);
+            for (Blob value : property.getValue(BINARIES)) {
+                InputStream stream = value.getNewStream();
+                try {
+                    byte[] b = new byte[1024];
+                    int n = stream.read(b);
+                    while (n != -1) {
+                        buffer.write(b, 0, n);
+                        n = stream.read(b);
+                    }
+                } finally {
+                    stream.close();
                 }
-            } finally {
-                stream.close();
+                generator.writeBinary(buffer.toByteArray());
             }
-            generator.writeBinary(buffer.toByteArray());
         } else {
-            generator.writeString(value.getString());
+            for (String value : property.getValue(STRINGS)) {
+                generator.writeString(value);
+            }
         }
     }
 

Modified: jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/PostRepresentation.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/PostRepresentation.java?rev=1394270&r1=1394269&r2=1394270&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/PostRepresentation.java
(original)
+++ jackrabbit/oak/trunk/oak-http/src/main/java/org/apache/jackrabbit/oak/http/PostRepresentation.java
Thu Oct  4 21:13:45 2012
@@ -23,11 +23,13 @@ import java.net.URLEncoder;
 
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.jackrabbit.oak.api.CoreValue;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.tika.mime.MediaType;
 
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.api.Type.STRINGS;
+
 class PostRepresentation implements Representation {
 
     private static final MediaType TYPE =
@@ -40,6 +42,7 @@ class PostRepresentation implements Repr
         return TYPE;
     }
 
+    @Override
     public void render(Tree tree, HttpServletResponse response)
             throws IOException {
         PrintWriter writer = startResponse(response);
@@ -48,39 +51,38 @@ class PostRepresentation implements Repr
         for (PropertyState property : tree.getProperties()) {
             String name = property.getName();
             if (property.isArray()) {
-                for (CoreValue value : property.getValues()) {
+                for (String value : property.getValue(STRINGS)) {
                     first = render(first, name, value, writer);
                 }
             } else {
-                CoreValue value = property.getValue();
-                first = render(first, name, value, writer);
+                first = render(first, name, property.getValue(STRING), writer);
             }
         }
     }
 
+    @Override
     public void render(PropertyState property, HttpServletResponse response)
             throws IOException {
         PrintWriter writer = startResponse(response);
         if (property.isArray()) {
-            for (CoreValue value : property.getValues()) {
+            for (String value : property.getValue(STRINGS)) {
                 render(value, writer);
                 writer.print('\n');
             }
         } else {
-            render(property.getValue(), writer);
+            render(property.getValue(STRING), writer);
         }
     }
 
-    private PrintWriter startResponse(HttpServletResponse response)
+    private static PrintWriter startResponse(HttpServletResponse response)
             throws IOException {
         response.setContentType(TYPE.toString());
         response.setCharacterEncoding(ENCODING);
         return response.getWriter();
     }
 
-    private boolean render(
-            boolean first, String name, CoreValue value, PrintWriter writer)
-            throws UnsupportedEncodingException {
+    private static boolean render(
+            boolean first, String name, String value, PrintWriter writer) {
         if (!first) {
             writer.print('&');
         }
@@ -90,11 +92,7 @@ class PostRepresentation implements Repr
         return false;
     }
 
-    private void render(CoreValue value, PrintWriter writer) {
-        render(value.getString(), writer);
-    }
-
-    private void render(String string, PrintWriter writer) {
+    private static void render(String string, PrintWriter writer) {
         try {
             writer.print(URLEncoder.encode(string, ENCODING));
         } catch (UnsupportedEncodingException e) {



Mime
View raw message