]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2b59ec2934876e33f25e259376ecc4745ac33823
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.authorization;
25
26 import javax.ws.rs.PathParam;
27 import javax.ws.rs.WebApplicationException;
28 import javax.ws.rs.core.Context;
29 import javax.ws.rs.core.MultivaluedMap;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.core.UriBuilder;
32 import javax.ws.rs.core.UriInfo;
33 import org.collectionspace.services.authorization.storage.PermissionRoleStorageClient;
34
35 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
36 import org.collectionspace.services.common.context.RemoteServiceContextImpl;
37 import org.collectionspace.services.common.context.ServiceContext;
38 import org.collectionspace.services.common.document.DocumentFilter;
39 import org.collectionspace.services.common.document.DocumentHandler;
40 import org.collectionspace.services.common.storage.StorageClient;
41 import org.jboss.resteasy.util.HttpResponseCodes;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * PermissionRoleSubResource is used to manage permission-role relationship
47  * @author
48  */
49 public class PermissionRoleSubResource
50         extends AbstractCollectionSpaceResourceImpl {
51
52     //this service is never exposed as standalone RESTful service...just use unique
53     //service name to identify binding
54     final private String serviceName = "authorization/permroles";
55     final Logger logger = LoggerFactory.getLogger(PermissionRoleSubResource.class);
56     final StorageClient storageClient = new PermissionRoleStorageClient();
57
58     @Override
59     protected String getVersionString() {
60         /** The last change revision. */
61         final String lastChangeRevision = "$LastChangedRevision: 1165 $";
62         return lastChangeRevision;
63     }
64
65     @Override
66     public String getServiceName() {
67         return serviceName;
68     }
69
70     private <T> ServiceContext createServiceContext(T obj, SubjectType subject) throws Exception {
71         ServiceContext ctx = new RemoteServiceContextImpl<T, T>(getServiceName());
72         ctx.setInput(obj);
73         ctx.setDocumentType(PermissionRole.class.getPackage().getName()); //persistence unit
74         ctx.setProperty("entity-name", PermissionRoleRel.class.getName());
75         //subject name is necessary to indicate if role or permission is a subject
76         ctx.setProperty("subject", subject);
77         return ctx;
78     }
79
80     @Override
81     public StorageClient getStorageClient(ServiceContext ctx) {
82         //FIXME use ctx to identify storage client
83         return storageClient;
84     }
85
86     @Override
87     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
88         DocumentHandler docHandler = ctx.getDocumentHandler();
89         docHandler.setCommonPart(ctx.getInput());
90         return docHandler;
91     }
92
93     /**
94      * createPermissionRole creates one or more permission-role relationships
95      * between object (permission/role) and subject (role/permission)
96      * @param input
97      * @param subject
98      * @return
99      * @throws Exception
100      */
101     public String createPermissionRole(PermissionRole input, SubjectType subject)
102             throws Exception {
103
104         ServiceContext ctx = createServiceContext(input, subject);
105         DocumentHandler handler = createDocumentHandler(ctx);
106         return getStorageClient(ctx).create(ctx, handler);
107     }
108
109     /**
110      * getPermissionRole retrieves permission-role relationships using given
111      * csid of object (permission/role) and subject (role/permission)
112      * @param csid
113      * @param subject
114      * @return
115      * @throws Exception
116      */
117     public PermissionRole getPermissionRole(
118             String csid, SubjectType subject) throws Exception {
119
120         if (logger.isDebugEnabled()) {
121             logger.debug("getPermissionRole with csid=" + csid);
122         }
123         PermissionRole result = null;
124         ServiceContext ctx = createServiceContext((PermissionRole) null, subject);
125         DocumentHandler handler = createDocumentHandler(ctx);
126         getStorageClient(ctx).get(ctx, csid, handler);
127         result = (PermissionRole) ctx.getOutput();
128
129         return result;
130     }
131
132     /**
133      * deletePermissionRole deletes permission-role relationships using given
134      * csid of object (permission/role) and subject (role/permission)
135      * @param csid
136      * @param subject
137      * @return
138      * @throws Exception
139      */
140     public void deletePermissionRole(String csid,
141             SubjectType subject) throws Exception {
142
143         if (logger.isDebugEnabled()) {
144             logger.debug("deletePermissionRole with csid=" + csid);
145         }
146         ServiceContext ctx = createServiceContext((PermissionRole) null, subject);
147         getStorageClient(ctx).delete(ctx, csid);
148     }
149 }