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.PropertyKey;
25  import org.apache.helix.PropertyKey.Builder;
26  import org.apache.helix.manager.zk.ZkClient;
27  import org.apache.helix.webapp.RestAdminApplication;
28  import org.apache.log4j.Logger;
29  import org.codehaus.jackson.JsonGenerationException;
30  import org.codehaus.jackson.map.JsonMappingException;
31  import org.restlet.Context;
32  import org.restlet.data.MediaType;
33  import org.restlet.data.Request;
34  import org.restlet.data.Response;
35  import org.restlet.resource.Representation;
36  import org.restlet.resource.Resource;
37  import org.restlet.resource.StringRepresentation;
38  import org.restlet.resource.Variant;
39  
40  
41  public class CurrentStateResource extends Resource
42  {
43    private final static Logger LOG = Logger.getLogger(CurrentStateResource.class);
44  
45    public CurrentStateResource(Context context, Request request, Response response)
46    {
47      super(context, request, response);
48      getVariants().add(new Variant(MediaType.TEXT_PLAIN));
49      getVariants().add(new Variant(MediaType.APPLICATION_JSON));
50    }
51  
52    @Override
53    public boolean allowGet()
54    {
55      return true;
56    }
57  
58    @Override
59    public boolean allowPost()
60    {
61      return false;
62    }
63  
64    @Override
65    public boolean allowPut()
66    {
67      return false;
68    }
69  
70    @Override
71    public boolean allowDelete()
72    {
73      return false;
74    }
75  
76    @Override
77    public Representation represent(Variant variant)
78    {
79      StringRepresentation presentation = null;
80      try
81      {
82        String clusterName = (String) getRequest().getAttributes().get("clusterName");
83        String instanceName = (String) getRequest().getAttributes().get("instanceName");
84        String resourceGroup = (String) getRequest().getAttributes().get("resourceName");
85  
86        presentation =
87            getInstanceCurrentStateRepresentation(
88                                                  clusterName,
89                                                  instanceName,
90                                                  resourceGroup);
91      }
92      catch (Exception e)
93      {
94        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
95        presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
96  
97        LOG.error("", e);
98      }
99      return presentation;
100   }
101 
102   StringRepresentation getInstanceCurrentStateRepresentation(
103                                                              String clusterName,
104                                                              String instanceName,
105                                                              String resourceGroup) throws JsonGenerationException,
106       JsonMappingException,
107       IOException
108   {
109     ZkClient zkClient = (ZkClient)getRequest().getAttributes().get(RestAdminApplication.ZKCLIENT);
110     String instanceSessionId =
111         ClusterRepresentationUtil.getInstanceSessionId(zkClient,
112                                                        clusterName,
113                                                        instanceName);
114     Builder keyBuilder = new PropertyKey.Builder(clusterName);
115 
116     String message =
117         ClusterRepresentationUtil.getInstancePropertyAsString(zkClient,
118                                                               clusterName,
119                                                               keyBuilder.currentState(instanceName,
120                                                                                       instanceSessionId,
121                                                                                       resourceGroup),
122                                                               MediaType.APPLICATION_JSON);
123     StringRepresentation representation =
124         new StringRepresentation(message, MediaType.APPLICATION_JSON);
125     return representation;
126   }
127 }