From d04eea727eff8f5ac37698f47ca214fe5444fbbb Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Wed, 15 Aug 2012 16:58:28 -0700 Subject: [PATCH] CSPACE-5271,CSPACE-5453: Moved population of the URI template registry to ServiceMain, per Patrick's suggestion. Initial work on integrating the registry with referencing objects (refObjs) URI generation; still containing bugs yet to be fixed. --- .../CollectionSpaceJaxRsApplication.java | 44 +------------------ .../common/vocabulary/AuthorityResource.java | 3 +- .../AuthorityItemDocumentModelHandler.java | 4 +- .../services/common/ServiceMain.java | 38 ++++++++++++++++ .../common/UriTemplateRegistryHolder.java | 6 --- .../common/UriTemplateRegistryKey.java | 4 +- .../vocabulary/RefNameServiceUtils.java | 38 ++++++++-------- 7 files changed, 67 insertions(+), 70 deletions(-) delete mode 100644 services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryHolder.java diff --git a/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java b/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java index 5173917a4..72a7ac736 100644 --- a/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java +++ b/services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java @@ -82,12 +82,11 @@ import org.jboss.resteasy.spi.ResteasyProviderFactory; * $LastChangedDate$ */ public class CollectionSpaceJaxRsApplication extends Application - implements ResourceMapHolder, UriTemplateRegistryHolder { + implements ResourceMapHolder { private Set singletons = new HashSet(); private Set> empty = new HashSet>(); private ResourceMap resourceMap = new ResourceMapImpl(); - private UriTemplateRegistry uriTemplateRegistry = new UriTemplateRegistry(); private ServletContext servletContext = null; public CollectionSpaceJaxRsApplication() { @@ -129,11 +128,6 @@ public class CollectionSpaceJaxRsApplication extends Application singletons.add(new IDResource()); - buildUriTemplateRegistry(); - // FIXME: Temporary for CSPACE-5271 - please remove once - // that issue is resolved - uriTemplateRegistry.dump(); - /* singletons.add(new WorkflowResource()); */ @@ -147,33 +141,6 @@ public class CollectionSpaceJaxRsApplication extends Application resourceMap.put(resource.getServiceName(), resource); } - /** - * Build a registry of URI templates by querying each resource - * for its own entries in the registry. - * - * These entries consist of one or more URI templates for - * building URIs for accessing that resource. - */ - private void buildUriTemplateRegistry() { - ResourceBase resource = null; - ResourceMap resources = getResourceMap(); - for (Map.Entry entry : resources.entrySet()) { - resource = entry.getValue(); - Map> entries = - resource.getUriRegistryEntries(); - getUriTemplateRegistry().putAll(entries); - } - // Contacts itself should not have an entry in the URI template registry; - // there should be a Contacts entry in that registry only for use in - // building URIs for resources that have contacts as a sub-resource - // - // FIXME: There may be a more elegant way to filter this out; or it may - // fall out during implementation of CSPACE-2698 - // - // final String CONTACT_DOCTYPE = "Contact"; - // uriTemplateRegistry.remove(CONTACT_DOCTYPE); - } - @Override public Set> getClasses() { @@ -185,18 +152,11 @@ public class CollectionSpaceJaxRsApplication extends Application return singletons; } + @Override public ResourceMap getResourceMap() { return resourceMap; } - private void setUriTemplateRegistry(UriTemplateRegistry registry) { - this.uriTemplateRegistry = registry; - } - - public UriTemplateRegistry getUriTemplateRegistry() { - return uriTemplateRegistry; - } - public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } 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 de9cf91d5..cd6c6caf1 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 @@ -791,6 +791,7 @@ public abstract class AuthorityResource public AuthorityRefDocList getReferencingObjects( @PathParam("csid") String parentspecifier, @PathParam("itemcsid") String itemspecifier, + @Context UriTemplateRegistry uriTemplateRegistry, @Context UriInfo ui) { AuthorityRefDocList authRefDocList = null; try { @@ -811,7 +812,7 @@ public abstract class AuthorityResource AuthorityItemDocumentModelHandler handler = (AuthorityItemDocumentModelHandler) createItemDocumentHandler(ctx, parentcsid, null); - authRefDocList = handler.getReferencingObjects(ctx, serviceTypes, getRefPropName(), itemcsid); + authRefDocList = handler.getReferencingObjects(ctx, uriTemplateRegistry, serviceTypes, getRefPropName(), itemcsid); } catch (Exception e) { throw bigReThrow(e, ServiceMessages.GET_FAILED); } diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java index 22919c7aa..a00b74f92 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java @@ -32,6 +32,7 @@ import org.collectionspace.services.client.PoxPayloadOut; import org.collectionspace.services.client.RelationClient; import org.collectionspace.services.common.ResourceBase; +import org.collectionspace.services.common.UriTemplateRegistry; import org.collectionspace.services.common.api.CommonAPI; import org.collectionspace.services.common.api.RefName; import org.collectionspace.services.common.api.Tools; @@ -454,6 +455,7 @@ public abstract class AuthorityItemDocumentModelHandler public AuthorityRefDocList getReferencingObjects( ServiceContext ctx, + UriTemplateRegistry uriTemplateRegistry, List serviceTypes, String propertyName, String itemcsid) throws Exception { @@ -475,7 +477,7 @@ public abstract class AuthorityItemDocumentModelHandler DocumentModel docModel = wrapper.getWrappedObject(); String refName = (String) docModel.getPropertyValue(AuthorityItemJAXBSchema.REF_NAME); authRefDocList = RefNameServiceUtils.getAuthorityRefDocs( - repoSession, ctx, repoClient, + repoSession, ctx, uriTemplateRegistry, repoClient, serviceTypes, refName, propertyName, diff --git a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java index 3a93a231b..51ccd3cd3 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java @@ -34,6 +34,7 @@ import org.collectionspace.services.config.types.PropertyItemType; import org.collectionspace.services.config.types.PropertyType; import org.collectionspace.services.nuxeo.client.java.NuxeoConnectorEmbedded; import org.collectionspace.services.nuxeo.client.java.TenantRepository; +import org.jboss.resteasy.spi.ResteasyProviderFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,6 +58,7 @@ public class ServiceMain { private String serverRootDir = null; private ServicesConfigReaderImpl servicesConfigReader; private TenantBindingConfigReaderImpl tenantBindingConfigReader; + private UriTemplateRegistry uriTemplateRegistry = new UriTemplateRegistry(); private static final String SERVER_HOME_PROPERTY = "catalina.home"; @@ -380,6 +382,42 @@ public class ServiceMain { public TenantBindingConfigReaderImpl getTenantBindingConfigReader() { return tenantBindingConfigReader; } + + /** + * Populate a registry of URI templates by querying each resource + * for its own entries in the registry. + * + * These entries consist of one or more URI templates for + * building URIs for accessing that resource. + */ + private synchronized void populateUriTemplateRegistry() { + if (uriTemplateRegistry.isEmpty()) { + ResourceBase resource = null; + ResourceMap resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class); + for (Map.Entry entry : resourceMap.entrySet()) { + resource = entry.getValue(); + Map entries = + resource.getUriRegistryEntries(); + uriTemplateRegistry.putAll(entries); + } + + // FIXME: Contacts itself should not have an entry in the URI template registry; + // there should be a Contacts entry in that registry only for use in + // building URIs for resources that have contacts as a sub-resource + // (This may also fall out during implementation of CSPACE-2698.) + + // FIXME: Temporary for debugging in-process work on CSPACE-5271. + // Please remove the following statement once that issue is resolved. + uriTemplateRegistry.dump(); + } + } + + public UriTemplateRegistry getUriTemplateRegistry() { + if (uriTemplateRegistry.isEmpty()) { + populateUriTemplateRegistry(); + } + return uriTemplateRegistry; + } } diff --git a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryHolder.java b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryHolder.java deleted file mode 100644 index a3ab98b03..000000000 --- a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryHolder.java +++ /dev/null @@ -1,6 +0,0 @@ - -package org.collectionspace.services.common; - -public interface UriTemplateRegistryHolder { - public UriTemplateRegistry getUriTemplateRegistry(); -} diff --git a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryKey.java b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryKey.java index d7d3a154c..f7e85bd67 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryKey.java +++ b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistryKey.java @@ -10,10 +10,10 @@ public class UriTemplateRegistryKey { private String tenantId; private String docType; - public void UriTemplateRegistryKey() { + public UriTemplateRegistryKey() { } - public void UriTemplateRegistryKey(String tenantId, String docType) { + public UriTemplateRegistryKey(String tenantId, String docType) { this.tenantId = tenantId; this.docType = docType; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java b/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java index cecee564f..cc9aa87a9 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/vocabulary/RefNameServiceUtils.java @@ -23,7 +23,6 @@ package org.collectionspace.services.common.vocabulary; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -43,15 +42,17 @@ import org.slf4j.LoggerFactory; import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; import org.collectionspace.services.common.ServiceMain; +import org.collectionspace.services.common.StoredValuesUriTemplate; +import org.collectionspace.services.common.UriTemplateFactory; +import org.collectionspace.services.common.UriTemplateRegistry; +import org.collectionspace.services.common.UriTemplateRegistryKey; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.context.AbstractServiceContextImpl; import org.collectionspace.services.common.api.RefNameUtils; import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.api.RefNameUtils.AuthorityTermInfo; import org.collectionspace.services.common.authorityref.AuthorityRefDocList; -import org.collectionspace.services.common.authorityref.AuthorityRefList; import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl; -import org.collectionspace.services.common.config.URIUtils; import org.collectionspace.services.common.context.ServiceBindingUtils; import org.collectionspace.services.common.document.DocumentException; import org.collectionspace.services.common.document.DocumentFilter; @@ -60,15 +61,14 @@ import org.collectionspace.services.common.document.DocumentUtils; import org.collectionspace.services.common.document.DocumentWrapper; import org.collectionspace.services.common.query.QueryManager; import org.collectionspace.services.common.repository.RepositoryClient; -// import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema; import org.collectionspace.services.nuxeo.client.java.DocHandlerBase; import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl; import org.collectionspace.services.common.security.SecurityUtils; import org.collectionspace.services.config.service.ServiceBindingType; import org.collectionspace.services.jaxb.AbstractCommonList; import org.collectionspace.services.nuxeo.util.NuxeoUtils; +import org.jboss.resteasy.spi.ResteasyProviderFactory; -import com.sun.xml.bind.v2.runtime.unmarshaller.XsiNilLoader.Array; /** * RefNameServiceUtils is a collection of services utilities related to refName @@ -223,6 +223,7 @@ public class RefNameServiceUtils { public static AuthorityRefDocList getAuthorityRefDocs( RepositoryInstance repoSession, ServiceContext ctx, + UriTemplateRegistry uriTemplateRegistry, RepositoryClient repoClient, List serviceTypes, String refName, @@ -278,7 +279,7 @@ public class RefNameServiceUtils { // item within any individual document scanned, so the number of // authority references may potentially exceed the total number // of documents scanned. - int nRefsFound = processRefObjsDocList(docList, refName, queriedServiceBindings, authRefFieldsByService, // the actual list size needs to be updated to the size of "list" + int nRefsFound = processRefObjsDocList(docList, ctx.getTenantId(), refName, queriedServiceBindings, authRefFieldsByService, // the actual list size needs to be updated to the size of "list" list, null); commonList.setPageSize(pageSize); @@ -356,7 +357,7 @@ public class RefNameServiceUtils { return refNameServiceTypes; } // Seems like a good value - no real data to set this well. - // Note: can set this value lower; e.g. to 3 during debugging; - ADR 2012-07-10 + // Note: can set this value lower during debugging; e.g. to 3 - ADR 2012-07-10 private static final int N_OBJS_TO_UPDATE_PER_LOOP = 100; public static int updateAuthorityRefDocs( @@ -404,7 +405,7 @@ public class RefNameServiceUtils { morePages = false; } - int nRefsFoundThisPage = processRefObjsDocList(docList, oldRefName, queriedServiceBindings, authRefFieldsByService, + int nRefsFoundThisPage = processRefObjsDocList(docList, ctx.getTenantId(), oldRefName, queriedServiceBindings, authRefFieldsByService, null, newRefName); if (nRefsFoundThisPage > 0) { ((RepositoryJavaClientImpl) repoClient).saveDocListWithoutHandlerProcessing(ctx, repoSession, docList, true); @@ -540,6 +541,7 @@ public class RefNameServiceUtils { */ private static int processRefObjsDocList( DocumentModelList docList, + String tenantId, String refName, Map queriedServiceBindings, Map> authRefFieldsByService, @@ -571,22 +573,22 @@ public class RefNameServiceUtils { ilistItem = new AuthorityRefDocList.AuthorityRefDocItem(); String csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString()); ilistItem.setDocId(csid); - String uri = ""; - // FIXME: Hack for CSPACE-5406; this instead should use the (forthcoming) - // URL pattern-to-Doctype registry described in CSPACE-5471 - ADR 2012-07-18 - if (sb.getType().equalsIgnoreCase(URIUtils.AUTHORITY_SERVICE_CATEGORY)) { - String authoritySvcName = URIUtils.getAuthoritySvcName(docType); - String inAuthorityCsid; + UriTemplateRegistry uriTemplateRegistry = ServiceMain.getInstance().getUriTemplateRegistry(); + StoredValuesUriTemplate storedValuesResourceTemplate = uriTemplateRegistry.get(new UriTemplateRegistryKey(docType, tenantId)); + Map additionalValues = new HashMap(); + if (storedValuesResourceTemplate.getUriTemplateType() == UriTemplateFactory.ITEM) { try { - inAuthorityCsid = (String) docModel.getPropertyValue("inAuthority"); // AuthorityItemJAXBSchema.IN_AUTHORITY - uri = URIUtils.getAuthorityItemUri(authoritySvcName, inAuthorityCsid, csid); + String inAuthorityCsid = (String) docModel.getPropertyValue("inAuthority"); // AuthorityItemJAXBSchema.IN_AUTHORITY + additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, inAuthorityCsid); + additionalValues.put(UriTemplateFactory.ITEM_IDENTIFIER_VAR, csid); } catch (Exception e) { logger.warn("Could not extract inAuthority property from authority item record: " + e.getMessage()); } } else { - uri = URIUtils.getUri(sb.getName(), csid);; + additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, csid); } - ilistItem.setUri(uri); + String uriStr = storedValuesResourceTemplate.buildUri(additionalValues); + ilistItem.setUri(uriStr); try { ilistItem.setWorkflowState(docModel.getCurrentLifeCycleState()); ilistItem.setUpdatedAt(DocHandlerBase.getUpdatedAtAsString(docModel)); -- 2.47.3