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.DocumentNotFoundException;
52 import org.collectionspace.services.common.security.UnauthorizedException;
53 import org.collectionspace.services.common.query.IQueryManager;
54 import org.collectionspace.services.person.nuxeo.PersonAuthorityHandlerFactory;
55 import org.collectionspace.services.person.nuxeo.PersonDocumentModelHandler;
56 import org.collectionspace.services.person.nuxeo.PersonHandlerFactory;
57 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
58 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
59 import org.jboss.resteasy.util.HttpResponseCodes;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 @Path("/personauthorities")
64 @Consumes("multipart/mixed")
65 @Produces("multipart/mixed")
66 public class PersonAuthorityResource extends AbstractCollectionSpaceResource {
68 private final static String personAuthorityServiceName = "personauthorities";
69 private final static String personServiceName = "persons";
70 final Logger logger = LoggerFactory.getLogger(PersonAuthorityResource.class);
71 //FIXME retrieve client type from configuration
72 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
74 public PersonAuthorityResource() {
79 protected String getVersionString() {
80 /** The last change revision. */
81 final String lastChangeRevision = "$LastChangedRevision$";
82 return lastChangeRevision;
86 public String getServiceName() {
87 return personAuthorityServiceName;
90 public String getItemServiceName() {
91 return personServiceName;
95 public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
96 RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
102 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
103 DocumentHandler docHandler = PersonAuthorityHandlerFactory.getInstance().getHandler(
104 ctx.getRepositoryClientType().toString());
105 docHandler.setServiceContext(ctx);
106 if (ctx.getInput() != null) {
107 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), PersonauthoritiesCommon.class);
109 docHandler.setCommonPart((PersonauthoritiesCommon) obj);
115 private DocumentHandler createItemDocumentHandler(
117 String inAuthority) throws Exception {
118 DocumentHandler docHandler = PersonHandlerFactory.getInstance().getHandler(
119 ctx.getRepositoryClientType().toString());
120 docHandler.setServiceContext(ctx);
121 ((PersonDocumentModelHandler) docHandler).setInAuthority(inAuthority);
122 if (ctx.getInput() != null) {
123 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
124 PersonsCommon.class);
126 docHandler.setCommonPart((PersonsCommon) obj);
133 public Response createPersonAuthority(MultipartInput input) {
135 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
136 DocumentHandler handler = createDocumentHandler(ctx);
137 String csid = getRepositoryClient(ctx).create(ctx, handler);
138 //personAuthorityObject.setCsid(csid);
139 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
140 path.path("" + csid);
141 Response response = Response.created(path.build()).build();
143 } catch (UnauthorizedException ue) {
144 Response response = Response.status(
145 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
146 throw new WebApplicationException(response);
147 } catch (Exception e) {
148 if (logger.isDebugEnabled()) {
149 logger.debug("Caught exception in createPersonAuthority", e);
151 Response response = Response.status(
152 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
153 throw new WebApplicationException(response);
159 public MultipartOutput getPersonAuthority(@PathParam("csid") String csid) {
160 String idValue = null;
162 logger.error("getPersonAuthority: missing csid!");
163 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
164 "get failed on PersonAuthority csid=" + csid).type(
165 "text/plain").build();
166 throw new WebApplicationException(response);
168 if (logger.isDebugEnabled()) {
169 logger.debug("getPersonAuthority with path(id)=" + csid);
171 MultipartOutput result = null;
173 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
174 DocumentHandler handler = createDocumentHandler(ctx);
175 getRepositoryClient(ctx).get(ctx, csid, handler);
176 result = (MultipartOutput) ctx.getOutput();
177 } catch (UnauthorizedException ue) {
178 Response response = Response.status(
179 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
180 throw new WebApplicationException(response);
181 } catch (DocumentNotFoundException dnfe) {
182 if (logger.isDebugEnabled()) {
183 logger.debug("getPersonAuthority", dnfe);
185 Response response = Response.status(Response.Status.NOT_FOUND).entity(
186 "Get failed on PersonAuthority csid=" + csid).type(
187 "text/plain").build();
188 throw new WebApplicationException(response);
189 } catch (Exception e) {
190 if (logger.isDebugEnabled()) {
191 logger.debug("getPersonAuthority", e);
193 Response response = Response.status(
194 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
195 throw new WebApplicationException(response);
197 if (result == null) {
198 Response response = Response.status(Response.Status.NOT_FOUND).entity(
199 "Get failed, the requested PersonAuthority CSID:" + csid + ": was not found.").type(
200 "text/plain").build();
201 throw new WebApplicationException(response);
207 @Produces("application/xml")
208 public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
209 PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
211 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
212 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
213 DocumentHandler handler = createDocumentHandler(ctx);
214 DocumentFilter myFilter = new DocumentFilter();
215 myFilter.setPagination(queryParams);
216 String nameQ = queryParams.getFirst("refName");
218 myFilter.setWhereClause("personauthorities_common:refName='" + nameQ + "'");
220 handler.setDocumentFilter(myFilter);
221 getRepositoryClient(ctx).getFiltered(ctx, handler);
222 personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
223 } catch (UnauthorizedException ue) {
224 Response response = Response.status(
225 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
226 throw new WebApplicationException(response);
227 } catch (Exception e) {
228 if (logger.isDebugEnabled()) {
229 logger.debug("Caught exception in getPersonAuthorityList", e);
231 Response response = Response.status(
232 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
233 throw new WebApplicationException(response);
235 return personAuthorityObjectList;
240 public MultipartOutput updatePersonAuthority(
241 @PathParam("csid") String csid,
242 MultipartInput theUpdate) {
243 if (logger.isDebugEnabled()) {
244 logger.debug("updatePersonAuthority with csid=" + csid);
246 if (csid == null || "".equals(csid)) {
247 logger.error("updatePersonAuthority: missing csid!");
248 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
249 "update failed on PersonAuthority csid=" + csid).type(
250 "text/plain").build();
251 throw new WebApplicationException(response);
253 MultipartOutput result = null;
255 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
256 DocumentHandler handler = createDocumentHandler(ctx);
257 getRepositoryClient(ctx).update(ctx, csid, handler);
258 result = (MultipartOutput) ctx.getOutput();
259 } catch (UnauthorizedException ue) {
260 Response response = Response.status(
261 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
262 throw new WebApplicationException(response);
263 } catch (DocumentNotFoundException dnfe) {
264 if (logger.isDebugEnabled()) {
265 logger.debug("caugth exception in updatePersonAuthority", dnfe);
267 Response response = Response.status(Response.Status.NOT_FOUND).entity(
268 "Update failed on PersonAuthority csid=" + csid).type(
269 "text/plain").build();
270 throw new WebApplicationException(response);
271 } catch (Exception e) {
272 Response response = Response.status(
273 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
274 throw new WebApplicationException(response);
281 public Response deletePersonAuthority(@PathParam("csid") String csid) {
283 if (logger.isDebugEnabled()) {
284 logger.debug("deletePersonAuthority with csid=" + csid);
286 if (csid == null || "".equals(csid)) {
287 logger.error("deletePersonAuthority: missing csid!");
288 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
289 "delete failed on PersonAuthority csid=" + csid).type(
290 "text/plain").build();
291 throw new WebApplicationException(response);
294 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
295 getRepositoryClient(ctx).delete(ctx, csid);
296 return Response.status(HttpResponseCodes.SC_OK).build();
297 } catch (UnauthorizedException ue) {
298 Response response = Response.status(
299 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
300 throw new WebApplicationException(response);
301 } catch (DocumentNotFoundException dnfe) {
302 if (logger.isDebugEnabled()) {
303 logger.debug("caught exception in deletePersonAuthority", dnfe);
305 Response response = Response.status(Response.Status.NOT_FOUND).entity(
306 "Delete failed on PersonAuthority csid=" + csid).type(
307 "text/plain").build();
308 throw new WebApplicationException(response);
309 } catch (Exception e) {
310 Response response = Response.status(
311 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
312 throw new WebApplicationException(response);
317 /*************************************************************************
318 * Person parts - this is a sub-resource of PersonAuthority
319 *************************************************************************/
321 @Path("{csid}/items")
322 public Response createPerson(@PathParam("csid") String parentcsid, MultipartInput input) {
324 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName());
325 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
326 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
327 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
328 path.path(parentcsid + "/items/" + itemcsid);
329 Response response = Response.created(path.build()).build();
331 } catch (UnauthorizedException ue) {
332 Response response = Response.status(
333 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
334 throw new WebApplicationException(response);
335 } catch (Exception e) {
336 if (logger.isDebugEnabled()) {
337 logger.debug("Caught exception in createPerson", e);
339 Response response = Response.status(
340 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
341 throw new WebApplicationException(response);
346 @Path("{csid}/items/{itemcsid}")
347 public MultipartOutput getPerson(
348 @PathParam("csid") String parentcsid,
349 @PathParam("itemcsid") String itemcsid) {
350 if (logger.isDebugEnabled()) {
351 logger.debug("getPerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
353 if (parentcsid == null || "".equals(parentcsid)) {
354 logger.error("getPerson: missing csid!");
355 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
356 "get failed on Person csid=" + parentcsid).type(
357 "text/plain").build();
358 throw new WebApplicationException(response);
360 if (itemcsid == null || "".equals(itemcsid)) {
361 logger.error("getPerson: missing itemcsid!");
362 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
363 "get failed on Person itemcsid=" + itemcsid).type(
364 "text/plain").build();
365 throw new WebApplicationException(response);
367 MultipartOutput result = null;
369 // Note that we have to create the service context for the Items, not the main service
370 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
371 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
372 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
373 // TODO should we assert that the item is in the passed personAuthority?
374 result = (MultipartOutput) ctx.getOutput();
375 } catch (UnauthorizedException ue) {
376 Response response = Response.status(
377 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
378 throw new WebApplicationException(response);
379 } catch (DocumentNotFoundException dnfe) {
380 if (logger.isDebugEnabled()) {
381 logger.debug("getPerson", dnfe);
383 Response response = Response.status(Response.Status.NOT_FOUND).entity(
384 "Get failed on Person csid=" + itemcsid).type(
385 "text/plain").build();
386 throw new WebApplicationException(response);
387 } catch (Exception e) {
388 if (logger.isDebugEnabled()) {
389 logger.debug("getPerson", e);
391 Response response = Response.status(
392 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
393 throw new WebApplicationException(response);
395 if (result == null) {
396 Response response = Response.status(Response.Status.NOT_FOUND).entity(
397 "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
398 "text/plain").build();
399 throw new WebApplicationException(response);
405 @Path("{csid}/items")
406 @Produces("application/xml")
407 public PersonsCommonList getPersonList(
408 @PathParam("csid") String parentcsid,
409 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
410 @Context UriInfo ui) {
411 PersonsCommonList personObjectList = new PersonsCommonList();
413 // Note that docType defaults to the ServiceName, so we're fine with that.
414 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
415 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
416 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
417 DocumentFilter myFilter = new DocumentFilter();
418 myFilter.setPagination(queryParams);
420 // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
421 myFilter.setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
422 PersonJAXBSchema.IN_AUTHORITY +
423 "='" + parentcsid + "'" + "AND ecm:isProxy = 0");
425 // AND persons_common:displayName LIKE '%partialTerm%'
426 if (partialTerm != null && !partialTerm.isEmpty()) {
427 String ptClause = "AND " +
428 PersonJAXBSchema.PERSONS_COMMON + ":" +
429 PersonJAXBSchema.DISPLAY_NAME +
431 "'%" + partialTerm + "%'";
432 myFilter.appendWhereClause(ptClause);
435 handler.setDocumentFilter(myFilter);
436 getRepositoryClient(ctx).getFiltered(ctx, handler);
437 personObjectList = (PersonsCommonList) handler.getCommonPartList();
438 } catch (UnauthorizedException ue) {
439 Response response = Response.status(
440 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
441 throw new WebApplicationException(response);
442 } catch (Exception e) {
443 if (logger.isDebugEnabled()) {
444 logger.debug("Caught exception in getPersonList", e);
446 Response response = Response.status(
447 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
448 throw new WebApplicationException(response);
450 return personObjectList;
454 @Path("{csid}/items/{itemcsid}")
455 public MultipartOutput updatePerson(
456 @PathParam("csid") String parentcsid,
457 @PathParam("itemcsid") String itemcsid,
458 MultipartInput theUpdate) {
459 if (logger.isDebugEnabled()) {
460 logger.debug("updatePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
462 if (parentcsid == null || "".equals(parentcsid)) {
463 logger.error("updatePerson: missing csid!");
464 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
465 "update failed on Person parentcsid=" + parentcsid).type(
466 "text/plain").build();
467 throw new WebApplicationException(response);
469 if (itemcsid == null || "".equals(itemcsid)) {
470 logger.error("updatePerson: missing itemcsid!");
471 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
472 "update failed on Person=" + itemcsid).type(
473 "text/plain").build();
474 throw new WebApplicationException(response);
476 MultipartOutput result = null;
478 // Note that we have to create the service context for the Items, not the main service
479 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName());
480 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
481 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
482 result = (MultipartOutput) ctx.getOutput();
483 } catch (UnauthorizedException ue) {
484 Response response = Response.status(
485 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
486 throw new WebApplicationException(response);
487 } catch (DocumentNotFoundException dnfe) {
488 if (logger.isDebugEnabled()) {
489 logger.debug("caugth exception in updatePerson", dnfe);
491 Response response = Response.status(Response.Status.NOT_FOUND).entity(
492 "Update failed on Person csid=" + itemcsid).type(
493 "text/plain").build();
494 throw new WebApplicationException(response);
495 } catch (Exception e) {
496 Response response = Response.status(
497 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
498 throw new WebApplicationException(response);
504 @Path("{csid}/items/{itemcsid}")
505 public Response deletePerson(
506 @PathParam("csid") String parentcsid,
507 @PathParam("itemcsid") String itemcsid) {
508 if (logger.isDebugEnabled()) {
509 logger.debug("deletePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
511 if (parentcsid == null || "".equals(parentcsid)) {
512 logger.error("deletePerson: missing csid!");
513 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
514 "delete failed on Person parentcsid=" + parentcsid).type(
515 "text/plain").build();
516 throw new WebApplicationException(response);
518 if (itemcsid == null || "".equals(itemcsid)) {
519 logger.error("deletePerson: missing itemcsid!");
520 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
521 "delete failed on Person=" + itemcsid).type(
522 "text/plain").build();
523 throw new WebApplicationException(response);
526 // Note that we have to create the service context for the Items, not the main service
527 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
528 getRepositoryClient(ctx).delete(ctx, itemcsid);
529 return Response.status(HttpResponseCodes.SC_OK).build();
530 } catch (UnauthorizedException ue) {
531 Response response = Response.status(
532 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
533 throw new WebApplicationException(response);
534 } catch (DocumentNotFoundException dnfe) {
535 if (logger.isDebugEnabled()) {
536 logger.debug("caught exception in deletePerson", dnfe);
538 Response response = Response.status(Response.Status.NOT_FOUND).entity(
539 "Delete failed on Person itemcsid=" + itemcsid).type(
540 "text/plain").build();
541 throw new WebApplicationException(response);
542 } catch (Exception e) {
543 Response response = Response.status(
544 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
545 throw new WebApplicationException(response);