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  
24  import org.apache.helix.PropertyType;
25  import org.apache.helix.manager.zk.ZkClient;
26  import org.apache.helix.webapp.RestAdminApplication;
27  import org.apache.log4j.Logger;
28  import org.codehaus.jackson.JsonGenerationException;
29  import org.codehaus.jackson.map.JsonMappingException;
30  import org.restlet.Context;
31  import org.restlet.data.MediaType;
32  import org.restlet.data.Request;
33  import org.restlet.data.Response;
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 CurrentStatesResource extends Resource
41  {
42    private final static Logger LOG = Logger.getLogger(CurrentStatesResource.class);
43  
44    public CurrentStatesResource(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    public boolean allowGet()
52    {
53      return true;
54    }
55  
56    public boolean allowPost()
57    {
58      return false;
59    }
60  
61    public boolean allowPut()
62    {
63      return false;
64    }
65  
66    public boolean allowDelete()
67    {
68      return false;
69    }
70  
71    public Representation represent(Variant variant)
72    {
73      StringRepresentation presentation = null;
74      try
75      {
76        String clusterName = (String) getRequest().getAttributes().get("clusterName");
77        String instanceName = (String) getRequest().getAttributes().get("instanceName");
78        presentation = getInstanceCurrentStatesRepresentation(clusterName, instanceName);
79      }
80      catch (Exception e)
81      {
82        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
83        presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
84  
85        LOG.error("", e);
86      }
87      return presentation;
88    }
89  
90    StringRepresentation getInstanceCurrentStatesRepresentation(String clusterName, String instanceName) throws JsonGenerationException, JsonMappingException, IOException
91    {
92      ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);;
93      String instanceSessionId = ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
94      
95      String message = ClusterRepresentationUtil.getInstancePropertyNameListAsString(zkClient, clusterName, instanceName, PropertyType.CURRENTSTATES, instanceSessionId, MediaType.APPLICATION_JSON);
96  
97      StringRepresentation representation = new StringRepresentation(message, MediaType.APPLICATION_JSON);
98  
99      return representation;
100   }
101   
102 }