1 package org.apache.helix.webapp.resources;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
118
119
120 MediaType.APPLICATION_JSON);
121 StringRepresentation representation =
122 new StringRepresentation(message, MediaType.APPLICATION_JSON);
123 return representation;
124 }
125 }