]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5271: Added additional error checking when manipulating maps of values for...
authorAron Roberts <aron@socrates.berkeley.edu>
Fri, 8 Jun 2012 01:08:51 +0000 (18:08 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Fri, 8 Jun 2012 01:08:51 +0000 (18:08 -0700)
services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java
services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java

index 451f98559b0fb46b3508c20043957774d3c13ec4..dd97ebbf9f2c2d47761ed10ee995efa80ae85a46 100644 (file)
@@ -24,6 +24,8 @@ package org.collectionspace.services.common;
 
 import java.util.HashMap;
 import java.util.Map;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * StoredValuesUriTemplate.java
@@ -31,13 +33,15 @@ import java.util.Map;
  * 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.
+ * 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.
  */
 public class StoredValuesUriTemplate extends UriTemplate {
 
+    private static final Logger logger = LoggerFactory.getLogger(StoredValuesUriTemplate.class);
     private Map<String, String> storedValuesMap = new HashMap<String, String>();
 
     public StoredValuesUriTemplate(String path, Map<String, String> storedValuesMap) {
@@ -61,15 +65,22 @@ public class StoredValuesUriTemplate extends UriTemplate {
      * 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
+     * @param additionalValuesMap 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);
+        try {
+            Map<String,String> storedValuesMap = getStoredValuesMap();
+            if (storedValuesMap != null && !storedValuesMap.isEmpty()) {
+                allValuesMap.putAll(storedValuesMap);
+            }
+            if (additionalValuesMap != null && !additionalValuesMap.isEmpty()) {
+                allValuesMap.putAll(additionalValuesMap);
+            }
+        } catch (Exception e) {
+            logger.warn("Some values could not be added to values map when building URI string: " + e.getMessage());
         }
         return super.buildUri(allValuesMap);
     }
index da0d06d3d63e63f52ea5a86a93ff736755b8446c..5d39d11b8da7474f83dbebc1642cf9f39ffe38f8 100644 (file)
@@ -85,12 +85,15 @@ public class UriTemplate {
     public String buildUri(Map<String, String> valuesMap) {\r
         URI uri = null;\r
         try {\r
+            if (valuesMap == null || valuesMap.isEmpty()) {\r
+                throw new IllegalArgumentException("Map of values for building URI string was null or empty");\r
+            }\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
+            logger.warn("One or more required values were missing "\r
+                    + "when building URI string: " + iae.getMessage());\r
         } catch (UriBuilderException ube) {\r
-            logger.warn("URI value can't be constructed due to state of URIBuilder: " + ube.getMessage());\r
+            logger.warn("URI string can't be constructed due to state of URIBuilder: " + ube.getMessage());\r
         } finally {\r
             if (uri != null) {\r
                 return uri.toString();\r