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 javax.ws.rs.Consumes;
21  import javax.ws.rs.GET;
22  import javax.ws.rs.PUT;
23  import javax.ws.rs.Path;
24  import javax.ws.rs.PathParam;
25  import javax.ws.rs.Produces;
26  import javax.ws.rs.WebApplicationException;
27  import javax.ws.rs.core.Response;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import org.apache.hadoop.chukwa.rest.bean.ReturnCodeBean;
33  import org.apache.hadoop.chukwa.rest.bean.UserBean;
34  import org.apache.hadoop.chukwa.datastore.UserStore;
35  import org.apache.hadoop.chukwa.util.ExceptionUtil;
36  
37  
38  @Path ("/user")
39  public class UserResource {
40    protected static final Log log = LogFactory.getLog(UserResource.class);
41    
42    @GET
43    @Path("uid/{uid}")
44    public UserBean getProfile(@PathParam("uid") String uid) {
45      UserStore user;
46      UserBean result;
47      try {
48        user = new UserStore(uid);
49        result = user.get();
50      } catch (Exception e) {
51        log.error(ExceptionUtil.getStackTrace(e));
52        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
53            .entity("User does not exist.").build());
54      }
55      return result;
56    }
57    
58    @PUT
59    @Consumes("application/json")
60    public ReturnCodeBean setProfile(UserBean user) {
61      try {
62        UserStore us = new UserStore(user.getId());
63        us.set(user);
64      } catch(Exception e) {
65        log.error(ExceptionUtil.getStackTrace(e));
66        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
67            .entity("User does not exist.").build());
68      }
69      return new ReturnCodeBean(ReturnCodeBean.SUCCESS,"Saved.");
70    }
71    
72    @GET
73    @Path("list")
74    @Produces("application/javascript")
75    public String getUserList() {
76      String result = "";
77      try {
78        result = UserStore.list().toString();
79      } catch (IllegalAccessException e) {
80        log.error(ExceptionUtil.getStackTrace(e));
81        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
82            .entity("User does not exist.").build());
83      }
84      return result;
85    }
86  }