Polishing KeyValueConverter

This commit is contained in:
sgibb
2024-03-11 22:08:45 -04:00
parent 7f1048a0df
commit df81812e4d
3 changed files with 9 additions and 9 deletions

View File

@@ -27,19 +27,19 @@ import org.springframework.util.StringUtils;
@Deprecated
public class KeyValueConverter implements Converter<String, KeyValue> {
private static final String INVALID_CONFIGURATION_MESSAGE = "Invalid configuration, expected format is: 'key:value'";
private static final String INVALID_CONFIGURATION_MESSAGE = "Invalid configuration, expected format is: 'key:value', received: ";
@Override
public KeyValue convert(String source) throws IllegalArgumentException {
try {
String[] split = source.split(":");
if (source.contains(":") && StringUtils.hasText(split[0])) {
if (split.length == 2) {
return new KeyValue(split[0], split.length == 1 ? "" : split[1]);
}
throw new IllegalArgumentException(INVALID_CONFIGURATION_MESSAGE);
throw new IllegalArgumentException(INVALID_CONFIGURATION_MESSAGE + source);
}
catch (ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException(INVALID_CONFIGURATION_MESSAGE);
throw new IllegalArgumentException(INVALID_CONFIGURATION_MESSAGE + source);
}
}

View File

@@ -24,19 +24,19 @@ import org.springframework.util.StringUtils;
*/
public class KeyValueConverter implements Converter<String, KeyValue> {
private static final String INVALID_CONFIGURATION_MESSAGE = "Invalid configuration, expected format is: 'key:value'";
private static final String INVALID_CONFIGURATION_MESSAGE = "Invalid configuration, expected format is: 'key:value', received: ";
@Override
public KeyValue convert(String source) throws IllegalArgumentException {
try {
String[] split = source.split(":");
if (source.contains(":") && StringUtils.hasText(split[0])) {
if (split.length == 2) {
return new KeyValue(split[0], split.length == 1 ? "" : split[1]);
}
throw new IllegalArgumentException(INVALID_CONFIGURATION_MESSAGE);
throw new IllegalArgumentException(INVALID_CONFIGURATION_MESSAGE + source);
}
catch (ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException(INVALID_CONFIGURATION_MESSAGE);
throw new IllegalArgumentException(INVALID_CONFIGURATION_MESSAGE + source);
}
}

View File

@@ -14,4 +14,4 @@ spring:
- Path=/multivalueheaders
filters:
- AddRequestHeadersIfNotPresent=X-Request-Example:ValueA,X-Request-Second-Example:ValueC
- AddRequestHeadersIfNotPresent=X-Request-Example:ValueB,X-Request-Second-Example:ValueD
- AddRequestHeadersIfNotPresent=X-Request-Example:ValueB, X-Request-Second-Example:ValueD