diff --git a/src/main/java/emissary/core/BaseDataObject.java b/src/main/java/emissary/core/BaseDataObject.java index 859b44b028..e390e27f89 100755 --- a/src/main/java/emissary/core/BaseDataObject.java +++ b/src/main/java/emissary/core/BaseDataObject.java @@ -965,33 +965,14 @@ public boolean appendUniqueParameter(final String key, final CharSequence value) return true; } - @Override - public String getStringParameter(final String key) { - return getStringParameter(key, DEFAULT_PARAM_SEPARATOR); - } - @Nullable @Override - public String getStringParameter(final String key, final String sep) { - final List obj = getParameter(key); - if (obj == null) { - return null; - } else if (obj.isEmpty()) { - return null; - } else if ((obj.size() == 1) && (obj.get(0) instanceof String)) { - return (String) obj.get(0); - } else if ((obj.size() == 1) && (obj.get(0) == null)) { - return null; - } else { - final StringBuilder sb = new StringBuilder(); - for (final Object item : obj) { - if (sb.length() > 0) { - sb.append(sep); - } - sb.append(item); - } - return sb.toString(); + public String getParameterAsString(final String key) { + final var obj = getParameterAsStrings(key); + if (obj.size() > 1) { + logger.warn("Multiple values for parameter, returning the first - parameter:{}, number of values:{}", key, obj.size()); } + return StringUtils.trimToNull(obj.stream().findFirst().orElse(null)); } /** diff --git a/src/main/java/emissary/core/IBaseDataObject.java b/src/main/java/emissary/core/IBaseDataObject.java index a8e68cb53e..f925b77d94 100755 --- a/src/main/java/emissary/core/IBaseDataObject.java +++ b/src/main/java/emissary/core/IBaseDataObject.java @@ -3,15 +3,20 @@ import emissary.core.channels.SeekableByteChannelFactory; import emissary.directory.DirectoryEntry; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; + import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.time.Instant; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; public interface IBaseDataObject { @@ -348,8 +353,12 @@ enum MergePolicy { * * @param key name of the metadata element * @return the string value or null if no such element + * @deprecated use {@link #getParameterAsConcatString(String)} */ - String getStringParameter(String key); + @Deprecated + default String getStringParameter(final String key) { + return getParameterAsConcatString(key); + } /** * Retrieve a specified metadata element as a string value @@ -357,8 +366,59 @@ enum MergePolicy { * @param key name of the metadata element * @param sep the separator for multivalued fields * @return the string value or null if no such element + * @deprecated use {@link #getParameterAsConcatString(String, String)} */ - String getStringParameter(String key, String sep); + @Deprecated + default String getStringParameter(final String key, final String sep) { + return getParameterAsConcatString(key, sep); + } + + /** + * Retrieve the Collection of metadata values identified by key where each element is converted to a string + * + * @param key name of the metadata element collection + * @return Collection of elements converted to strings + */ + default Collection getParameterAsStrings(final String key) { + final var obj = getParameter(key); + if (CollectionUtils.isEmpty(obj) || ((obj.size() == 1) && (obj.get(0) == null))) { + return Collections.emptyList(); + } else if ((obj.size() == 1) && (obj.get(0) instanceof String)) { + return Collections.singletonList((String) obj.get(0)); + } else { + return obj.stream().map(String::valueOf).collect(Collectors.toList()); + } + } + + /** + * Retrieve the metadata value identified by key where the element is converted to a string + * + * @param key name of the metadata element + * @return parameter converted to strings + */ + String getParameterAsString(String key); + + /** + * Retrieve a specified metadata element as a string of concatenated values + * + * @param key name of the metadata element + * @return the string value or null if no such element + */ + default String getParameterAsConcatString(final String key) { + return getParameterAsConcatString(key, DEFAULT_PARAM_SEPARATOR); + } + + /** + * Retrieve a specified metadata element as a string of concatenated values + * + * @param key name of the metadata element + * @param sep the separator for multivalued fields + * @return the string value or null if no such element + */ + default String getParameterAsConcatString(final String key, final String sep) { + final var strParameter = String.join(sep, getParameterAsStrings(key)); + return StringUtils.isBlank(strParameter) ? null : strParameter; + } /** * Retrieve all the metadata elements of this object diff --git a/src/test/java/emissary/core/BaseDataObjectTest.java b/src/test/java/emissary/core/BaseDataObjectTest.java index 2261e6a541..0b3b503298 100755 --- a/src/test/java/emissary/core/BaseDataObjectTest.java +++ b/src/test/java/emissary/core/BaseDataObjectTest.java @@ -1333,4 +1333,46 @@ void testNewInputStream() throws IOException { assertArrayEquals(bytes2, byteArrayOutputStream.toByteArray()); } } + + @Test + void testGetParameterAsString() throws IOException { + assertNotNull(this.b); + + try (LogbackTester logbackTester = new LogbackTester(BaseDataObject.class.getName())) { + this.b.putParameter("A", 1L); + this.b.appendParameter("A", "TWO"); + this.b.appendParameter("A", "THREE"); + assertEquals("1", this.b.getParameterAsString("A")); + assertEquals("1;TWO;THREE", this.b.getParameterAsConcatString("A")); + LogbackTester.SimplifiedLogEvent logEvent = new LogbackTester.SimplifiedLogEvent(Level.WARN, + "Multiple values for parameter, returning the first - parameter:A, number of values:3", null); + logbackTester.checkLogList(Collections.singletonList(logEvent)); + } + + this.b.putParameter("A", 2L); + assertEquals("2", this.b.getParameterAsString("A")); + assertEquals("2", this.b.getParameterAsConcatString("A")); + + this.b.putParameter("A", "THREE"); + assertEquals("THREE", this.b.getParameterAsString("A")); + assertEquals("THREE", this.b.getParameterAsConcatString("A")); + + this.b.putParameter("A", null); + assertNull(this.b.getParameterAsString("A")); + assertNull(this.b.getParameterAsConcatString("A")); + + this.b.putParameter("A", ""); + assertNull(this.b.getParameterAsString("A")); + assertNull(this.b.getParameterAsConcatString("A")); + + assertNull(this.b.getParameterAsString("DNE")); + assertNull(this.b.getParameterAsConcatString("DNE")); + + this.b.putParameter("A", null); + this.b.appendParameter("A", "FOUR"); + this.b.appendParameter("A", " "); + assertEquals("null", this.b.getParameterAsString("A")); + assertEquals("null;FOUR; ", this.b.getParameterAsConcatString("A")); + } + }