1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.hadoop.chukwa.rest.resource;
1920import java.util.HashMap;
2122import org.apache.commons.logging.Log;
23import org.apache.commons.logging.LogFactory;
24import org.apache.hadoop.chukwa.rest.bean.CatalogBean;
2526import org.apache.hadoop.chukwa.rest.bean.ReturnCodeBean;
27import org.apache.hadoop.chukwa.rest.bean.WidgetBean;
28import org.apache.hadoop.chukwa.datastore.WidgetStore;
293031import javax.ws.rs.Consumes;
32import javax.ws.rs.GET;
33import javax.ws.rs.PUT;
34import javax.ws.rs.Path;
35import javax.ws.rs.PathParam;
36import javax.ws.rs.WebApplicationException;
37import javax.ws.rs.core.Response;
3839 @Path ("/widget")
40publicclassWidgetResource {
41privatestatic Log log = LogFactory.getLog(WidgetResource.class);
4243 @GET
44 @Path("wid/{wid}")
45publicWidgetBean getProfile(@PathParam("wid") String wid) {
46 HashMap<String, WidgetBean> list;
47try {
48 list = WidgetStore.list();
49 } catch (IllegalAccessException e) {
50thrownew WebApplicationException(Response.status(Response.Status.NOT_FOUND)
51 .entity("Widget does not exist.").build());
52 }
53return list.get(wid);
54 }
5556 @PUT
57 @Consumes("application/json")
58publicReturnCodeBean saveWidget(WidgetBean widget) {
59try {
60WidgetStore ws = newWidgetStore();
61 ws.set(widget);
62 } catch(Exception e) {
63thrownew WebApplicationException(Response.status(Response.Status.NOT_FOUND)
64 .entity("Widget save failed.").build());
65 }
66returnnewReturnCodeBean(ReturnCodeBean.SUCCESS,"Saved");
67 }
6869 @GET
70 @Path("catalog")
71publicCatalogBean getWidgetCatalog() {
72CatalogBean result;
73try {
74 result = WidgetStore.getCatalog();
75 } catch (IllegalAccessException e) {
76thrownew WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
77 .entity("No catalog exists.").build());
78 }
79return result;
80 }
81 }