View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
18   */
19  
20  package eu.ehri.extension;
21  
22  import eu.ehri.extension.base.AbstractResource;
23  import eu.ehri.extension.base.DeleteResource;
24  import eu.ehri.extension.base.GetResource;
25  import eu.ehri.project.core.Tx;
26  import eu.ehri.project.definitions.Entities;
27  import eu.ehri.project.exceptions.ItemNotFound;
28  import eu.ehri.project.exceptions.PermissionDenied;
29  import eu.ehri.project.models.EntityClass;
30  import eu.ehri.project.models.PermissionGrant;
31  import org.neo4j.graphdb.GraphDatabaseService;
32  
33  import javax.ws.rs.DELETE;
34  import javax.ws.rs.GET;
35  import javax.ws.rs.Path;
36  import javax.ws.rs.PathParam;
37  import javax.ws.rs.Produces;
38  import javax.ws.rs.core.Context;
39  import javax.ws.rs.core.MediaType;
40  import javax.ws.rs.core.Response;
41  
42  /**
43   * Provides a web service interface for the PermissionGrant model.
44   */
45  @Path(AbstractResource.RESOURCE_ENDPOINT_PREFIX + "/" + Entities.PERMISSION_GRANT)
46  public class PermissionGrantResource extends AbstractResource implements DeleteResource, GetResource {
47  
48      public PermissionGrantResource(@Context GraphDatabaseService database) {
49          super(database);
50      }
51  
52      /**
53       * Fetch a given permission grant.
54       *
55       * @param id The ID of the permission grant
56       * @return The permission grant
57       */
58      @GET
59      @Produces(MediaType.APPLICATION_JSON)
60      @Path("{id:[^/]+}")
61      @Override
62      public Response get(@PathParam("id") String id) throws ItemNotFound {
63          try (final Tx tx = beginTx()) {
64              PermissionGrant grant = manager.getEntity(id,
65                      EntityClass.PERMISSION_GRANT, PermissionGrant.class);
66              Response response = single(grant);
67              tx.success();
68              return response;
69          }
70      }
71  
72      /**
73       * Revoke a particular permission grant.
74       *
75       * @param id The ID of the permission grant
76       */
77      @DELETE
78      @Path("{id:[^/]+}")
79      @Override
80      public void delete(@PathParam("id") String id) throws ItemNotFound, PermissionDenied {
81          try (final Tx tx = beginTx()) {
82              api().acl().revokePermissionGrant(manager.getEntity(id,
83                      EntityClass.PERMISSION_GRANT, PermissionGrant.class));
84              tx.success();
85          }
86      }
87  }