Simple verification of the format of the language tag used in DateProcessor. (#25513)

Closes #26186
This commit is contained in:
Stuart Neivandt 2017-08-28 03:59:00 -05:00 committed by Adrien Grand
parent a827d545d8
commit f842ff1ae1
3 changed files with 29 additions and 12 deletions

View File

@ -104,4 +104,14 @@ public class LocaleUtils {
} }
} }
/**
* Validate a {@link Locale} object
*/
public static boolean isValid(Locale locale) {
try {
return locale.getISO3Language() != null && locale.getISO3Country() != null;
} catch (MissingResourceException e) {
return false;
}
}
} }

View File

@ -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);

View File

@ -95,17 +95,20 @@ public class DateProcessorFactoryTests extends ESTestCase {
} }
public void testParseInvalidLocale() throws Exception { public void testParseInvalidLocale() throws Exception {
String[] locales = new String[] { "invalid_locale", "english" };
for (String locale : locales) {
DateProcessor.Factory factory = new DateProcessor.Factory(); DateProcessor.Factory factory = new DateProcessor.Factory();
Map<String, Object> config = new HashMap<>(); Map<String, Object> config = new HashMap<>();
String sourceField = randomAlphaOfLengthBetween(1, 10); String sourceField = randomAlphaOfLengthBetween(1, 10);
config.put("field", sourceField); config.put("field", sourceField);
config.put("formats", Collections.singletonList("dd/MM/yyyyy")); config.put("formats", Collections.singletonList("dd/MM/yyyyy"));
config.put("locale", "invalid_locale"); config.put("locale", locale);
try { try {
factory.create(null, null, config); factory.create(null, null, config);
fail("should fail with invalid locale"); fail("should fail with invalid locale");
} catch(IllegalArgumentException e) { } catch(IllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("Invalid language tag specified: invalid_locale")); assertThat(e.getMessage(), equalTo("Invalid language tag specified: " + locale));
}
} }
} }