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.person;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.PUT;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
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.AbstractCollectionSpaceResource;
42 import org.collectionspace.services.common.ClientType;
43 import org.collectionspace.services.common.ServiceMain;
44 import org.collectionspace.services.common.context.MultipartServiceContext;
45 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
46 import org.collectionspace.services.common.context.ServiceContext;
47 import org.collectionspace.services.common.document.DocumentFilter;
48 import org.collectionspace.services.common.document.DocumentHandler;
49 import org.collectionspace.services.common.document.DocumentNotFoundException;
50 import org.collectionspace.services.common.security.UnauthorizedException;
51 import org.collectionspace.services.person.nuxeo.PersonAuthorityHandlerFactory;
52 import org.collectionspace.services.person.nuxeo.PersonDocumentModelHandler;
53 import org.collectionspace.services.person.nuxeo.PersonHandlerFactory;
54 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
55 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
56 import org.jboss.resteasy.util.HttpResponseCodes;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
60 @Path("/personauthorities")
61 @Consumes("multipart/mixed")
62 @Produces("multipart/mixed")
63 public class PersonAuthorityResource extends AbstractCollectionSpaceResource {
65 private final static String personAuthorityServiceName = "personauthorities";
66 private final static String personServiceName = "persons";
67 final Logger logger = LoggerFactory.getLogger(PersonAuthorityResource.class);
68 //FIXME retrieve client type from configuration
69 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
71 public PersonAuthorityResource() {
76 public String getServiceName() {
77 return personAuthorityServiceName;
80 public String getItemServiceName() {
81 return personServiceName;
85 public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
86 RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
92 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
93 DocumentHandler docHandler = PersonAuthorityHandlerFactory.getInstance().getHandler(
94 ctx.getRepositoryClientType().toString());
95 docHandler.setServiceContext(ctx);
96 if (ctx.getInput() != null) {
97 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), PersonauthoritiesCommon.class);
99 docHandler.setCommonPart((PersonauthoritiesCommon) obj);
105 private DocumentHandler createItemDocumentHandler(
107 String inAuthority) throws Exception {
108 DocumentHandler docHandler = PersonHandlerFactory.getInstance().getHandler(
109 ctx.getRepositoryClientType().toString());
110 docHandler.setServiceContext(ctx);
111 ((PersonDocumentModelHandler) docHandler).setInAuthority(inAuthority);
112 if (ctx.getInput() != null) {
113 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
114 PersonsCommon.class);
116 docHandler.setCommonPart((PersonsCommon) obj);
123 public Response createPersonAuthority(MultipartInput input) {
125 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
126 DocumentHandler handler = createDocumentHandler(ctx);
127 String csid = getRepositoryClient(ctx).create(ctx, handler);
128 //personAuthorityObject.setCsid(csid);
129 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
130 path.path("" + csid);
131 Response response = Response.created(path.build()).build();
133 } catch (UnauthorizedException ue) {
134 Response response = Response.status(
135 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
136 throw new WebApplicationException(response);
137 } catch (Exception e) {
138 if (logger.isDebugEnabled()) {
139 logger.debug("Caught exception in createPersonAuthority", e);
141 Response response = Response.status(
142 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
143 throw new WebApplicationException(response);
149 public MultipartOutput getPersonAuthority(@PathParam("csid") String csid) {
150 String idValue = null;
152 logger.error("getPersonAuthority: missing csid!");
153 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
154 "get failed on PersonAuthority csid=" + csid).type(
155 "text/plain").build();
156 throw new WebApplicationException(response);
158 if (logger.isDebugEnabled()) {
159 logger.debug("getPersonAuthority with path(id)=" + csid);
161 MultipartOutput result = null;
163 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
164 DocumentHandler handler = createDocumentHandler(ctx);
165 getRepositoryClient(ctx).get(ctx, csid, handler);
166 result = (MultipartOutput) ctx.getOutput();
167 } catch (UnauthorizedException ue) {
168 Response response = Response.status(
169 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
170 throw new WebApplicationException(response);
171 } catch (DocumentNotFoundException dnfe) {
172 if (logger.isDebugEnabled()) {
173 logger.debug("getPersonAuthority", dnfe);
175 Response response = Response.status(Response.Status.NOT_FOUND).entity(
176 "Get failed on PersonAuthority csid=" + csid).type(
177 "text/plain").build();
178 throw new WebApplicationException(response);
179 } catch (Exception e) {
180 if (logger.isDebugEnabled()) {
181 logger.debug("getPersonAuthority", e);
183 Response response = Response.status(
184 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
185 throw new WebApplicationException(response);
187 if (result == null) {
188 Response response = Response.status(Response.Status.NOT_FOUND).entity(
189 "Get failed, the requested PersonAuthority CSID:" + csid + ": was not found.").type(
190 "text/plain").build();
191 throw new WebApplicationException(response);
197 @Produces("application/xml")
198 public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
199 PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
201 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
202 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
203 DocumentHandler handler = createDocumentHandler(ctx);
204 DocumentFilter myFilter =
205 DocumentFilter.CreatePaginatedDocumentFilter(queryParams);
206 String nameQ = queryParams.getFirst("refName");
208 myFilter.setWhereClause("personauthorities_common:refName='" + nameQ + "'");
210 handler.setDocumentFilter(myFilter);
211 getRepositoryClient(ctx).getFiltered(ctx, handler);
212 personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
213 } catch (UnauthorizedException ue) {
214 Response response = Response.status(
215 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
216 throw new WebApplicationException(response);
217 } catch (Exception e) {
218 if (logger.isDebugEnabled()) {
219 logger.debug("Caught exception in getPersonAuthorityList", e);
221 Response response = Response.status(
222 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
223 throw new WebApplicationException(response);
225 return personAuthorityObjectList;
230 public MultipartOutput updatePersonAuthority(
231 @PathParam("csid") String csid,
232 MultipartInput theUpdate) {
233 if (logger.isDebugEnabled()) {
234 logger.debug("updatePersonAuthority with csid=" + csid);
236 if (csid == null || "".equals(csid)) {
237 logger.error("updatePersonAuthority: missing csid!");
238 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
239 "update failed on PersonAuthority csid=" + csid).type(
240 "text/plain").build();
241 throw new WebApplicationException(response);
243 MultipartOutput result = null;
245 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
246 DocumentHandler handler = createDocumentHandler(ctx);
247 getRepositoryClient(ctx).update(ctx, csid, handler);
248 result = (MultipartOutput) ctx.getOutput();
249 } catch (UnauthorizedException ue) {
250 Response response = Response.status(
251 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
252 throw new WebApplicationException(response);
253 } catch (DocumentNotFoundException dnfe) {
254 if (logger.isDebugEnabled()) {
255 logger.debug("caugth exception in updatePersonAuthority", dnfe);
257 Response response = Response.status(Response.Status.NOT_FOUND).entity(
258 "Update failed on PersonAuthority csid=" + csid).type(
259 "text/plain").build();
260 throw new WebApplicationException(response);
261 } catch (Exception e) {
262 Response response = Response.status(
263 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
264 throw new WebApplicationException(response);
271 public Response deletePersonAuthority(@PathParam("csid") String csid) {
273 if (logger.isDebugEnabled()) {
274 logger.debug("deletePersonAuthority with csid=" + csid);
276 if (csid == null || "".equals(csid)) {
277 logger.error("deletePersonAuthority: missing csid!");
278 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
279 "delete failed on PersonAuthority csid=" + csid).type(
280 "text/plain").build();
281 throw new WebApplicationException(response);
284 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
285 getRepositoryClient(ctx).delete(ctx, csid);
286 return Response.status(HttpResponseCodes.SC_OK).build();
287 } catch (UnauthorizedException ue) {
288 Response response = Response.status(
289 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
290 throw new WebApplicationException(response);
291 } catch (DocumentNotFoundException dnfe) {
292 if (logger.isDebugEnabled()) {
293 logger.debug("caught exception in deletePersonAuthority", dnfe);
295 Response response = Response.status(Response.Status.NOT_FOUND).entity(
296 "Delete failed on PersonAuthority csid=" + csid).type(
297 "text/plain").build();
298 throw new WebApplicationException(response);
299 } catch (Exception e) {
300 Response response = Response.status(
301 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
302 throw new WebApplicationException(response);
307 /*************************************************************************
308 * Person parts - this is a sub-resource of PersonAuthority
309 *************************************************************************/
311 @Path("{csid}/items")
312 public Response createPerson(@PathParam("csid") String parentcsid, MultipartInput input) {
314 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName());
315 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
316 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
317 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
318 path.path(parentcsid + "/items/" + itemcsid);
319 Response response = Response.created(path.build()).build();
321 } catch (UnauthorizedException ue) {
322 Response response = Response.status(
323 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
324 throw new WebApplicationException(response);
325 } catch (Exception e) {
326 if (logger.isDebugEnabled()) {
327 logger.debug("Caught exception in createPerson", e);
329 Response response = Response.status(
330 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
331 throw new WebApplicationException(response);
336 @Path("{csid}/items/{itemcsid}")
337 public MultipartOutput getPerson(
338 @PathParam("csid") String parentcsid,
339 @PathParam("itemcsid") String itemcsid) {
340 if (logger.isDebugEnabled()) {
341 logger.debug("getPerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
343 if (parentcsid == null || "".equals(parentcsid)) {
344 logger.error("getPerson: missing csid!");
345 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
346 "get failed on Person csid=" + parentcsid).type(
347 "text/plain").build();
348 throw new WebApplicationException(response);
350 if (itemcsid == null || "".equals(itemcsid)) {
351 logger.error("getPerson: missing itemcsid!");
352 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
353 "get failed on Person itemcsid=" + itemcsid).type(
354 "text/plain").build();
355 throw new WebApplicationException(response);
357 MultipartOutput result = null;
359 // Note that we have to create the service context for the Items, not the main service
360 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
361 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
362 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
363 // TODO should we assert that the item is in the passed personAuthority?
364 result = (MultipartOutput) ctx.getOutput();
365 } catch (UnauthorizedException ue) {
366 Response response = Response.status(
367 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
368 throw new WebApplicationException(response);
369 } catch (DocumentNotFoundException dnfe) {
370 if (logger.isDebugEnabled()) {
371 logger.debug("getPerson", dnfe);
373 Response response = Response.status(Response.Status.NOT_FOUND).entity(
374 "Get failed on Person csid=" + itemcsid).type(
375 "text/plain").build();
376 throw new WebApplicationException(response);
377 } catch (Exception e) {
378 if (logger.isDebugEnabled()) {
379 logger.debug("getPerson", e);
381 Response response = Response.status(
382 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
383 throw new WebApplicationException(response);
385 if (result == null) {
386 Response response = Response.status(Response.Status.NOT_FOUND).entity(
387 "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
388 "text/plain").build();
389 throw new WebApplicationException(response);
395 @Path("{csid}/items")
396 @Produces("application/xml")
397 public PersonsCommonList getPersonList(
398 @PathParam("csid") String parentcsid,
399 @Context UriInfo ui) {
400 PersonsCommonList personObjectList = new PersonsCommonList();
402 // Note that docType defaults to the ServiceName, so we're fine with that.
403 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
404 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
405 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
406 DocumentFilter myFilter =
407 DocumentFilter.CreatePaginatedDocumentFilter(queryParams);
408 myFilter.setWhereClause(
409 "persons_common:inAuthority='" + parentcsid + "'");
410 handler.setDocumentFilter(myFilter);
411 getRepositoryClient(ctx).getFiltered(ctx, handler);
412 personObjectList = (PersonsCommonList) handler.getCommonPartList();
413 } catch (UnauthorizedException ue) {
414 Response response = Response.status(
415 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
416 throw new WebApplicationException(response);
417 } catch (Exception e) {
418 if (logger.isDebugEnabled()) {
419 logger.debug("Caught exception in getPersonList", e);
421 Response response = Response.status(
422 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
423 throw new WebApplicationException(response);
425 return personObjectList;
429 @Path("{csid}/items/{itemcsid}")
430 public MultipartOutput updatePerson(
431 @PathParam("csid") String parentcsid,
432 @PathParam("itemcsid") String itemcsid,
433 MultipartInput theUpdate) {
434 if (logger.isDebugEnabled()) {
435 logger.debug("updatePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
437 if (parentcsid == null || "".equals(parentcsid)) {
438 logger.error("updatePerson: missing csid!");
439 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
440 "update failed on Person parentcsid=" + parentcsid).type(
441 "text/plain").build();
442 throw new WebApplicationException(response);
444 if (itemcsid == null || "".equals(itemcsid)) {
445 logger.error("updatePerson: missing itemcsid!");
446 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
447 "update failed on Person=" + itemcsid).type(
448 "text/plain").build();
449 throw new WebApplicationException(response);
451 MultipartOutput result = null;
453 // Note that we have to create the service context for the Items, not the main service
454 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName());
455 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
456 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
457 result = (MultipartOutput) ctx.getOutput();
458 } catch (UnauthorizedException ue) {
459 Response response = Response.status(
460 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
461 throw new WebApplicationException(response);
462 } catch (DocumentNotFoundException dnfe) {
463 if (logger.isDebugEnabled()) {
464 logger.debug("caugth exception in updatePerson", dnfe);
466 Response response = Response.status(Response.Status.NOT_FOUND).entity(
467 "Update failed on Person csid=" + itemcsid).type(
468 "text/plain").build();
469 throw new WebApplicationException(response);
470 } catch (Exception e) {
471 Response response = Response.status(
472 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
473 throw new WebApplicationException(response);
479 @Path("{csid}/items/{itemcsid}")
480 public Response deletePerson(
481 @PathParam("csid") String parentcsid,
482 @PathParam("itemcsid") String itemcsid) {
483 if (logger.isDebugEnabled()) {
484 logger.debug("deletePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
486 if (parentcsid == null || "".equals(parentcsid)) {
487 logger.error("deletePerson: missing csid!");
488 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
489 "delete failed on Person parentcsid=" + parentcsid).type(
490 "text/plain").build();
491 throw new WebApplicationException(response);
493 if (itemcsid == null || "".equals(itemcsid)) {
494 logger.error("deletePerson: missing itemcsid!");
495 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
496 "delete failed on Person=" + itemcsid).type(
497 "text/plain").build();
498 throw new WebApplicationException(response);
501 // Note that we have to create the service context for the Items, not the main service
502 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
503 getRepositoryClient(ctx).delete(ctx, itemcsid);
504 return Response.status(HttpResponseCodes.SC_OK).build();
505 } catch (UnauthorizedException ue) {
506 Response response = Response.status(
507 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
508 throw new WebApplicationException(response);
509 } catch (DocumentNotFoundException dnfe) {
510 if (logger.isDebugEnabled()) {
511 logger.debug("caught exception in deletePerson", dnfe);
513 Response response = Response.status(Response.Status.NOT_FOUND).entity(
514 "Delete failed on Person itemcsid=" + itemcsid).type(
515 "text/plain").build();
516 throw new WebApplicationException(response);
517 } catch (Exception e) {
518 Response response = Response.status(
519 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
520 throw new WebApplicationException(response);