You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2014/03/22 14:35:19 UTC

[04/10] git commit: wip

wip


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/2fa263e9
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/2fa263e9
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/2fa263e9

Branch: refs/heads/rest
Commit: 2fa263e94044a813d6fff47268626fd0bfa69df7
Parents: dd1fe96
Author: randgalt <ra...@apache.org>
Authored: Wed Jan 8 21:00:07 2014 -0500
Committer: randgalt <ra...@apache.org>
Committed: Wed Jan 8 21:00:07 2014 -0500

----------------------------------------------------------------------
 .../curator/x/rest/ConnectionResource.java      |   2 +-
 .../curator/x/rest/PathCacheRecipeResource.java | 121 +++++++++++++++----
 .../entity/PathChildrenCacheDataEntity.java     |  72 +++++++++++
 .../entity/PathChildrenCacheEventEntity.java    |  34 +-----
 4 files changed, 173 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/2fa263e9/curator-x-rest/src/main/java/org/apache/curator/x/rest/ConnectionResource.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/ConnectionResource.java b/curator-x-rest/src/main/java/org/apache/curator/x/rest/ConnectionResource.java
index 4f70111..cc05369 100644
--- a/curator-x-rest/src/main/java/org/apache/curator/x/rest/ConnectionResource.java
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/ConnectionResource.java
@@ -49,7 +49,7 @@ public class ConnectionResource
     }
 
     @GET
-    @Path("{id}/block-on-state-change")
+    @Path("block-on-state-change/{id}")
     public void registerConnectionStateChange(@Suspended final AsyncResponse asyncResponse, @PathParam("id") String id, @QueryParam("state-count") String currentStateCountArg)
     {
         final Connection connection = connectionsManager.get(id);

http://git-wip-us.apache.org/repos/asf/curator/blob/2fa263e9/curator-x-rest/src/main/java/org/apache/curator/x/rest/PathCacheRecipeResource.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/PathCacheRecipeResource.java b/curator-x-rest/src/main/java/org/apache/curator/x/rest/PathCacheRecipeResource.java
index d953bd6..2c44b88 100644
--- a/curator-x-rest/src/main/java/org/apache/curator/x/rest/PathCacheRecipeResource.java
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/PathCacheRecipeResource.java
@@ -22,9 +22,11 @@ package org.apache.curator.x.rest;
 import com.google.common.base.Function;
 import com.google.common.collect.Lists;
 import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.recipes.cache.ChildData;
 import org.apache.curator.framework.recipes.cache.PathChildrenCache;
 import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
 import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
+import org.apache.curator.x.rest.entity.PathChildrenCacheDataEntity;
 import org.apache.curator.x.rest.entity.PathChildrenCacheEntity;
 import org.apache.curator.x.rest.entity.PathChildrenCacheEventEntity;
 import org.apache.curator.x.rest.entity.StatEntity;
@@ -105,7 +107,8 @@ public class PathCacheRecipeResource
     }
 
     @GET
-    @Path("{id}/block-on-events")
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("block-on-events/{id}")
     public void getEvents(@Suspended final AsyncResponse asyncResponse, @PathParam("connectionId") String connectionId, @PathParam("id") String cacheId)
     {
         Connection connection = connectionsManager.get(connectionId);
@@ -147,39 +150,105 @@ public class PathCacheRecipeResource
         connection.putThing(new ThingKey<Future>(ThingType.FUTURE), future);
     }
 
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("{id}")
+    public Response getCurrentData(@PathParam("connectionId") String connectionId, @PathParam("id") String cacheId)
+    {
+        Connection connection = connectionsManager.get(connectionId);
+        if ( connection == null )
+        {
+            return Response.status(Response.Status.NOT_FOUND).build();
+        }
+
+        PathChildrenCacheThing cacheThing = connection.removeThing(new ThingKey<PathChildrenCacheThing>(cacheId, ThingType.PATH_CACHE));
+        if ( cacheThing == null )
+        {
+            return Response.status(Response.Status.NOT_FOUND).build();
+        }
+
+        List<ChildData> currentData = cacheThing.getCache().getCurrentData();
+        List<PathChildrenCacheDataEntity> transformed = Lists.transform(currentData, new Function<ChildData, PathChildrenCacheDataEntity>()
+        {
+            @Override
+            public PathChildrenCacheDataEntity apply(ChildData data)
+            {
+                return toEntity(data);
+            }
+        });
+
+        GenericEntity<List<PathChildrenCacheDataEntity>> entity = new GenericEntity<List<PathChildrenCacheDataEntity>>(transformed){};
+        return Response.ok(entity).build();
+    }
+
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("{id}/{path:.*}")
+    public Response getCurrentData(@PathParam("connectionId") String connectionId, @PathParam("id") String cacheId, @PathParam("path") String fullPath)
+    {
+        Connection connection = connectionsManager.get(connectionId);
+        if ( connection == null )
+        {
+            return Response.status(Response.Status.NOT_FOUND).build();
+        }
+
+        PathChildrenCacheThing cacheThing = connection.removeThing(new ThingKey<PathChildrenCacheThing>(cacheId, ThingType.PATH_CACHE));
+        if ( cacheThing == null )
+        {
+            return Response.status(Response.Status.NOT_FOUND).build();
+        }
+
+        ChildData currentData = cacheThing.getCache().getCurrentData(fullPath);
+        if (currentData == null )
+        {
+            return Response.status(Response.Status.NOT_FOUND).build();
+        }
+
+        return Response.ok(toEntity(currentData)).build();
+    }
+
     private static final Function<PathChildrenCacheEvent, PathChildrenCacheEventEntity> toEntity = new Function<PathChildrenCacheEvent, PathChildrenCacheEventEntity>()
     {
         @Override
         public PathChildrenCacheEventEntity apply(PathChildrenCacheEvent event)
         {
-            String path = (event.getData() != null) ? event.getData().getPath() : null;
-            String data = ((event.getData() != null) && (event.getData().getData() != null)) ? new String((event.getData().getData())) : null;
-            StatEntity stat = ((event.getData() != null) && (event.getData().getStat() != null))
-                ? new StatEntity
-                (
-                    event.getData().getStat().getCzxid(),
-                    event.getData().getStat().getMzxid(),
-                    event.getData().getStat().getCtime(),
-                    event.getData().getStat().getMtime(),
-                    event.getData().getStat().getVersion(),
-                    event.getData().getStat().getCversion(),
-                    event.getData().getStat().getAversion(),
-                    event.getData().getStat().getEphemeralOwner(),
-                    event.getData().getStat().getDataLength(),
-                    event.getData().getStat().getNumChildren(),
-                    event.getData().getStat().getPzxid()
-                )
-                : null;
-            return new PathChildrenCacheEventEntity
-            (
-                event.getType().name(),
-                path,
-                data,
-                stat
-            );
+            PathChildrenCacheDataEntity data = null;
+            ChildData childData = event.getData();
+            if ( childData != null )
+            {
+                data = toEntity(childData);
+            }
+            return new PathChildrenCacheEventEntity(event.getType().name(), data);
         }
     };
 
+    private static PathChildrenCacheDataEntity toEntity(ChildData childData)
+    {
+        PathChildrenCacheDataEntity data;StatEntity stat = (childData.getStat() != null)
+            ? new StatEntity
+            (
+                childData.getStat().getCzxid(),
+                childData.getStat().getMzxid(),
+                childData.getStat().getCtime(),
+                childData.getStat().getMtime(),
+                childData.getStat().getVersion(),
+                childData.getStat().getCversion(),
+                childData.getStat().getAversion(),
+                childData.getStat().getEphemeralOwner(),
+                childData.getStat().getDataLength(),
+                childData.getStat().getNumChildren(),
+                childData.getStat().getPzxid()
+            )
+            : null;
+        data = new PathChildrenCacheDataEntity
+        (
+            childData.getPath(),
+            (childData.getData() != null) ? new String((childData.getData())) : null,
+            stat
+        );
+        return data;
+    }
+
     private static class LocalListener implements PathChildrenCacheListener
     {
         private final PathChildrenCacheThing cacheThing;

http://git-wip-us.apache.org/repos/asf/curator/blob/2fa263e9/curator-x-rest/src/main/java/org/apache/curator/x/rest/entity/PathChildrenCacheDataEntity.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/entity/PathChildrenCacheDataEntity.java b/curator-x-rest/src/main/java/org/apache/curator/x/rest/entity/PathChildrenCacheDataEntity.java
new file mode 100644
index 0000000..7b302ff
--- /dev/null
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/entity/PathChildrenCacheDataEntity.java
@@ -0,0 +1,72 @@
+/**
+ * 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.curator.x.rest.entity;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement
+public class PathChildrenCacheDataEntity
+{
+    private String path;
+    private String data;
+    private StatEntity stat;
+
+    public PathChildrenCacheDataEntity()
+    {
+        this("", "", new StatEntity());
+    }
+
+    public PathChildrenCacheDataEntity(String path, String data, StatEntity stat)
+    {
+        this.path = path;
+        this.data = data;
+        this.stat = stat;
+    }
+
+    public String getPath()
+    {
+        return path;
+    }
+
+    public void setPath(String path)
+    {
+        this.path = path;
+    }
+
+    public String getData()
+    {
+        return data;
+    }
+
+    public void setData(String data)
+    {
+        this.data = data;
+    }
+
+    public StatEntity getStat()
+    {
+        return stat;
+    }
+
+    public void setStat(StatEntity stat)
+    {
+        this.stat = stat;
+    }
+}

http://git-wip-us.apache.org/repos/asf/curator/blob/2fa263e9/curator-x-rest/src/main/java/org/apache/curator/x/rest/entity/PathChildrenCacheEventEntity.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/main/java/org/apache/curator/x/rest/entity/PathChildrenCacheEventEntity.java b/curator-x-rest/src/main/java/org/apache/curator/x/rest/entity/PathChildrenCacheEventEntity.java
index 9b08d32..a730d48 100644
--- a/curator-x-rest/src/main/java/org/apache/curator/x/rest/entity/PathChildrenCacheEventEntity.java
+++ b/curator-x-rest/src/main/java/org/apache/curator/x/rest/entity/PathChildrenCacheEventEntity.java
@@ -25,21 +25,17 @@ import javax.xml.bind.annotation.XmlRootElement;
 public class PathChildrenCacheEventEntity
 {
     private String eventType;
-    private String path;
-    private String data;
-    private StatEntity stat;
+    private PathChildrenCacheDataEntity data;
 
     public PathChildrenCacheEventEntity()
     {
-        this("", "", "", new StatEntity());
+        this("", new PathChildrenCacheDataEntity());
     }
 
-    public PathChildrenCacheEventEntity(String eventType, String path, String data, StatEntity stat)
+    public PathChildrenCacheEventEntity(String eventType, PathChildrenCacheDataEntity data)
     {
         this.eventType = eventType;
-        this.path = path;
         this.data = data;
-        this.stat = stat;
     }
 
     public String getEventType()
@@ -52,33 +48,13 @@ public class PathChildrenCacheEventEntity
         this.eventType = eventType;
     }
 
-    public String getPath()
-    {
-        return path;
-    }
-
-    public void setPath(String path)
-    {
-        this.path = path;
-    }
-
-    public String getData()
+    public PathChildrenCacheDataEntity getData()
     {
         return data;
     }
 
-    public void setData(String data)
+    public void setData(PathChildrenCacheDataEntity data)
     {
         this.data = data;
     }
-
-    public StatEntity getStat()
-    {
-        return stat;
-    }
-
-    public void setStat(StatEntity stat)
-    {
-        this.stat = stat;
-    }
 }