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

Closes 
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
core/src/main/java/org/elasticsearch/common/util
modules/ingest-common/src
main/java/org/elasticsearch/ingest/common
test/java/org/elasticsearch/ingest/common

View File

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

View File

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

View File

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