From: Aron Roberts Date: Fri, 8 Jun 2012 01:08:51 +0000 (-0700) Subject: CSPACE-5271: Added additional error checking when manipulating maps of values for... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=9369c038f744450000e4a54a10b68bea9b6dc86d;p=tmp%2Fjakarta-migration.git CSPACE-5271: Added additional error checking when manipulating maps of values for constructing URI strings. --- 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 451f98559..dd97ebbf9 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 @@ -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 storedValuesMap = new HashMap(); public StoredValuesUriTemplate(String path, Map 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 additionalValuesMap) { Map allValuesMap = new HashMap(); - allValuesMap.putAll(getStoredValuesMap()); - if (storedValuesMap != null && !storedValuesMap.isEmpty()) { - allValuesMap.putAll(additionalValuesMap); + try { + Map 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); } 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 da0d06d3d..5d39d11b8 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 @@ -85,12 +85,15 @@ public class UriTemplate { public String buildUri(Map valuesMap) { URI uri = null; try { + if (valuesMap == null || valuesMap.isEmpty()) { + throw new IllegalArgumentException("Map of values for building URI string was null or empty"); + } 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()); + logger.warn("One or more required values were missing " + + "when building URI string: " + iae.getMessage()); } catch (UriBuilderException ube) { - logger.warn("URI value can't be constructed due to state of URIBuilder: " + ube.getMessage()); + logger.warn("URI string can't be constructed due to state of URIBuilder: " + ube.getMessage()); } finally { if (uri != null) { return uri.toString();