mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 08:25:44 +01:00
Merge with 2.0.0-m3
This commit is contained in:
@@ -23,6 +23,7 @@ import java.net.URL;
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Singleton
|
||||
@Priority(WebResourceServlet.PRIORITY)
|
||||
@WebElement(value = WebResourceServlet.PATTERN, regex = true)
|
||||
public class WebResourceServlet extends HttpServlet {
|
||||
|
||||
@@ -35,6 +36,9 @@ public class WebResourceServlet extends HttpServlet {
|
||||
@VisibleForTesting
|
||||
static final String PATTERN = "/(?!api/|git/|hg/|svn/|hook/|repo/).*";
|
||||
|
||||
// Be sure that this servlet is the last one in the servlet chain.
|
||||
static final int PRIORITY = Integer.MAX_VALUE;
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(WebResourceServlet.class);
|
||||
|
||||
private final WebResourceSender sender = WebResourceSender.create()
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
import de.otto.edison.hal.Links;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class BrowserResultDto extends HalRepresentation {
|
||||
private String revision;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("squid:S1185") // We want to have this method available in this package
|
||||
protected HalRepresentation add(Links links) {
|
||||
return super.add(links);
|
||||
}
|
||||
|
||||
public void setFiles(List<FileObjectDto> files) {
|
||||
this.withEmbedded("files", files);
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.Links;
|
||||
import sonia.scm.repository.BrowserResult;
|
||||
import sonia.scm.repository.FileObject;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BrowserResultToBrowserResultDtoMapper {
|
||||
|
||||
@Inject
|
||||
private FileObjectToFileObjectDtoMapper fileObjectToFileObjectDtoMapper;
|
||||
|
||||
@Inject
|
||||
private ResourceLinks resourceLinks;
|
||||
|
||||
public BrowserResultDto map(BrowserResult browserResult, NamespaceAndName namespaceAndName, String path) {
|
||||
BrowserResultDto browserResultDto = new BrowserResultDto();
|
||||
|
||||
browserResultDto.setRevision(browserResult.getRevision());
|
||||
|
||||
List<FileObjectDto> fileObjectDtoList = new ArrayList<>();
|
||||
for (FileObject fileObject : browserResult.getFiles()) {
|
||||
fileObjectDtoList.add(mapFileObject(fileObject, namespaceAndName, browserResult.getRevision()));
|
||||
}
|
||||
|
||||
browserResultDto.setFiles(fileObjectDtoList);
|
||||
this.addLinks(browserResult, browserResultDto, namespaceAndName, path);
|
||||
return browserResultDto;
|
||||
}
|
||||
|
||||
private FileObjectDto mapFileObject(FileObject fileObject, NamespaceAndName namespaceAndName, String revision) {
|
||||
return fileObjectToFileObjectDtoMapper.map(fileObject, namespaceAndName, revision);
|
||||
}
|
||||
|
||||
private void addLinks(BrowserResult browserResult, BrowserResultDto dto, NamespaceAndName namespaceAndName, String path) {
|
||||
if (path.equals("/")) {
|
||||
path = "";
|
||||
}
|
||||
if (browserResult.getRevision() == null) {
|
||||
throw new IllegalStateException("missing revision in browser result for repository " + namespaceAndName + " and path " + path);
|
||||
} else {
|
||||
dto.add(Links.linkingTo().self(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), browserResult.getRevision(), path)).build());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import sonia.scm.repository.BrowserResult;
|
||||
import sonia.scm.repository.NamespaceAndName;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class BrowserResultToFileObjectDtoMapper {
|
||||
|
||||
private final FileObjectToFileObjectDtoMapper fileObjectToFileObjectDtoMapper;
|
||||
|
||||
@Inject
|
||||
public BrowserResultToFileObjectDtoMapper(FileObjectToFileObjectDtoMapper fileObjectToFileObjectDtoMapper) {
|
||||
this.fileObjectToFileObjectDtoMapper = fileObjectToFileObjectDtoMapper;
|
||||
}
|
||||
|
||||
public FileObjectDto map(BrowserResult browserResult, NamespaceAndName namespaceAndName) {
|
||||
FileObjectDto fileObjectDto = fileObjectToFileObjectDtoMapper.map(browserResult.getFile(), namespaceAndName, browserResult.getRevision());
|
||||
fileObjectDto.setRevision( browserResult.getRevision() );
|
||||
return fileObjectDto;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
import de.otto.edison.hal.Links;
|
||||
import lombok.Getter;
|
||||
@@ -7,6 +8,7 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@@ -15,14 +17,26 @@ public class FileObjectDto extends HalRepresentation {
|
||||
private String name;
|
||||
private String path;
|
||||
private boolean directory;
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
private String description;
|
||||
private int length;
|
||||
private long length;
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
private Instant lastModified;
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
private SubRepositoryDto subRepository;
|
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
private String revision;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("squid:S1185") // We want to have this method available in this package
|
||||
protected HalRepresentation add(Links links) {
|
||||
return super.add(links);
|
||||
}
|
||||
|
||||
public void setChildren(List<FileObjectDto> children) {
|
||||
if (!children.isEmpty()) {
|
||||
// prevent empty embedded attribute in json
|
||||
this.withEmbedded("children", children);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ import sonia.scm.repository.SubRepository;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static de.otto.edison.hal.Link.link;
|
||||
|
||||
@Mapper
|
||||
|
||||
@@ -4,6 +4,7 @@ import sonia.scm.repository.NamespaceAndName;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
class ResourceLinks {
|
||||
|
||||
@@ -16,7 +17,11 @@ class ResourceLinks {
|
||||
|
||||
// we have to add the file path using URI, so that path separators (aka '/') will not be encoded as '%2F'
|
||||
private static String addPath(String sourceWithPath, String path) {
|
||||
return URI.create(sourceWithPath).resolve(path).toASCIIString();
|
||||
try {
|
||||
return new URI(sourceWithPath).resolve(new URI(null, null, path, null)).toASCIIString();
|
||||
} catch (URISyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
GroupLinks group() {
|
||||
|
||||
@@ -18,13 +18,13 @@ import java.io.IOException;
|
||||
public class SourceRootResource {
|
||||
|
||||
private final RepositoryServiceFactory serviceFactory;
|
||||
private final BrowserResultToBrowserResultDtoMapper browserResultToBrowserResultDtoMapper;
|
||||
private final BrowserResultToFileObjectDtoMapper browserResultToFileObjectDtoMapper;
|
||||
|
||||
|
||||
@Inject
|
||||
public SourceRootResource(RepositoryServiceFactory serviceFactory, BrowserResultToBrowserResultDtoMapper browserResultToBrowserResultDtoMapper) {
|
||||
public SourceRootResource(RepositoryServiceFactory serviceFactory, BrowserResultToFileObjectDtoMapper browserResultToFileObjectDtoMapper) {
|
||||
this.serviceFactory = serviceFactory;
|
||||
this.browserResultToBrowserResultDtoMapper = browserResultToBrowserResultDtoMapper;
|
||||
this.browserResultToFileObjectDtoMapper = browserResultToFileObjectDtoMapper;
|
||||
}
|
||||
|
||||
@GET
|
||||
@@ -56,10 +56,11 @@ public class SourceRootResource {
|
||||
if (revision != null && !revision.isEmpty()) {
|
||||
browseCommand.setRevision(revision);
|
||||
}
|
||||
browseCommand.setDisableCache(true);
|
||||
BrowserResult browserResult = browseCommand.getBrowserResult();
|
||||
|
||||
if (browserResult != null) {
|
||||
return Response.ok(browserResultToBrowserResultDtoMapper.map(browserResult, namespaceAndName, path)).build();
|
||||
return Response.ok(browserResultToFileObjectDtoMapper.map(browserResult, namespaceAndName)).build();
|
||||
} else {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
|
||||
188
scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java
Normal file
188
scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java
Normal file
@@ -0,0 +1,188 @@
|
||||
package sonia.scm.web.i18n;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.github.legman.Subscribe;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
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 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.Iterator;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
/**
|
||||
* Collect the plugin translations.
|
||||
*/
|
||||
@Singleton
|
||||
@WebElement(value = I18nServlet.PATTERN, regex = true)
|
||||
@Slf4j
|
||||
public class I18nServlet extends HttpServlet {
|
||||
|
||||
public static final String PLUGINS_JSON = "plugins.json";
|
||||
public static final String PATTERN = "/locales/[a-z\\-A-Z]*/" + PLUGINS_JSON;
|
||||
public static final String CACHE_NAME = "sonia.cache.plugins.translations";
|
||||
|
||||
private final ClassLoader classLoader;
|
||||
private final Cache<String, JsonNode> cache;
|
||||
private static ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
|
||||
@Inject
|
||||
public I18nServlet(PluginLoader pluginLoader, CacheManager cacheManager) {
|
||||
this.classLoader = pluginLoader.getUberClassLoader();
|
||||
this.cache = cacheManager.getCache(CACHE_NAME);
|
||||
}
|
||||
|
||||
@Subscribe(async = false)
|
||||
public void handleRestartEvent(RestartEvent event) {
|
||||
log.debug("Clear cache on restart event with reason {}", event.getReason());
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
private JsonNode getCollectedJson(String path,
|
||||
Function<String, Optional<JsonNode>> jsonFileProvider,
|
||||
BiConsumer<String, JsonNode> createdJsonFileConsumer) {
|
||||
return Optional.ofNullable(jsonFileProvider.apply(path)
|
||||
.orElseGet(() -> {
|
||||
Optional<JsonNode> createdFile = collectJsonFile(path);
|
||||
createdFile.ifPresent(map -> createdJsonFileConsumer.accept(path, map));
|
||||
return createdFile.orElse(null);
|
||||
}
|
||||
)).orElseThrow(() -> NotFoundException.notFound("jsonprovider", path).build());
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse response) {
|
||||
try (PrintWriter out = response.getWriter()) {
|
||||
response.setContentType("application/json");
|
||||
String path = req.getServletPath();
|
||||
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);
|
||||
if (isProductionStage()) {
|
||||
log.debug("In Production Stage get the plugin translations from the cache");
|
||||
jsonFileProvider = usedPath -> Optional.ofNullable(
|
||||
cache.get(usedPath));
|
||||
createdJsonFileConsumer = createdJsonFileConsumer
|
||||
.andThen((usedPath, jsonNode) -> log.debug("Put the created json File in the cache with the key {}", usedPath))
|
||||
.andThen(cache::put);
|
||||
}
|
||||
objectMapper.writeValue(out, getCollectedJson(path, jsonFileProvider, createdJsonFileConsumer));
|
||||
} catch (IOException e) {
|
||||
log.error("Error on getting the translation of the plugins", e);
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
} catch (NotFoundException e) {
|
||||
log.error("Plugin translations are not found", e);
|
||||
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected boolean isProductionStage() {
|
||||
return SCMContext.getContext().getStage() == Stage.PRODUCTION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a collected Json File as JsonNode from the given path from all plugins in the class 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
|
||||
*/
|
||||
@VisibleForTesting
|
||||
protected Optional<JsonNode> collectJsonFile(String path) {
|
||||
log.debug("Collect plugin translations from path {} for every plugin", path);
|
||||
JsonNode mergedJsonNode = null;
|
||||
try {
|
||||
Enumeration<URL> resources = classLoader.getResources(path.replaceFirst("/", ""));
|
||||
while (resources.hasMoreElements()) {
|
||||
URL url = resources.nextElement();
|
||||
JsonNode jsonNode = objectMapper.readTree(url);
|
||||
if (mergedJsonNode != null) {
|
||||
merge(mergedJsonNode, jsonNode);
|
||||
} else {
|
||||
mergedJsonNode = jsonNode;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Error on loading sources from {}", path, e);
|
||||
return Optional.empty();
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user