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

@@ -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);
}