mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-03 03:55:51 +01:00
Reduce code duplication for json object modification
This commit is contained in:
@@ -2,44 +2,39 @@ package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import sonia.scm.config.ConfigurationPermissions;
|
||||
import sonia.scm.plugin.Extension;
|
||||
import sonia.scm.web.JsonEnricher;
|
||||
import sonia.scm.web.JsonEnricherBase;
|
||||
import sonia.scm.web.JsonEnricherContext;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static sonia.scm.web.VndMediaType.INDEX;
|
||||
|
||||
@Extension
|
||||
public class SvnConfigInIndexResource implements JsonEnricher {
|
||||
public class SvnConfigInIndexResource extends JsonEnricherBase {
|
||||
|
||||
private final Provider<ScmPathInfoStore> scmPathInfoStore;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public SvnConfigInIndexResource(Provider<ScmPathInfoStore> scmPathInfoStore, ObjectMapper objectMapper) {
|
||||
super(objectMapper);
|
||||
this.scmPathInfoStore = scmPathInfoStore;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enrich(JsonEnricherContext context) {
|
||||
if (isIndexRequest(context) && ConfigurationPermissions.list().isPermitted()) {
|
||||
if (resultHasMediaType(INDEX, context) && ConfigurationPermissions.list().isPermitted()) {
|
||||
String gitConfigUrl = new LinkBuilder(scmPathInfoStore.get().get(), SvnConfigResource.class)
|
||||
.method("get")
|
||||
.parameters()
|
||||
.href();
|
||||
|
||||
ObjectNode gitConfigRefNode = objectMapper.createObjectNode();
|
||||
gitConfigRefNode.set("href", objectMapper.convertValue(gitConfigUrl, JsonNode.class));
|
||||
JsonNode gitConfigRefNode = createObject(singletonMap("href", value(gitConfigUrl)));
|
||||
|
||||
((ObjectNode) context.getResponseEntity().get("_links")).put("svnConfig", gitConfigRefNode);
|
||||
addPropertyNode(context.getResponseEntity().get("_links"), "svnConfig", gitConfigRefNode);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isIndexRequest(JsonEnricherContext context) {
|
||||
return VndMediaType.INDEX.equals(context.getResponseMediaType().toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user