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.QueryParam;
35 import javax.ws.rs.WebApplicationException;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.core.MultivaluedMap;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.UriBuilder;
40 import javax.ws.rs.core.UriInfo;
42 import org.collectionspace.services.PersonJAXBSchema;
43 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
44 import org.collectionspace.services.common.ClientType;
45 import org.collectionspace.services.common.ServiceMain;
46 import org.collectionspace.services.common.context.MultipartServiceContext;
47 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
48 import org.collectionspace.services.common.context.ServiceContext;
49 import org.collectionspace.services.common.document.DocumentFilter;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.collectionspace.services.common.document.DocumentHandlerFactory;
52 import org.collectionspace.services.common.document.DocumentNotFoundException;
53 import org.collectionspace.services.common.security.UnauthorizedException;
54 import org.collectionspace.services.common.query.IQueryManager;
55 import org.collectionspace.services.person.nuxeo.PersonDocumentModelHandler;
56 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
57 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
58 import org.jboss.resteasy.util.HttpResponseCodes;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
62 @Path("/personauthorities")
63 @Consumes("multipart/mixed")
64 @Produces("multipart/mixed")
65 public class PersonAuthorityResource extends AbstractCollectionSpaceResource {
67 private final static String personAuthorityServiceName = "personauthorities";
68 private final static String personServiceName = "persons";
69 final Logger logger = LoggerFactory.getLogger(PersonAuthorityResource.class);
70 //FIXME retrieve client type from configuration
71 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
73 public PersonAuthorityResource() {
78 protected String getVersionString() {
79 /** The last change revision. */
80 final String lastChangeRevision = "$LastChangedRevision$";
81 return lastChangeRevision;
85 public String getServiceName() {
86 return personAuthorityServiceName;
89 public String getItemServiceName() {
90 return personServiceName;
94 public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
95 RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
101 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
102 DocumentHandler docHandler = DocumentHandlerFactory.getInstance().getHandler(
103 ctx.getDocumentHandlerClass());
104 docHandler.setServiceContext(ctx);
105 if (ctx.getInput() != null) {
106 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), PersonauthoritiesCommon.class);
108 docHandler.setCommonPart((PersonauthoritiesCommon) obj);
114 private DocumentHandler createItemDocumentHandler(
116 String inAuthority) throws Exception {
117 DocumentHandler docHandler = DocumentHandlerFactory.getInstance().getHandler(
118 ctx.getDocumentHandlerClass());
119 docHandler.setServiceContext(ctx);
120 ((PersonDocumentModelHandler) docHandler).setInAuthority(inAuthority);
121 if (ctx.getInput() != null) {
122 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
123 PersonsCommon.class);
125 docHandler.setCommonPart((PersonsCommon) obj);
132 public Response createPersonAuthority(MultipartInput input) {
134 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
135 DocumentHandler handler = createDocumentHandler(ctx);
136 String csid = getRepositoryClient(ctx).create(ctx, handler);
137 //personAuthorityObject.setCsid(csid);
138 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
139 path.path("" + csid);
140 Response response = Response.created(path.build()).build();
142 } catch (UnauthorizedException ue) {
143 Response response = Response.status(
144 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
145 throw new WebApplicationException(response);
146 } catch (Exception e) {
147 if (logger.isDebugEnabled()) {
148 logger.debug("Caught exception in createPersonAuthority", e);
150 Response response = Response.status(
151 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
152 throw new WebApplicationException(response);
158 public MultipartOutput getPersonAuthority(@PathParam("csid") String csid) {
159 String idValue = null;
161 logger.error("getPersonAuthority: missing csid!");
162 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
163 "get failed on PersonAuthority csid=" + csid).type(
164 "text/plain").build();
165 throw new WebApplicationException(response);
167 if (logger.isDebugEnabled()) {
168 logger.debug("getPersonAuthority with path(id)=" + csid);
170 MultipartOutput result = null;
172 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
173 DocumentHandler handler = createDocumentHandler(ctx);
174 getRepositoryClient(ctx).get(ctx, csid, handler);
175 result = (MultipartOutput) ctx.getOutput();
176 } catch (UnauthorizedException ue) {
177 Response response = Response.status(
178 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
179 throw new WebApplicationException(response);
180 } catch (DocumentNotFoundException dnfe) {
181 if (logger.isDebugEnabled()) {
182 logger.debug("getPersonAuthority", dnfe);
184 Response response = Response.status(Response.Status.NOT_FOUND).entity(
185 "Get failed on PersonAuthority csid=" + csid).type(
186 "text/plain").build();
187 throw new WebApplicationException(response);
188 } catch (Exception e) {
189 if (logger.isDebugEnabled()) {
190 logger.debug("getPersonAuthority", e);
192 Response response = Response.status(
193 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
194 throw new WebApplicationException(response);
196 if (result == null) {
197 Response response = Response.status(Response.Status.NOT_FOUND).entity(
198 "Get failed, the requested PersonAuthority CSID:" + csid + ": was not found.").type(
199 "text/plain").build();
200 throw new WebApplicationException(response);
206 @Produces("application/xml")
207 public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
208 PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
210 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
211 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
212 DocumentHandler handler = createDocumentHandler(ctx);
213 DocumentFilter myFilter = new DocumentFilter();
214 myFilter.setPagination(queryParams);
215 String nameQ = queryParams.getFirst("refName");
217 myFilter.setWhereClause("personauthorities_common:refName='" + nameQ + "'");
219 handler.setDocumentFilter(myFilter);
220 getRepositoryClient(ctx).getFiltered(ctx, handler);
221 personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
222 } catch (UnauthorizedException ue) {
223 Response response = Response.status(
224 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
225 throw new WebApplicationException(response);
226 } catch (Exception e) {
227 if (logger.isDebugEnabled()) {
228 logger.debug("Caught exception in getPersonAuthorityList", e);
230 Response response = Response.status(
231 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
232 throw new WebApplicationException(response);
234 return personAuthorityObjectList;
239 public MultipartOutput updatePersonAuthority(
240 @PathParam("csid") String csid,
241 MultipartInput theUpdate) {
242 if (logger.isDebugEnabled()) {
243 logger.debug("updatePersonAuthority with csid=" + csid);
245 if (csid == null || "".equals(csid)) {
246 logger.error("updatePersonAuthority: missing csid!");
247 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
248 "update failed on PersonAuthority csid=" + csid).type(
249 "text/plain").build();
250 throw new WebApplicationException(response);
252 MultipartOutput result = null;
254 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
255 DocumentHandler handler = createDocumentHandler(ctx);
256 getRepositoryClient(ctx).update(ctx, csid, handler);
257 result = (MultipartOutput) ctx.getOutput();
258 } catch (UnauthorizedException ue) {
259 Response response = Response.status(
260 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
261 throw new WebApplicationException(response);
262 } catch (DocumentNotFoundException dnfe) {
263 if (logger.isDebugEnabled()) {
264 logger.debug("caugth exception in updatePersonAuthority", dnfe);
266 Response response = Response.status(Response.Status.NOT_FOUND).entity(
267 "Update failed on PersonAuthority csid=" + csid).type(
268 "text/plain").build();
269 throw new WebApplicationException(response);
270 } catch (Exception e) {
271 Response response = Response.status(
272 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
273 throw new WebApplicationException(response);
280 public Response deletePersonAuthority(@PathParam("csid") String csid) {
282 if (logger.isDebugEnabled()) {
283 logger.debug("deletePersonAuthority with csid=" + csid);
285 if (csid == null || "".equals(csid)) {
286 logger.error("deletePersonAuthority: missing csid!");
287 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
288 "delete failed on PersonAuthority csid=" + csid).type(
289 "text/plain").build();
290 throw new WebApplicationException(response);
293 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
294 getRepositoryClient(ctx).delete(ctx, csid);
295 return Response.status(HttpResponseCodes.SC_OK).build();
296 } catch (UnauthorizedException ue) {
297 Response response = Response.status(
298 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
299 throw new WebApplicationException(response);
300 } catch (DocumentNotFoundException dnfe) {
301 if (logger.isDebugEnabled()) {
302 logger.debug("caught exception in deletePersonAuthority", dnfe);
304 Response response = Response.status(Response.Status.NOT_FOUND).entity(
305 "Delete failed on PersonAuthority csid=" + csid).type(
306 "text/plain").build();
307 throw new WebApplicationException(response);
308 } catch (Exception e) {
309 Response response = Response.status(
310 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
311 throw new WebApplicationException(response);
316 /*************************************************************************
317 * Person parts - this is a sub-resource of PersonAuthority
318 *************************************************************************/
320 @Path("{csid}/items")
321 public Response createPerson(@PathParam("csid") String parentcsid, MultipartInput input) {
323 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName());
324 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
325 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
326 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
327 path.path(parentcsid + "/items/" + itemcsid);
328 Response response = Response.created(path.build()).build();
330 } catch (UnauthorizedException ue) {
331 Response response = Response.status(
332 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
333 throw new WebApplicationException(response);
334 } catch (Exception e) {
335 if (logger.isDebugEnabled()) {
336 logger.debug("Caught exception in createPerson", e);
338 Response response = Response.status(
339 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
340 throw new WebApplicationException(response);
345 @Path("{csid}/items/{itemcsid}")
346 public MultipartOutput getPerson(
347 @PathParam("csid") String parentcsid,
348 @PathParam("itemcsid") String itemcsid) {
349 if (logger.isDebugEnabled()) {
350 logger.debug("getPerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
352 if (parentcsid == null || "".equals(parentcsid)) {
353 logger.error("getPerson: missing csid!");
354 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
355 "get failed on Person csid=" + parentcsid).type(
356 "text/plain").build();
357 throw new WebApplicationException(response);
359 if (itemcsid == null || "".equals(itemcsid)) {
360 logger.error("getPerson: missing itemcsid!");
361 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
362 "get failed on Person itemcsid=" + itemcsid).type(
363 "text/plain").build();
364 throw new WebApplicationException(response);
366 MultipartOutput result = null;
368 // Note that we have to create the service context for the Items, not the main service
369 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
370 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
371 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
372 // TODO should we assert that the item is in the passed personAuthority?
373 result = (MultipartOutput) ctx.getOutput();
374 } catch (UnauthorizedException ue) {
375 Response response = Response.status(
376 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
377 throw new WebApplicationException(response);
378 } catch (DocumentNotFoundException dnfe) {
379 if (logger.isDebugEnabled()) {
380 logger.debug("getPerson", dnfe);
382 Response response = Response.status(Response.Status.NOT_FOUND).entity(
383 "Get failed on Person csid=" + itemcsid).type(
384 "text/plain").build();
385 throw new WebApplicationException(response);
386 } catch (Exception e) {
387 if (logger.isDebugEnabled()) {
388 logger.debug("getPerson", e);
390 Response response = Response.status(
391 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
392 throw new WebApplicationException(response);
394 if (result == null) {
395 Response response = Response.status(Response.Status.NOT_FOUND).entity(
396 "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
397 "text/plain").build();
398 throw new WebApplicationException(response);
404 @Path("{csid}/items")
405 @Produces("application/xml")
406 public PersonsCommonList getPersonList(
407 @PathParam("csid") String parentcsid,
408 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
409 @Context UriInfo ui) {
410 PersonsCommonList personObjectList = new PersonsCommonList();
412 // Note that docType defaults to the ServiceName, so we're fine with that.
413 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
414 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
415 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
416 DocumentFilter myFilter = new DocumentFilter();
417 myFilter.setPagination(queryParams);
419 // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
420 myFilter.setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
421 PersonJAXBSchema.IN_AUTHORITY +
422 "='" + parentcsid + "'" + "AND ecm:isProxy = 0");
424 // AND persons_common:displayName LIKE '%partialTerm%'
425 if (partialTerm != null && !partialTerm.isEmpty()) {
426 String ptClause = "AND " +
427 PersonJAXBSchema.PERSONS_COMMON + ":" +
428 PersonJAXBSchema.DISPLAY_NAME +
430 "'%" + partialTerm + "%'";
431 myFilter.appendWhereClause(ptClause);
434 handler.setDocumentFilter(myFilter);
435 getRepositoryClient(ctx).getFiltered(ctx, handler);
436 personObjectList = (PersonsCommonList) handler.getCommonPartList();
437 } catch (UnauthorizedException ue) {
438 Response response = Response.status(
439 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
440 throw new WebApplicationException(response);
441 } catch (Exception e) {
442 if (logger.isDebugEnabled()) {
443 logger.debug("Caught exception in getPersonList", e);
445 Response response = Response.status(
446 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
447 throw new WebApplicationException(response);
449 return personObjectList;
453 @Path("{csid}/items/{itemcsid}")
454 public MultipartOutput updatePerson(
455 @PathParam("csid") String parentcsid,
456 @PathParam("itemcsid") String itemcsid,
457 MultipartInput theUpdate) {
458 if (logger.isDebugEnabled()) {
459 logger.debug("updatePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
461 if (parentcsid == null || "".equals(parentcsid)) {
462 logger.error("updatePerson: missing csid!");
463 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
464 "update failed on Person parentcsid=" + parentcsid).type(
465 "text/plain").build();
466 throw new WebApplicationException(response);
468 if (itemcsid == null || "".equals(itemcsid)) {
469 logger.error("updatePerson: missing itemcsid!");
470 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
471 "update failed on Person=" + itemcsid).type(
472 "text/plain").build();
473 throw new WebApplicationException(response);
475 MultipartOutput result = null;
477 // Note that we have to create the service context for the Items, not the main service
478 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName());
479 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
480 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
481 result = (MultipartOutput) ctx.getOutput();
482 } catch (UnauthorizedException ue) {
483 Response response = Response.status(
484 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
485 throw new WebApplicationException(response);
486 } catch (DocumentNotFoundException dnfe) {
487 if (logger.isDebugEnabled()) {
488 logger.debug("caugth exception in updatePerson", dnfe);
490 Response response = Response.status(Response.Status.NOT_FOUND).entity(
491 "Update failed on Person csid=" + itemcsid).type(
492 "text/plain").build();
493 throw new WebApplicationException(response);
494 } catch (Exception e) {
495 Response response = Response.status(
496 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
497 throw new WebApplicationException(response);
503 @Path("{csid}/items/{itemcsid}")
504 public Response deletePerson(
505 @PathParam("csid") String parentcsid,
506 @PathParam("itemcsid") String itemcsid) {
507 if (logger.isDebugEnabled()) {
508 logger.debug("deletePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
510 if (parentcsid == null || "".equals(parentcsid)) {
511 logger.error("deletePerson: missing csid!");
512 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
513 "delete failed on Person parentcsid=" + parentcsid).type(
514 "text/plain").build();
515 throw new WebApplicationException(response);
517 if (itemcsid == null || "".equals(itemcsid)) {
518 logger.error("deletePerson: missing itemcsid!");
519 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
520 "delete failed on Person=" + itemcsid).type(
521 "text/plain").build();
522 throw new WebApplicationException(response);
525 // Note that we have to create the service context for the Items, not the main service
526 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
527 getRepositoryClient(ctx).delete(ctx, itemcsid);
528 return Response.status(HttpResponseCodes.SC_OK).build();
529 } catch (UnauthorizedException ue) {
530 Response response = Response.status(
531 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
532 throw new WebApplicationException(response);
533 } catch (DocumentNotFoundException dnfe) {
534 if (logger.isDebugEnabled()) {
535 logger.debug("caught exception in deletePerson", dnfe);
537 Response response = Response.status(Response.Status.NOT_FOUND).entity(
538 "Delete failed on Person itemcsid=" + itemcsid).type(
539 "text/plain").build();
540 throw new WebApplicationException(response);
541 } catch (Exception e) {
542 Response response = Response.status(
543 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
544 throw new WebApplicationException(response);