pkuwm commented on a change in pull request #671: Add ZooKeeperAccessor to helix-rest URL: https://github.com/apache/helix/pull/671#discussion_r366658812
########## File path: helix-rest/src/main/java/org/apache/helix/rest/server/resources/zookeeper/ZooKeeperAccessor.java ########## @@ -0,0 +1,183 @@ +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.base.Enums; +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 = getZooKeeperCommandIfPresent(commandStr); + if (cmd == null) { + 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: + case getStringData: + return getData(path, cmd); + case getChildren: + return getChildren(path); + default: + LOG.error("Unsupported command :" + commandStr); + return badRequest("Unsupported command :" + commandStr); + } + } + + /** + * Checks if a ZNode exists in the given path. + * @param path + * @return true if a ZNode exists, false otherwise + */ + private Response exists(String path) { + if (!isPathValid(path)) { + String errMsg = "exists(): The given path is not a valid ZooKeeper path: " + path; + LOG.info(errMsg); + return badRequest(errMsg); + } + + Map<String, Boolean> result = ImmutableMap.of(ZooKeeperCommand.exists.name(), + _zkBaseDataAccessor.exists(path, AccessOption.PERSISTENT)); + return JSONRepresentation(result); + } + + /** + * Reads the given path from ZooKeeper and returns the binary data for the ZNode. + * @param path + * @param command denotes whether return type should be binary or String + * @return binary data in the ZNode + */ + private Response getData(String path, ZooKeeperCommand command) { + if (!isPathValid(path)) { + String errMsg = "getData(): The given path is not a valid ZooKeeper path: " + path; + LOG.info(errMsg); + return badRequest(errMsg); + } + + if (_zkBaseDataAccessor.exists(path, AccessOption.PERSISTENT)) { + byte[] bytes = _zkBaseDataAccessor.get(path, null, AccessOption.PERSISTENT); + String s = new String(bytes); + + switch (command) { + case getBinaryData: + Map<String, byte[]> binaryResult = + ImmutableMap.of(ZooKeeperCommand.getBinaryData.name(), bytes); + // Note: this serialization (using ObjectMapper) will convert this byte[] into + // a Base64 String! The REST client (user) must convert the resulting String back into + // a byte[] using Base64. + return JSONRepresentation(binaryResult); + case getStringData: + Map<String, String> stringResult = + ImmutableMap.of(ZooKeeperCommand.getStringData.name(), new String(bytes)); + return JSONRepresentation(stringResult); + default: + LOG.error("Unsupported command : " + command); + return badRequest("Unsupported command : " + command); Review comment: Nit: Maybe make this `"Unsupported command: " + command` a variable so one less string object is created? ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@helix.apache.org For additional commands, e-mail: reviews-h...@helix.apache.org