Search in sources :

Example 1 with MoveableResource

use of io.milton.resource.MoveableResource in project lobcder by skoulouzis.

the class MoveHandler method processExistingResource.

@Override
public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException {
    MoveableResource r = (MoveableResource) resource;
    Dest dest = Utils.getDecodedDestination(request.getDestinationHeader());
    Resource rDest = manager.getResourceFactory().getResource(dest.host, dest.url);
    log.debug("process: moving from: " + r.getName() + " -> " + dest.url + " with name: " + dest.name);
    if (rDest == null) {
        log.debug("process: destination parent does not exist: " + dest);
        responseHandler.respondConflict(resource, response, request, "Destination parent does not exist: " + dest);
    } else if (!(rDest instanceof CollectionResource)) {
        log.debug("process: destination exists but is not a collection");
        responseHandler.respondConflict(resource, response, request, "Destination exists but is not a collection: " + dest);
    } else {
        boolean wasDeleted = false;
        CollectionResource colDest = (CollectionResource) rDest;
        // check if the dest exists
        Resource rExisting = colDest.child(dest.name);
        if (rExisting != null) {
            // check for overwrite header
            if (!canOverwrite(request)) {
                log.info("destination resource exists, and overwrite header is not set. dest name: " + dest.name + " dest folder: " + colDest.getName());
                responseHandler.respondPreconditionFailed(request, response, rExisting);
                return;
            } else {
                if (deleteExistingBeforeMove) {
                    if (rExisting instanceof DeletableResource) {
                        log.debug("deleting existing resource");
                        DeletableResource drExisting = (DeletableResource) rExisting;
                        if (deleteHelper.isLockedOut(request, drExisting)) {
                            log.debug("destination resource exists but is locked");
                            responseHandler.respondLocked(request, response, drExisting);
                            return;
                        }
                        log.debug("deleting pre-existing destination resource");
                        deleteHelper.delete(drExisting, manager.getEventManager());
                        wasDeleted = true;
                    } else {
                        log.warn("destination exists, and overwrite header is set, but destination is not a DeletableResource");
                        responseHandler.respondConflict(resource, response, request, "A resource exists at the destination, and it cannot be deleted");
                        return;
                    }
                }
            }
        }
        log.debug("process: moving resource to: " + rDest.getName());
        try {
            if (!handlerHelper.checkAuthorisation(manager, colDest, request, request.getMethod(), request.getAuthorization())) {
                responseHandler.respondUnauthorised(colDest, response, request);
                return;
            }
            manager.getEventManager().fireEvent(new MoveEvent(resource, colDest, dest.name));
            r.moveTo(colDest, dest.name);
            // See http://www.ettrema.com:8080/browse/MIL-87
            if (wasDeleted) {
                responseHandler.respondNoContent(resource, response, request);
            } else {
                responseHandler.respondCreated(resource, response, request);
            }
        } catch (ConflictException ex) {
            log.warn("conflict", ex);
            responseHandler.respondConflict(resource, response, request, dest.toString());
        }
    }
    log.debug("process: finished");
}
Also used : CollectionResource(io.milton.resource.CollectionResource) MoveableResource(io.milton.resource.MoveableResource) ConflictException(io.milton.http.exceptions.ConflictException) DeletableResource(io.milton.resource.DeletableResource) Resource(io.milton.resource.Resource) CollectionResource(io.milton.resource.CollectionResource) MoveableResource(io.milton.resource.MoveableResource) DeletableResource(io.milton.resource.DeletableResource) MoveEvent(io.milton.event.MoveEvent)

Aggregations

MoveEvent (io.milton.event.MoveEvent)1 ConflictException (io.milton.http.exceptions.ConflictException)1 CollectionResource (io.milton.resource.CollectionResource)1 DeletableResource (io.milton.resource.DeletableResource)1 MoveableResource (io.milton.resource.MoveableResource)1 Resource (io.milton.resource.Resource)1