Files
SCM-Manager/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java

130 lines
4.8 KiB
Java
Raw Normal View History

2018-10-19 11:15:37 +02:00
package sonia.scm.web.i18n;
2018-10-22 17:37:10 +02:00
import com.fasterxml.jackson.databind.JsonNode;
2018-10-19 11:15:37 +02:00
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.legman.Subscribe;
import com.google.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
import sonia.scm.NotFoundException;
import sonia.scm.SCMContext;
import sonia.scm.Stage;
import sonia.scm.boot.RestartEvent;
import sonia.scm.cache.Cache;
import sonia.scm.cache.CacheManager;
import sonia.scm.filter.WebElement;
import sonia.scm.plugin.PluginLoader;
2018-10-22 17:37:10 +02:00
import sonia.scm.util.JacksonUtils;
2018-10-19 11:15:37 +02:00
import javax.inject.Inject;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Enumeration;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;
/**
2018-10-20 14:40:03 +02:00
* Collect the plugin translations.
2018-10-19 11:15:37 +02:00
*/
@Singleton
@WebElement(value = I18nServlet.PATTERN, regex = true)
@Slf4j
public class I18nServlet extends HttpServlet {
public static final String PATH = "/locales";
public static final String PLUGINS_JSON = "plugins.json";
public static final String PATTERN = PATH + "/[a-z\\-A-Z]*/" + PLUGINS_JSON;
public static final String CACHE_NAME = "sonia.cache.plugins.translations";
2018-10-22 17:37:10 +02:00
private final ClassLoader classLoader;
private final Cache<String, JsonNode> cache;
2018-10-20 14:40:03 +02:00
private static ObjectMapper objectMapper = new ObjectMapper();
2018-10-19 11:15:37 +02:00
@Inject
public I18nServlet(PluginLoader pluginLoader, CacheManager cacheManager) {
2018-10-22 17:37:10 +02:00
this.classLoader = pluginLoader.getUberClassLoader();
2018-10-19 11:15:37 +02:00
this.cache = cacheManager.getCache(CACHE_NAME);
}
2018-10-20 14:40:03 +02:00
@Subscribe(async = false)
2018-10-19 11:15:37 +02:00
public void handleRestartEvent(RestartEvent event) {
2018-10-22 17:37:10 +02:00
log.debug("Clear cache on restart event with reason {}", event.getReason());
2018-10-19 11:15:37 +02:00
cache.clear();
}
2018-10-22 17:37:10 +02:00
private JsonNode getCollectedJson(String path,
Function<String, Optional<JsonNode>> jsonFileProvider,
BiConsumer<String, JsonNode> createdJsonFileConsumer) {
2018-10-19 11:15:37 +02:00
return Optional.ofNullable(jsonFileProvider.apply(path)
.orElseGet(() -> {
2018-10-22 17:37:10 +02:00
Optional<JsonNode> createdFile = collectJsonFile(path);
2018-10-19 11:15:37 +02:00
createdFile.ifPresent(map -> createdJsonFileConsumer.accept(path, map));
return createdFile.orElse(null);
}
)).orElseThrow(NotFoundException::new);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) {
2018-10-22 17:37:10 +02:00
try (PrintWriter out = response.getWriter()) {
2018-10-19 11:15:37 +02:00
response.setContentType("application/json");
String path = req.getServletPath();
2018-10-22 17:37:10 +02:00
Function<String, Optional<JsonNode>> jsonFileProvider = usedPath -> Optional.empty();
BiConsumer<String, JsonNode> createdJsonFileConsumer = (usedPath, jsonNode) -> log.debug("A json File is created from the path {}", usedPath);
2018-10-20 14:40:03 +02:00
if (isProductionStage()) {
2018-10-22 17:37:10 +02:00
log.debug("In Production Stage get the plugin translations from the cache");
2018-10-19 11:15:37 +02:00
jsonFileProvider = usedPath -> Optional.ofNullable(
cache.get(usedPath));
createdJsonFileConsumer = createdJsonFileConsumer
2018-10-22 17:37:10 +02:00
.andThen((usedPath, jsonNode) -> log.debug("Put the created json File in the cache with the key {}", usedPath))
2018-10-19 11:15:37 +02:00
.andThen(cache::put);
}
2018-10-22 17:37:10 +02:00
objectMapper.writeValue(out, getCollectedJson(path, jsonFileProvider, createdJsonFileConsumer));
2018-10-19 11:15:37 +02:00
} catch (IOException e) {
2018-10-20 14:40:03 +02:00
log.error("Error on getting the translation of the plugins", e);
2018-10-19 11:15:37 +02:00
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (NotFoundException e) {
log.error("Plugin translations are not found", e);
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
2018-10-20 14:40:03 +02:00
}
protected boolean isProductionStage() {
return SCMContext.getContext().getStage() == Stage.PRODUCTION;
2018-10-19 11:15:37 +02:00
}
/**
2018-10-22 17:37:10 +02:00
* Return a collected Json File as JsonNode from the given path from all plugins in the class path
2018-10-19 11:15:37 +02:00
*
* @param path the searched resource path
2018-10-22 17:37:10 +02:00
* @return a collected Json File as JsonNode from the given path from all plugins in the class path
2018-10-19 11:15:37 +02:00
*/
2018-10-22 17:37:10 +02:00
protected Optional<JsonNode> collectJsonFile(String path) {
log.debug("Collect plugin translations from path {} for every plugin", path);
JsonNode mergedJsonNode = null;
2018-10-19 11:15:37 +02:00
try {
2018-10-22 17:37:10 +02:00
Enumeration<URL> resources = classLoader.getResources(path.replaceFirst("/", ""));
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
JsonNode jsonNode = objectMapper.readTree(url);
if (mergedJsonNode != null) {
JacksonUtils.merge(mergedJsonNode, jsonNode);
} else {
mergedJsonNode = jsonNode;
2018-10-19 11:15:37 +02:00
}
}
} catch (IOException e) {
log.error("Error on loading sources from {}", path, e);
2018-10-20 14:40:03 +02:00
return Optional.empty();
2018-10-19 11:15:37 +02:00
}
2018-10-22 17:37:10 +02:00
return Optional.ofNullable(mergedJsonNode);
2018-10-19 11:15:37 +02:00
}
}