This project has retired. For details please refer to its Attic page.
WidgetController xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.chukwa.hicc.rest;
19  
20  import java.util.List;
21  
22  import javax.annotation.PostConstruct;
23  import javax.inject.Singleton;
24  import javax.servlet.ServletContext;
25  import javax.ws.rs.Consumes;
26  import javax.ws.rs.DELETE;
27  import javax.ws.rs.DefaultValue;
28  import javax.ws.rs.GET;
29  import javax.ws.rs.POST;
30  import javax.ws.rs.PUT;
31  import javax.ws.rs.Path;
32  import javax.ws.rs.PathParam;
33  import javax.ws.rs.Produces;
34  import javax.ws.rs.QueryParam;
35  import javax.ws.rs.core.Context;
36  import javax.ws.rs.core.MediaType;
37  import javax.ws.rs.core.Response;
38  import javax.ws.rs.core.Response.Status;
39  
40  import org.apache.hadoop.chukwa.datastore.ChukwaHBaseStore;
41  import org.apache.hadoop.chukwa.hicc.bean.Widget;
42  import org.apache.log4j.Logger;
43  
44  import com.google.gson.Gson;
45  
46  @Path("widget")
47  public class WidgetController {
48  
49    static Logger LOG = Logger.getLogger(WidgetController.class);
50  
51    @Context
52    private ServletContext context;
53  
54    @PostConstruct
55    @Singleton
56    public void init() {
57    }
58  
59    /**
60     * List widgets
61     * 
62     * @param limit is number of widgets to return
63     * @param offset is position in the widget list
64     * @return list of widgets in HBase database
65     * 
66     * @response.representation.200.doc Display list of HICC supported widgets
67     * @response.representation.200.mediaType application/json
68     * @response.representation.200.example {@link Examples#WIDGET_LIST}
69     */
70    @GET
71    @Path("list")
72    @Produces(MediaType.APPLICATION_JSON)
73    public String listWidget(@DefaultValue("1000") @QueryParam("limit") int limit, 
74        @DefaultValue("0") @QueryParam("offset") int offset) {
75      List<Widget> widgets = ChukwaHBaseStore.listWidget(limit, offset);
76      Gson gson = new Gson();
77      String json = gson.toJson(widgets);
78      return json;
79    }
80  
81    /**
82     * Search for widget base on widget title
83     * @param query is search critieria
84     * @return list of widgets matched search critieria
85     * 
86     * @response.representation.200.doc Display list of HICC widget that matches query
87     * @response.representation.200.mediaType application/json
88     * @response.representation.200.example {@link Examples#WIDGET_LIST}
89     */
90    @GET
91    @Path("search/{query}")
92    public String searchWidget(@PathParam("query") String query) {
93      List<Widget> widgets = ChukwaHBaseStore.searchWidget(query);
94      Gson gson = new Gson();
95      String json = gson.toJson(widgets);
96      return json;
97    }
98  
99    /**
100    * View widget details
101    * @param title is title of the widget
102    * @return widget configuration
103    * 
104    * @response.representation.200.doc View widget details
105    * @response.representation.200.mediaType application/json
106    * @response.representation.200.example {@link Examples#SYSTEM_LOAD_AVERAGE_WIDGET}
107    */
108   @GET
109   @Path("view/{title}")
110   public String viewWidget(@PathParam("title") String title) {
111     Widget w = ChukwaHBaseStore.viewWidget(title);
112     Gson gson = new Gson();
113     String json = gson.toJson(w);
114     return json;
115   }
116 
117   /**
118    * Create a widget
119    * @param buffer is widget configuration
120    * @return Creation status code
121    * 
122    * @request.representation.example {@link Examples#WELCOME_PAGE_WIDGET}
123    * @response.representation.200.doc Widget creation successful
124    * @response.representation.200.mediaType text/plain
125    * @response.representation.200.example 200 OK
126    * @response.representation.400.doc Widget creation unsuccessful
127    * @response.representation.400.mediaType text/plain
128    * @response.representation.400.example 400 Bad Request
129    */
130   @POST
131   @Path("create")
132   @Consumes(MediaType.APPLICATION_JSON)
133   public Response createWidget(String buffer) {
134     Gson gson = new Gson();
135     Widget widget = gson.fromJson(buffer, Widget.class);
136     boolean result = ChukwaHBaseStore.createWidget(widget);
137     if(!result) {
138       return Response.status(Status.BAD_REQUEST).build();
139     }
140     return Response.ok().build();
141   }
142 
143   /**
144    * Update a widget
145    * @param title is widget title
146    * @param buffer is widget object in JSON
147    * @return Update status code
148    * 
149    * @request.representation.example {@link Examples#WELCOME_PAGE_WIDGET}
150    * @response.representation.200.doc Widget update is successful
151    * @response.representation.200.mediaType text/plain
152    * @response.representation.200.example 200 OK
153    * @response.representation.400.doc Widget update unsuccessful
154    * @response.representation.400.mediaType text/plain
155    * @response.representation.400.example 400 Bad Request
156    */
157   @PUT
158   @Path("update/{title}")
159   @Consumes(MediaType.APPLICATION_JSON)
160   public Response updateWidget(@PathParam("title") String title, String buffer){
161     Gson gson = new Gson();
162     Widget widget = gson.fromJson(buffer, Widget.class);
163     boolean result = ChukwaHBaseStore.updateWidget(title, widget);
164     if(!result) {
165       return Response.status(Status.BAD_REQUEST).build();
166     }
167     return Response.ok().build();
168   }
169 
170   /**
171    * Delete a widget
172    * @param title is widget title
173    * @return Widget delete status code
174    * 
175    * @response.representation.200.doc Widget deletion successful
176    * @response.representation.200.mediaType text/plain
177    * @response.representation.200.example 200 OK
178    * @response.representation.400.doc Widget deletion unsuccessful
179    * @response.representation.400.mediaType text/plain
180    * @response.representation.400.example 400 Bad Request
181    */
182   @DELETE
183   @Path("delete/{title}")
184   public Response deleteWidget(@PathParam("title") String title) {
185     boolean result = ChukwaHBaseStore.deleteWidget(title);
186     if(!result) {
187       return Response.status(Status.BAD_REQUEST).build();
188     }
189     return Response.ok().build();
190   }
191 }