mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Clean up
This commit is contained in:
@@ -1,14 +1,10 @@
|
|||||||
package sonia.scm.api.v2.resources;
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
import de.otto.edison.hal.HalRepresentation;
|
import de.otto.edison.hal.HalRepresentation;
|
||||||
import org.apache.shiro.authz.AuthorizationException;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import sonia.scm.ConcurrentModificationException;
|
import sonia.scm.ConcurrentModificationException;
|
||||||
import sonia.scm.Manager;
|
import sonia.scm.Manager;
|
||||||
import sonia.scm.ModelObject;
|
import sonia.scm.ModelObject;
|
||||||
import sonia.scm.NotFoundException;
|
import sonia.scm.NotFoundException;
|
||||||
import sonia.scm.api.rest.RestExceptionResult;
|
|
||||||
|
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -32,8 +28,6 @@ import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
|
|||||||
class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
|
class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
|
||||||
DTO extends HalRepresentation> {
|
DTO extends HalRepresentation> {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(SingleResourceManagerAdapter.class);
|
|
||||||
|
|
||||||
private final Function<Throwable, Optional<Response>> errorHandler;
|
private final Function<Throwable, Optional<Response>> errorHandler;
|
||||||
protected final Manager<MODEL_OBJECT> manager;
|
protected final Manager<MODEL_OBJECT> manager;
|
||||||
protected final Class<MODEL_OBJECT> type;
|
protected final Class<MODEL_OBJECT> type;
|
||||||
@@ -76,36 +70,18 @@ class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
|
|||||||
else if (modelObjectWasModifiedConcurrently(existingModelObject, changedModelObject)) {
|
else if (modelObjectWasModifiedConcurrently(existingModelObject, changedModelObject)) {
|
||||||
throw new ConcurrentModificationException(type, keyExtractor.apply(existingModelObject));
|
throw new ConcurrentModificationException(type, keyExtractor.apply(existingModelObject));
|
||||||
}
|
}
|
||||||
return update(getId(existingModelObject), changedModelObject);
|
return update(changedModelObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Response update(String name, MODEL_OBJECT item)
|
private Response update(MODEL_OBJECT item) {
|
||||||
{
|
try {
|
||||||
Response response = null;
|
|
||||||
|
|
||||||
preUpdate(item);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
manager.modify(item);
|
manager.modify(item);
|
||||||
response = Response.noContent().build();
|
return Response.noContent().build();
|
||||||
|
} catch (RuntimeException ex) {
|
||||||
|
return createErrorResponse(ex);
|
||||||
}
|
}
|
||||||
catch (AuthorizationException ex)
|
|
||||||
{
|
|
||||||
LOG.warn("update not allowed", ex);
|
|
||||||
response = Response.status(Response.Status.FORBIDDEN).build();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
LOG.error("error during update", ex);
|
|
||||||
response = createErrorResponse(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void preUpdate(MODEL_OBJECT item) {}
|
|
||||||
|
|
||||||
private boolean modelObjectWasModifiedConcurrently(MODEL_OBJECT existing, MODEL_OBJECT updated) {
|
private boolean modelObjectWasModifiedConcurrently(MODEL_OBJECT existing, MODEL_OBJECT updated) {
|
||||||
return existing.getLastModified() != null
|
return existing.getLastModified() != null
|
||||||
&& (updated.getLastModified() == null || existing.getLastModified() > updated.getLastModified());
|
&& (updated.getLastModified() == null || existing.getLastModified() > updated.getLastModified());
|
||||||
@@ -120,48 +96,24 @@ class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Response delete(String name)
|
public Response delete(String name) {
|
||||||
{
|
|
||||||
Response response = null;
|
|
||||||
MODEL_OBJECT item = manager.get(name);
|
MODEL_OBJECT item = manager.get(name);
|
||||||
|
|
||||||
if (item != null)
|
if (item != null) {
|
||||||
{
|
try {
|
||||||
preDelete(item);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
manager.delete(item);
|
manager.delete(item);
|
||||||
response = Response.noContent().build();
|
return Response.noContent().build();
|
||||||
}
|
} catch (RuntimeException ex) {
|
||||||
catch (AuthorizationException ex)
|
return createErrorResponse(ex);
|
||||||
{
|
|
||||||
LOG.warn("delete not allowd", ex);
|
|
||||||
response = Response.status(Response.Status.FORBIDDEN).build();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
LOG.error("error during delete", ex);
|
|
||||||
response = createErrorResponse(ex);
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return Response.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void preDelete(MODEL_OBJECT item) {}
|
private Response createErrorResponse(RuntimeException throwable) {
|
||||||
|
|
||||||
|
|
||||||
protected Response createErrorResponse(Throwable throwable) {
|
|
||||||
return errorHandler.apply(throwable)
|
return errorHandler.apply(throwable)
|
||||||
.orElse(createErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, throwable.getMessage(), throwable));
|
.orElseThrow(() -> throwable);
|
||||||
}
|
|
||||||
|
|
||||||
protected Response createErrorResponse(Response.Status status, String message,
|
|
||||||
Throwable throwable)
|
|
||||||
{
|
|
||||||
return Response.status(status).entity(new RestExceptionResult(message,
|
|
||||||
throwable)).build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getId(MODEL_OBJECT item) {
|
protected String getId(MODEL_OBJECT item) {
|
||||||
|
|||||||
Reference in New Issue
Block a user