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.servlet.http.HttpServletRequest;
21  import javax.ws.rs.Consumes;
22  import javax.ws.rs.DELETE;
23  import javax.ws.rs.FormParam;
24  import javax.ws.rs.GET;
25  import javax.ws.rs.POST;
26  import javax.ws.rs.PUT;
27  import javax.ws.rs.Path;
28  import javax.ws.rs.PathParam;
29  import javax.ws.rs.WebApplicationException;
30  import javax.ws.rs.core.Context;
31  import javax.ws.rs.core.Response;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import org.apache.hadoop.chukwa.rest.bean.ReturnCodeBean;
37  import org.apache.hadoop.chukwa.rest.bean.ViewBean;
38  import org.apache.hadoop.chukwa.datastore.ViewStore;
39  import org.apache.hadoop.chukwa.util.ExceptionUtil;
40  
41  @Path ("/view")
42  public class ViewResource {
43    protected static final Log log = LogFactory.getLog(ViewResource.class);
44  
45    @GET
46    @Path("vid/{vid}")
47    public ViewBean getView(@Context HttpServletRequest request, @PathParam("vid") String vid) {
48      ViewStore view;
49      ViewBean vr;
50      String uid = request.getRemoteUser();
51      try {
52        view = new ViewStore(uid, vid);
53        vr = view.get();
54        if(request.getRemoteUser().intern()!=vr.getOwner().intern() && vr.getPermissionType().intern()!="public".intern()) {
55      	  throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN).entity("permission denied.").build());
56        }
57      } catch (IllegalAccessException e) {
58        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
59            .entity("View does not exist.").build());
60      }
61      return vr;
62    }
63    
64    @PUT
65    @Consumes("application/json")
66    public ReturnCodeBean setView(@Context HttpServletRequest request, ViewBean view) {
67      try {
68        if(request.getRemoteUser().intern()==view.getOwner().intern()) {
69          ViewStore vs = new ViewStore(view.getOwner(), view.getName());
70          vs.set(view);
71        } else {
72            throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN)
73                .entity("Permission denied.").build());    	  
74        }
75      } catch (IllegalAccessException e) {
76        log.error(ExceptionUtil.getStackTrace(e));
77        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
78            .entity("View save failed.").build());
79      }
80      return new ReturnCodeBean(ReturnCodeBean.SUCCESS,"Saved");
81    }
82  
83    @POST
84    @Path("permission")
85    public ReturnCodeBean changeViewPermission(@Context HttpServletRequest request, @FormParam("owner") String owner, @FormParam("view_vid") String vid, @FormParam("permission") String permission) {
86      try {
87        if(owner.intern()==request.getRemoteUser().intern()) {
88          ViewStore vs = new ViewStore(owner, vid);
89          ViewBean view = vs.get();
90          vs.delete();
91          view.setPermissionType(permission);
92          vs.set(view);
93        } else {
94          throw new Exception("Permission denied.");
95        }
96      } catch (Exception e) {
97        log.error(ExceptionUtil.getStackTrace(e));
98        throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
99            .entity("View save failed.").build());      
100     }
101     return new ReturnCodeBean(ReturnCodeBean.SUCCESS,"Saved");
102   }
103 
104   @POST
105   public ReturnCodeBean changeView(@Context HttpServletRequest request, @FormParam("owner") String owner, @FormParam("view_vid") String oldVid, @FormParam("view_name") String name) {
106     try {
107       ViewStore vs;
108       if(oldVid!=null) {
109         vs = new ViewStore(owner, oldVid);
110       } else {
111         vs = new ViewStore(null, "default");
112       }
113       ViewBean view = vs.get();
114       view.setOwner(request.getRemoteUser());
115       view.setName(name);
116       view.setDescription(name);
117       if(oldVid==null) {
118         view.setPermissionType("private");
119       }
120       vs = new ViewStore(request.getRemoteUser(), name);
121       vs.set(view);
122     } catch (Exception e) {
123       log.error(ExceptionUtil.getStackTrace(e));
124       throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
125           .entity("View save failed.").build());      
126     }
127     return new ReturnCodeBean(ReturnCodeBean.SUCCESS,"Saved");
128   }
129 
130   @DELETE
131   @Path("delete/{owner}/vid/{vid}")
132   public ReturnCodeBean deleteView(@Context HttpServletRequest request, @PathParam("owner") String owner, @PathParam("vid") String vid) {
133     try {
134       if(owner.intern()==request.getRemoteUser().intern()) {
135         ViewStore vs = new ViewStore(owner, vid);
136         vs.delete();
137       } else {
138         throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN)
139             .entity("View delete failed.").build());              
140       }
141     } catch (Exception e) {
142       log.error(ExceptionUtil.getStackTrace(e));
143       throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
144           .entity("View delete failed.").build());      
145     }
146     return new ReturnCodeBean(ReturnCodeBean.SUCCESS,"Deleted");
147   }
148 
149   @GET
150   @Path("list")
151   public String getUserViewList(@Context HttpServletRequest request) {
152     String result = "";
153     String uid = null;
154     try {
155       if(uid==null) {
156         uid = request.getRemoteUser();
157       }
158       result = ViewStore.list(uid).toJSONString();
159     } catch (Exception e) {
160       throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)
161           .entity("View does not exist.").build());
162     }
163     return result;
164   }
165 }