Skip to content

Commit 0b531a0

Browse files
committed
fixed an issue where missing locales would generate a lot of ERROR level log messages
1 parent 64fbee7 commit 0b531a0

File tree

3 files changed

+86
-28
lines changed

3 files changed

+86
-28
lines changed

openid-connect-server/src/main/java/org/mitre/openid/connect/config/JsonMessageSource.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,20 @@
1616

1717
package org.mitre.openid.connect.config;
1818

19+
import com.google.common.base.Splitter;
20+
import com.google.gson.JsonElement;
21+
import com.google.gson.JsonIOException;
22+
import com.google.gson.JsonObject;
23+
import com.google.gson.JsonParser;
24+
import com.google.gson.JsonSyntaxException;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.context.support.AbstractMessageSource;
29+
import org.springframework.core.io.Resource;
30+
1931
import java.io.File;
32+
import java.io.FileNotFoundException;
2033
import java.io.IOException;
2134
import java.io.InputStreamReader;
2235
import java.text.MessageFormat;
@@ -27,25 +40,11 @@
2740
import java.util.Locale;
2841
import java.util.Map;
2942

30-
import org.slf4j.Logger;
31-
import org.slf4j.LoggerFactory;
32-
import org.springframework.beans.factory.annotation.Autowired;
33-
import org.springframework.context.support.AbstractMessageSource;
34-
import org.springframework.core.io.Resource;
35-
36-
import com.google.common.base.Splitter;
37-
import com.google.gson.JsonElement;
38-
import com.google.gson.JsonIOException;
39-
import com.google.gson.JsonObject;
40-
import com.google.gson.JsonParser;
41-
import com.google.gson.JsonSyntaxException;
42-
4343
/**
4444
* @author jricher
45-
*
4645
*/
4746
public class JsonMessageSource extends AbstractMessageSource {
48-
// Logger for this class
47+
4948
private static final Logger logger = LoggerFactory.getLogger(JsonMessageSource.class);
5049

5150
private Resource baseDirectory;
@@ -54,9 +53,13 @@ public class JsonMessageSource extends AbstractMessageSource {
5453

5554
private Map<Locale, List<JsonObject>> languageMaps = new HashMap<>();
5655

57-
@Autowired
5856
private ConfigurationPropertiesBean config;
5957

58+
@Autowired
59+
public JsonMessageSource(ConfigurationPropertiesBean config) {
60+
this.config = config;
61+
}
62+
6063
@Override
6164
protected MessageFormat resolveCode(String code, Locale locale) {
6265

@@ -82,9 +85,6 @@ protected MessageFormat resolveCode(String code, Locale locale) {
8285

8386
/**
8487
* Get a value from the set of maps, taking the first match in order
85-
* @param code
86-
* @param langs
87-
* @return
8888
*/
8989
private String getValue(String code, List<JsonObject> langs) {
9090
if (langs == null || langs.isEmpty()) {
@@ -106,10 +106,6 @@ private String getValue(String code, List<JsonObject> langs) {
106106

107107
/**
108108
* Get a value from a single map
109-
* @param code
110-
* @param locale
111-
* @param lang
112-
* @return
113109
*/
114110
private String getValue(String code, JsonObject lang) {
115111

@@ -147,7 +143,6 @@ private String getValue(String code, JsonObject lang) {
147143
}
148144
}
149145

150-
151146
return value;
152147

153148
}
@@ -156,7 +151,7 @@ private String getValue(String code, JsonObject lang) {
156151
* @param locale
157152
* @return
158153
*/
159-
private List<JsonObject> getLanguageMap(Locale locale) {
154+
List<JsonObject> getLanguageMap(Locale locale) {
160155

161156
if (!languageMaps.containsKey(locale)) {
162157
try {
@@ -174,23 +169,24 @@ private List<JsonObject> getLanguageMap(Locale locale) {
174169
r = getBaseDirectory().createRelative(filename);
175170
}
176171

177-
logger.info("No locale loaded, trying to load from " + r);
172+
logger.info("No locale loaded, trying to load from {}", r);
178173

179174
JsonParser parser = new JsonParser();
180175
JsonObject obj = (JsonObject) parser.parse(new InputStreamReader(r.getInputStream(), "UTF-8"));
181176

182177
set.add(obj);
183178
}
184179
languageMaps.put(locale, set);
180+
} catch (FileNotFoundException e) {
181+
logger.info("Unable to load locale because no messages file was found for locale {}", locale.getDisplayName());
182+
languageMaps.put(locale, null);
185183
} catch (JsonIOException | JsonSyntaxException | IOException e) {
186184
logger.error("Unable to load locale", e);
187185
}
188186
}
189187

190188
return languageMaps.get(locale);
191189

192-
193-
194190
}
195191

196192
/**
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.mitre.openid.connect.config;
2+
3+
import com.google.gson.JsonObject;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.springframework.core.io.ClassPathResource;
7+
import org.springframework.core.io.Resource;
8+
9+
import java.text.MessageFormat;
10+
import java.util.List;
11+
import java.util.Locale;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertNotNull;
15+
import static org.junit.Assert.assertNull;
16+
17+
public class TestJsonMessageSource {
18+
19+
private JsonMessageSource jsonMessageSource;
20+
21+
private Locale localeThatHasAFile = new Locale("en");
22+
23+
private Locale localeThatDoesNotHaveAFile = new Locale("xx");
24+
25+
@Before
26+
public void setup() {
27+
ConfigurationPropertiesBean config = new ConfigurationPropertiesBean();
28+
jsonMessageSource = new JsonMessageSource(config);
29+
30+
//test message files are located in test/resources/js/locale/
31+
Resource resource = new ClassPathResource("/resources/js/locale/");
32+
jsonMessageSource.setBaseDirectory(resource);
33+
}
34+
35+
@Test
36+
public void verifyWhenLocaleExists_languageMapIsLoaded() {
37+
List<JsonObject> languageMap = jsonMessageSource.getLanguageMap(localeThatHasAFile);
38+
assertNotNull(languageMap);
39+
}
40+
41+
@Test
42+
public void verifyWhenLocaleDoesNotExist_languageMapIsNotLoaded() {
43+
List<JsonObject> languageMap = jsonMessageSource.getLanguageMap(localeThatDoesNotHaveAFile);
44+
assertNull(languageMap);
45+
}
46+
47+
@Test
48+
public void verifyWhenLocaleExists_canResolveCode() {
49+
MessageFormat mf = jsonMessageSource.resolveCode("testAttribute", localeThatHasAFile);
50+
assertEquals(mf.getLocale().getLanguage(), "en");
51+
assertEquals(mf.toPattern(), "testValue");
52+
}
53+
54+
@Test
55+
public void verifyWhenLocaleDoesNotExist_cannotResolveCode() {
56+
MessageFormat mf = jsonMessageSource.resolveCode("test", localeThatDoesNotHaveAFile);
57+
assertNull(mf);
58+
}
59+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"testAttribute": "testValue"
3+
}

0 commit comments

Comments
 (0)