Don't allow empty context keys/values (#688)

This commit is contained in:
Luck
2018-01-15 20:06:20 +00:00
Unverified
parent 8a374aed04
commit ed223f0e4e
3 changed files with 29 additions and 5 deletions
@@ -122,11 +122,33 @@ abstract class AbstractContextSet implements ContextSet {
}
static String sanitizeKey(String key) {
return Objects.requireNonNull(key, "key is null").toLowerCase().intern();
Objects.requireNonNull(key, "key is null");
if (stringIsEmpty(key)) {
throw new IllegalArgumentException("key is (effectively) empty");
}
return key.toLowerCase().intern();
}
static String sanitizeValue(String value) {
return Objects.requireNonNull(value, "value is null").intern();
Objects.requireNonNull(value, "value is null");
if (stringIsEmpty(value)) {
throw new IllegalArgumentException("value is (effectively) empty");
}
return value.intern();
}
private static boolean stringIsEmpty(String s) {
if (s.isEmpty()) {
return true;
}
for (char c : s.toCharArray()) {
if (c != ' ') {
return false;
}
}
return true;
}
}
@@ -51,7 +51,9 @@ import javax.annotation.Nonnull;
* {@link String#toLowerCase() lowercase} by all implementations.
* Values however are case-sensitive.</p>
*
* <p>Context keys and values may not be null.</p>
* <p>Context keys and values may not be null or empty. A key/value will be
* deemed empty if it's length is zero, or if it consists of only space
* characters.</p>
*
* <p>Two default ContextSet implementations are provided.
* {@link MutableContextSet} allows the addition and removal of context keys