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 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 }