LocaleCodeSanitizer implements Sanitizer<String> { @Override public String execute(String content) { if (StringUtils.isBlank(content)) { return content; } Locale locale; try { locale = LocaleUtils.toLocale(content); } catch (Exception e) { return getDefaultLocale(content); } if (!Arrays.asList(Locale.getAvailableLocales()).contains(locale)) { return getDefaultLocale(content); } return locale.toString(); }  @Override String execute(String content);  }
@Test public void testExecute() throws Exception { LocaleCodeSanitizer localeCodeSanitizer = new LocaleCodeSanitizer(); assertEquals("en_US", localeCodeSanitizer.execute("en_US")); assertEquals("en", localeCodeSanitizer.execute("en")); assertEquals("fr_FR", localeCodeSanitizer.execute("fr_FR")); assertEquals("en_US", localeCodeSanitizer.execute("foo")); assertEquals("en_US", localeCodeSanitizer.execute("<script>")); assertEquals("", localeCodeSanitizer.execute("")); assertEquals(" ", localeCodeSanitizer.execute(" ")); assertEquals(null, localeCodeSanitizer.execute(null)); }