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  
24  import org.apache.helix.PropertyKey;
25  import org.apache.helix.PropertyKey.Builder;
26  import org.apache.helix.manager.zk.ZkClient;
27  import org.apache.helix.webapp.RestAdminApplication;
28  import org.apache.log4j.Logger;
29  import org.codehaus.jackson.JsonGenerationException;
30  import org.codehaus.jackson.map.JsonMappingException;
31  import org.restlet.Context;
32  import org.restlet.data.MediaType;
33  import org.restlet.data.Request;
34  import org.restlet.data.Response;
35  import org.restlet.resource.Representation;
36  import org.restlet.resource.Resource;
37  import org.restlet.resource.StringRepresentation;
38  import org.restlet.resource.Variant;
39  
40  
41  public class ControllerStatusUpdateResource extends Resource
42  {
43    private final static Logger LOG = Logger.getLogger(ControllerStatusUpdateResource.class);
44  
45    public ControllerStatusUpdateResource(Context context, Request request,
46        Response response)
47    {
48      super(context, request, response);
49      getVariants().add(new Variant(MediaType.TEXT_PLAIN));
50      getVariants().add(new Variant(MediaType.APPLICATION_JSON));
51    }
52  
53    @Override
54    public boolean allowGet()
55    {
56      return true;
57    }
58  
59    @Override
60    public boolean allowPost()
61    {
62      return false;
63    }
64  
65    @Override
66    public boolean allowPut()
67    {
68      return false;
69    }
70  
71    @Override
72    public boolean allowDelete()
73    {
74      return false;
75    }
76  
77    @Override
78    public Representation represent(Variant variant)
79    {
80      StringRepresentation presentation = null;
81      try
82      {
83        String zkServer = (String) getContext().getAttributes().get(
84            RestAdminApplication.ZKSERVERADDRESS);
85        String clusterName = (String) getRequest().getAttributes().get(
86            "clusterName");
87        String messageType = (String) getRequest().getAttributes().get(
88            "MessageType");
89        String messageId = (String) getRequest().getAttributes().get("MessageId");
90        // TODO: need pass sessionId to this represent()
91        String sessionId = (String) getRequest().getAttributes().get("SessionId");
92  
93        presentation = getControllerStatusUpdateRepresentation(zkServer,
94            clusterName, sessionId, messageType, messageId);
95      } catch (Exception e)
96      {
97        String error = ClusterRepresentationUtil
98            .getErrorAsJsonStringFromException(e);
99        presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
100       LOG.error("", e);
101     }
102     return presentation;
103   }
104 
105   StringRepresentation getControllerStatusUpdateRepresentation(
106       String zkServerAddress, String clusterName, String sessionId,
107       String messageType, String messageId) throws JsonGenerationException,
108       JsonMappingException, IOException
109   {
110     Builder keyBuilder = new PropertyKey.Builder(clusterName);
111     ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
112     String message = ClusterRepresentationUtil.getPropertyAsString(
113         zkClient,
114         clusterName,
115         keyBuilder.controllerTaskStatus(messageType, messageId),
116         MediaType.APPLICATION_JSON);
117     StringRepresentation representation = new StringRepresentation(message,
118         MediaType.APPLICATION_JSON);
119     return representation;
120   }
121 }