View Javadoc

1   package org.apache.helix.webapp.resources;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import org.apache.helix.ZNRecord;
25  import org.apache.helix.manager.zk.ZkClient;
26  import org.apache.helix.webapp.RestAdminApplication;
27  import org.apache.log4j.Logger;
28  import org.apache.zookeeper.data.Stat;
29  import org.restlet.Context;
30  import org.restlet.data.MediaType;
31  import org.restlet.data.Request;
32  import org.restlet.data.Response;
33  import org.restlet.data.Status;
34  import org.restlet.resource.Representation;
35  import org.restlet.resource.Resource;
36  import org.restlet.resource.StringRepresentation;
37  import org.restlet.resource.Variant;
38  
39  
40  public class ZkChildResource extends Resource
41  {
42    private final static Logger LOG = Logger.getLogger(ZkChildResource.class);
43  
44    public ZkChildResource(Context context, Request request, Response response)
45    {
46      super(context, request, response);
47      getVariants().add(new Variant(MediaType.TEXT_PLAIN));
48      getVariants().add(new Variant(MediaType.APPLICATION_JSON));
49    }
50  
51    @Override
52    public boolean allowGet()
53    {
54      return true;
55    }
56  
57    @Override
58    public boolean allowPost()
59    {
60      return false;
61    }
62  
63    @Override
64    public boolean allowPut()
65    {
66      return false;
67    }
68  
69    @Override
70    public boolean allowDelete()
71    {
72      return true;
73    }
74  
75    private String getZKPath()
76    {
77      String relativeRef = getRequest().getResourceRef().getRelativeRef().toString();
78      if (relativeRef.equals("."))
79      {
80        relativeRef = "";
81      }
82  
83      // strip off trailing "/"
84      while (relativeRef.endsWith("/"))
85      {
86        relativeRef = relativeRef.substring(0, relativeRef.length() - 1);
87      }
88  
89      return "/" + relativeRef;
90    }
91  
92    @Override
93    public Representation represent(Variant variant)
94    {
95      StringRepresentation presentation = null;
96      String zkPath = getZKPath();
97  
98      try
99      {
100       ZkClient zkClient =
101           (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
102       ZNRecord result = readZkChild(zkPath, zkClient);
103 
104       presentation =
105           new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(result),
106                                    MediaType.APPLICATION_JSON);
107     }
108     catch (Exception e)
109     {
110       String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
111       presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
112 
113       LOG.error("Error in read zkPath: " + zkPath, e);
114     }
115 
116     return presentation;
117   }
118 
119   private ZNRecord readZkChild(String zkPath, ZkClient zkClient)
120   {
121     ZNRecord result = null;
122 
123     // read data and stat
124     Stat stat = new Stat();
125     ZNRecord data = zkClient.readDataAndStat(zkPath, stat, true);
126     if (data != null)
127     {
128       result = data;
129     }
130     else
131     {
132       result = new ZNRecord("");
133     }
134 
135     // read childrenList
136     List<String> children = zkClient.getChildren(zkPath);
137     if (children != null && children.size() > 0)
138     {
139       result.setSimpleField("numChildren", "" + children.size());
140       result.setListField("childrenList", children);
141     } else
142     {
143       result.setSimpleField("numChildren", "" + 0);
144     }
145     return result;
146   }
147 
148   @Override
149   public void removeRepresentations()
150   {
151     String zkPath = getZKPath();
152     try
153     {
154       ZkClient zkClient =
155           (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
156       
157       List<String> childNames = zkClient.getChildren(zkPath);
158       if (childNames != null)
159       {
160         for (String childName : childNames)
161         {
162           String childPath = zkPath.equals("/")? "/" + childName : zkPath + "/" + childName;
163           zkClient.deleteRecursive(childPath);
164         }
165       }
166       
167       getResponse().setStatus(Status.SUCCESS_OK);
168     }
169     catch (Exception e)
170     {
171       getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
172                               MediaType.APPLICATION_JSON);
173       getResponse().setStatus(Status.SUCCESS_OK);
174       LOG.error("Error in delete zkChild: " + zkPath, e);
175     }
176   }
177 }