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.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 CurrentStatesResource extends Resource
41 {
42 private final static Logger LOG = Logger.getLogger(CurrentStatesResource.class);
43
44 public CurrentStatesResource(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 public boolean allowGet()
52 {
53 return true;
54 }
55
56 public boolean allowPost()
57 {
58 return false;
59 }
60
61 public boolean allowPut()
62 {
63 return false;
64 }
65
66 public boolean allowDelete()
67 {
68 return false;
69 }
70
71 public Representation represent(Variant variant)
72 {
73 StringRepresentation presentation = null;
74 try
75 {
76 String clusterName = (String) getRequest().getAttributes().get("clusterName");
77 String instanceName = (String) getRequest().getAttributes().get("instanceName");
78 presentation = getInstanceCurrentStatesRepresentation(clusterName, instanceName);
79 }
80 catch (Exception e)
81 {
82 String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
83 presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
84
85 LOG.error("", e);
86 }
87 return presentation;
88 }
89
90 StringRepresentation getInstanceCurrentStatesRepresentation(String clusterName, String instanceName) throws JsonGenerationException, JsonMappingException, IOException
91 {
92 ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);;
93 String instanceSessionId = ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
94
95 String message = ClusterRepresentationUtil.getInstancePropertyNameListAsString(zkClient, clusterName, instanceName, PropertyType.CURRENTSTATES, instanceSessionId, MediaType.APPLICATION_JSON);
96
97 StringRepresentation representation = new StringRepresentation(message, MediaType.APPLICATION_JSON);
98
99 return representation;
100 }
101
102 }