From 8641a8ad33c2453b48296a0d36b35910451522b9 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Tue, 7 Jun 2011 20:29:23 +0000 Subject: [PATCH] CSPACE-3175: Committing Laramie's many changes, with one minor fix, to allow resource classes in the Vocabulary service and in the various Authority services to inherit from ResourceBase, via AuthorityResource and AuthorityResourceWithContacts. --- .../common/vocabulary/AuthorityResource.java | 349 ++++++------------ .../AbstractCollectionSpaceResourceImpl.java | 23 +- .../AuthorityResourceWithContacts.java | 306 +++------------ 3 files changed, 189 insertions(+), 489 deletions(-) diff --git a/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java b/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java index 2c293c42c..f0a80c538 100644 --- a/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java +++ b/services/authority/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java @@ -88,8 +88,8 @@ import java.util.List; @Consumes("application/xml") @Produces("application/xml") public abstract class AuthorityResource - //extends ResourceBase { - extends AbstractMultiPartCollectionSpaceResourceImpl { + extends ResourceBase { + //extends AbstractMultiPartCollectionSpaceResourceImpl { protected Class authCommonClass; protected Class resourceClass; @@ -163,19 +163,11 @@ public abstract class AuthorityResource getCommonPartClass() { return authCommonClass; @@ -216,7 +208,7 @@ public abstract class AuthorityResource handler = (AuthorityDocumentModelHandler)createDocumentHandler(ctx); shortIdentifier = handler.getShortIdentifier(wrapDoc, authorityCommonSchemaName); - } catch (DocumentNotFoundException dnfe) { - throw dnfe; - } catch (IllegalArgumentException iae) { - throw iae; - } catch (DocumentException de) { - throw de; } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("Caught exception ", e); @@ -251,13 +237,52 @@ public abstract class AuthorityResource queryParams) + throws Exception { + CsidAndShortIdentifier tempResult = lookupParentCSIDAndShortIdentifer(parentspecifier, method, op, queryParams); + return tempResult.CSID; + } + + public CsidAndShortIdentifier lookupParentCSIDAndShortIdentifer (String parentspecifier, String method, String op, MultivaluedMap queryParams) + throws Exception { + CsidAndShortIdentifier result = new CsidAndShortIdentifier(); + Specifier parentSpec = getSpecifier(parentspecifier, method, op); + // Note that we have to create the service context for the Items, not the main service + String parentcsid; + String parentShortIdentifier; + if(parentSpec.form==SpecifierForm.CSID) { + parentShortIdentifier = null; + parentcsid = parentSpec.value; + // Uncomment when app layer is ready to integrate + // parentShortIdentifier = FETCH_SHORT_ID; + } else { + parentShortIdentifier = parentSpec.value; + String whereClause = buildWhereForAuthByName(parentSpec.value); + ServiceContext ctx = createServiceContext(getServiceName(), queryParams); + parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); //FIXME: REM - If the parent has been soft-deleted, should we be looking for the item? + } + result.CSID = parentcsid; + return result; + } + + public String lookupItemCSID (String itemspecifier, String parentcsid, String method, String op, ServiceContext ctx) + throws DocumentException { + String itemcsid; + Specifier itemSpec = getSpecifier(itemspecifier, method, op); + if(itemSpec.form==SpecifierForm.CSID) { + itemcsid = itemSpec.value; + } else { + String itemWhereClause = buildWhereForAuthItemByName(itemSpec.value, parentcsid); + itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause); //FIXME: REM - Should we be looking for the 'wf_deleted' query param and filtering on it? + } + return itemcsid; + } - /** - * Creates the authority. - * - * @return the response - */ @POST public Response createAuthority(String xmlPayload) { try { @@ -299,16 +324,17 @@ public abstract class AuthorityResource queryParams = ui.getQueryParameters(); - Specifier spec = getSpecifier(specifier, "getAuthority", "GET"); - ServiceContext ctx = createServiceContext(queryParams); - DocumentHandler handler = createDocumentHandler(ctx); - if(spec.form == SpecifierForm.CSID) { + ServiceContext ctx = createServiceContext(ui); + DocumentHandler handler = createDocumentHandler(ctx); + + Specifier spec = getSpecifier(specifier, "getAuthority", "GET"); + if(spec.form == SpecifierForm.CSID) { if (logger.isDebugEnabled()) { logger.debug("getAuthority with csid=" + spec.value); } @@ -320,25 +346,9 @@ public abstract class AuthorityResource ctx = createServiceContext(); + ensureCSID(csid, ServiceMessages.DELETE_FAILED, "Authority.csid"); + ServiceContext ctx = createServiceContext(); getRepositoryClient(ctx).delete(ctx, csid); return Response.status(HttpResponseCodes.SC_OK).build(); - } catch (UnauthorizedException ue) { - Response response = Response.status( - Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (DocumentNotFoundException dnfe) { - if (logger.isDebugEnabled()) { - logger.debug("caught exception in deleteAuthority", dnfe); - } - Response response = Response.status(Response.Status.NOT_FOUND).entity( - "Delete failed on Authority csid=" + csid).type( - "text/plain").build(); - throw new WebApplicationException(response); } catch (Exception e) { - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build(); - throw new WebApplicationException(response); + throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid); } - } /************************************************************************* @@ -473,37 +452,19 @@ public abstract class AuthorityResource ctx = null; - Specifier spec = getSpecifier(specifier, "createAuthorityItem", "CREATE_ITEM"); - String parentcsid; - String parentShortIdentifier; - if(spec.form==SpecifierForm.CSID) { - parentcsid = spec.value; - // Uncomment when app layer is ready to integrate - // parentShortIdentifier = FETCH_SHORT_ID; - parentShortIdentifier = null; - } else { - parentShortIdentifier = spec.value; - String whereClause = buildWhereForAuthByName(spec.value); - ctx = createServiceContext(getServiceName()); - parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); - } - ctx = createServiceContext(getItemServiceName(), input); + ServiceContext ctx = createServiceContext(getItemServiceName(), input); ctx.setUriInfo(ui); //Laramie - // Note: must have the parentShortId, to do the create. - DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid, parentShortIdentifier); + + // Note: must have the parentShortId, to do the create. + CsidAndShortIdentifier parent = lookupParentCSIDAndShortIdentifer(specifier,"createAuthorityItem", "CREATE_ITEM", null); + DocumentHandler handler = createItemDocumentHandler(ctx, parent.CSID, parent.shortIdentifier); String itemcsid = getRepositoryClient(ctx).create(ctx, handler); UriBuilder path = UriBuilder.fromResource(resourceClass); - path.path(parentcsid + "/items/" + itemcsid); + path.path(parent.CSID + "/items/" + itemcsid); Response response = Response.created(path.build()).build(); - - //updateRelations(ui, itemcsid, input); - return response; } catch (Exception e) { - //TODO: if e is 400 type error, then call throwWebAppException(400,...); throw bigReThrow(e, ServiceMessages.CREATE_FAILED); } } @@ -553,7 +514,7 @@ public abstract class AuthorityResource queryParams = ui.getQueryParameters(); + String parentcsid = lookupParentCSID(parentspecifier, "getAuthorityItem(parent)", "GET_ITEM", queryParams); - Specifier parentSpec = getSpecifier(parentspecifier, "getAuthorityItem(parent)", "GET_ITEM"); - Specifier itemSpec = getSpecifier(itemspecifier, "getAuthorityItem(item)", "GET_ITEM"); - // Note that we have to create the service context for the Items, not the main service - RemoteServiceContext ctx = null; - String parentcsid; - if(parentSpec.form==SpecifierForm.CSID) { - parentcsid = parentSpec.value; - } else { - String whereClause = buildWhereForAuthByName(parentSpec.value); - ctx = (RemoteServiceContext)createServiceContext(getServiceName(), queryParams); - parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); //FIXME: REM - If the parent has been soft-deleted, should we be looking for the item? - } + RemoteServiceContext ctx = null; ctx = (RemoteServiceContext)createServiceContext(getItemServiceName(), queryParams); ctx.setJaxRsContext(jaxRsContext); ctx.setUriInfo(ui); //ARG! must pass this or subsequent calls will not have a ui. // We omit the parentShortId, only needed when doing a create... - DocumentHandler handler = createItemDocumentHandler(ctx, - parentcsid, null); + DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid, null); + + Specifier itemSpec = getSpecifier(itemspecifier, "getAuthorityItem(item)", "GET_ITEM"); if(itemSpec.form==SpecifierForm.CSID) { getRepositoryClient(ctx).get(ctx, itemSpec.value, handler); } else { @@ -617,7 +569,6 @@ public abstract class AuthorityResource queryParams = ui.getQueryParameters(); - // Note that docType defaults to the ServiceName, so we're fine with that. - ServiceContext ctx = null; - String parentcsid; - if(spec.form==SpecifierForm.CSID) { - parentcsid = spec.value; - } else { - String whereClause = buildWhereForAuthByName(spec.value); - ctx = createServiceContext(getServiceName(), queryParams); - parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); - } + // Note that docType defaults to the ServiceName, so we're fine with that. + ServiceContext ctx = null; + + String parentcsid = lookupParentCSID(specifier, "getAuthorityItemList", "LIST", queryParams); + ctx = createServiceContext(getItemServiceName(), queryParams); // We omit the parentShortId, only needed when doing a create... DocumentHandler handler = createItemDocumentHandler(ctx, @@ -703,30 +648,13 @@ public abstract class AuthorityResource queryParams = ui.getQueryParameters(); - Specifier parentSpec = getSpecifier(parentspecifier, - "getReferencingObjects(parent)", "GET_ITEM_REF_OBJS"); - Specifier itemSpec = getSpecifier(itemspecifier, - "getReferencingObjects(item)", "GET_ITEM_REF_OBJS"); - // Note that we have to create the service context for the Items, not the main service - ServiceContext ctx = null; - String parentcsid; - if(parentSpec.form==SpecifierForm.CSID) { - parentcsid = parentSpec.value; - } else { - String whereClause = buildWhereForAuthByName(parentSpec.value); - ctx = createServiceContext(getServiceName(), queryParams); - parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); //FIXME: REM - If the parent is soft-deleted should we still try to find the item? - } - ctx = createServiceContext(getItemServiceName(), queryParams); - String itemcsid; - if(itemSpec.form==SpecifierForm.CSID) { - itemcsid = itemSpec.value; - } else { - String itemWhereClause = - buildWhereForAuthItemByName(itemSpec.value, parentcsid); - itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause); //FIXME: REM - Should we be looking for the 'wf_deleted' query param and filtering on it? - } - // Note that we have to create the service context for the Items, not the main service + + String parentcsid = lookupParentCSID(parentspecifier, "getReferencingObjects(parent)", "GET_ITEM_REF_OBJS", queryParams); + + ServiceContext ctx = createServiceContext(getItemServiceName(), queryParams); + String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getReferencingObjects(item)", "GET_ITEM_REF_OBJS", ctx); + + // Note that we have to create the service context for the Items, not the main service // We omit the parentShortId, only needed when doing a create... DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid, null); RepositoryClient repoClient = getRepositoryClient(ctx); @@ -775,34 +703,20 @@ public abstract class AuthorityResource queryParams = ui.getQueryParameters(); ServiceContext ctx = null; - String parentcsid; - if(parentSpec.form==SpecifierForm.CSID) { - parentcsid = parentSpec.value; - } else { - String whereClause = buildWhereForAuthByName(parentSpec.value); - ctx = createServiceContext(getServiceName()); - parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); - } + + String parentcsid = lookupParentCSID(parentspecifier, "getAuthorityItemAuthRefs(parent)", "GET_ITEM_AUTH_REFS", queryParams); + ctx = createServiceContext(getItemServiceName(), queryParams); - // We omit the parentShortId, only needed when doing a create... + // We omit the parentShortId, only needed when doing a create... RemoteDocumentModelHandlerImpl handler = - (RemoteDocumentModelHandlerImpl) createItemDocumentHandler(ctx, - parentcsid, null); - String itemcsid; - if(itemSpec.form==SpecifierForm.CSID) { - itemcsid = itemSpec.value; - } else { - String itemWhereClause = - buildWhereForAuthItemByName(itemSpec.value, parentcsid); - itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause); - } - DocumentWrapper docWrapper = - getRepositoryClient(ctx).getDoc(ctx, itemcsid); + (RemoteDocumentModelHandlerImpl) createItemDocumentHandler(ctx, parentcsid, null); + + String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getAuthorityItemAuthRefs(item)", "GET_ITEM_AUTH_REFS", ctx); + + DocumentWrapper docWrapper = getRepositoryClient(ctx).getDoc(ctx, itemcsid); List authRefFields = ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues( ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES); @@ -830,39 +744,19 @@ public abstract class AuthorityResource ctx = null; - String parentcsid; - if(parentSpec.form==SpecifierForm.CSID) { - parentcsid = parentSpec.value; - } else { - String whereClause = buildWhereForAuthByName(parentSpec.value); - ctx = createServiceContext(getServiceName()); - parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); - } - ctx = createServiceContext(getItemServiceName(), theUpdate); - String itemcsid; - if(itemSpec.form==SpecifierForm.CSID) { - itemcsid = itemSpec.value; - } else { - String itemWhereClause = - buildWhereForAuthItemByName(itemSpec.value, parentcsid); - itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause); - } - // Note that we have to create the service context for the Items, not the main service + // Note that we have to create the service context for the Items, not the main service + //Laramie CSPACE-3175. passing null for queryParams, because prior to this refactor, the code moved to lookupParentCSID in this instance called the version of getServiceContext() that passes null + String parentcsid = lookupParentCSID(parentspecifier, "updateAuthorityItem(parent)", "UPDATE_ITEM", null); + + ServiceContext ctx = createServiceContext(getItemServiceName(), theUpdate); + String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "updateAuthorityItem(item)", "UPDATE_ITEM", ctx); + // We omit the parentShortId, only needed when doing a create... - DocumentHandler handler = createItemDocumentHandler(ctx, - parentcsid, null); + DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid, null); ctx.setUriInfo(ui); getRepositoryClient(ctx).update(ctx, itemcsid, handler); result = ctx.getOutput(); - //PoxPayloadIn input = new PoxPayloadIn(xmlPayload); - //updateRelations(itemcsid, input); } catch (Exception e) { throw bigReThrow(e, ServiceMessages.UPDATE_FAILED); } @@ -882,34 +776,23 @@ public abstract class AuthorityResource ctx = createServiceContext(getItemServiceName()); + ServiceContext ctx = createServiceContext(getItemServiceName()); getRepositoryClient(ctx).delete(ctx, itemcsid); return Response.status(HttpResponseCodes.SC_OK).build(); - } catch (Exception e) { + } catch (Exception e) { throw bigReThrow(e, ServiceMessages.DELETE_FAILED + " itemcsid: " + itemcsid+ " parentcsid:" + parentcsid); } } diff --git a/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResourceImpl.java b/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResourceImpl.java index 98be84586..a091d7424 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResourceImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResourceImpl.java @@ -31,6 +31,7 @@ import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.context.ServiceContext; @@ -224,13 +225,25 @@ public abstract class AbstractCollectionSpaceResourceImpl * * @throws Exception the exception */ - protected ServiceContext createServiceContext(MultivaluedMap queryParams) throws Exception { + protected ServiceContext createServiceContext(MultivaluedMap queryParams) throws Exception { ServiceContext ctx = createServiceContext( (IT)null, /*input*/ queryParams, (Class)null /*input type's Class*/); return ctx; - } + } + + protected ServiceContext createServiceContext(UriInfo ui) throws Exception { + MultivaluedMap queryParams = ui.getQueryParameters(); + ServiceContext ctx = createServiceContext( + (IT)null, /*input*/ + queryParams, + (Class)null /*input type's Class*/); + return ctx; + } + + + /** * Creates the service context. @@ -350,12 +363,16 @@ public abstract class AbstractCollectionSpaceResourceImpl } protected void ensureCSID(String csid, String crudType) throws WebApplicationException { + ensureCSID(csid, crudType, "csid"); + } + + protected void ensureCSID(String csid, String crudType, String whichCsid) throws WebApplicationException { if (logger.isDebugEnabled()) { logger.debug(crudType + " for " + getClass().getName() + " with csid=" + csid); } if (csid == null || "".equals(csid)) { logger.error(crudType + " for " + getClass().getName() + " missing csid!"); - Response response = Response.status(Response.Status.BAD_REQUEST).entity(crudType + " failed on " + getClass().getName() + " csid=" + csid).type("text/plain").build(); + Response response = Response.status(Response.Status.BAD_REQUEST).entity(crudType + " failed on " + getClass().getName() + ' '+whichCsid+'=' + csid).type("text/plain").build(); throw new WebApplicationException(response); } } diff --git a/services/contact/service/src/main/java/org/collectionspace/services/contact/AuthorityResourceWithContacts.java b/services/contact/service/src/main/java/org/collectionspace/services/contact/AuthorityResourceWithContacts.java index 11d5c077c..7b6813225 100644 --- a/services/contact/service/src/main/java/org/collectionspace/services/contact/AuthorityResourceWithContacts.java +++ b/services/contact/service/src/main/java/org/collectionspace/services/contact/AuthorityResourceWithContacts.java @@ -87,14 +87,10 @@ import org.slf4j.LoggerFactory; public abstract class AuthorityResourceWithContacts extends //FIXME: REM - Why is this resource in this package instead of somewhere in 'common'? AuthorityResource { - /** The contact resource. */ private ContactResource contactResource = new ContactResource(); // Warning: ContactResource is a singleton. final Logger logger = LoggerFactory.getLogger(AuthorityResourceWithContacts.class); - /** - * Instantiates a new Authority resource. - */ public AuthorityResourceWithContacts( Class authCommonClass, Class resourceClass, String authorityCommonSchemaName, String authorityItemCommonSchemaName) { @@ -104,37 +100,19 @@ public abstract class AuthorityResourceWithContacts ctx, String inAuthority, String inItem) throws Exception { - ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler( ctx, ctx.getCommonPartLabel(getContactServiceName()), ContactsCommon.class); docHandler.setInAuthority(inAuthority); docHandler.setInItem(inItem); - return docHandler; } @@ -142,7 +120,6 @@ public abstract class AuthorityResourceWithContacts ctx = null; - String parentcsid; - if(parentSpec.form==SpecifierForm.CSID) { - parentcsid = parentSpec.value; - } else { - String whereClause = buildWhereForAuthByName(parentSpec.value); - ctx = createServiceContext(getServiceName()); - parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); - } - String itemcsid; - if(itemSpec.form==SpecifierForm.CSID) { - itemcsid = itemSpec.value; - } else { - String itemWhereClause = - buildWhereForAuthItemByName(itemSpec.value, parentcsid); - ctx = createServiceContext(getItemServiceName()); - itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause); - } + String parentcsid = lookupParentCSID(parentspecifier, "createContact(authority)", "CREATE_ITEM_CONTACT", null); + + ServiceContext itemCtx = createServiceContext(getItemServiceName()); + String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "createContact(item)", "CREATE_ITEM_CONTACT", itemCtx); + // Note that we have to create the service context and document // handler for the Contact service, not the main service. - ctx = createServiceContext(getContactServiceName(), input); + ServiceContext ctx = createServiceContext(getContactServiceName(), input); DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid); String csid = getRepositoryClient(ctx).create(ctx, handler); UriBuilder path = UriBuilder.fromResource(resourceClass); path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid); Response response = Response.created(path.build()).build(); return response; - } catch (BadRequestException bre) { - Response response = Response.status( - Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (UnauthorizedException ue) { - Response response = Response.status( - Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (DocumentNotFoundException dnfe) { - if (logger.isDebugEnabled()) { - logger.debug("createContact", dnfe); - } - Response response = Response.status(Response.Status.NOT_FOUND) - .entity("Create Contact failed; one of the requested specifiers for authority:" - +parentspecifier+": and item:"+itemspecifier+": was not found.") - .type("text/plain").build(); - throw new WebApplicationException(response); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Caught exception in createContact", e); - } - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR) - .entity("Attempt to create Contact failed.") - .type("text/plain").build(); - throw new WebApplicationException(response); + throw bigReThrow(e, + "Create Contact failed; one of the requested specifiers for authority:" + +parentspecifier+": and item:"+itemspecifier+": was not found.", + itemspecifier); } - } /** @@ -233,31 +170,13 @@ public abstract class AuthorityResourceWithContacts ctx = null; - String parentcsid; - if(parentSpec.form==SpecifierForm.CSID) { - parentcsid = parentSpec.value; - } else { - String whereClause = buildWhereForAuthByName(parentSpec.value); - ctx = createServiceContext(getServiceName()); - parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); - } - String itemcsid; - if(itemSpec.form==SpecifierForm.CSID) { - itemcsid = itemSpec.value; - } else { - String itemWhereClause = - buildWhereForAuthItemByName(itemSpec.value, parentcsid); - ctx = createServiceContext(getItemServiceName()); - itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause); - } + String parentcsid = lookupParentCSID(parentspecifier, "getContactList(parent)", "GET_CONTACT_LIST", null); + + ServiceContext itemCtx = createServiceContext(getItemServiceName()); + String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getContactList(item)", "GET_CONTACT_LIST", itemCtx); + MultivaluedMap queryParams = ui.getQueryParameters(); - ctx = createServiceContext(getContactServiceName(), queryParams); + ServiceContext ctx = createServiceContext(getContactServiceName(), queryParams); DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid); DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter(); myFilter.appendWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" + @@ -270,26 +189,11 @@ public abstract class AuthorityResourceWithContacts ctx = null; - String parentcsid; - if(parentSpec.form==SpecifierForm.CSID) { - parentcsid = parentSpec.value; - } else { - String whereClause = buildWhereForAuthByName(parentSpec.value); - ctx = createServiceContext(getServiceName()); - parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); - } - String itemcsid; - if(itemSpec.form==SpecifierForm.CSID) { - itemcsid = itemSpec.value; - } else { - String itemWhereClause = - buildWhereForAuthItemByName(itemSpec.value, parentcsid); - ctx = createServiceContext(getItemServiceName()); - itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause); - } - // Note that we have to create the service context and document - // handler for the Contact service, not the main service. - ctx = createServiceContext(getContactServiceName()); + String parentcsid = lookupParentCSID(parentspecifier, "getContact(parent)", "GET_ITEM_CONTACT", null); + + ServiceContext itemCtx = createServiceContext(getItemServiceName()); + String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getContact(item)", "GET_ITEM_CONTACT", itemCtx); + + // Note that we have to create the service context and document handler for the Contact service, not the main service. + ServiceContext ctx = createServiceContext(getContactServiceName()); DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid); getRepositoryClient(ctx).get(ctx, csid, handler); result = ctx.getOutput(); - } catch (UnauthorizedException ue) { - Response response = Response.status( - Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (DocumentNotFoundException dnfe) { - if (logger.isDebugEnabled()) { - logger.debug("getContact", dnfe); - } - Response response = Response.status(Response.Status.NOT_FOUND) - .entity("Get failed, the requested Contact CSID:" + csid + - ": or one of the specifiers for authority:"+parentspecifier+ - ": and item:"+itemspecifier+": was not found.") - .type("text/plain").build(); - throw new WebApplicationException(response); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("getContact", e); - } - Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity("Get contact failed") - .type("text/plain").build(); - throw new WebApplicationException(response); + throw bigReThrow(e, "Get failed, the requested Contact CSID:" + csid + + ": or one of the specifiers for authority:"+parentspecifier+ + ": and item:"+itemspecifier+": was not found.", + csid); } if (result == null) { Response response = Response.status(Response.Status.NOT_FOUND) @@ -370,7 +238,6 @@ public abstract class AuthorityResourceWithContacts ctx = null; - String parentcsid; - if(parentSpec.form==SpecifierForm.CSID) { - parentcsid = parentSpec.value; - } else { - String whereClause = buildWhereForAuthByName(parentSpec.value); - ctx = createServiceContext(getServiceName()); - parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); - } - String itemcsid; - if(itemSpec.form==SpecifierForm.CSID) { - itemcsid = itemSpec.value; - } else { - String itemWhereClause = - buildWhereForAuthItemByName(itemSpec.value, parentcsid); - ctx = createServiceContext(getItemServiceName()); - itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause); - } - // Note that we have to create the service context and document - // handler for the Contact service, not the main service. + // Note that we have to create the service context and document handler for the Contact service, not the main service. ctx = createServiceContext(getContactServiceName(), theUpdate); DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid); getRepositoryClient(ctx).update(ctx, csid, handler); result = ctx.getOutput(); - } catch (BadRequestException bre) { - Response response = Response.status( - Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (UnauthorizedException ue) { - Response response = Response.status( - Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (DocumentNotFoundException dnfe) { - if (logger.isDebugEnabled()) { - logger.debug("caught exception in updateContact", dnfe); - } - Response response = Response.status(Response.Status.NOT_FOUND) - .entity("Update failed, the requested Contact CSID:" + csid + - ": or one of the specifiers for authority:"+parentspecifier+ - ": and item:"+itemspecifier+": was not found.") - .type("text/plain").build(); - throw new WebApplicationException(response); } catch (Exception e) { - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build(); - throw new WebApplicationException(response); + throw bigReThrow(e, "Update failed, the requested Contact CSID:" + csid + + ": or one of the specifiers for authority:"+parentspecifier+ + ": and item:"+itemspecifier+": was not found.", + csid); } return result.toXML(); } @@ -464,52 +295,21 @@ public abstract class AuthorityResourceWithContacts ctx = null; - String parentcsid; - if(parentSpec.form==SpecifierForm.CSID) { - parentcsid = parentSpec.value; - } else { - String whereClause = buildWhereForAuthByName(parentSpec.value); - ctx = createServiceContext(getServiceName()); - parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); - } - String itemcsid; - if(itemSpec.form==SpecifierForm.CSID) { - itemcsid = itemSpec.value; - } else { - String itemWhereClause = - buildWhereForAuthItemByName(itemSpec.value, parentcsid); - ctx = createServiceContext(getItemServiceName()); - itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause); - } - // Note that we have to create the service context for the - // Contact service, not the main service. + // Note that we have to create the service context for the Contact service, not the main service. ctx = createServiceContext(getContactServiceName()); getRepositoryClient(ctx).delete(ctx, csid); return Response.status(HttpResponseCodes.SC_OK).build(); - } catch (UnauthorizedException ue) { - Response response = Response.status( - Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (DocumentNotFoundException dnfe) { - if (logger.isDebugEnabled()) { - logger.debug("Caught exception in deleteContact", dnfe); - } - Response response = Response.status(Response.Status.NOT_FOUND) - .entity("Delete failed, the requested Contact CSID:" + csid + - ": or one of the specifiers for authority:"+parentspecifier+ - ": and item:"+itemspecifier+": was not found.") - .type("text/plain").build(); - throw new WebApplicationException(response); - } catch (Exception e) { - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build(); - throw new WebApplicationException(response); + } catch (Exception e) { + throw bigReThrow(e, "DELETE failed, the requested Contact CSID:" + csid + + ": or one of the specifiers for authority:"+parentspecifier+ + ": and item:"+itemspecifier+": was not found.", csid); } } -- 2.47.3