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 ErrorResource extends Resource
42  {
43    private final static Logger LOG = Logger.getLogger(ErrorResource.class);
44  
45    public ErrorResource(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            getInstanceErrorsRepresentation(clusterName,
88                                            instanceName,
89                                            resourceGroup);
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 getInstanceErrorsRepresentation(String clusterName,
102                                                        String instanceName,
103                                                        String resourceGroup) throws JsonGenerationException,
104       JsonMappingException,
105       IOException
106   {
107     ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
108     String instanceSessionId =
109         ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName,
110                                                        instanceName);
111     Builder keyBuilder = new PropertyKey.Builder(clusterName);
112     String message =
113         ClusterRepresentationUtil.getInstancePropertiesAsString(zkClient, clusterName,
114                                                                 keyBuilder.stateTransitionErrors(instanceName,
115                                                                                                  instanceSessionId,
116                                                                                                  resourceGroup),
117                                                                 // instanceSessionId
118                                                                 // + "__"
119                                                                 // + resourceGroup,
120                                                                 MediaType.APPLICATION_JSON);
121     StringRepresentation representation =
122         new StringRepresentation(message, MediaType.APPLICATION_JSON);
123     return representation;
124   }
125 }