mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
Move the json merge in the I18nServlet class
This commit is contained in:
@@ -1,49 +0,0 @@
|
|||||||
package sonia.scm.util;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
public class JacksonUtils {
|
|
||||||
|
|
||||||
private JacksonUtils() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static JsonNode merge(JsonNode mainNode, JsonNode updateNode) {
|
|
||||||
Iterator<String> fieldNames = updateNode.fieldNames();
|
|
||||||
|
|
||||||
while (fieldNames.hasNext()) {
|
|
||||||
|
|
||||||
String fieldName = fieldNames.next();
|
|
||||||
JsonNode jsonNode = mainNode.get(fieldName);
|
|
||||||
|
|
||||||
if (jsonNode != null) {
|
|
||||||
if (jsonNode.isObject()) {
|
|
||||||
merge(jsonNode, updateNode.get(fieldName));
|
|
||||||
} else if (jsonNode.isArray()) {
|
|
||||||
for (int i = 0; i < jsonNode.size(); i++) {
|
|
||||||
merge(jsonNode.get(i), updateNode.get(fieldName).get(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (mainNode instanceof ObjectNode) {
|
|
||||||
// Overwrite field
|
|
||||||
JsonNode value = updateNode.get(fieldName);
|
|
||||||
if (value.isNull()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (value.isIntegralNumber() && value.toString().equals("0")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (value.isFloatingPointNumber() && value.toString().equals("0.0")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
((ObjectNode) mainNode).put(fieldName, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return mainNode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,7 +3,9 @@ package sonia.scm.web.i18n;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import com.github.legman.Subscribe;
|
import com.github.legman.Subscribe;
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.inject.Singleton;
|
import com.google.inject.Singleton;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import sonia.scm.NotFoundException;
|
import sonia.scm.NotFoundException;
|
||||||
@@ -14,7 +16,6 @@ import sonia.scm.cache.Cache;
|
|||||||
import sonia.scm.cache.CacheManager;
|
import sonia.scm.cache.CacheManager;
|
||||||
import sonia.scm.filter.WebElement;
|
import sonia.scm.filter.WebElement;
|
||||||
import sonia.scm.plugin.PluginLoader;
|
import sonia.scm.plugin.PluginLoader;
|
||||||
import sonia.scm.util.JacksonUtils;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
@@ -24,6 +25,7 @@ import java.io.IOException;
|
|||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
@@ -37,9 +39,8 @@ import java.util.function.Function;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class I18nServlet extends HttpServlet {
|
public class I18nServlet extends HttpServlet {
|
||||||
|
|
||||||
public static final String PATH = "/locales";
|
|
||||||
public static final String PLUGINS_JSON = "plugins.json";
|
public static final String PLUGINS_JSON = "plugins.json";
|
||||||
public static final String PATTERN = PATH + "/[a-z\\-A-Z]*/" + PLUGINS_JSON;
|
public static final String PATTERN = "/locales/[a-z\\-A-Z]*/" + PLUGINS_JSON;
|
||||||
public static final String CACHE_NAME = "sonia.cache.plugins.translations";
|
public static final String CACHE_NAME = "sonia.cache.plugins.translations";
|
||||||
|
|
||||||
private final ClassLoader classLoader;
|
private final ClassLoader classLoader;
|
||||||
@@ -71,6 +72,7 @@ public class I18nServlet extends HttpServlet {
|
|||||||
)).orElseThrow(NotFoundException::new);
|
)).orElseThrow(NotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse response) {
|
protected void doGet(HttpServletRequest req, HttpServletResponse response) {
|
||||||
try (PrintWriter out = response.getWriter()) {
|
try (PrintWriter out = response.getWriter()) {
|
||||||
@@ -96,6 +98,7 @@ public class I18nServlet extends HttpServlet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
protected boolean isProductionStage() {
|
protected boolean isProductionStage() {
|
||||||
return SCMContext.getContext().getStage() == Stage.PRODUCTION;
|
return SCMContext.getContext().getStage() == Stage.PRODUCTION;
|
||||||
}
|
}
|
||||||
@@ -106,6 +109,7 @@ public class I18nServlet extends HttpServlet {
|
|||||||
* @param path the searched resource path
|
* @param path the searched resource path
|
||||||
* @return a collected Json File as JsonNode from the given path from all plugins in the class path
|
* @return a collected Json File as JsonNode from the given path from all plugins in the class path
|
||||||
*/
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
protected Optional<JsonNode> collectJsonFile(String path) {
|
protected Optional<JsonNode> collectJsonFile(String path) {
|
||||||
log.debug("Collect plugin translations from path {} for every plugin", path);
|
log.debug("Collect plugin translations from path {} for every plugin", path);
|
||||||
JsonNode mergedJsonNode = null;
|
JsonNode mergedJsonNode = null;
|
||||||
@@ -115,7 +119,7 @@ public class I18nServlet extends HttpServlet {
|
|||||||
URL url = resources.nextElement();
|
URL url = resources.nextElement();
|
||||||
JsonNode jsonNode = objectMapper.readTree(url);
|
JsonNode jsonNode = objectMapper.readTree(url);
|
||||||
if (mergedJsonNode != null) {
|
if (mergedJsonNode != null) {
|
||||||
JacksonUtils.merge(mergedJsonNode, jsonNode);
|
merge(mergedJsonNode, jsonNode);
|
||||||
} else {
|
} else {
|
||||||
mergedJsonNode = jsonNode;
|
mergedJsonNode = jsonNode;
|
||||||
}
|
}
|
||||||
@@ -126,4 +130,59 @@ public class I18nServlet extends HttpServlet {
|
|||||||
}
|
}
|
||||||
return Optional.ofNullable(mergedJsonNode);
|
return Optional.ofNullable(mergedJsonNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merge the <code>updateNode</code> into the <code>mainNode</code> and return it.
|
||||||
|
*
|
||||||
|
* This is not a deep merge.
|
||||||
|
*
|
||||||
|
* @param mainNode the main node
|
||||||
|
* @param updateNode the update node
|
||||||
|
* @return the merged mainNode
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
protected JsonNode merge(JsonNode mainNode, JsonNode updateNode) {
|
||||||
|
Iterator<String> fieldNames = updateNode.fieldNames();
|
||||||
|
|
||||||
|
while (fieldNames.hasNext()) {
|
||||||
|
|
||||||
|
String fieldName = fieldNames.next();
|
||||||
|
JsonNode jsonNode = mainNode.get(fieldName);
|
||||||
|
|
||||||
|
if (jsonNode != null) {
|
||||||
|
mergeNode(updateNode, fieldName, jsonNode);
|
||||||
|
} else {
|
||||||
|
mergeField(mainNode, updateNode, fieldName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mainNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void mergeField(JsonNode mainNode, JsonNode updateNode, String fieldName) {
|
||||||
|
if (mainNode instanceof ObjectNode) {
|
||||||
|
JsonNode value = updateNode.get(fieldName);
|
||||||
|
if (value.isNull()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (value.isIntegralNumber() && value.toString().equals("0")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (value.isFloatingPointNumber() && value.toString().equals("0.0")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
((ObjectNode) mainNode).set(fieldName, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void mergeNode(JsonNode updateNode, String fieldName, JsonNode jsonNode) {
|
||||||
|
if (jsonNode.isObject()) {
|
||||||
|
merge(jsonNode, updateNode.get(fieldName));
|
||||||
|
} else if (jsonNode.isArray()) {
|
||||||
|
for (int i = 0; i < jsonNode.size(); i++) {
|
||||||
|
merge(jsonNode.get(i), updateNode.get(fieldName).get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ import sonia.scm.cache.Cache;
|
|||||||
import sonia.scm.cache.CacheManager;
|
import sonia.scm.cache.CacheManager;
|
||||||
import sonia.scm.event.ScmEventBus;
|
import sonia.scm.event.ScmEventBus;
|
||||||
import sonia.scm.plugin.PluginLoader;
|
import sonia.scm.plugin.PluginLoader;
|
||||||
import sonia.scm.util.JacksonUtils;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
@@ -205,8 +204,8 @@ public class I18nServletTest {
|
|||||||
when(classLoader.getResources("locales/de/plugins.json")).thenReturn(resources);
|
when(classLoader.getResources("locales/de/plugins.json")).thenReturn(resources);
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
JsonNode node = objectMapper.readTree(GIT_PLUGIN_JSON);
|
JsonNode node = objectMapper.readTree(GIT_PLUGIN_JSON);
|
||||||
node = JacksonUtils.merge(node, objectMapper.readTree(HG_PLUGIN_JSON));
|
node = servlet.merge(node, objectMapper.readTree(HG_PLUGIN_JSON));
|
||||||
node = JacksonUtils.merge(node, objectMapper.readTree(SVN_PLUGIN_JSON));
|
node = servlet.merge(node, objectMapper.readTree(SVN_PLUGIN_JSON));
|
||||||
when(cache.get(path)).thenReturn(node);
|
when(cache.get(path)).thenReturn(node);
|
||||||
|
|
||||||
servlet.doGet(request, response);
|
servlet.doGet(request, response);
|
||||||
|
|||||||
Reference in New Issue
Block a user