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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.authorization;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.MultivaluedMap;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.UriBuilder;
39 import javax.ws.rs.core.UriInfo;
41 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
42 import org.collectionspace.services.common.context.RemoteServiceContextImpl;
43 import org.collectionspace.services.common.context.ServiceContext;
44 import org.collectionspace.services.common.document.BadRequestException;
45 import org.collectionspace.services.common.document.DocumentFilter;
46 import org.collectionspace.services.common.document.DocumentNotFoundException;
47 import org.collectionspace.services.common.document.DocumentHandler;
48 import org.collectionspace.services.common.security.UnauthorizedException;
49 import org.collectionspace.services.common.storage.StorageClient;
50 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
51 import org.jboss.resteasy.util.HttpResponseCodes;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 @Path("/authorization/roles")
57 @Consumes("application/xml")
58 @Produces("application/xml")
59 public class RoleResource
60 extends AbstractCollectionSpaceResourceImpl {
62 final private String serviceName = "authorization/roles";
63 final Logger logger = LoggerFactory.getLogger(RoleResource.class);
64 final StorageClient storageClient = new JpaStorageClientImpl();
67 protected String getVersionString() {
68 /** The last change revision. */
69 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
70 return lastChangeRevision;
74 public String getServiceName() {
78 private <T> ServiceContext createServiceContext(T obj) throws Exception {
79 ServiceContext ctx = new RemoteServiceContextImpl<T, T>(getServiceName());
81 ctx.setDocumentType(Role.class.getPackage().getName()); //persistence unit
82 ctx.setProperty("entity-name", Role.class.getName());
87 public StorageClient getStorageClient(ServiceContext ctx) {
88 //FIXME use ctx to identify storage client
93 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
94 DocumentHandler docHandler = ctx.getDocumentHandler();
95 docHandler.setCommonPart(ctx.getInput());
100 public Response createRole(Role input) {
102 ServiceContext ctx = createServiceContext(input);
103 DocumentHandler handler = createDocumentHandler(ctx);
104 String csid = getStorageClient(ctx).create(ctx, handler);
105 UriBuilder path = UriBuilder.fromResource(RoleResource.class);
106 path.path("" + csid);
107 Response response = Response.created(path.build()).build();
109 } catch (BadRequestException bre) {
110 Response response = Response.status(
111 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
112 throw new WebApplicationException(response);
113 } catch (UnauthorizedException ue) {
114 Response response = Response.status(
115 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
116 throw new WebApplicationException(response);
117 } catch (Exception e) {
118 if (logger.isDebugEnabled()) {
119 logger.debug("Caught exception in createRole", e);
121 Response response = Response.status(
122 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
123 throw new WebApplicationException(response);
130 @PathParam("csid") String csid) {
131 if (logger.isDebugEnabled()) {
132 logger.debug("getRole with csid=" + csid);
134 if (csid == null || "".equals(csid)) {
135 logger.error("getRole: missing csid!");
136 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
137 "get failed on Role csid=" + csid).type(
138 "text/plain").build();
139 throw new WebApplicationException(response);
143 ServiceContext ctx = createServiceContext((Role) null);
144 DocumentHandler handler = createDocumentHandler(ctx);
145 getStorageClient(ctx).get(ctx, csid, handler);
146 result = (Role) ctx.getOutput();
147 } catch (UnauthorizedException ue) {
148 Response response = Response.status(
149 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
150 throw new WebApplicationException(response);
151 } catch (DocumentNotFoundException dnfe) {
152 if (logger.isDebugEnabled()) {
153 logger.debug("getRole", dnfe);
155 Response response = Response.status(Response.Status.NOT_FOUND).entity(
156 "Get failed on Role csid=" + csid).type(
157 "text/plain").build();
158 throw new WebApplicationException(response);
159 } catch (Exception e) {
160 if (logger.isDebugEnabled()) {
161 logger.debug("getRole", e);
163 Response response = Response.status(
164 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
165 throw new WebApplicationException(response);
168 if (result == null) {
169 Response response = Response.status(Response.Status.NOT_FOUND).entity(
170 "Get failed, the requested Role CSID:" + csid + ": was not found.").type(
171 "text/plain").build();
172 throw new WebApplicationException(response);
178 @Produces("application/xml")
179 public RolesList getRoleList(
180 @Context UriInfo ui) {
181 RolesList roleList = new RolesList();
183 ServiceContext ctx = createServiceContext((RolesList) null);
184 DocumentHandler handler = createDocumentHandler(ctx);
185 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
186 DocumentFilter myFilter = handler.createDocumentFilter(ctx);
187 myFilter.setPagination(queryParams);
188 myFilter.setQueryParams(queryParams);
189 handler.setDocumentFilter(myFilter);
190 getStorageClient(ctx).getFiltered(ctx, handler);
191 roleList = (RolesList) handler.getCommonPartList();
192 } catch (UnauthorizedException ue) {
193 Response response = Response.status(
194 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
195 throw new WebApplicationException(response);
197 } catch (Exception e) {
198 if (logger.isDebugEnabled()) {
199 logger.debug("Caught exception in getRoleList", e);
201 Response response = Response.status(
202 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
203 throw new WebApplicationException(response);
210 public Role updateRole(
211 @PathParam("csid") String csid,
213 if (logger.isDebugEnabled()) {
214 logger.debug("updateRole with csid=" + csid);
216 if (csid == null || "".equals(csid)) {
217 logger.error("updateRole: missing csid!");
218 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
219 "update failed on Role csid=" + csid).type(
220 "text/plain").build();
221 throw new WebApplicationException(response);
225 ServiceContext ctx = createServiceContext(theUpdate);
226 DocumentHandler handler = createDocumentHandler(ctx);
227 getStorageClient(ctx).update(ctx, csid, handler);
228 result = (Role) ctx.getOutput();
229 } catch (BadRequestException bre) {
230 Response response = Response.status(
231 Response.Status.BAD_REQUEST).entity("Update failed reason " + bre.getErrorReason()).type("text/plain").build();
232 throw new WebApplicationException(response);
233 } catch (UnauthorizedException ue) {
234 Response response = Response.status(
235 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
236 throw new WebApplicationException(response);
237 } catch (DocumentNotFoundException dnfe) {
238 if (logger.isDebugEnabled()) {
239 logger.debug("caugth exception in updateRole", dnfe);
241 Response response = Response.status(Response.Status.NOT_FOUND).entity(
242 "Update failed on Role csid=" + csid).type(
243 "text/plain").build();
244 throw new WebApplicationException(response);
245 } catch (Exception e) {
246 Response response = Response.status(
247 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
248 throw new WebApplicationException(response);
255 public Response deleteRole(@PathParam("csid") String csid) {
257 if (logger.isDebugEnabled()) {
258 logger.debug("deleteRole with csid=" + csid);
260 if (csid == null || "".equals(csid)) {
261 logger.error("deleteRole: missing csid!");
262 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
263 "delete failed on Role csid=" + csid).type(
264 "text/plain").build();
265 throw new WebApplicationException(response);
268 ServiceContext ctx = createServiceContext((Role) null);
269 ((JpaStorageClientImpl)getStorageClient(ctx)).deleteWhere(ctx, csid);
270 return Response.status(HttpResponseCodes.SC_OK).build();
271 } catch (UnauthorizedException ue) {
272 Response response = Response.status(
273 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
274 throw new WebApplicationException(response);
276 } catch (DocumentNotFoundException dnfe) {
277 if (logger.isDebugEnabled()) {
278 logger.debug("caught exception in deleteRole", dnfe);
280 Response response = Response.status(Response.Status.NOT_FOUND).entity(
281 "Delete failed on Role csid=" + csid).type(
282 "text/plain").build();
283 throw new WebApplicationException(response);
284 } catch (Exception e) {
285 Response response = Response.status(
286 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
287 throw new WebApplicationException(response);