Fix handling of RepositoryIsNotArchivedException

This commit is contained in:
René Pfeuffer
2018-08-02 08:56:03 +02:00
parent d49c07b992
commit f191f2833d
4 changed files with 53 additions and 18 deletions

View File

@@ -374,20 +374,6 @@ public abstract class AbstractManagerResource<T extends ModelObject,
throwable.getMessage(), throwable);
}
/**
* Method description
*
*
* @param status
* @param throwable
*
* @return
*/
protected Response createErrorResponse(Status status, Throwable throwable)
{
return createErrorResponse(status, throwable.getMessage(), throwable);
}
/**
* Method description
*

View File

@@ -5,12 +5,19 @@ import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryException;
import sonia.scm.repository.RepositoryIsNotArchivedException;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.web.VndMediaType;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.*;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import java.util.Optional;
import java.util.function.Predicate;
@@ -40,7 +47,7 @@ public class RepositoryResource {
this.dtoToRepositoryMapper = dtoToRepositoryMapper;
this.manager = manager;
this.repositoryToDtoMapper = repositoryToDtoMapper;
this.adapter = new SingleResourceManagerAdapter<>(manager, Repository.class);
this.adapter = new SingleResourceManagerAdapter<>(manager, Repository.class, this::handleNotArchived);
this.tagRootResource = tagRootResource;
this.branchRootResource = branchRootResource;
this.changesetRootResource = changesetRootResource;
@@ -148,6 +155,14 @@ public class RepositoryResource {
return permissionRootResource.get();
}
private Optional<Response> handleNotArchived(Throwable throwable) {
if (throwable instanceof RepositoryIsNotArchivedException) {
return Optional.of(Response.status(Response.Status.PRECONDITION_FAILED).build());
} else {
return Optional.empty();
}
}
private Supplier<Optional<Repository>> loadBy(String namespace, String name) {
return () -> manager.getByNamespace(namespace, name);
}

View File

@@ -31,8 +31,15 @@ class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
DTO extends HalRepresentation,
EXCEPTION extends Exception> extends AbstractManagerResource<MODEL_OBJECT, EXCEPTION> {
private final Function<Throwable, Optional<Response>> errorHandler;
SingleResourceManagerAdapter(Manager<MODEL_OBJECT, EXCEPTION> manager, Class<MODEL_OBJECT> type) {
this(manager, type, e -> Optional.empty());
}
SingleResourceManagerAdapter(Manager<MODEL_OBJECT, EXCEPTION> manager, Class<MODEL_OBJECT> type, Function<Throwable, Optional<Response>> errorHandler) {
super(manager, type);
this.errorHandler = errorHandler;
}
/**
@@ -70,6 +77,11 @@ class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
.orElse(null);
}
@Override
protected Response createErrorResponse(Throwable throwable) {
return errorHandler.apply(throwable).orElse(super.createErrorResponse(throwable));
}
@Override
protected GenericEntity<Collection<MODEL_OBJECT>> createGenericEntity(Collection<MODEL_OBJECT> modelObjects) {
throw new UnsupportedOperationException();