From df7c88e146b3aec89a5740f17828c22ad3342df7 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Thu, 7 Jun 2012 17:56:11 -0700 Subject: [PATCH] CSPACE-5271: Added UriTemplate subclass in which static values, like path components, can be stored. Created skeletal UriTemplateRegistry. --- .../CollectionSpaceJaxRsApplication.java | 3 + .../common/StoredValuesUriTemplate.java | 76 +++++++++++++++++++ .../services/common/UriTemplate.java | 12 ++- .../services/common/UriTemplateFactory.java | 12 ++- .../services/common/UriTemplateRegistry.java | 34 +++++++++ .../services/common/test/UriTemplateTest.java | 8 +- 6 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java create mode 100644 services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistry.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 7ed9eef6a..85808b5d7 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 @@ -68,6 +68,7 @@ import org.collectionspace.services.common.ResourceBase; import org.collectionspace.services.common.ResourceMap; import org.collectionspace.services.common.ResourceMapHolder; import org.collectionspace.services.common.ResourceMapImpl; +import org.collectionspace.services.common.UriTemplateRegistry; import org.collectionspace.services.common.security.SecurityInterceptor; import org.jboss.resteasy.core.Dispatcher; import org.jboss.resteasy.spi.ResteasyProviderFactory; @@ -89,6 +90,7 @@ public class CollectionSpaceJaxRsApplication extends Application 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() { @@ -140,6 +142,7 @@ public class CollectionSpaceJaxRsApplication extends Application private void addResourceToMapAndSingletons(ResourceBase resource) { singletons.add(resource); resourceMap.put(resource.getServiceName(), resource); + // uriTemplateRegistry.put(resource.getDocType(), resource.getUriTemplate()); } @Override 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 new file mode 100644 index 000000000..451f98559 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java @@ -0,0 +1,76 @@ +/** + * This document is a part of the source code and related artifacts for + * CollectionSpace, an open source collections management system for museums and + * related institutions: + * + * http://www.collectionspace.org http://wiki.collectionspace.org + * + * Copyright © 2009-2012 University of California, Berkeley + * + * Licensed under the Educational Community License (ECL), Version 2.0. You may + * not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * + * https://source.collectionspace.org/collection-space/LICENSE.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.collectionspace.services.common; + +import java.util.HashMap; +import java.util.Map; + +/** + * StoredValuesUriTemplate.java + * + * Generates URI strings by combining a URI template with provided values, which + * replace variables within the template. + * + * In this subclass, 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. + */ +public class StoredValuesUriTemplate extends UriTemplate { + + private Map storedValuesMap = new HashMap(); + + public StoredValuesUriTemplate(String path, Map storedValuesMap) { + super(path); + setStoredValuesMap(storedValuesMap); + } + + private void setStoredValuesMap(Map storedValuesMap) { + if (storedValuesMap != null && !storedValuesMap.isEmpty()) { + this.storedValuesMap = storedValuesMap; + } + } + + private Map getStoredValuesMap() { + return this.storedValuesMap; + } + + /** + * Builds a URI string from a combination of previously-stored values (such + * as static URI path components) and additional values (such as resource + * identifiers), both of which will replace variables within the URI + * template. + * + * @param varsMap a map of values that will replace variables within the URI + * template + * @return a URI string + */ + public String buildUri(Map additionalValuesMap) { + Map allValuesMap = new HashMap(); + allValuesMap.putAll(getStoredValuesMap()); + if (storedValuesMap != null && !storedValuesMap.isEmpty()) { + allValuesMap.putAll(additionalValuesMap); + } + return super.buildUri(allValuesMap); + } +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java b/services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java index bb8cdf746..da0d06d3d 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java +++ b/services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java @@ -5,7 +5,7 @@ * * http://www.collectionspace.org http://wiki.collectionspace.org * - * Copyright 2009-2012 University of California, Berkeley + * Copyright © 2009-2012 University of California, Berkeley * * Licensed under the Educational Community License (ECL), Version 2.0. You may * not use this file except in compliance with this License. @@ -30,6 +30,12 @@ import org.collectionspace.services.common.api.Tools; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * UriTemplate.java + * + * Generates URI strings by combining a URI template with provided values, which + * replace variables within the template. + */ public class UriTemplate { private static final Logger logger = LoggerFactory.getLogger(UriTemplate.class); @@ -76,10 +82,10 @@ public class UriTemplate { return getUriPath(); } - public String buildUri(Map varsMap) { + public String buildUri(Map valuesMap) { URI uri = null; try { - uri = getBuilder().buildFromMap(varsMap); + uri = getBuilder().buildFromMap(valuesMap); } catch (IllegalArgumentException iae) { logger.warn("One or more required parameter values were missing " + "when building URI value via URI Template: " + iae.getMessage()); 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 2764206cf..945892c6a 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 @@ -5,7 +5,7 @@ * * http://www.collectionspace.org http://wiki.collectionspace.org * - * Copyright 2009-2012 University of California, Berkeley + * Copyright © 2009-2012 University of California, Berkeley * * Licensed under the Educational Community License (ECL), Version 2.0. You may * not use this file except in compliance with this License. @@ -22,12 +22,18 @@ */ package org.collectionspace.services.common; +/** + * UriTemplateFactory.java + * + * A factory for building instances of URITemplate classes, based on a provided + * template type. + */ public class UriTemplateFactory { public final static String RESOURCE_TEMPLATE_PATTERN = "/{servicename}/{identifier}"; - // FIXME: Get static strings below (e.g. "items") from already-declared - // constants elsewhere + // FIXME: Get static strings below (e.g. "items", "contacts") from + // already-declared constants elsewhere public final static String ITEM_TEMPLATE_PATTERN = "/{servicename}/{identifier}/items/{itemIdentifier}"; public final static String CONTACT_TEMPLATE_PATTERN = 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 new file mode 100644 index 000000000..7ac6fa8fc --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistry.java @@ -0,0 +1,34 @@ +/** + * This document is a part of the source code and related artifacts for + * CollectionSpace, an open source collections management system for museums and + * related institutions: + * + * http://www.collectionspace.org http://wiki.collectionspace.org + * + * Copyright © 2009-2012 University of California, Berkeley + * + * Licensed under the Educational Community License (ECL), Version 2.0. You may + * not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * + * https://source.collectionspace.org/collection-space/LICENSE.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.collectionspace.services.common; + +import java.util.HashMap; + +/** + * UriTemplateRegistry.java + * + * Maps document types to templates for building URIs, used in turn for + * accessing instances of those documents. + */ +public class UriTemplateRegistry extends HashMap { +} diff --git a/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateTest.java b/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateTest.java index bdc2e4020..3ef8b6888 100644 --- a/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateTest.java +++ b/services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateTest.java @@ -34,7 +34,7 @@ import org.testng.annotations.Test; public class UriTemplateTest { final static String EXAMPLE_SERVICE_NAME = "examples"; - final static String CSID = "a87f6616-4146-4c17-a41a-048597cc12aa"; + final static String EXAMPLE_CSID = "a87f6616-4146-4c17-a41a-048597cc12aa"; private static final Logger logger = LoggerFactory.getLogger(UriTemplateTest.class); private void testBanner(String msg) { @@ -59,13 +59,13 @@ public class UriTemplateTest { UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE); Map resourceUriVars = new HashMap(); resourceUriVars.put("servicename", EXAMPLE_SERVICE_NAME); - resourceUriVars.put("identifier", CSID); + resourceUriVars.put("identifier", EXAMPLE_CSID); String uriStr = resourceTemplate.buildUri(resourceUriVars); Assert.assertFalse(Tools.isBlank(uriStr), "Generated URI string is null or blank."); logger.debug("Generated URI string = " + uriStr); } - @Test(dependsOnMethods = {"createResourceUriTemplate"}) + @Test(dependsOnMethods = {"buildResourceUri"}) public void buildResourceUriWithMissingValue() { testBanner("buildResourceUriWithMissingValue"); UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE); @@ -78,7 +78,7 @@ public class UriTemplateTest { logger.debug("Generated URI string = " + uriStr); } - @Test(dependsOnMethods = {"createResourceUriTemplate"}) + @Test(dependsOnMethods = {"buildResourceUri"}) public void buildResourceUriWithNullValue() { testBanner("buildResourceUriWithNullValue"); UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE); -- 2.47.3