View Javadoc

1   package org.apache.helix.webapp.resources;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.util.List;
24  
25  import org.apache.helix.HelixDataAccessor;
26  import org.apache.helix.HelixException;
27  import org.apache.helix.ZNRecord;
28  import org.apache.helix.manager.zk.ZkClient;
29  import org.apache.helix.model.StateModelDefinition;
30  import org.apache.helix.tools.ClusterSetup;
31  import org.apache.helix.webapp.RestAdminApplication;
32  import org.apache.log4j.Logger;
33  import org.codehaus.jackson.JsonGenerationException;
34  import org.codehaus.jackson.map.JsonMappingException;
35  import org.restlet.Context;
36  import org.restlet.data.MediaType;
37  import org.restlet.data.Request;
38  import org.restlet.data.Response;
39  import org.restlet.data.Status;
40  import org.restlet.resource.Representation;
41  import org.restlet.resource.Resource;
42  import org.restlet.resource.StringRepresentation;
43  import org.restlet.resource.Variant;
44  
45  
46  public class StateModelsResource extends Resource
47  {
48    private final static Logger LOG = Logger.getLogger(StateModelsResource.class);
49  
50    public StateModelsResource(Context context,
51        Request request,
52        Response response) 
53    {
54      super(context, request, response);
55      getVariants().add(new Variant(MediaType.TEXT_PLAIN));
56      getVariants().add(new Variant(MediaType.APPLICATION_JSON));
57    }
58  
59    @Override
60    public boolean allowGet()
61    {
62      return true;
63    }
64    
65    @Override
66    public boolean allowPost()
67    {
68      return true;
69    }
70    
71    @Override
72    public boolean allowPut()
73    {
74      return false;
75    }
76    
77    @Override
78    public boolean allowDelete()
79    {
80      return false;
81    }
82    
83    @Override
84    public Representation represent(Variant variant)
85    {
86      StringRepresentation presentation = null;
87      try
88      {
89        presentation = getStateModelsRepresentation();
90      }
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 getStateModelsRepresentation() throws JsonGenerationException, JsonMappingException, IOException
103   {
104     String clusterName = (String)getRequest().getAttributes().get("clusterName");
105     ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
106     ClusterSetup setupTool = new ClusterSetup(zkClient);
107     
108     List<String> models = setupTool.getClusterManagementTool().getStateModelDefs(clusterName);
109     
110     ZNRecord modelDefinitions = new ZNRecord("modelDefinitions");
111     modelDefinitions.setListField("models", models);
112     
113     StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(modelDefinitions), MediaType.APPLICATION_JSON);
114     
115     return representation;
116   }
117   
118   @Override
119   public void acceptRepresentation(Representation entity)
120   {
121     try
122     {
123       String clusterName = (String)getRequest().getAttributes().get("clusterName");
124       ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);;
125       
126       JsonParameters jsonParameters = new JsonParameters(entity);
127       String command = jsonParameters.getCommand();
128 
129         
130       if(command.equalsIgnoreCase(ClusterSetup.addStateModelDef))
131       {
132         ZNRecord newStateModel = jsonParameters.getExtraParameter(JsonParameters.NEW_STATE_MODEL_DEF);
133         HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
134          
135         accessor.setProperty(accessor.keyBuilder().stateModelDef(newStateModel.getId()), new StateModelDefinition(newStateModel) );
136         getResponse().setEntity(getStateModelsRepresentation());
137       }
138       else
139       {
140           throw new HelixException("Unsupported command: " + command
141                                    + ". Should be one of [" + ClusterSetup.addStateModelDef + "]");
142       }
143       
144       getResponse().setStatus(Status.SUCCESS_OK);
145     }
146     catch(Exception e)
147     {
148       getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
149           MediaType.APPLICATION_JSON);
150       getResponse().setStatus(Status.SUCCESS_OK);
151       LOG.error("Error in posting " + entity, e);
152     }  
153   }
154 }