2018-10-19 11:15:37 +02:00
|
|
|
package sonia.scm.web.i18n;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
import sonia.scm.plugin.UberClassLoader;
|
|
|
|
|
|
|
|
|
|
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.nio.file.Files;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.util.Enumeration;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
private final UberClassLoader uberClassLoader;
|
2018-10-20 14:40:03 +02:00
|
|
|
private final Cache<String, Map> cache;
|
|
|
|
|
private static ObjectMapper objectMapper = new ObjectMapper();
|
2018-10-19 11:15:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
public I18nServlet(PluginLoader pluginLoader, CacheManager cacheManager) {
|
|
|
|
|
this.uberClassLoader = (UberClassLoader) pluginLoader.getUberClassLoader();
|
|
|
|
|
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-20 14:40:03 +02:00
|
|
|
log.info("Clear cache on restart event with reason {}", event.getReason());
|
2018-10-19 11:15:37 +02:00
|
|
|
cache.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-20 14:40:03 +02:00
|
|
|
private Map getCollectedJson(String path,
|
|
|
|
|
Function<String, Optional<Map>> jsonFileProvider,
|
|
|
|
|
BiConsumer<String, Map> createdJsonFileConsumer) {
|
2018-10-19 11:15:37 +02:00
|
|
|
return Optional.ofNullable(jsonFileProvider.apply(path)
|
|
|
|
|
.orElseGet(() -> {
|
2018-10-20 14:40:03 +02:00
|
|
|
Optional<Map> 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) {
|
|
|
|
|
try {
|
|
|
|
|
response.setContentType("application/json");
|
|
|
|
|
PrintWriter out = response.getWriter();
|
|
|
|
|
String path = req.getServletPath();
|
2018-10-20 14:40:03 +02:00
|
|
|
Function<String, Optional<Map>> jsonFileProvider = usedPath -> Optional.empty();
|
|
|
|
|
BiConsumer<String, Map> createdJsonFileConsumer = (usedPath, foundJsonMap) -> log.info("A json File is created from the path {}", usedPath);
|
|
|
|
|
if (isProductionStage()) {
|
2018-10-19 11:15:37 +02:00
|
|
|
log.info("In Production Stage get the plugin translations from the cache");
|
|
|
|
|
jsonFileProvider = usedPath -> Optional.ofNullable(
|
|
|
|
|
cache.get(usedPath));
|
|
|
|
|
createdJsonFileConsumer = createdJsonFileConsumer
|
|
|
|
|
.andThen((usedPath, map) -> log.info("Put the created json File in the cache with the key {}", usedPath))
|
|
|
|
|
.andThen(cache::put);
|
|
|
|
|
}
|
|
|
|
|
out.write(objectMapper.writeValueAsString(getCollectedJson(path, jsonFileProvider, createdJsonFileConsumer)));
|
|
|
|
|
} 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-20 14:40:03 +02:00
|
|
|
* Return a collected Json File as map 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-20 14:40:03 +02:00
|
|
|
* @return a collected Json File as map from the given path from all plugins in the class path
|
2018-10-19 11:15:37 +02:00
|
|
|
*/
|
2018-10-20 14:40:03 +02:00
|
|
|
protected Optional<Map> collectJsonFile(String path) {
|
2018-10-19 11:15:37 +02:00
|
|
|
log.info("Collect plugin translations from path {} for every plugin", path);
|
2018-10-20 14:40:03 +02:00
|
|
|
Map result = null;
|
2018-10-19 11:15:37 +02:00
|
|
|
try {
|
|
|
|
|
Enumeration<URL> resources = uberClassLoader.getResources(path.replaceFirst("/", ""));
|
|
|
|
|
if (resources.hasMoreElements()) {
|
2018-10-20 14:40:03 +02:00
|
|
|
result = new HashMap();
|
2018-10-19 11:15:37 +02:00
|
|
|
while (resources.hasMoreElements()) {
|
|
|
|
|
URL url = resources.nextElement();
|
2018-10-20 14:40:03 +02:00
|
|
|
result.putAll(objectMapper.readValue(Files.readAllBytes(Paths.get(url.getPath())), Map.class));
|
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
|
|
|
}
|
|
|
|
|
return Optional.ofNullable(result);
|
|
|
|
|
}
|
|
|
|
|
}
|