mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-16 02:06:18 +01:00
Merged in feature/namespace_strategies (pull request #214)
Feature/namespace strategies
This commit is contained in:
@@ -35,7 +35,7 @@ public class ConfigDto extends HalRepresentation {
|
||||
private String pluginUrl;
|
||||
private long loginAttemptLimitTimeout;
|
||||
private boolean enabledXsrfProtection;
|
||||
private String defaultNamespaceStrategy;
|
||||
private String namespaceStrategy;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("squid:S1185") // We want to have this method available in this package
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||
import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||
import sonia.scm.config.ConfigurationPermissions;
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
import sonia.scm.repository.NamespaceStrategyValidator;
|
||||
import sonia.scm.util.ScmConfigurationUtil;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
@@ -27,12 +28,16 @@ public class ConfigResource {
|
||||
private final ConfigDtoToScmConfigurationMapper dtoToConfigMapper;
|
||||
private final ScmConfigurationToConfigDtoMapper configToDtoMapper;
|
||||
private final ScmConfiguration configuration;
|
||||
private final NamespaceStrategyValidator namespaceStrategyValidator;
|
||||
|
||||
@Inject
|
||||
public ConfigResource(ConfigDtoToScmConfigurationMapper dtoToConfigMapper, ScmConfigurationToConfigDtoMapper configToDtoMapper, ScmConfiguration configuration) {
|
||||
public ConfigResource(ConfigDtoToScmConfigurationMapper dtoToConfigMapper,
|
||||
ScmConfigurationToConfigDtoMapper configToDtoMapper,
|
||||
ScmConfiguration configuration, NamespaceStrategyValidator namespaceStrategyValidator) {
|
||||
this.dtoToConfigMapper = dtoToConfigMapper;
|
||||
this.configToDtoMapper = configToDtoMapper;
|
||||
this.configuration = configuration;
|
||||
this.namespaceStrategyValidator = namespaceStrategyValidator;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,6 +83,9 @@ public class ConfigResource {
|
||||
// But to where to check? load() or store()? Leave it for now, SCMv1 legacy that can be cleaned up later.
|
||||
ConfigurationPermissions.write(configuration).check();
|
||||
|
||||
// ensure the namespace strategy is valid
|
||||
namespaceStrategyValidator.check(configDto.getNamespaceStrategy());
|
||||
|
||||
ScmConfiguration config = dtoToConfigMapper.map(configDto);
|
||||
synchronized (ScmConfiguration.class) {
|
||||
configuration.load(config);
|
||||
|
||||
@@ -59,6 +59,9 @@ public class IndexDtoGenerator extends HalAppenderMapper {
|
||||
builder.single(link("permissions", resourceLinks.permissions().self()));
|
||||
}
|
||||
builder.single(link("availableRepositoryPermissions", resourceLinks.availableRepositoryPermissions().self()));
|
||||
|
||||
builder.single(link("repositoryTypes", resourceLinks.repositoryTypeCollection().self()));
|
||||
builder.single(link("namespaceStrategies", resourceLinks.namespaceStrategies().self()));
|
||||
} else {
|
||||
builder.single(link("login", resourceLinks.authentication().jsonLogin()));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
import de.otto.edison.hal.Links;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
public class NamespaceStrategiesDto extends HalRepresentation {
|
||||
|
||||
private String current;
|
||||
private List<String> available;
|
||||
|
||||
public NamespaceStrategiesDto(String current, List<String> available, Links links) {
|
||||
super(links);
|
||||
this.current = current;
|
||||
this.available = available;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.Links;
|
||||
import sonia.scm.repository.NamespaceStrategy;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* RESTFul WebService Endpoint for namespace strategies.
|
||||
*/
|
||||
@Path(NamespaceStrategyResource.PATH)
|
||||
public class NamespaceStrategyResource {
|
||||
|
||||
static final String PATH = "v2/namespaceStrategies";
|
||||
|
||||
private Set<NamespaceStrategy> namespaceStrategies;
|
||||
private Provider<NamespaceStrategy> namespaceStrategyProvider;
|
||||
|
||||
@Inject
|
||||
public NamespaceStrategyResource(Set<NamespaceStrategy> namespaceStrategies, Provider<NamespaceStrategy> namespaceStrategyProvider) {
|
||||
this.namespaceStrategies = namespaceStrategies;
|
||||
this.namespaceStrategyProvider = namespaceStrategyProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all available namespace strategies and the current selected.
|
||||
*
|
||||
* @param uriInfo uri info
|
||||
*
|
||||
* @return available and current namespace strategies
|
||||
*/
|
||||
@GET
|
||||
@Path("")
|
||||
@Produces(VndMediaType.NAMESPACE_STRATEGIES)
|
||||
public NamespaceStrategiesDto get(@Context UriInfo uriInfo) {
|
||||
String currentStrategy = strategyAsString(namespaceStrategyProvider.get());
|
||||
List<String> availableStrategies = collectStrategyNames();
|
||||
|
||||
return new NamespaceStrategiesDto(currentStrategy, availableStrategies, createLinks(uriInfo));
|
||||
}
|
||||
|
||||
private Links createLinks(@Context UriInfo uriInfo) {
|
||||
return Links.linkingTo().self(uriInfo.getAbsolutePath().toASCIIString()).build();
|
||||
}
|
||||
|
||||
private String strategyAsString(NamespaceStrategy namespaceStrategy) {
|
||||
return namespaceStrategy.getClass().getSimpleName();
|
||||
}
|
||||
|
||||
private List<String> collectStrategyNames() {
|
||||
return namespaceStrategies.stream().map(this::strategyAsString).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.validator.constraints.Email;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
import sonia.scm.util.ValidationUtil;
|
||||
|
||||
import javax.validation.constraints.Pattern;
|
||||
import java.time.Instant;
|
||||
@@ -25,8 +26,9 @@ public class RepositoryDto extends HalRepresentation {
|
||||
private List<HealthCheckFailureDto> healthCheckFailures;
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private Instant lastModified;
|
||||
// we could not validate the namespace, this must be done by the namespace strategy
|
||||
private String namespace;
|
||||
@Pattern(regexp = "^[A-z0-9\\-_]+$")
|
||||
@Pattern(regexp = ValidationUtil.REGEX_REPOSITORYNAME)
|
||||
private String name;
|
||||
private boolean archived = false;
|
||||
@NotEmpty
|
||||
|
||||
@@ -277,6 +277,23 @@ class ResourceLinks {
|
||||
}
|
||||
}
|
||||
|
||||
public NamespaceStrategiesLinks namespaceStrategies() {
|
||||
return new NamespaceStrategiesLinks(scmPathInfoStore.get());
|
||||
}
|
||||
|
||||
static class NamespaceStrategiesLinks {
|
||||
|
||||
private final LinkBuilder namespaceStrategiesLinkBuilder;
|
||||
|
||||
NamespaceStrategiesLinks(ScmPathInfo pathInfo) {
|
||||
namespaceStrategiesLinkBuilder = new LinkBuilder(pathInfo, NamespaceStrategyResource.class);
|
||||
}
|
||||
|
||||
String self() {
|
||||
return namespaceStrategiesLinkBuilder.method("get").parameters().href();
|
||||
}
|
||||
}
|
||||
|
||||
public RepositoryTypeLinks repositoryType() {
|
||||
return new RepositoryTypeLinks(scmPathInfoStore.get());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user