Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ibdo :: get parameter as string #1014

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 5 additions & 24 deletions src/main/java/emissary/core/BaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -993,33 +993,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<Object> 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));
}

/**
Expand Down
64 changes: 62 additions & 2 deletions src/main/java/emissary/core/IBaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -350,17 +355,72 @@ 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
*
* @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<String> 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
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/emissary/core/BaseDataObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1463,4 +1463,39 @@ void testNewInputStream() throws IOException {
assertArrayEquals(bytes2, byteArrayOutputStream.toByteArray());
}
}

@Test
void testGetParameterAsString() {
this.b.putParameter("A", 1L);
dev-mlb marked this conversation as resolved.
Show resolved Hide resolved
this.b.appendParameter("A", "TWO");
this.b.appendParameter("A", "THREE");
assertEquals("1", this.b.getParameterAsString("A"));
dev-mlb marked this conversation as resolved.
Show resolved Hide resolved
assertEquals("1;TWO;THREE", this.b.getParameterAsConcatString("A"));

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"));
}

}