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.io.IOException;
23  import java.util.Arrays;
24  
25  import org.apache.helix.HelixException;
26  import org.apache.helix.PropertyKey;
27  import org.apache.helix.PropertyKey.Builder;
28  import org.apache.helix.manager.zk.ZkClient;
29  import org.apache.helix.tools.ClusterSetup;
30  import org.apache.helix.webapp.RestAdminApplication;
31  import org.apache.log4j.Logger;
32  import org.codehaus.jackson.JsonGenerationException;
33  import org.codehaus.jackson.map.JsonMappingException;
34  import org.restlet.Context;
35  import org.restlet.data.MediaType;
36  import org.restlet.data.Request;
37  import org.restlet.data.Response;
38  import org.restlet.data.Status;
39  import org.restlet.resource.Representation;
40  import org.restlet.resource.Resource;
41  import org.restlet.resource.StringRepresentation;
42  import org.restlet.resource.Variant;
43  
44  
45  public class ResourceGroupResource extends Resource
46  {
47    private final static Logger LOG = Logger.getLogger(ResourceGroupResource.class);
48  
49    public ResourceGroupResource(Context context, Request request, Response response)
50    {
51      super(context, request, response);
52      getVariants().add(new Variant(MediaType.TEXT_PLAIN));
53      getVariants().add(new Variant(MediaType.APPLICATION_JSON));
54    }
55  
56    @Override
57    public boolean allowGet()
58    {
59      return true;
60    }
61  
62    @Override
63    public boolean allowPost()
64    {
65      return true;
66    }
67  
68    @Override
69    public boolean allowPut()
70    {
71      return false;
72    }
73  
74    @Override
75    public boolean allowDelete()
76    {
77      return true;
78    }
79  
80    @Override
81    public Representation represent(Variant variant)
82    {
83      StringRepresentation presentation = null;
84      try
85      {
86        String clusterName = (String) getRequest().getAttributes().get("clusterName");
87        String resourceName = (String) getRequest().getAttributes().get("resourceName");
88        presentation = getIdealStateRepresentation(clusterName, resourceName);
89      }
90  
91      catch (Exception e)
92      {
93        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
94        presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
95  
96        LOG.error("", e);
97      }
98      return presentation;
99    }
100 
101   StringRepresentation getIdealStateRepresentation(String clusterName, String resourceName) throws JsonGenerationException,
102       JsonMappingException,
103       IOException
104   {
105     Builder keyBuilder = new PropertyKey.Builder(clusterName);
106     ZkClient zkClient =
107         (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
108 
109     String message =
110         ClusterRepresentationUtil.getClusterPropertyAsString(zkClient,
111                                                              clusterName,
112                                                              keyBuilder.idealStates(resourceName),
113                                                              MediaType.APPLICATION_JSON);
114 
115     StringRepresentation representation =
116         new StringRepresentation(message, MediaType.APPLICATION_JSON);
117 
118     return representation;
119   }
120 
121   @Override
122   public void removeRepresentations()
123   {
124     try
125     {
126       String clusterName = (String) getRequest().getAttributes().get("clusterName");
127       String resourceGroupName =
128           (String) getRequest().getAttributes().get("resourceName");
129       ZkClient zkClient =
130           (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
131       
132       ClusterSetup setupTool = new ClusterSetup(zkClient);
133       setupTool.dropResourceFromCluster(clusterName, resourceGroupName);
134       getResponse().setStatus(Status.SUCCESS_OK);
135     }
136     catch (Exception e)
137     {
138       getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
139                               MediaType.APPLICATION_JSON);
140       getResponse().setStatus(Status.SUCCESS_OK);
141       LOG.error("", e);
142     }
143   }
144 
145   @Override
146   public void acceptRepresentation(Representation entity)
147   {
148     try
149     {
150       String clusterName = (String) getRequest().getAttributes().get("clusterName");
151       String resourceName = (String) getRequest().getAttributes().get("resourceName");
152 
153       JsonParameters jsonParameters = new JsonParameters(entity);
154       String command = jsonParameters.getCommand();
155       if (command.equalsIgnoreCase(ClusterSetup.resetResource))
156       {
157         ZkClient zkClient =
158             (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
159         ClusterSetup setupTool = new ClusterSetup(zkClient);
160         setupTool.getClusterManagementTool().resetResource(clusterName, Arrays.asList(resourceName));
161       }
162       else
163       {
164         throw new HelixException("Unsupported command: " + command
165                                  + ". Should be one of [" + ClusterSetup.resetResource + "]");
166       }
167     }
168     catch (Exception e)
169     {
170       getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
171                               MediaType.APPLICATION_JSON);
172       getResponse().setStatus(Status.SUCCESS_OK);
173       LOG.error("", e);
174     }
175   }
176 
177 }