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 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 }