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