Use namespace and name for exceptions instead of technical id

This commit is contained in:
René Pfeuffer
2019-03-05 10:42:06 +01:00
parent 4e6dd62cb5
commit 8928ca9676
6 changed files with 56 additions and 25 deletions

View File

@@ -3,8 +3,8 @@ package sonia.scm;
import com.github.sdorra.ssp.PermissionCheck;
import sonia.scm.util.AssertUtil;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class ManagerDaoAdapter<T extends ModelObject> {
@@ -35,15 +35,17 @@ public class ManagerDaoAdapter<T extends ModelObject> {
}
public T create(T newObject, Supplier<PermissionCheck> permissionCheck, AroundHandler<T> beforeCreate, AroundHandler<T> afterCreate) {
return create(newObject, permissionCheck, beforeCreate, afterCreate, dao::contains);
return create(newObject, permissionCheck, beforeCreate, afterCreate, o -> {
if (dao.contains(o)) {
throw new AlreadyExistsException(newObject);
}
});
}
public T create(T newObject, Supplier<PermissionCheck> permissionCheck, AroundHandler<T> beforeCreate, AroundHandler<T> afterCreate, Predicate<T> existsCheck) {
public T create(T newObject, Supplier<PermissionCheck> permissionCheck, AroundHandler<T> beforeCreate, AroundHandler<T> afterCreate, Consumer<T> existsCheck) {
permissionCheck.get().check();
AssertUtil.assertIsValid(newObject);
if (existsCheck.test(newObject)) {
throw new AlreadyExistsException(newObject);
}
existsCheck.accept(newObject);
newObject.setCreationDate(System.currentTimeMillis());
beforeCreate.handle(newObject);
dao.add(newObject);

View File

@@ -148,7 +148,8 @@ public class RepositoryResource {
return adapter.update(
loadBy(namespace, name),
existing -> processUpdate(repository, existing),
nameAndNamespaceStaysTheSame(namespace, name)
nameAndNamespaceStaysTheSame(namespace, name),
r -> r.getNamespaceAndName().logString()
);
}

View File

@@ -56,17 +56,21 @@ class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
}
/**
* Update the model object for the given id according to the given function and returns a corresponding http response.
* This handles all corner cases, eg. no matching object for the id or missing privileges.
* Updates the model object provided by the reader according to the given function and returns a corresponding http
* response. This handles all corner cases, eg. no matching object for the id or missing privileges.
*/
Response update(Supplier<MODEL_OBJECT> reader, Function<MODEL_OBJECT, MODEL_OBJECT> applyChanges, Predicate<MODEL_OBJECT> hasSameKey) {
return update(reader, applyChanges, hasSameKey, ModelObject::getId);
}
Response update(Supplier<MODEL_OBJECT> reader, Function<MODEL_OBJECT, MODEL_OBJECT> applyChanges, Predicate<MODEL_OBJECT> hasSameKey, Function<MODEL_OBJECT, String> keyExtractor) {
MODEL_OBJECT existingModelObject = reader.get();
MODEL_OBJECT changedModelObject = applyChanges.apply(existingModelObject);
if (!hasSameKey.test(changedModelObject)) {
return Response.status(BAD_REQUEST).entity("illegal change of id").build();
}
else if (modelObjectWasModifiedConcurrently(existingModelObject, changedModelObject)) {
throw new ConcurrentModificationException(type, existingModelObject.getId());
throw new ConcurrentModificationException(type, keyExtractor.apply(existingModelObject));
}
return update(getId(existingModelObject), changedModelObject);
}

View File

@@ -64,6 +64,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import static sonia.scm.AlreadyExistsException.alreadyExists;
import static sonia.scm.ContextEntry.ContextBuilder.entity;
import static sonia.scm.NotFoundException.notFound;
@@ -149,7 +150,11 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager {
}
}
},
newRepository -> repositoryDAO.contains(newRepository.getNamespaceAndName())
newRepository -> {
if (repositoryDAO.contains(newRepository.getNamespaceAndName())) {
throw alreadyExists(entity(newRepository.getClass(), newRepository.getNamespaceAndName().logString()));
}
}
);
}