Simplify type support check (#1658)

Simplifies the class RepositoryTypeSupportChecker. There is no need to fall back to the super type Type`.
So we do not need to manually check for type safety.

Co-authored-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com>
This commit is contained in:
René Pfeuffer
2021-05-17 08:51:15 +02:00
committed by GitHub
parent a626c31ce5
commit 20bf646c4f
4 changed files with 16 additions and 22 deletions

View File

@@ -39,7 +39,6 @@ import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import sonia.scm.BadRequestException;
import sonia.scm.ConcurrentModificationException;
import sonia.scm.NotFoundException;
import sonia.scm.Type;
import sonia.scm.importexport.ExportFileExtensionResolver;
import sonia.scm.importexport.ExportNotificationHandler;
import sonia.scm.importexport.ExportService;
@@ -50,6 +49,7 @@ import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.RepositoryType;
import sonia.scm.repository.api.BundleCommandBuilder;
import sonia.scm.repository.api.Command;
import sonia.scm.repository.api.ExportFailedException;
@@ -442,7 +442,7 @@ public class RepositoryExportResource {
if (!type.equals(repository.getType())) {
throw new WrongTypeException(repository);
}
Type repositoryType = type(manager, type);
RepositoryType repositoryType = type(manager, type);
checkSupport(repositoryType, Command.BUNDLE);
return repository;
}

View File

@@ -28,7 +28,6 @@ import com.google.common.io.Files;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.Type;
import sonia.scm.event.ScmEventBus;
import sonia.scm.repository.ImportRepositoryHookEvent;
import sonia.scm.repository.InternalRepositoryException;
@@ -38,6 +37,7 @@ import sonia.scm.repository.RepositoryImportEvent;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryPermission;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.RepositoryType;
import sonia.scm.repository.api.Command;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
@@ -78,7 +78,7 @@ public class FromBundleImporter {
public Repository importFromBundle(boolean compressed, InputStream inputStream, Repository repository) {
RepositoryPermissions.create().check();
Type t = type(manager, repository.getType());
RepositoryType t = type(manager, repository.getType());
checkSupport(t, Command.UNBUNDLE);
repository.setPermissions(singletonList(

View File

@@ -31,7 +31,6 @@ import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.AlreadyExistsException;
import sonia.scm.Type;
import sonia.scm.event.ScmEventBus;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Repository;
@@ -39,6 +38,7 @@ import sonia.scm.repository.RepositoryImportEvent;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryPermission;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.RepositoryType;
import sonia.scm.repository.api.Command;
import sonia.scm.repository.api.ImportFailedException;
import sonia.scm.repository.api.PullCommandBuilder;
@@ -73,7 +73,7 @@ public class FromUrlImporter {
}
public Repository importFromUrl(RepositoryImportParameters parameters, Repository repository) {
Type t = type(manager, repository.getType());
RepositoryType t = type(manager, repository.getType());
RepositoryPermissions.create().check();
checkSupport(t, Command.PULL);

View File

@@ -27,14 +27,11 @@ package sonia.scm.importexport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.BadRequestException;
import sonia.scm.Type;
import sonia.scm.repository.RepositoryHandler;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.RepositoryType;
import sonia.scm.repository.api.Command;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import java.util.Set;
import static sonia.scm.ContextEntry.ContextBuilder.noContext;
@@ -44,7 +41,7 @@ public class RepositoryTypeSupportChecker {
private RepositoryTypeSupportChecker() {
}
private static final Logger logger = LoggerFactory.getLogger(RepositoryTypeSupportChecker.class);
private static final Logger LOG = LoggerFactory.getLogger(RepositoryTypeSupportChecker.class);
/**
* Check repository type for support for the given command.
@@ -52,31 +49,28 @@ public class RepositoryTypeSupportChecker {
* @param type repository type
* @param cmd command
*/
public static void checkSupport(Type type, Command cmd) {
if (!(type instanceof RepositoryType)) {
logger.warn("type {} is not a repository type", type.getName());
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}
Set<Command> cmds = ((RepositoryType) type).getSupportedCommands();
public static void checkSupport(RepositoryType type, Command cmd) {
Set<Command> cmds = type.getSupportedCommands();
if (!cmds.contains(cmd)) {
logger.warn("type {} does not support this command {}",
LOG.debug("type {} does not support this command {}",
type.getName(),
cmd.name());
cmd);
throw new IllegalTypeForImportException("type does not support command");
}
}
@SuppressWarnings("javasecurity:S5145") // the type parameter is validated in the resource to only contain valid characters (\w)
public static Type type(RepositoryManager manager, String type) {
@SuppressWarnings("javasecurity:S5145")
// the type parameter is validated in the resource to only contain valid characters (\w)
public static RepositoryType type(RepositoryManager manager, String type) {
RepositoryHandler handler = manager.getHandler(type);
if (handler == null) {
logger.warn("no handler for type {} found", type);
LOG.debug("no handler for type {} found", type);
throw new IllegalTypeForImportException("unsupported repository type: " + type);
}
return handler.getType();
}
@SuppressWarnings("java:S110") // this is fine for exceptions
private static class IllegalTypeForImportException extends BadRequestException {
public IllegalTypeForImportException(String message) {
super(noContext(), message);