This project has retired. For details please refer to its Attic page.
WidgetResource 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.rest.resource;
19  
20  import java.util.HashMap;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.chukwa.rest.bean.CatalogBean;
25  
26  import org.apache.hadoop.chukwa.rest.bean.ReturnCodeBean;
27  import org.apache.hadoop.chukwa.rest.bean.WidgetBean;
28  import org.apache.hadoop.chukwa.datastore.WidgetStore;
29  
30  
31  import javax.ws.rs.Consumes;
32  import javax.ws.rs.GET;
33  import javax.ws.rs.PUT;
34  import javax.ws.rs.Path;
35  import javax.ws.rs.PathParam;
36  import javax.ws.rs.WebApplicationException;
37  import javax.ws.rs.core.Response;
38  
39  @Path ("/widget1")
40  public class WidgetResource {
41    private static Log log = LogFactory.getLog(WidgetResource.class);
42    
43    @GET
44    @Path("wid/{wid}")
45    public WidgetBean getProfile(@PathParam("wid") String wid) {
46      HashMap<String, WidgetBean> list;
47      try {
48        list = WidgetStore.list();
49      } catch (IllegalAccessException e) {
50        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
51            .entity("Widget does not exist.").build());
52      }
53      return list.get(wid);
54    }
55    
56    @PUT
57    @Consumes("application/json")
58    public ReturnCodeBean saveWidget(WidgetBean widget) {
59      try {
60        WidgetStore ws = new WidgetStore();
61        ws.set(widget);
62      } catch(Exception e) {
63        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
64            .entity("Widget save failed.").build());
65      }
66      return new ReturnCodeBean(ReturnCodeBean.SUCCESS,"Saved");
67    }
68    
69    @GET
70    @Path("catalog")
71    public CatalogBean getWidgetCatalog() {
72      CatalogBean result;
73      try {
74        result = WidgetStore.getCatalog();
75      } catch (IllegalAccessException e) {
76        throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
77            .entity("No catalog exists.").build());
78      }
79      return result;
80    }
81  }