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 import java.util.List;
24
25 import org.apache.helix.HelixException;
26 import org.apache.helix.ZNRecord;
27 import org.apache.helix.manager.zk.ZkClient;
28 import org.apache.helix.tools.ClusterSetup;
29 import org.apache.helix.webapp.RestAdminApplication;
30 import org.apache.log4j.Logger;
31 import org.codehaus.jackson.JsonGenerationException;
32 import org.codehaus.jackson.map.JsonMappingException;
33 import org.restlet.Context;
34 import org.restlet.data.MediaType;
35 import org.restlet.data.Request;
36 import org.restlet.data.Response;
37 import org.restlet.data.Status;
38 import org.restlet.resource.Representation;
39 import org.restlet.resource.Resource;
40 import org.restlet.resource.StringRepresentation;
41 import org.restlet.resource.Variant;
42
43
44 public class ClustersResource extends Resource
45 {
46 private final static Logger LOG = Logger.getLogger(ClustersResource.class);
47
48 public ClustersResource(Context context, Request request, Response response)
49 {
50 super(context, request, response);
51 getVariants().add(new Variant(MediaType.TEXT_PLAIN));
52 getVariants().add(new Variant(MediaType.APPLICATION_JSON));
53
54 }
55
56 @Override
57 public boolean allowPost()
58 {
59 return true;
60 }
61
62 @Override
63 public boolean allowPut()
64 {
65 return false;
66 }
67
68 @Override
69 public boolean allowDelete()
70 {
71 return true;
72 }
73
74 @Override
75 public Representation represent(Variant variant)
76 {
77 StringRepresentation presentation = null;
78 try
79 {
80 presentation = getClustersRepresentation();
81 }
82 catch (Exception e)
83 {
84 LOG.error("", e);
85 String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
86 presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
87
88 e.printStackTrace();
89 }
90 return presentation;
91 }
92
93 StringRepresentation getClustersRepresentation() throws JsonGenerationException,
94 JsonMappingException,
95 IOException
96 {
97 ZkClient zkClient =
98 (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
99 ClusterSetup setupTool = new ClusterSetup(zkClient);
100 List<String> clusters = setupTool.getClusterManagementTool().getClusters();
101
102 ZNRecord clustersRecord = new ZNRecord("Clusters Summary");
103 clustersRecord.setListField("clusters", clusters);
104 StringRepresentation representation =
105 new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(clustersRecord),
106 MediaType.APPLICATION_JSON);
107
108 return representation;
109 }
110
111 @Override
112 public void acceptRepresentation(Representation entity)
113 {
114 try
115 {
116 JsonParameters jsonParameters = new JsonParameters(entity);
117 String command = jsonParameters.getCommand();
118
119 if (command == null)
120 {
121 throw new HelixException("Could NOT find 'command' in parameterMap: " + jsonParameters._parameterMap);
122 }
123 else if (command.equalsIgnoreCase(ClusterSetup.addCluster))
124 {
125 jsonParameters.verifyCommand(ClusterSetup.addCluster);
126
127 ZkClient zkClient =
128 (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
129 ClusterSetup setupTool = new ClusterSetup(zkClient);
130 setupTool.addCluster(jsonParameters.getParameter(JsonParameters.CLUSTER_NAME),
131 false);
132 }
133 else
134 {
135 throw new HelixException("Unsupported command: " + command
136 + ". Should be one of [" + ClusterSetup.addCluster + "]");
137 }
138
139 getResponse().setEntity(getClustersRepresentation());
140 getResponse().setStatus(Status.SUCCESS_OK);
141 }
142 catch (Exception e)
143 {
144 getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
145 MediaType.APPLICATION_JSON);
146 getResponse().setStatus(Status.SUCCESS_OK);
147 LOG.error("Error in posting " + entity, e);
148 }
149 }
150
151 @Override
152 public void removeRepresentations()
153 {
154
155 }
156 }