fixed an issue where missing locales would generate a lot of ERROR level log messages
parent
64fbee7935
commit
0b531a0fd3
|
@ -16,7 +16,20 @@
|
||||||
|
|
||||||
package org.mitre.openid.connect.config;
|
package org.mitre.openid.connect.config;
|
||||||
|
|
||||||
|
import com.google.common.base.Splitter;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonIOException;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
|
import com.google.gson.JsonSyntaxException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.support.AbstractMessageSource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
|
@ -27,25 +40,11 @@ import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.support.AbstractMessageSource;
|
|
||||||
import org.springframework.core.io.Resource;
|
|
||||||
|
|
||||||
import com.google.common.base.Splitter;
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonIOException;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import com.google.gson.JsonParser;
|
|
||||||
import com.google.gson.JsonSyntaxException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author jricher
|
* @author jricher
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class JsonMessageSource extends AbstractMessageSource {
|
public class JsonMessageSource extends AbstractMessageSource {
|
||||||
// Logger for this class
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(JsonMessageSource.class);
|
private static final Logger logger = LoggerFactory.getLogger(JsonMessageSource.class);
|
||||||
|
|
||||||
private Resource baseDirectory;
|
private Resource baseDirectory;
|
||||||
|
@ -54,9 +53,13 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
|
|
||||||
private Map<Locale, List<JsonObject>> languageMaps = new HashMap<>();
|
private Map<Locale, List<JsonObject>> languageMaps = new HashMap<>();
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ConfigurationPropertiesBean config;
|
private ConfigurationPropertiesBean config;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public JsonMessageSource(ConfigurationPropertiesBean config) {
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected MessageFormat resolveCode(String code, Locale locale) {
|
protected MessageFormat resolveCode(String code, Locale locale) {
|
||||||
|
|
||||||
|
@ -82,9 +85,6 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a value from the set of maps, taking the first match in order
|
* Get a value from the set of maps, taking the first match in order
|
||||||
* @param code
|
|
||||||
* @param langs
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
private String getValue(String code, List<JsonObject> langs) {
|
private String getValue(String code, List<JsonObject> langs) {
|
||||||
if (langs == null || langs.isEmpty()) {
|
if (langs == null || langs.isEmpty()) {
|
||||||
|
@ -106,10 +106,6 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a value from a single map
|
* Get a value from a single map
|
||||||
* @param code
|
|
||||||
* @param locale
|
|
||||||
* @param lang
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
private String getValue(String code, JsonObject lang) {
|
private String getValue(String code, JsonObject lang) {
|
||||||
|
|
||||||
|
@ -147,7 +143,6 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -156,7 +151,7 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
* @param locale
|
* @param locale
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private List<JsonObject> getLanguageMap(Locale locale) {
|
List<JsonObject> getLanguageMap(Locale locale) {
|
||||||
|
|
||||||
if (!languageMaps.containsKey(locale)) {
|
if (!languageMaps.containsKey(locale)) {
|
||||||
try {
|
try {
|
||||||
|
@ -174,7 +169,7 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
r = getBaseDirectory().createRelative(filename);
|
r = getBaseDirectory().createRelative(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("No locale loaded, trying to load from " + r);
|
logger.info("No locale loaded, trying to load from {}", r);
|
||||||
|
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new JsonParser();
|
||||||
JsonObject obj = (JsonObject) parser.parse(new InputStreamReader(r.getInputStream(), "UTF-8"));
|
JsonObject obj = (JsonObject) parser.parse(new InputStreamReader(r.getInputStream(), "UTF-8"));
|
||||||
|
@ -182,6 +177,9 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
set.add(obj);
|
set.add(obj);
|
||||||
}
|
}
|
||||||
languageMaps.put(locale, set);
|
languageMaps.put(locale, set);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
logger.info("Unable to load locale because no messages file was found for locale {}", locale.getDisplayName());
|
||||||
|
languageMaps.put(locale, null);
|
||||||
} catch (JsonIOException | JsonSyntaxException | IOException e) {
|
} catch (JsonIOException | JsonSyntaxException | IOException e) {
|
||||||
logger.error("Unable to load locale", e);
|
logger.error("Unable to load locale", e);
|
||||||
}
|
}
|
||||||
|
@ -189,8 +187,6 @@ public class JsonMessageSource extends AbstractMessageSource {
|
||||||
|
|
||||||
return languageMaps.get(locale);
|
return languageMaps.get(locale);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
package org.mitre.openid.connect.config;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
public class TestJsonMessageSource {
|
||||||
|
|
||||||
|
private JsonMessageSource jsonMessageSource;
|
||||||
|
|
||||||
|
private Locale localeThatHasAFile = new Locale("en");
|
||||||
|
|
||||||
|
private Locale localeThatDoesNotHaveAFile = new Locale("xx");
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
ConfigurationPropertiesBean config = new ConfigurationPropertiesBean();
|
||||||
|
jsonMessageSource = new JsonMessageSource(config);
|
||||||
|
|
||||||
|
//test message files are located in test/resources/js/locale/
|
||||||
|
Resource resource = new ClassPathResource("/resources/js/locale/");
|
||||||
|
jsonMessageSource.setBaseDirectory(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void verifyWhenLocaleExists_languageMapIsLoaded() {
|
||||||
|
List<JsonObject> languageMap = jsonMessageSource.getLanguageMap(localeThatHasAFile);
|
||||||
|
assertNotNull(languageMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void verifyWhenLocaleDoesNotExist_languageMapIsNotLoaded() {
|
||||||
|
List<JsonObject> languageMap = jsonMessageSource.getLanguageMap(localeThatDoesNotHaveAFile);
|
||||||
|
assertNull(languageMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void verifyWhenLocaleExists_canResolveCode() {
|
||||||
|
MessageFormat mf = jsonMessageSource.resolveCode("testAttribute", localeThatHasAFile);
|
||||||
|
assertEquals(mf.getLocale().getLanguage(), "en");
|
||||||
|
assertEquals(mf.toPattern(), "testValue");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void verifyWhenLocaleDoesNotExist_cannotResolveCode() {
|
||||||
|
MessageFormat mf = jsonMessageSource.resolveCode("test", localeThatDoesNotHaveAFile);
|
||||||
|
assertNull(mf);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"testAttribute": "testValue"
|
||||||
|
}
|
Loading…
Reference in New Issue