1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 ("/widget")
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 }