Search in sources :

Example 1 with CopyableResource

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

the class CopyHandler method processExistingResource.

@Override
public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException {
    CopyableResource r = (CopyableResource) resource;
    Dest dest = Utils.getDecodedDestination(request.getDestinationHeader());
    Resource rDest = manager.getResourceFactory().getResource(dest.host, dest.url);
    log.debug("process: copying from: " + r.getName() + " -> " + dest.url + "/" + dest.name);
    if (rDest == null) {
        log.debug("process: destination parent does not exist: " + dest);
        responseHandler.respondConflict(resource, response, request, "Destination 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 {
        log.debug("process: copy resource to: " + rDest.getName());
        Resource fDest = manager.getResourceFactory().getResource(dest.host, dest.url + "/" + dest.name);
        if (handlerHelper.isLockedOut(request, fDest)) {
            responseHandler.respondLocked(request, response, resource);
            return;
        } else {
            boolean wasDeleted = false;
            CollectionResource colDest = (CollectionResource) rDest;
            Resource rExisting = colDest.child(dest.name);
            if (rExisting != null) {
                if (!canOverwrite(request)) {
                    // Exists, and overwrite = F, disallow - http://www.webdav.org/specs/rfc4918.html#rfc.section.9.8.4
                    log.info("destination resource exists, and overwrite header is not set. dest name: " + dest.name + " dest folder: " + colDest.getName());
                    responseHandler.respondPreconditionFailed(request, response, resource);
                    return;
                } else {
                    // Overwrite is absent or T, so continue
                    if (deleteHelper.isLockedOut(request, rExisting)) {
                        log.info("destination resource exists, and overwrite header IS set, but destination is locked. dest name: " + dest.name + " dest folder: " + colDest.getName());
                        responseHandler.respondPreconditionFailed(request, response, resource);
                        return;
                    } else {
                        if (deleteExistingBeforeCopy) {
                            if (rExisting instanceof DeletableResource) {
                                log.debug("copy destination exists and is deletable, delete it..");
                                DeletableResource dr = (DeletableResource) rExisting;
                                // Check the user can delete
                                if (!handlerHelper.checkAuthorisation(manager, dr, request, Method.DELETE, request.getAuthorization())) {
                                    responseHandler.respondUnauthorised(colDest, response, request);
                                    return;
                                }
                                deleteHelper.delete(dr, manager.getEventManager());
                                wasDeleted = true;
                            } else {
                                log.warn("copy destination exists and is a collection so must be deleted, but does not implement: " + DeletableResource.class);
                                responseHandler.respondConflict(rExisting, response, request, dest.toString());
                                return;
                            }
                        }
                    }
                }
            }
            // the resource identified in the dest header
            if (!handlerHelper.checkAuthorisation(manager, colDest, request, request.getMethod(), request.getAuthorization())) {
                responseHandler.respondUnauthorised(colDest, response, request);
                return;
            }
            r.copyTo(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);
            }
        }
    }
}
Also used : CollectionResource(io.milton.resource.CollectionResource) CopyableResource(io.milton.resource.CopyableResource) DeletableResource(io.milton.resource.DeletableResource) Resource(io.milton.resource.Resource) CollectionResource(io.milton.resource.CollectionResource) CopyableResource(io.milton.resource.CopyableResource) DeletableResource(io.milton.resource.DeletableResource)

Aggregations

CollectionResource (io.milton.resource.CollectionResource)1 CopyableResource (io.milton.resource.CopyableResource)1 DeletableResource (io.milton.resource.DeletableResource)1 Resource (io.milton.resource.Resource)1