Simple verification of the format of the language tag used in DateProcessor. (#25513)
Closes #26186
This commit is contained in:
parent
a827d545d8
commit
f842ff1ae1
core/src/main/java/org/elasticsearch/common/util
modules/ingest-common/src
main/java/org/elasticsearch/ingest/common
test/java/org/elasticsearch/ingest/common
|
@ -101,7 +101,17 @@ public class LocaleUtils {
|
|||
return new Locale(parts[0]);
|
||||
default:
|
||||
throw new IllegalArgumentException("Locales can have at most 3 parts but got " + parts.length + ": " + Arrays.asList(parts));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a {@link Locale} object
|
||||
*/
|
||||
public static boolean isValid(Locale locale) {
|
||||
try {
|
||||
return locale.getISO3Language() != null && locale.getISO3Country() != null;
|
||||
} catch (MissingResourceException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
package org.elasticsearch.ingest.common;
|
||||
|
||||
import org.elasticsearch.ExceptionsHelper;
|
||||
import org.elasticsearch.common.util.LocaleUtils;
|
||||
import org.elasticsearch.ingest.AbstractProcessor;
|
||||
import org.elasticsearch.ingest.ConfigurationUtils;
|
||||
import org.elasticsearch.ingest.IngestDocument;
|
||||
|
@ -125,6 +126,9 @@ public final class DateProcessor extends AbstractProcessor {
|
|||
} catch (IllformedLocaleException e) {
|
||||
throw new IllegalArgumentException("Invalid language tag specified: " + localeString);
|
||||
}
|
||||
if (!LocaleUtils.isValid(locale)) {
|
||||
throw new IllegalArgumentException("Invalid language tag specified: " + localeString);
|
||||
}
|
||||
}
|
||||
List<String> formats = ConfigurationUtils.readList(TYPE, processorTag, config, "formats");
|
||||
return new DateProcessor(processorTag, timezone, locale, field, formats, targetField);
|
||||
|
|
|
@ -95,17 +95,20 @@ public class DateProcessorFactoryTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testParseInvalidLocale() throws Exception {
|
||||
DateProcessor.Factory factory = new DateProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
String sourceField = randomAlphaOfLengthBetween(1, 10);
|
||||
config.put("field", sourceField);
|
||||
config.put("formats", Collections.singletonList("dd/MM/yyyyy"));
|
||||
config.put("locale", "invalid_locale");
|
||||
try {
|
||||
factory.create(null, null, config);
|
||||
fail("should fail with invalid locale");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("Invalid language tag specified: invalid_locale"));
|
||||
String[] locales = new String[] { "invalid_locale", "english" };
|
||||
for (String locale : locales) {
|
||||
DateProcessor.Factory factory = new DateProcessor.Factory();
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
String sourceField = randomAlphaOfLengthBetween(1, 10);
|
||||
config.put("field", sourceField);
|
||||
config.put("formats", Collections.singletonList("dd/MM/yyyyy"));
|
||||
config.put("locale", locale);
|
||||
try {
|
||||
factory.create(null, null, config);
|
||||
fail("should fail with invalid locale");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), equalTo("Invalid language tag specified: " + locale));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue