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