pkuwm commented on a change in pull request #671: Add ZooKeeperAccessor to 
helix-rest
URL: https://github.com/apache/helix/pull/671#discussion_r365978278
 
 

 ##########
 File path: 
helix-rest/src/main/java/org/apache/helix/rest/server/resources/zookeeper/ZooKeeperAccessor.java
 ##########
 @@ -0,0 +1,181 @@
+package org.apache.helix.rest.server.resources.zookeeper;
+
+/*
+ * 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.
+ */
+
+import java.io.UnsupportedEncodingException;
+import java.util.Base64;
+import java.util.List;
+import java.util.Map;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+
+import com.google.common.collect.ImmutableMap;
+import org.apache.helix.AccessOption;
+import org.apache.helix.manager.zk.ZkBaseDataAccessor;
+import org.apache.helix.rest.common.ContextPropertyKeys;
+import org.apache.helix.rest.server.ServerContext;
+import org.apache.helix.rest.server.resources.AbstractResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * ZooKeeperAccessor provides methods for accessing ZooKeeper resources 
(ZNodes).
+ * It provides basic ZooKeeper features supported by ZkClient.
+ */
+@Path("/zookeeper")
+public class ZooKeeperAccessor extends AbstractResource {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ZooKeeperAccessor.class.getName());
+  private ZkBaseDataAccessor<byte[]> _zkBaseDataAccessor;
+
+  public enum ZooKeeperCommand {
+    exists, getBinaryData, getStringData, getChildren
+  }
+
+  @GET
+  @Path("{path: .+}")
+  public Response get(@PathParam("path") String path, @QueryParam("command") 
String commandStr) {
+    ZooKeeperCommand cmd;
+    try {
+      cmd = ZooKeeperCommand.valueOf(commandStr);
+    } catch (Exception e) {
+      return badRequest("Invalid ZooKeeper command: " + commandStr);
+    }
+
+    // Lazily initialize ZkBaseDataAccessor
+    ServerContext _serverContext =
+        (ServerContext) 
_application.getProperties().get(ContextPropertyKeys.SERVER_CONTEXT.name());
+    _zkBaseDataAccessor = _serverContext.getByteArrayZkBaseDataAccessor();
+
+    // Need to prepend a "/" since JAX-RS regex removes it
+    path = "/" + path;
+    switch (cmd) {
+      case exists:
+        return exists(path);
+      case getBinaryData:
+        return getData(path, cmd);
+      case getStringData:
+        return getData(path, cmd);
 
 Review comment:
   These 2 get data commands could be combined?
   ```
          case getBinaryData:
          case getStringData:
            return getData(path, cmd);
   ```
   Or, can we split getData() to have 2 methods: getBinaryData() and 
getStringData(). I prefer this. Since we already check the command types here, 
it would be cleaner just to call the exact APIs, instead of checking the 
command in getData() again, which is passing the command enum and use an 
if-else to check.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to