You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by to...@apache.org on 2016/07/05 12:29:51 UTC

svn commit: r1751459 - in /sling/trunk/contrib/extensions/distribution/extensions/src/main/java/org/apache/sling/distribution/serialization/impl: avro/AvroContentSerializer.java kryo/KryoContentSerializer.java

Author: tommaso
Date: Tue Jul  5 12:29:51 2016
New Revision: 1751459

URL: http://svn.apache.org/viewvc?rev=1751459&view=rev
Log:
SLING-5822 - added getFilters(path) handling in avro and kyro serializers

Modified:
    sling/trunk/contrib/extensions/distribution/extensions/src/main/java/org/apache/sling/distribution/serialization/impl/avro/AvroContentSerializer.java
    sling/trunk/contrib/extensions/distribution/extensions/src/main/java/org/apache/sling/distribution/serialization/impl/kryo/KryoContentSerializer.java

Modified: sling/trunk/contrib/extensions/distribution/extensions/src/main/java/org/apache/sling/distribution/serialization/impl/avro/AvroContentSerializer.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/extensions/src/main/java/org/apache/sling/distribution/serialization/impl/avro/AvroContentSerializer.java?rev=1751459&r1=1751458&r2=1751459&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/distribution/extensions/src/main/java/org/apache/sling/distribution/serialization/impl/avro/AvroContentSerializer.java (original)
+++ sling/trunk/contrib/extensions/distribution/extensions/src/main/java/org/apache/sling/distribution/serialization/impl/avro/AvroContentSerializer.java Tue Jul  5 12:29:51 2016
@@ -36,6 +36,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import org.apache.avro.Schema;
 import org.apache.avro.file.DataFileReader;
@@ -110,7 +111,7 @@ public class AvroContentSerializer imple
         try {
             for (String path : request.getPaths()) {
                 Resource resource = resourceResolver.getResource(path);
-                AvroShallowResource avroShallowResource = getAvroShallowResource(request.isDeep(path), path, resource);
+                AvroShallowResource avroShallowResource = getAvroShallowResource(request, path, resource);
                 writer.append(avroShallowResource);
             }
             outputStream.flush();
@@ -144,7 +145,7 @@ public class AvroContentSerializer imple
         return name;
     }
 
-    private AvroShallowResource getAvroShallowResource(boolean deep, String path, Resource resource) throws IOException {
+    private AvroShallowResource getAvroShallowResource(DistributionRequest request, String path, Resource resource) throws IOException {
         AvroShallowResource avroShallowResource = new AvroShallowResource();
         avroShallowResource.setName("avro_" + System.nanoTime());
         avroShallowResource.setPath(path);
@@ -167,11 +168,20 @@ public class AvroContentSerializer imple
         }
         avroShallowResource.setValueMap(map);
         List<AvroShallowResource> children = new LinkedList<AvroShallowResource>();
+        boolean deep = request.isDeep(path);
+        String[] filters = request.getFilters(path);
         if (deep) {
             for (Resource child : resource.getChildren()) {
                 String childPath = child.getPath();
                 if (!ignoredNodeNames.contains(child.getName())) {
-                    children.add(getAvroShallowResource(true, childPath, child));
+                    children.add(getAvroShallowResource(request, childPath, child));
+                }
+            }
+        } else {
+            for (Resource child : resource.getChildren()) {
+                String childPath = child.getPath();
+                if (filtersAllow(filters, childPath) && !ignoredNodeNames.contains(child.getName())) {
+                    children.add(getAvroShallowResource(request, childPath, child));
                 }
             }
         }
@@ -179,6 +189,25 @@ public class AvroContentSerializer imple
         return avroShallowResource;
     }
 
+    private boolean filtersAllow(String[] filters, String path) {
+        boolean allowed = false;
+        for (String pattern : filters) {
+            if (pattern.startsWith("+")) {
+                if (Pattern.compile(pattern.substring(1)).matcher(path).matches()) {
+                    allowed = true;
+                }
+            } else if (pattern.startsWith("-")) {
+                if (Pattern.compile(pattern.substring(1)).matcher(path).matches()) {
+                    allowed = false;
+                }
+            } else {
+                allowed = Pattern.compile(pattern).matcher(path).matches();
+            }
+        }
+        return allowed;
+    }
+
+
     private Collection<AvroShallowResource> readAvroResources(byte[] bytes) throws IOException {
         DatumReader<AvroShallowResource> datumReader = new SpecificDatumReader<AvroShallowResource>(AvroShallowResource.class);
         DataFileReader<AvroShallowResource> dataFileReader = new DataFileReader<AvroShallowResource>(new SeekableByteArrayInput(bytes), datumReader);

Modified: sling/trunk/contrib/extensions/distribution/extensions/src/main/java/org/apache/sling/distribution/serialization/impl/kryo/KryoContentSerializer.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/distribution/extensions/src/main/java/org/apache/sling/distribution/serialization/impl/kryo/KryoContentSerializer.java?rev=1751459&r1=1751458&r2=1751459&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/distribution/extensions/src/main/java/org/apache/sling/distribution/serialization/impl/kryo/KryoContentSerializer.java (original)
+++ sling/trunk/contrib/extensions/distribution/extensions/src/main/java/org/apache/sling/distribution/serialization/impl/kryo/KryoContentSerializer.java Tue Jul  5 12:29:51 2016
@@ -29,6 +29,7 @@ import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 import com.esotericsoftware.kryo.Kryo;
 import com.esotericsoftware.kryo.Serializer;
@@ -92,7 +93,7 @@ public class KryoContentSerializer imple
         for (String p : paths) {
             Resource resource = resourceResolver.getResource(p);
             if (resource != null) {
-                addResource(request.isDeep(p), resources, resource);
+                addResource(request, resources, resource);
             }
         }
         kryo.writeObject(output, resources);
@@ -228,12 +229,45 @@ public class KryoContentSerializer imple
         }
     }
 
-    private void addResource(boolean deep, LinkedList<Resource> resources, Resource resource) {
+    private void addResource(DistributionRequest request, LinkedList<Resource> resources, Resource resource) {
         resources.add(resource);
-        for (Resource child : resource.getChildren()) {
-            if (deep && !ignoredNodeNames.contains(resource.getName())) {
-                addResource(true, resources, child);
+        String path = resource.getPath();
+        boolean deep = request.isDeep(path);
+        String[] filters = request.getFilters(path);
+        if (deep) {
+            for (Resource child : resource.getChildren()) {
+                if (!ignoredNodeNames.contains(resource.getName())) {
+                    addResource(request, resources, child);
+                }
+            }
+        } else {
+            for (Resource child : resource.getChildren()) {
+                String childPath = child.getPath();
+                if (filtersAllow(filters, childPath) && !ignoredNodeNames.contains(child.getName())) {
+                    addResource(request, resources, child);
+                }
+            }
+        }
+
+    }
+
+    private boolean filtersAllow(String[] filters, String path) {
+        boolean allowed = false;
+        for (String pattern : filters) {
+            if (pattern.startsWith("+")) {
+                if (Pattern.compile(pattern.substring(1)).matcher(path).matches()) {
+                    allowed = true;
+                }
+            } else if (pattern.startsWith("-")) {
+                if (Pattern.compile(pattern.substring(1)).matcher(path).matches()) {
+                    allowed = false;
+                }
+            } else {
+                allowed = Pattern.compile(pattern).matcher(path).matches();
             }
         }
+        return allowed;
     }
+
+
 }