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.QueryParam;
34  import javax.ws.rs.core.Context;
35  import javax.ws.rs.core.MediaType;
36  import javax.ws.rs.core.Response;
37  import javax.ws.rs.core.Response.Status;
38  
39  import org.apache.hadoop.chukwa.datastore.ChukwaHBaseStore;
40  import org.apache.hadoop.chukwa.hicc.bean.Widget;
41  import org.apache.log4j.Logger;
42  
43  import com.google.gson.Gson;
44  
45  @Path("widget")
46  public class WidgetController {
47  
48    static Logger LOG = Logger.getLogger(WidgetController.class);
49  
50    @Context
51    private ServletContext context;
52  
53    @PostConstruct
54    @Singleton
55    public void init() {
56    }
57  
58    @GET
59    @Path("list")
60    public String listWidget(@DefaultValue("1000") @QueryParam("limit") int limit, 
61        @DefaultValue("0") @QueryParam("offset") int offset) {
62      List<Widget> widgets = ChukwaHBaseStore.listWidget(limit, offset);
63      Gson gson = new Gson();
64      String json = gson.toJson(widgets);
65      return json;
66    }
67  
68    @GET
69    @Path("search/{query}")
70    public String searchWidget(@PathParam("query") String query) {
71      List<Widget> widgets = ChukwaHBaseStore.searchWidget(query);
72      Gson gson = new Gson();
73      String json = gson.toJson(widgets);
74      return json;
75    }
76  
77    @GET
78    @Path("view/{title}")
79    public String viewWidget(@PathParam("title") String title) {
80      Widget w = ChukwaHBaseStore.viewWidget(title);
81      Gson gson = new Gson();
82      String json = gson.toJson(w);
83      return json;
84    }
85  
86    @POST
87    @Path("create")
88    @Consumes(MediaType.APPLICATION_JSON)
89    public Response createWidget(String buffer) {
90      Gson gson = new Gson();
91      Widget widget = gson.fromJson(buffer, Widget.class);
92      boolean result = ChukwaHBaseStore.createWidget(widget);
93      if(!result) {
94        return Response.status(Status.BAD_REQUEST).build();
95      }
96      return Response.ok().build();
97    }
98  
99    @PUT
100   @Path("update/{title}")
101   @Consumes(MediaType.APPLICATION_JSON)
102   public Response updateWidget(@PathParam("title") String title, String buffer){
103     Gson gson = new Gson();
104     Widget widget = gson.fromJson(buffer, Widget.class);
105     boolean result = ChukwaHBaseStore.updateWidget(title, widget);
106     if(!result) {
107       return Response.status(Status.BAD_REQUEST).build();
108     }
109     return Response.ok().build();
110   }
111 
112   @DELETE
113   @Path("delete/{title}")
114   public Response deleteWidget(@PathParam("title") String title) {
115     boolean result = ChukwaHBaseStore.deleteWidget(title);
116     if(!result) {
117       return Response.status(Status.BAD_REQUEST).build();
118     }
119     return Response.ok().build();
120   }
121 }