From f39ff44f65fc7d6dd74962e73026b3450fe90531 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Fri, 10 Aug 2012 16:31:36 -0700 Subject: [PATCH] CSPACE-5271: URI template registry now returns templates retrievable via a composite key of template ID and document type. --- .../CollectionSpaceJaxRsApplication.java | 13 ++-- .../common/vocabulary/AuthorityResource.java | 20 +++--- .../services/common/ResourceBase.java | 66 ++++++++++++------- .../services/common/UriTemplateRegistry.java | 32 ++++++--- .../AuthorityResourceWithContacts.java | 27 +++----- 5 files changed, 88 insertions(+), 70 deletions(-) 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 6910ea3f4..313693ecd 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 @@ -87,7 +87,7 @@ public class CollectionSpaceJaxRsApplication extends Application private Set singletons = new HashSet(); private Set> empty = new HashSet>(); private ResourceMap resourceMap = new ResourceMapImpl(); - private static UriTemplateRegistry uriTemplateRegistry = new UriTemplateRegistry(); + private UriTemplateRegistry uriTemplateRegistry = new UriTemplateRegistry(); private ServletContext servletContext = null; public CollectionSpaceJaxRsApplication() { @@ -159,9 +159,9 @@ public class CollectionSpaceJaxRsApplication extends Application ResourceMap resources = getResourceMap(); for (Map.Entry entry : resources.entrySet()) { resource = entry.getValue(); - System.out.println(resource.getServiceName()); // for debugging - getUriTemplateRegistry().putAll(resource.getUriRegistryEntries()); - getUriTemplateRegistry().dump(); // for debugging + 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 @@ -189,6 +189,10 @@ public class CollectionSpaceJaxRsApplication extends Application return resourceMap; } + private void setUriTemplateRegistry(UriTemplateRegistry registry) { + this.uriTemplateRegistry = registry; + } + public UriTemplateRegistry getUriTemplateRegistry() { return uriTemplateRegistry; } @@ -200,5 +204,6 @@ public class CollectionSpaceJaxRsApplication extends Application public ServletContext getServletContext() { return this.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 37aa1ae66..e5b70c6fa 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 @@ -964,28 +964,24 @@ public abstract class AuthorityResource protected String getItemDocType(String tenantId) { return super.getDocType(tenantId, getItemServiceName()); } - + /** - * Constructs and returns a map of URI templates for the current resource, - * for the specified tenant + * Constructs and returns a map of URI templates for the current resource. + * This map assumes that there will be only one URI template of a given type + * ("resource", "item", etc.) for each resource. * - * @param tenantId a tenant ID - * @return a map of URI templates for the current resource, for the specified tenant + * @return a map of URI templates for the current resource */ @Override - protected Map getUriTemplateMap(String tenantId) { + protected Map getUriTemplateMap() { // Get the resource URI template from the superclass - Map uriTemplateMap = super.getUriTemplateMap(tenantId); + Map uriTemplateMap = super.getUriTemplateMap(); // Add the item URI template here, and return both templates in the map - String itemDocType = getItemDocType(tenantId); - if (itemDocType == null) { - return uriTemplateMap; // return map as obtained from superclass - } StoredValuesUriTemplate itemUriTemplate = getUriTemplate(UriTemplateFactory.ITEM); if (itemUriTemplate == null) { return uriTemplateMap; // return map as obtained from superclass } - uriTemplateMap.put(itemDocType, itemUriTemplate); + uriTemplateMap.put(itemUriTemplate.getUriTemplateType(), itemUriTemplate); return uriTemplateMap; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java b/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java index 0a3ba97f4..04b919f49 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java @@ -27,6 +27,8 @@ import java.util.*; import org.collectionspace.services.client.IQueryManager; import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; +import org.collectionspace.services.common.UriTemplateFactory.UriTemplateType; +import org.collectionspace.services.common.UriTemplateRegistryKey; import org.collectionspace.services.common.api.RefName; import org.collectionspace.services.common.api.Tools; import org.collectionspace.services.common.authorityref.AuthorityRefList; @@ -417,13 +419,25 @@ public abstract class ResourceBase return getDocType(tenantId, getServiceName()); } + /** + * Returns the Nuxeo document type associated with a specified service, within a specified tenant. + * + * @param tenantId a tenant ID + * @param serviceName a service name + * @return the Nuxeo document type associated with that service and tenant. + */ // FIXME: This method may properly belong in a different services package or class. + // Also, we need to check for any existing methods that may duplicate this one. protected String getDocType(String tenantId, String serviceName) { String docType = ""; - if (Tools.notBlank(tenantId)) { - ServiceBindingType sb = getTenantBindingsReader().getServiceBinding(tenantId, serviceName); - docType = sb.getObject().getName(); // Reads the Nuxeo Document Type from tenant bindings configuration + if (Tools.isBlank(tenantId)) { + return docType; } + ServiceBindingType sb = getTenantBindingsReader().getServiceBinding(tenantId, serviceName); + if (sb == null) { + return docType; + } + docType = sb.getObject().getName(); // Reads the Nuxeo Document Type from tenant bindings configuration return docType; } @@ -433,47 +447,48 @@ public abstract class ResourceBase * * @return a map of URI templates for the current resource, for all tenants */ - public HashMap> getUriRegistryEntries() { - HashMap> uriRegistryEntriesMap = - new HashMap>(); + public Map> getUriRegistryEntries() { + Map> uriRegistryEntriesMap = + new HashMap>(); List tenantIds = getTenantIds(); + UriTemplateRegistryKey key; + String docType = ""; for (String tenantId : tenantIds) { - uriRegistryEntriesMap.put(tenantId, getUriTemplateMap(tenantId)); + docType = getDocType(tenantId); + if (Tools.notBlank(docType)) { + key = new UriTemplateRegistryKey(); + key.setTenantId(tenantId); + key.setDocType(docType); + uriRegistryEntriesMap.put(key, getUriTemplateMap()); + } } return uriRegistryEntriesMap; } /** - * Constructs and returns a map of URI templates for the current resource, - * for the specified tenant + * Constructs and returns a map of URI templates for the current resource. + * This map assumes that there will be only one URI template of a given type + * ("reesource", "item", etc.) for each resource. * - * @param tenantId a tenant ID - * @return a map of URI templates for the current resource, for the specified tenant + * @return a map of URI templates for the current resource */ - protected Map getUriTemplateMap(String tenantId) { - Map uriTemplateMap = new HashMap(); - if (tenantId == null) { - return uriTemplateMap; // return an empty map - } - String docType = getDocType(tenantId); - if (docType == null) { - return uriTemplateMap; // return an empty map - } + protected Map getUriTemplateMap() { + Map uriTemplateMap = new HashMap(); StoredValuesUriTemplate resourceUriTemplate = getUriTemplate(UriTemplateFactory.RESOURCE); if (resourceUriTemplate == null) { return uriTemplateMap; // return an empty map } - uriTemplateMap.put(docType, resourceUriTemplate); + uriTemplateMap.put(resourceUriTemplate.getUriTemplateType(), resourceUriTemplate); return uriTemplateMap; } /** - * Returns a UriTemplate of the appropriate type, populated with the + * Returns a URI template of the appropriate type, populated with the * current service name as one of its stored values. - * - * @param type a UriTemplate type - * @return a UriTemplate of the appropriate type + * * + * @param type a URI template type + * @return a URI template of the appropriate type. */ protected StoredValuesUriTemplate getUriTemplate(UriTemplateFactory.UriTemplateType type) { Map storedValuesMap = new HashMap(); @@ -489,6 +504,7 @@ public abstract class ResourceBase * @return a list of tenant IDs */ // FIXME: This method may properly belong in a different services package or class. + // Also, we need to check for any existing methods that may duplicate this one. protected List getTenantIds() { List tenantIds = new ArrayList(); String tenantId; diff --git a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistry.java b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistry.java index 3929120e0..968554152 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistry.java +++ b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistry.java @@ -24,24 +24,36 @@ package org.collectionspace.services.common; import java.util.HashMap; import java.util.Map; +import java.util.Set; +import org.collectionspace.services.common.UriTemplateRegistryKey; +import org.collectionspace.services.common.UriTemplateFactory.UriTemplateType; /** * UriTemplateRegistry.java * * Maps document types to templates for building URIs, per tenant. */ -public class UriTemplateRegistry extends HashMap> { +public class UriTemplateRegistry extends HashMap> { - // For debugging + /** + * Dump all registry settings, For debugging purposes. + */ public void dump() { - for (String tenantId : this.keySet()) { - System.out.println("###############################"); - System.out.println("Tenant ID = " + tenantId); - System.out.println("###############################"); - for (Map.Entry uriTemplateEntry : this.get(tenantId).entrySet()) { - System.out.println("Key = " + uriTemplateEntry.getKey() - + ", Value = " + uriTemplateEntry.getValue().getUriTemplateType() - + " : " + uriTemplateEntry.getValue().toString()); + for (Map.Entry> uriTemplateEntry : this.entrySet()) { + + System.out.println( + "Tenant : DocType = " + + uriTemplateEntry.getKey().getTenantId() + + " : " + + uriTemplateEntry.getKey().getDocType()); + + System.out.println(" Value(s) of TemplateType : Template = "); + for (Map.Entry template : uriTemplateEntry.getValue().entrySet()) { + System.out.println( + " " + + template.getKey() + + " : " + + template.getValue().toString()); } } } 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 fc581dd27..2bc3eb136 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 @@ -300,35 +300,24 @@ public abstract class AuthorityResourceWithContacts protected String getContactDocType() { return ContactConstants.NUXEO_DOCTYPE; } - + /** - * Constructs and returns a map of URI templates for the current resource, - * for the specified tenant + * Constructs and returns a map of URI templates for the current resource. + * This map assumes that there will be only one URI template of a given type + * ("resource", "item", "contacts" etc.) for each resource. * - * @param tenantId a tenant ID - * @return a map of URI templates for the current resource, for the specified tenant + * @return a map of URI templates for the current resource */ - // FIXME: This method currently populates only one entry in the UriTemplateRegistry - // for the Contacts docType, even though contacts can be a sub-resource of - // multiple authority item resources. (This method may be called more than once, - // but each time the existing item with the same key in the map is overwritten.) @Override - protected Map getUriTemplateMap(String tenantId) { + protected Map getUriTemplateMap() { // Get the resource and item URI templates from the superclass - Map uriTemplateMap = super.getUriTemplateMap(tenantId); + Map uriTemplateMap = super.getUriTemplateMap(); // Add the contact URI template here, and return all three templates in the map - String contactDocType = getContactDocType(); - if (contactDocType == null) { - return uriTemplateMap; // return map as obtained from superclass - } StoredValuesUriTemplate contactUriTemplate = getUriTemplate(UriTemplateFactory.CONTACT); if (contactUriTemplate == null) { return uriTemplateMap; // return map as obtained from superclass } - // Remove any service name value stored in the template, as the service name - // for contact resources will vary, and must be provided at URI build time - contactUriTemplate.getStoredValuesMap().put(UriTemplateFactory.SERVICENAME_VAR, ""); - uriTemplateMap.put(contactDocType, contactUriTemplate); + uriTemplateMap.put(contactUriTemplate.getUriTemplateType(), contactUriTemplate); return uriTemplateMap; } -- 2.47.3