From: Aron Roberts Date: Fri, 8 Jun 2012 19:26:07 +0000 (-0700) Subject: CSPACE-5271: More work on returning UriTemplates from resource classes, for inclusion... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=ecdeaf07a667af0117119306e96657de720b78c0;p=tmp%2Fjakarta-migration.git CSPACE-5271: More work on returning UriTemplates from resource classes, for inclusion in the registry. --- 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 56eaf47c8..b476f5620 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 @@ -142,9 +142,7 @@ public class CollectionSpaceJaxRsApplication extends Application private void addResourceToMapAndSingletons(ResourceBase resource) { singletons.add(resource); resourceMap.put(resource.getServiceName(), resource); - // FIXME: Accept multiple entries from each resource into the registry. - // See also similar comments in ResourceBase. - uriTemplateRegistry.put(resource.getDocType(), resource.getUriTemplate()); + uriTemplateRegistry.putAll(resource.getUriTemplateMap()); } @Override 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 cdf3b4c8b..f8ca689d5 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 @@ -81,8 +81,11 @@ import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.collectionspace.services.client.*; +import org.collectionspace.services.common.*; import org.collectionspace.services.nuxeo.util.NuxeoUtils; /** @@ -957,4 +960,36 @@ public abstract class AuthorityResource throw bigReThrow(e, "Error showing hierarchy", itemcsid); } } + + public String getItemDocType() { + // FIXME: Proof of concept placeholder + return getServiceName(); + } + + @Override + public Map getUriTemplateMap() { + // Get resource URI template from superclass + Map uriTemplateMap = super.getUriTemplateMap(); + // Add item URI template + String itemDocType = getItemDocType(); + StoredValuesUriTemplate itemUriTemplate = getItemUriTemplate(); + if (itemDocType == null) { + return uriTemplateMap; // return map as obtained from superclass + } + if (itemUriTemplate == null) { + return uriTemplateMap; // return map as obtained from superclass + } + uriTemplateMap.put(itemDocType, itemUriTemplate); + cacheUriTemplateMap(uriTemplateMap); + return uriTemplateMap; + } + + private StoredValuesUriTemplate getItemUriTemplate() { + Map storedValuesMap = new HashMap(); + storedValuesMap.put(UriTemplateFactory.SERVICENAME_VAR, getServiceName()); + StoredValuesUriTemplate template = + UriTemplateFactory.getURITemplate(UriTemplateFactory.ITEM, + storedValuesMap); + return template; + } } 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 8ba0c0cf6..433bc9956 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 @@ -66,6 +66,8 @@ public abstract class ResourceBase public static final String LIST = "list"; //FIXME retrieve client type from configuration static ClientType CLIENT_TYPE; + + Map cachedUriTemplateMap = new HashMap(); /* * REM - 11/14/2011 - I discovered this static block of code and don't understand why it exists. However, a side-effect of this static block is that ServiceMain is trying @@ -393,21 +395,26 @@ public abstract class ResourceBase return getServiceName(); } - // As generally mentioned by Patrick, we will want to cache generated values, and - // only generate one time if the cached value has not yet been populated. - public Map getUriTemplateMap() { + // Return cached copy of URI template map, if available Map uriTemplateMap = new HashMap(); - String docType = getDocType(); - StoredValuesUriTemplate resourceUriTemplate = getResourceUriTemplate(); - if (docType == null) { - return uriTemplateMap; // return an empty map - } - if (resourceUriTemplate == null) { - return uriTemplateMap; // return an empty map + if (this.cachedUriTemplateMap != null && !this.cachedUriTemplateMap.isEmpty()) { + uriTemplateMap.putAll(cachedUriTemplateMap); + return uriTemplateMap; + } else { + // Construct and return a resource URI template as the sole item in the map + String docType = getDocType(); + StoredValuesUriTemplate resourceUriTemplate = getResourceUriTemplate(); + if (docType == null) { + return uriTemplateMap; // return an empty map + } + if (resourceUriTemplate == null) { + return uriTemplateMap; // return an empty map + } + uriTemplateMap.put(docType, resourceUriTemplate); + cacheUriTemplateMap(uriTemplateMap); + return uriTemplateMap; } - uriTemplateMap.put(docType, resourceUriTemplate); - return uriTemplateMap; } private StoredValuesUriTemplate getResourceUriTemplate() { @@ -419,4 +426,8 @@ public abstract class ResourceBase return template; } + public void cacheUriTemplateMap(Map uriTemplateMap) { + this.cachedUriTemplateMap = uriTemplateMap; + } + } diff --git a/services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java b/services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java index 47efe1f75..2769945fa 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java +++ b/services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java @@ -35,10 +35,10 @@ import org.slf4j.LoggerFactory; * replace variables within the template. * * In this subclass of UriTemplate, some of the values which will replace - * variables in a URI template are static, and can be stored alongside the - * template for reuse. Additional values that will replace variables within the - * template are also dynamically accepted, and are merged with stored values - * when building URIs. + * variables in an internal URI template are static, and can be stored alongside + * the URI template for reuse. Additional values that will replace variables + * within the template are also dynamically accepted, and will be merged with + * stored values when building URIs. */ public class StoredValuesUriTemplate extends UriTemplate { @@ -54,13 +54,14 @@ public class StoredValuesUriTemplate extends UriTemplate { setStoredValuesMap(storedValuesMap); } - final public void setStoredValuesMap(Map storedValuesMap) { + // Private access ensures that stored values can only be set via a constructor + private void setStoredValuesMap(Map storedValuesMap) { if (storedValuesMap != null && !storedValuesMap.isEmpty()) { this.storedValuesMap = storedValuesMap; } } - private Map getStoredValuesMap() { + public Map getStoredValuesMap() { return this.storedValuesMap; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateFactory.java b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateFactory.java index 757558161..72dedc4f1 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateFactory.java +++ b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateFactory.java @@ -64,7 +64,7 @@ public class UriTemplateFactory { + "/contacts/" + "{" + CONTACT_IDENTIFIER_VAR + "}"; - public static StoredValuesUriTemplate getURITemplate(UriTemplateType type) { + public static UriTemplate getURITemplate(UriTemplateType type) { return new StoredValuesUriTemplate(type, getUriPathPattern(type)); } 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 cd2c047a0..fa381b66c 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 @@ -23,6 +23,8 @@ */ package org.collectionspace.services.contact; +import java.util.HashMap; +import java.util.Map; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -41,6 +43,8 @@ import javax.ws.rs.core.UriInfo; import org.collectionspace.services.client.IQueryManager; import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; +import org.collectionspace.services.common.StoredValuesUriTemplate; +import org.collectionspace.services.common.UriTemplateFactory; import org.collectionspace.services.common.vocabulary.AuthorityResource; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentFilter; @@ -294,4 +298,36 @@ public abstract class AuthorityResourceWithContacts + ": and item:" + itemspecifier + ": was not found.", csid); } } + + public String getContactDocType() { + // FIXME: Proof of concept placeholder + return getServiceName(); + } + + @Override + public Map getUriTemplateMap() { + // Get resource and item URI templates from superclass + Map uriTemplateMap = super.getUriTemplateMap(); + // Add item URI template + String contactDocType = getContactDocType(); + StoredValuesUriTemplate itemUriTemplate = getItemUriTemplate(); + if (contactDocType == null) { + return uriTemplateMap; // return map as obtained from superclass + } + if (itemUriTemplate == null) { + return uriTemplateMap; // return map as obtained from superclass + } + uriTemplateMap.put(contactDocType, itemUriTemplate); + cacheUriTemplateMap(uriTemplateMap); + return uriTemplateMap; + } + + private StoredValuesUriTemplate getItemUriTemplate() { + Map storedValuesMap = new HashMap(); + storedValuesMap.put(UriTemplateFactory.SERVICENAME_VAR, getServiceName()); + StoredValuesUriTemplate template = + UriTemplateFactory.getURITemplate(UriTemplateFactory.ITEM, + storedValuesMap); + return template; + } }