]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5271: Added UriTemplate subclass in which static values, like path components...
authorAron Roberts <aron@socrates.berkeley.edu>
Fri, 8 Jun 2012 00:56:11 +0000 (17:56 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Fri, 8 Jun 2012 00:56:11 +0000 (17:56 -0700)
services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java
services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java [new file with mode: 0644]
services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java
services/common/src/main/java/org/collectionspace/services/common/UriTemplateFactory.java
services/common/src/main/java/org/collectionspace/services/common/UriTemplateRegistry.java [new file with mode: 0644]
services/common/src/test/java/org/collectionspace/services/common/test/UriTemplateTest.java

index 7ed9eef6a48f44bd8a56b7335069fc3d21fb4264..85808b5d74f8e05b78bf2903b1e178e50c2ca793 100644 (file)
@@ -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<Object> singletons = new HashSet<Object>();
     private Set<Class<?>> empty = new HashSet<Class<?>>();    
     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 (file)
index 0000000..451f985
--- /dev/null
@@ -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<String, String> storedValuesMap = new HashMap<String, String>();
+
+    public StoredValuesUriTemplate(String path, Map<String, String> storedValuesMap) {
+        super(path);
+        setStoredValuesMap(storedValuesMap);
+    }
+
+    private void setStoredValuesMap(Map<String, String> storedValuesMap) {
+        if (storedValuesMap != null && !storedValuesMap.isEmpty()) {
+            this.storedValuesMap = storedValuesMap;
+        }
+    }
+
+    private Map<String, String> 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<String, String> additionalValuesMap) {
+        Map<String, String> allValuesMap = new HashMap<String, String>();
+        allValuesMap.putAll(getStoredValuesMap());
+        if (storedValuesMap != null && !storedValuesMap.isEmpty()) {
+            allValuesMap.putAll(additionalValuesMap);
+        }
+        return super.buildUri(allValuesMap);
+    }
+}
index bb8cdf7460e9704981ca73ac1c5a308240218ba8..da0d06d3d63e63f52ea5a86a93ff736755b8446c 100644 (file)
@@ -5,7 +5,7 @@
  *\r
  * http://www.collectionspace.org http://wiki.collectionspace.org\r
  *\r
- * Copyright 2009-2012 University of California, Berkeley\r
+ * Copyright © 2009-2012 University of California, Berkeley\r
  *\r
  * Licensed under the Educational Community License (ECL), Version 2.0. You may\r
  * not use this file except in compliance with this License.\r
@@ -30,6 +30,12 @@ import org.collectionspace.services.common.api.Tools;
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
+/**\r
+ * UriTemplate.java\r
+ *\r
+ * Generates URI strings by combining a URI template with provided values, which\r
+ * replace variables within the template.\r
+ */\r
 public class UriTemplate {\r
 \r
     private static final Logger logger = LoggerFactory.getLogger(UriTemplate.class);\r
@@ -76,10 +82,10 @@ public class UriTemplate {
         return getUriPath();\r
     }\r
 \r
-    public String buildUri(Map<String, String> varsMap) {\r
+    public String buildUri(Map<String, String> valuesMap) {\r
         URI uri = null;\r
         try {\r
-            uri = getBuilder().buildFromMap(varsMap);\r
+            uri = getBuilder().buildFromMap(valuesMap);\r
         } catch (IllegalArgumentException iae) {\r
             logger.warn("One or more required parameter values were missing "\r
                     + "when building URI value via URI Template: " + iae.getMessage());\r
index 2764206cfc7411fb4a833bee5680c87dc381dd27..945892c6a89e0f07e7335bb086a0b45b2ce3d56b 100644 (file)
@@ -5,7 +5,7 @@
  *\r
  * http://www.collectionspace.org http://wiki.collectionspace.org\r
  *\r
- * Copyright 2009-2012 University of California, Berkeley\r
+ * Copyright © 2009-2012 University of California, Berkeley\r
  *\r
  * Licensed under the Educational Community License (ECL), Version 2.0. You may\r
  * not use this file except in compliance with this License.\r
  */\r
 package org.collectionspace.services.common;\r
 \r
+/**\r
+ * UriTemplateFactory.java\r
+ *\r
+ * A factory for building instances of URITemplate classes, based on a provided\r
+ * template type.\r
+ */\r
 public class UriTemplateFactory {\r
 \r
     public final static String RESOURCE_TEMPLATE_PATTERN =\r
             "/{servicename}/{identifier}";\r
-    // FIXME: Get static strings below (e.g. "items") from already-declared\r
-    // constants elsewhere\r
+    // FIXME: Get static strings below (e.g. "items", "contacts") from\r
+    // already-declared constants elsewhere\r
     public final static String ITEM_TEMPLATE_PATTERN =\r
             "/{servicename}/{identifier}/items/{itemIdentifier}";\r
     public final static String CONTACT_TEMPLATE_PATTERN =\r
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 (file)
index 0000000..7ac6fa8
--- /dev/null
@@ -0,0 +1,34 @@
+/**\r
+ * This document is a part of the source code and related artifacts for\r
+ * CollectionSpace, an open source collections management system for museums and\r
+ * related institutions:\r
+ *\r
+ * http://www.collectionspace.org http://wiki.collectionspace.org\r
+ *\r
+ * Copyright © 2009-2012 University of California, Berkeley\r
+ *\r
+ * Licensed under the Educational Community License (ECL), Version 2.0. You may\r
+ * not use this file except in compliance with this License.\r
+ *\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+ *\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\r
+ * License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+package org.collectionspace.services.common;\r
+\r
+import java.util.HashMap;\r
+\r
+/**\r
+ * UriTemplateRegistry.java\r
+ *\r
+ * Maps document types to templates for building URIs, used in turn for\r
+ * accessing instances of those documents.\r
+ */\r
+public class UriTemplateRegistry extends HashMap<String, StoredValuesUriTemplate> {\r
+}\r
index bdc2e4020fae516535f2a0ebc196c417355083ab..3ef8b688838596a64641ff8bc4adbe568612e27f 100644 (file)
@@ -34,7 +34,7 @@ import org.testng.annotations.Test;
 public class UriTemplateTest {\r
 \r
     final static String EXAMPLE_SERVICE_NAME = "examples";\r
-    final static String CSID = "a87f6616-4146-4c17-a41a-048597cc12aa";\r
+    final static String EXAMPLE_CSID = "a87f6616-4146-4c17-a41a-048597cc12aa";\r
     private static final Logger logger = LoggerFactory.getLogger(UriTemplateTest.class);\r
 \r
     private void testBanner(String msg) {\r
@@ -59,13 +59,13 @@ public class UriTemplateTest {
         UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE);\r
         Map<String, String> resourceUriVars = new HashMap<String, String>();\r
         resourceUriVars.put("servicename", EXAMPLE_SERVICE_NAME);\r
-        resourceUriVars.put("identifier", CSID);\r
+        resourceUriVars.put("identifier", EXAMPLE_CSID);\r
         String uriStr = resourceTemplate.buildUri(resourceUriVars);\r
         Assert.assertFalse(Tools.isBlank(uriStr), "Generated URI string is null or blank.");\r
         logger.debug("Generated URI string = " + uriStr);\r
     }\r
 \r
-    @Test(dependsOnMethods = {"createResourceUriTemplate"})\r
+    @Test(dependsOnMethods = {"buildResourceUri"})\r
     public void buildResourceUriWithMissingValue() {\r
         testBanner("buildResourceUriWithMissingValue");\r
         UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE);\r
@@ -78,7 +78,7 @@ public class UriTemplateTest {
         logger.debug("Generated URI string = " + uriStr);\r
     }\r
 \r
-    @Test(dependsOnMethods = {"createResourceUriTemplate"})\r
+    @Test(dependsOnMethods = {"buildResourceUri"})\r
     public void buildResourceUriWithNullValue() {\r
         testBanner("buildResourceUriWithNullValue");\r
         UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE);\r