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.PersonDocumentModelHandler;
55 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
56 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
57 import org.jboss.resteasy.util.HttpResponseCodes;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 @Path("/personauthorities")
62 @Consumes("multipart/mixed")
63 @Produces("multipart/mixed")
64 public class PersonAuthorityResource extends AbstractCollectionSpaceResource {
66 private final static String personAuthorityServiceName = "personauthorities";
67 private final static String personServiceName = "persons";
68 final Logger logger = LoggerFactory.getLogger(PersonAuthorityResource.class);
69 //FIXME retrieve client type from configuration
70 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
72 public PersonAuthorityResource() {
77 protected String getVersionString() {
78 /** The last change revision. */
79 final String lastChangeRevision = "$LastChangedRevision$";
80 return lastChangeRevision;
84 public String getServiceName() {
85 return personAuthorityServiceName;
88 public String getItemServiceName() {
89 return personServiceName;
93 public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
94 RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
100 public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
101 DocumentHandler docHandler = ctx.getDocumentHandler();
102 if (ctx.getInput() != null) {
103 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), PersonauthoritiesCommon.class);
105 docHandler.setCommonPart((PersonauthoritiesCommon) obj);
111 private DocumentHandler createItemDocumentHandler(
113 String inAuthority) throws Exception {
114 DocumentHandler docHandler = ctx.getDocumentHandler();
115 ((PersonDocumentModelHandler) docHandler).setInAuthority(inAuthority);
116 if (ctx.getInput() != null) {
117 Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
118 PersonsCommon.class);
120 docHandler.setCommonPart((PersonsCommon) obj);
127 public Response createPersonAuthority(MultipartInput input) {
129 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
130 DocumentHandler handler = createDocumentHandler(ctx);
131 String csid = getRepositoryClient(ctx).create(ctx, handler);
132 //personAuthorityObject.setCsid(csid);
133 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
134 path.path("" + csid);
135 Response response = Response.created(path.build()).build();
137 } catch (UnauthorizedException ue) {
138 Response response = Response.status(
139 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
140 throw new WebApplicationException(response);
141 } catch (Exception e) {
142 if (logger.isDebugEnabled()) {
143 logger.debug("Caught exception in createPersonAuthority", e);
145 Response response = Response.status(
146 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
147 throw new WebApplicationException(response);
153 public MultipartOutput getPersonAuthority(@PathParam("csid") String csid) {
154 String idValue = null;
156 logger.error("getPersonAuthority: missing csid!");
157 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
158 "get failed on PersonAuthority csid=" + csid).type(
159 "text/plain").build();
160 throw new WebApplicationException(response);
162 if (logger.isDebugEnabled()) {
163 logger.debug("getPersonAuthority with path(id)=" + csid);
165 MultipartOutput result = null;
167 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
168 DocumentHandler handler = createDocumentHandler(ctx);
169 getRepositoryClient(ctx).get(ctx, csid, handler);
170 result = (MultipartOutput) ctx.getOutput();
171 } catch (UnauthorizedException ue) {
172 Response response = Response.status(
173 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
174 throw new WebApplicationException(response);
175 } catch (DocumentNotFoundException dnfe) {
176 if (logger.isDebugEnabled()) {
177 logger.debug("getPersonAuthority", dnfe);
179 Response response = Response.status(Response.Status.NOT_FOUND).entity(
180 "Get failed on PersonAuthority csid=" + csid).type(
181 "text/plain").build();
182 throw new WebApplicationException(response);
183 } catch (Exception e) {
184 if (logger.isDebugEnabled()) {
185 logger.debug("getPersonAuthority", e);
187 Response response = Response.status(
188 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
189 throw new WebApplicationException(response);
191 if (result == null) {
192 Response response = Response.status(Response.Status.NOT_FOUND).entity(
193 "Get failed, the requested PersonAuthority CSID:" + csid + ": was not found.").type(
194 "text/plain").build();
195 throw new WebApplicationException(response);
201 @Produces("application/xml")
202 public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
203 PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
205 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
206 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
207 DocumentHandler handler = createDocumentHandler(ctx);
208 DocumentFilter myFilter = new DocumentFilter();
209 myFilter.setPagination(queryParams);
210 String nameQ = queryParams.getFirst("refName");
212 myFilter.setWhereClause("personauthorities_common:refName='" + nameQ + "'");
214 handler.setDocumentFilter(myFilter);
215 getRepositoryClient(ctx).getFiltered(ctx, handler);
216 personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
217 } catch (UnauthorizedException ue) {
218 Response response = Response.status(
219 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
220 throw new WebApplicationException(response);
221 } catch (Exception e) {
222 if (logger.isDebugEnabled()) {
223 logger.debug("Caught exception in getPersonAuthorityList", e);
225 Response response = Response.status(
226 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
227 throw new WebApplicationException(response);
229 return personAuthorityObjectList;
234 public MultipartOutput updatePersonAuthority(
235 @PathParam("csid") String csid,
236 MultipartInput theUpdate) {
237 if (logger.isDebugEnabled()) {
238 logger.debug("updatePersonAuthority with csid=" + csid);
240 if (csid == null || "".equals(csid)) {
241 logger.error("updatePersonAuthority: missing csid!");
242 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
243 "update failed on PersonAuthority csid=" + csid).type(
244 "text/plain").build();
245 throw new WebApplicationException(response);
247 MultipartOutput result = null;
249 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
250 DocumentHandler handler = createDocumentHandler(ctx);
251 getRepositoryClient(ctx).update(ctx, csid, handler);
252 result = (MultipartOutput) ctx.getOutput();
253 } catch (UnauthorizedException ue) {
254 Response response = Response.status(
255 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
256 throw new WebApplicationException(response);
257 } catch (DocumentNotFoundException dnfe) {
258 if (logger.isDebugEnabled()) {
259 logger.debug("caugth exception in updatePersonAuthority", dnfe);
261 Response response = Response.status(Response.Status.NOT_FOUND).entity(
262 "Update failed on PersonAuthority csid=" + csid).type(
263 "text/plain").build();
264 throw new WebApplicationException(response);
265 } catch (Exception e) {
266 Response response = Response.status(
267 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
268 throw new WebApplicationException(response);
275 public Response deletePersonAuthority(@PathParam("csid") String csid) {
277 if (logger.isDebugEnabled()) {
278 logger.debug("deletePersonAuthority with csid=" + csid);
280 if (csid == null || "".equals(csid)) {
281 logger.error("deletePersonAuthority: missing csid!");
282 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
283 "delete failed on PersonAuthority csid=" + csid).type(
284 "text/plain").build();
285 throw new WebApplicationException(response);
288 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
289 getRepositoryClient(ctx).delete(ctx, csid);
290 return Response.status(HttpResponseCodes.SC_OK).build();
291 } catch (UnauthorizedException ue) {
292 Response response = Response.status(
293 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
294 throw new WebApplicationException(response);
295 } catch (DocumentNotFoundException dnfe) {
296 if (logger.isDebugEnabled()) {
297 logger.debug("caught exception in deletePersonAuthority", dnfe);
299 Response response = Response.status(Response.Status.NOT_FOUND).entity(
300 "Delete failed on PersonAuthority csid=" + csid).type(
301 "text/plain").build();
302 throw new WebApplicationException(response);
303 } catch (Exception e) {
304 Response response = Response.status(
305 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
306 throw new WebApplicationException(response);
311 /*************************************************************************
312 * Person parts - this is a sub-resource of PersonAuthority
313 *************************************************************************/
315 @Path("{csid}/items")
316 public Response createPerson(@PathParam("csid") String parentcsid, MultipartInput input) {
318 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName());
319 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
320 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
321 UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
322 path.path(parentcsid + "/items/" + itemcsid);
323 Response response = Response.created(path.build()).build();
325 } catch (UnauthorizedException ue) {
326 Response response = Response.status(
327 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
328 throw new WebApplicationException(response);
329 } catch (Exception e) {
330 if (logger.isDebugEnabled()) {
331 logger.debug("Caught exception in createPerson", e);
333 Response response = Response.status(
334 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
335 throw new WebApplicationException(response);
340 @Path("{csid}/items/{itemcsid}")
341 public MultipartOutput getPerson(
342 @PathParam("csid") String parentcsid,
343 @PathParam("itemcsid") String itemcsid) {
344 if (logger.isDebugEnabled()) {
345 logger.debug("getPerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
347 if (parentcsid == null || "".equals(parentcsid)) {
348 logger.error("getPerson: missing csid!");
349 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
350 "get failed on Person csid=" + parentcsid).type(
351 "text/plain").build();
352 throw new WebApplicationException(response);
354 if (itemcsid == null || "".equals(itemcsid)) {
355 logger.error("getPerson: missing itemcsid!");
356 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
357 "get failed on Person itemcsid=" + itemcsid).type(
358 "text/plain").build();
359 throw new WebApplicationException(response);
361 MultipartOutput result = null;
363 // Note that we have to create the service context for the Items, not the main service
364 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
365 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
366 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
367 // TODO should we assert that the item is in the passed personAuthority?
368 result = (MultipartOutput) ctx.getOutput();
369 } catch (UnauthorizedException ue) {
370 Response response = Response.status(
371 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
372 throw new WebApplicationException(response);
373 } catch (DocumentNotFoundException dnfe) {
374 if (logger.isDebugEnabled()) {
375 logger.debug("getPerson", dnfe);
377 Response response = Response.status(Response.Status.NOT_FOUND).entity(
378 "Get failed on Person csid=" + itemcsid).type(
379 "text/plain").build();
380 throw new WebApplicationException(response);
381 } catch (Exception e) {
382 if (logger.isDebugEnabled()) {
383 logger.debug("getPerson", e);
385 Response response = Response.status(
386 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
387 throw new WebApplicationException(response);
389 if (result == null) {
390 Response response = Response.status(Response.Status.NOT_FOUND).entity(
391 "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
392 "text/plain").build();
393 throw new WebApplicationException(response);
399 @Path("{csid}/items")
400 @Produces("application/xml")
401 public PersonsCommonList getPersonList(
402 @PathParam("csid") String parentcsid,
403 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
404 @Context UriInfo ui) {
405 PersonsCommonList personObjectList = new PersonsCommonList();
407 // Note that docType defaults to the ServiceName, so we're fine with that.
408 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
409 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
410 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
411 DocumentFilter myFilter = new DocumentFilter();
412 myFilter.setPagination(queryParams);
414 // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
415 myFilter.setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
416 PersonJAXBSchema.IN_AUTHORITY +
417 "='" + parentcsid + "'" + "AND ecm:isProxy = 0");
419 // AND persons_common:displayName LIKE '%partialTerm%'
420 if (partialTerm != null && !partialTerm.isEmpty()) {
421 String ptClause = "AND " +
422 PersonJAXBSchema.PERSONS_COMMON + ":" +
423 PersonJAXBSchema.DISPLAY_NAME +
425 "'%" + partialTerm + "%'";
426 myFilter.appendWhereClause(ptClause);
429 handler.setDocumentFilter(myFilter);
430 getRepositoryClient(ctx).getFiltered(ctx, handler);
431 personObjectList = (PersonsCommonList) handler.getCommonPartList();
432 } catch (UnauthorizedException ue) {
433 Response response = Response.status(
434 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
435 throw new WebApplicationException(response);
436 } catch (Exception e) {
437 if (logger.isDebugEnabled()) {
438 logger.debug("Caught exception in getPersonList", e);
440 Response response = Response.status(
441 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
442 throw new WebApplicationException(response);
444 return personObjectList;
448 @Path("{csid}/items/{itemcsid}")
449 public MultipartOutput updatePerson(
450 @PathParam("csid") String parentcsid,
451 @PathParam("itemcsid") String itemcsid,
452 MultipartInput theUpdate) {
453 if (logger.isDebugEnabled()) {
454 logger.debug("updatePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
456 if (parentcsid == null || "".equals(parentcsid)) {
457 logger.error("updatePerson: missing csid!");
458 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
459 "update failed on Person parentcsid=" + parentcsid).type(
460 "text/plain").build();
461 throw new WebApplicationException(response);
463 if (itemcsid == null || "".equals(itemcsid)) {
464 logger.error("updatePerson: missing itemcsid!");
465 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
466 "update failed on Person=" + itemcsid).type(
467 "text/plain").build();
468 throw new WebApplicationException(response);
470 MultipartOutput result = null;
472 // Note that we have to create the service context for the Items, not the main service
473 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName());
474 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
475 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
476 result = (MultipartOutput) ctx.getOutput();
477 } catch (UnauthorizedException ue) {
478 Response response = Response.status(
479 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
480 throw new WebApplicationException(response);
481 } catch (DocumentNotFoundException dnfe) {
482 if (logger.isDebugEnabled()) {
483 logger.debug("caugth exception in updatePerson", dnfe);
485 Response response = Response.status(Response.Status.NOT_FOUND).entity(
486 "Update failed on Person csid=" + itemcsid).type(
487 "text/plain").build();
488 throw new WebApplicationException(response);
489 } catch (Exception e) {
490 Response response = Response.status(
491 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
492 throw new WebApplicationException(response);
498 @Path("{csid}/items/{itemcsid}")
499 public Response deletePerson(
500 @PathParam("csid") String parentcsid,
501 @PathParam("itemcsid") String itemcsid) {
502 if (logger.isDebugEnabled()) {
503 logger.debug("deletePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
505 if (parentcsid == null || "".equals(parentcsid)) {
506 logger.error("deletePerson: missing csid!");
507 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
508 "delete failed on Person parentcsid=" + parentcsid).type(
509 "text/plain").build();
510 throw new WebApplicationException(response);
512 if (itemcsid == null || "".equals(itemcsid)) {
513 logger.error("deletePerson: missing itemcsid!");
514 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
515 "delete failed on Person=" + itemcsid).type(
516 "text/plain").build();
517 throw new WebApplicationException(response);
520 // Note that we have to create the service context for the Items, not the main service
521 ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
522 getRepositoryClient(ctx).delete(ctx, itemcsid);
523 return Response.status(HttpResponseCodes.SC_OK).build();
524 } catch (UnauthorizedException ue) {
525 Response response = Response.status(
526 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
527 throw new WebApplicationException(response);
528 } catch (DocumentNotFoundException dnfe) {
529 if (logger.isDebugEnabled()) {
530 logger.debug("caught exception in deletePerson", dnfe);
532 Response response = Response.status(Response.Status.NOT_FOUND).entity(
533 "Delete failed on Person itemcsid=" + itemcsid).type(
534 "text/plain").build();
535 throw new WebApplicationException(response);
536 } catch (Exception e) {
537 Response response = Response.status(
538 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
539 throw new WebApplicationException(response);