/
ResourcesController.java
100 lines (82 loc) · 3.51 KB
/
ResourcesController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package me.noroutine;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.util.JSONPObject;
import me.noroutine.cache.IfNoneMatch;
import me.noroutine.util.ReloadableMessageSource;
import me.noroutine.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* @author Oleksii Khilkevych
* @since 02.12.12
*/
@Controller
public class ResourcesController {
private static final Logger log = LoggerFactory.getLogger(ResourcesController.class);
@Autowired
@Qualifier("messageSource")
private ReloadableMessageSource messageSource;
@Autowired
@Qualifier("applicationMessageSource")
private ReloadableMessageSource applicationMessageSource;
private static final String VALUE = "$";
private final ConcurrentMap<Locale, ObjectNode> resourcesMap = new ConcurrentHashMap<Locale, ObjectNode>();
@PostConstruct
private void prepareResources() {
for (Locale locale: Arrays.asList(Locale.getDefault(), Locale.ENGLISH, Locale.GERMAN, Locale.GERMANY)) {
resourcesMap.putIfAbsent(locale, prepareLocalizedResources(locale));
}
}
private ObjectNode prepareLocalizedResources(Locale locale) {
ObjectNode resources = JsonNodeFactory.instance.objectNode();
ObjectNode resourcesTree = JsonNodeFactory.instance.objectNode();
resources.put("$tree", resourcesTree);
Iterable<Map.Entry<Object, Object>> entries = Utils.IterableIterator.from(
messageSource.getProperties(locale).entrySet(),
applicationMessageSource.getProperties(locale).entrySet()
);
for (Map.Entry<Object, Object> entry: entries) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
String[] keyParts = key.split("\\.");
// go down the object properties
ObjectNode node = resourcesTree;
for (String keyPart : keyParts) {
JsonNode child = node.get(keyPart);
ObjectNode childObject;
if (child == null) {
childObject = JsonNodeFactory.instance.objectNode();
node.put(keyPart, childObject);
} else {
childObject = (ObjectNode) child;
}
node = childObject;
}
node.put(VALUE, value);
resources.put(key, value);
}
return resources;
}
@RequestMapping("/resources.js")
@ResponseBody
@IfNoneMatch
public JSONPObject getResources(Locale locale) {
ObjectNode localizedResources = resourcesMap.get(locale);
if (localizedResources == null) {
log.info("Creating locale resources for {}, consider doing this on initialization", locale);
resourcesMap.putIfAbsent(locale, prepareLocalizedResources(locale));
}
return new JSONPObject("R.registerResources", resourcesMap.get(locale));
}
}