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