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 StatusUpdatesResource extends Resource
41  {
42    private final static Logger LOG = Logger.getLogger(StatusUpdatesResource.class);
43  
44    public StatusUpdatesResource(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 false;
73    }
74  
75    @Override
76    public Representation represent(Variant variant)
77    {
78      StringRepresentation presentation = null;
79      try
80      {
81        String clusterName = (String) getRequest().getAttributes().get("clusterName");
82        String instanceName = (String) getRequest().getAttributes().get("instanceName");
83        presentation = getInstanceErrorsRepresentation( clusterName, instanceName);
84      }
85      catch (Exception e)
86      {
87        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
88        presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
89  
90        LOG.error("", e);
91      }
92      return presentation;
93    }
94  
95    StringRepresentation getInstanceErrorsRepresentation( String clusterName, String instanceName) throws JsonGenerationException, JsonMappingException, IOException
96    {
97      ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
98      String instanceSessionId = ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
99      
100     String message = ClusterRepresentationUtil.getInstancePropertyNameListAsString(zkClient, clusterName, instanceName, PropertyType.CURRENTSTATES, instanceSessionId, MediaType.APPLICATION_JSON);
101 
102     StringRepresentation representation = new StringRepresentation(message, MediaType.APPLICATION_JSON);
103 
104     return representation;
105   }
106   
107 }