From fc4cfcd0b50f06b0a0f7c75852a41c7347abb2be Mon Sep 17 00:00:00 2001 From: remillet Date: Tue, 16 Jan 2018 11:41:52 -0800 Subject: [PATCH] DRYD-202: Added 'showItems' query param to GET requests on vocabularies that will show the vocabulary items. --- .gitignore | 1 + .../common/vocabulary/AuthorityResource.java | 82 +++++++++++++------ .../services/client/PoxPayload.java | 7 +- .../AbstractCollectionSpaceResourceImpl.java | 7 ++ .../services/client/VocabularyClient.java | 1 + .../vocabulary/VocabularyResource.java | 46 ++++++++++- 6 files changed, 115 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index b7d897e97..31642b6ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.out* *.iml *.org.*.bindings.xml *org.eclipse.wst* diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java index f1d81fab4..7277ca066 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java @@ -44,6 +44,7 @@ import javax.ws.rs.core.Response.ResponseBuilder; import org.collectionspace.services.client.IClientQueryParams; import org.collectionspace.services.client.IQueryManager; +import org.collectionspace.services.client.PoxPayload; import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; import org.collectionspace.services.client.XmlTools; @@ -102,12 +103,15 @@ import org.slf4j.LoggerFactory; * The Class AuthorityResource. */ +@SuppressWarnings({"rawtypes", "unchecked"}) @Consumes("application/xml") @Produces("application/xml") public abstract class AuthorityResource extends NuxeoBasedResource { - final static String SEARCH_TYPE_TERMSTATUS = "ts"; + final Logger logger = LoggerFactory.getLogger(AuthorityResource.class); + + final static String SEARCH_TYPE_TERMSTATUS = "ts"; public final static String hierarchy = "hierarchy"; protected Class authCommonClass; @@ -118,9 +122,9 @@ public abstract class AuthorityResource final static String FETCH_SHORT_ID = "_fetch_"; public final static String PARENT_WILDCARD = "_ALL_"; + protected static final boolean DONT_INCLUDE_ITEMS = false; + protected static final boolean INCLUDE_ITEMS = true; - final Logger logger = LoggerFactory.getLogger(AuthorityResource.class); - /** * Instantiates a new Authority resource. */ @@ -383,7 +387,7 @@ public abstract class AuthorityResource if (supportsReplicating(ctx.getTenantId(), ctx.getServiceName()) == false) { throw new DocumentException(Response.Status.FORBIDDEN.getStatusCode()); } - AuthorityDocumentModelHandler handler = (AuthorityDocumentModelHandler)createDocumentHandler(ctx); + AuthorityDocumentModelHandler handler = (AuthorityDocumentModelHandler)createDocumentHandler(ctx); specifier = Specifier.getSpecifier(identifier, "getAuthority", "GET"); handler.setShouldUpdateRevNumber(AuthorityServiceUtils.DONT_UPDATE_REV); // Never update rev number on sync calls neededSync = getRepositoryClient(ctx).synchronize(ctx, specifier, handler); @@ -407,7 +411,20 @@ public abstract class AuthorityResource return result; } + + /* + * Builds a cached JAX-RS response. + */ + protected Response buildResponse(ServiceContext ctx, PoxPayloadOut payloadOut) { + Response result = null; + ResponseBuilder responseBuilder = Response.ok(payloadOut.getBytes()); + this.setCacheControl(ctx, responseBuilder); + result = responseBuilder.build(); + + return result; + } + /** * Gets the authority. * @@ -424,29 +441,11 @@ public abstract class AuthorityResource @PathParam("csid") String specifier) { Response result = null; uriInfo = new UriInfoWrapper(uriInfo); - PoxPayloadOut payloadout = null; try { ServiceContext ctx = createServiceContext(request, uriInfo); - DocumentHandler handler = createDocumentHandler(ctx); - - Specifier spec = Specifier.getSpecifier(specifier, "getAuthority", "GET"); - if (spec.form == SpecifierForm.CSID) { - if (logger.isDebugEnabled()) { - logger.debug("getAuthority with csid=" + spec.value); - } - getRepositoryClient(ctx).get(ctx, spec.value, handler); - } else { - String whereClause = RefNameServiceUtils.buildWhereForAuthByName(authorityCommonSchemaName, spec.value); - DocumentFilter myFilter = new NuxeoDocumentFilter(whereClause, 0, 1); - handler.setDocumentFilter(myFilter); - getRepositoryClient(ctx).get(ctx, handler); - } - - payloadout = ctx.getOutput(); - ResponseBuilder responseBuilder = Response.ok(payloadout.getBytes()); - this.setCacheControl(ctx, responseBuilder); - result = responseBuilder.build(); + PoxPayloadOut payloadout = getAuthority(ctx, request, uriInfo, specifier, DONT_INCLUDE_ITEMS); + result = buildResponse(ctx, payloadout); } catch (Exception e) { throw bigReThrow(e, ServiceMessages.GET_FAILED, specifier); } @@ -460,6 +459,38 @@ public abstract class AuthorityResource return result; } + + protected PoxPayloadOut getAuthority( + ServiceContext ctx, + Request request, + UriInfo uriInfo, + String specifier, + boolean includeItems) throws Exception { + uriInfo = new UriInfoWrapper(uriInfo); + PoxPayloadOut payloadout = null; + + DocumentHandler docHandler = createDocumentHandler(ctx); + Specifier spec = Specifier.getSpecifier(specifier, "getAuthority", "GET"); + if (spec.form == SpecifierForm.CSID) { + if (logger.isDebugEnabled()) { + logger.debug("getAuthority with csid=" + spec.value); + } + getRepositoryClient(ctx).get(ctx, spec.value, docHandler); + } else { + String whereClause = RefNameServiceUtils.buildWhereForAuthByName(authorityCommonSchemaName, spec.value); + DocumentFilter myFilter = new NuxeoDocumentFilter(whereClause, 0, 1); + docHandler.setDocumentFilter(myFilter); + getRepositoryClient(ctx).get(ctx, docHandler); + } + + payloadout = ctx.getOutput(); + if (includeItems == true) { + AbstractCommonList itemsList = this.getAuthorityItemList(ctx, specifier, uriInfo); + payloadout.addPart(PoxPayload.ABSTRACT_COMMON_LIST_ROOT_ELEMENT_LABEL, itemsList); + } + + return payloadout; + } /** * Finds and populates the authority list. @@ -478,7 +509,7 @@ public abstract class AuthorityResource MultivaluedMap queryParams = uriInfo.getQueryParameters(); ServiceContext ctx = createServiceContext(uriInfo); - DocumentHandler handler = createDocumentHandler(ctx); + DocumentHandler handler = createDocumentHandler(ctx); DocumentFilter myFilter = handler.getDocumentFilter(); // Need to make the default sort order for authority items // be on the displayName field @@ -1332,7 +1363,6 @@ public abstract class AuthorityResource * @param itemIdentifier * @throws Exception */ - @SuppressWarnings("rawtypes") public boolean deleteAuthorityItem(ServiceContext existingCtx, String parentIdentifier, String itemIdentifier, diff --git a/services/client/src/main/java/org/collectionspace/services/client/PoxPayload.java b/services/client/src/main/java/org/collectionspace/services/client/PoxPayload.java index b495d1848..52508e553 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/PoxPayload.java +++ b/services/client/src/main/java/org/collectionspace/services/client/PoxPayload.java @@ -40,6 +40,10 @@ public abstract class PoxPayload { /** The Constant logger. */ protected static final Logger logger = LoggerFactory.getLogger(PayloadPart.class); + /** String constant for JAX-B root element labels */ + public static final String DOCUMENT_ROOT_ELEMENT_LABEL = "document"; + public static final String ABSTRACT_COMMON_LIST_ROOT_ELEMENT_LABEL = "abstract-common-list"; + /** The xml text. */ private String xmlPayload; @@ -53,7 +57,8 @@ public abstract class PoxPayload { private List parts = new ArrayList(); // Valid root element labels - private static Set validRootElementLabels = new HashSet(Arrays.asList("document", "abstract-common-list")); + private static Set validRootElementLabels = new HashSet(Arrays.asList(DOCUMENT_ROOT_ELEMENT_LABEL, + ABSTRACT_COMMON_LIST_ROOT_ELEMENT_LABEL)); /** * Instantiates a new pox payload. 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 57dc5c4e9..6f668f619 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 @@ -708,6 +708,13 @@ public abstract class AbstractCollectionSpaceResourceImpl return getCacheControl(ctx, "default"); // Look for a default one if we couldn't find based on the resource request } + /** + * FIXME: This code around cache control needs some documentation. + * + * @param ctx + * @param cacheKey + * @return + */ protected CacheControl getCacheControl(ServiceContext ctx, String cacheKey) { CacheControl result = null; diff --git a/services/vocabulary/client/src/main/java/org/collectionspace/services/client/VocabularyClient.java b/services/vocabulary/client/src/main/java/org/collectionspace/services/client/VocabularyClient.java index 91a1888fe..3159c1b6d 100644 --- a/services/vocabulary/client/src/main/java/org/collectionspace/services/client/VocabularyClient.java +++ b/services/vocabulary/client/src/main/java/org/collectionspace/services/client/VocabularyClient.java @@ -38,6 +38,7 @@ public class VocabularyClient extends AuthorityClientImpl queryParams = uriInfo.getQueryParameters(); + String showItemsValue = (String)queryParams.getFirst(VocabularyClient.SHOW_ITEMS_QP); + boolean showItems = Tools.isTrue(showItemsValue); + + ServiceContext ctx = createServiceContext(request, uriInfo); + PoxPayloadOut payloadout = getAuthority(ctx, request, uriInfo, specifier, showItems); + result = buildResponse(ctx, payloadout); + } catch (Exception e) { + throw bigReThrow(e, ServiceMessages.GET_FAILED, specifier); + } + + if (result == null) { + Response response = Response.status(Response.Status.NOT_FOUND).entity( + "GET request failed. The requested Authority specifier:" + specifier + ": was not found.").type( + "text/plain").build(); + throw new CSWebApplicationException(response); + } + + return result; + } + @Override public String getServiceName() { return vocabularyServiceName; -- 2.47.3