Search in sources :

Example 1 with DeletableResource

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

the class DeleteHelperImpl method isLockedOut.

@Override
public boolean isLockedOut(Request req, Resource r) throws NotAuthorizedException, BadRequestException {
    if (r instanceof DeletableCollectionResource) {
        DeletableCollectionResource dcr = (DeletableCollectionResource) r;
        boolean locked = dcr.isLockedOutRecursive(req);
        if (locked && log.isInfoEnabled()) {
            log.info("isLocked, as reported by DeletableCollectionResource: " + dcr.getName());
        }
        return locked;
    } else if (r instanceof CollectionResource) {
        CollectionResource col = (CollectionResource) r;
        List<Resource> list = new ArrayList<Resource>();
        list.addAll(col.getChildren());
        for (Resource rChild : list) {
            if (rChild instanceof DeletableResource) {
                DeletableResource rChildDel = (DeletableResource) rChild;
                if (isLockedOut(req, rChildDel)) {
                    if (log.isInfoEnabled()) {
                        log.info("isLocked: " + rChild.getName() + " type:" + rChild.getClass());
                    }
                    return true;
                }
            } else {
                if (log.isInfoEnabled()) {
                    log.info("a child resource is not deletable: " + rChild.getName() + " type: " + rChild.getClass());
                }
                return true;
            }
        }
        return false;
    } else {
        boolean locked = handlerHelper.isLockedOut(req, r);
        if (locked && log.isInfoEnabled()) {
            log.info("isLocked, as reported by handlerHelper on resource: " + r.getName());
        }
        return locked;
    }
}
Also used : CollectionResource(io.milton.resource.CollectionResource) DeletableCollectionResource(io.milton.resource.DeletableCollectionResource) DeletableCollectionResource(io.milton.resource.DeletableCollectionResource) DeletableResource(io.milton.resource.DeletableResource) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource) DeletableCollectionResource(io.milton.resource.DeletableCollectionResource) ArrayList(java.util.ArrayList) List(java.util.List) DeletableResource(io.milton.resource.DeletableResource)

Example 2 with DeletableResource

use of io.milton.resource.DeletableResource 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)

Example 3 with DeletableResource

use of io.milton.resource.DeletableResource 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)

Example 4 with DeletableResource

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

the class PutJsonResource method processForm.

@Override
public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws ConflictException, NotAuthorizedException, BadRequestException {
    log.info("processForm: " + wrapped.getClass());
    if (files.isEmpty()) {
        log.debug("no files uploaded");
        return null;
    }
    newFiles = new ArrayList<NewFile>();
    for (FileItem file : files.values()) {
        NewFile nf = new NewFile();
        String ua = HttpManager.request().getUserAgentHeader();
        String f = Utils.truncateFileName(ua, file.getName());
        nf.setOriginalName(f);
        nf.setContentType(file.getContentType());
        nf.setLength(file.getSize());
        newFiles.add(nf);
        String newName = getName(f, parameters);
        log.info("creating resource: " + newName + " size: " + file.getSize());
        InputStream in = null;
        Resource newResource;
        try {
            in = file.getInputStream();
            Resource existing = wrapped.child(newName);
            if (existing != null) {
                if (existing instanceof ReplaceableResource) {
                    log.trace("existing resource is replaceable, so replace content");
                    ReplaceableResource rr = (ReplaceableResource) existing;
                    rr.replaceContent(in, null);
                    log.trace("completed POST processing for file. Updated: " + existing.getName());
                    eventManager.fireEvent(new PutEvent(rr));
                    newResource = rr;
                } else {
                    log.trace("existing resource is not replaceable, will be deleted");
                    if (existing instanceof DeletableResource) {
                        DeletableResource dr = (DeletableResource) existing;
                        dr.delete();
                        newResource = wrapped.createNew(newName, in, file.getSize(), file.getContentType());
                        log.trace("completed POST processing for file. Deleted, then created: " + newResource.getName());
                        eventManager.fireEvent(new PutEvent(newResource));
                    } else {
                        throw new BadRequestException(existing, "existing resource could not be deleted, is not deletable");
                    }
                }
            } else {
                newResource = wrapped.createNew(newName, in, file.getSize(), file.getContentType());
                log.info("completed POST processing for file. Created: " + newResource.getName());
                eventManager.fireEvent(new PutEvent(newResource));
            }
            String newHref = buildNewHref(href, newResource.getName());
            nf.setHref(newHref);
        } catch (NotAuthorizedException ex) {
            throw new RuntimeException(ex);
        } catch (BadRequestException ex) {
            throw new RuntimeException(ex);
        } catch (ConflictException ex) {
            throw new RuntimeException(ex);
        } catch (IOException ex) {
            throw new RuntimeException("Exception creating resource", ex);
        } finally {
            FileUtils.close(in);
        }
    }
    log.trace("completed all POST processing");
    return null;
}
Also used : ConflictException(io.milton.http.exceptions.ConflictException) InputStream(java.io.InputStream) PutEvent(io.milton.event.PutEvent) DeletableResource(io.milton.resource.DeletableResource) PutableResource(io.milton.resource.PutableResource) ReplaceableResource(io.milton.resource.ReplaceableResource) Resource(io.milton.resource.Resource) PostableResource(io.milton.resource.PostableResource) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) IOException(java.io.IOException) ReplaceableResource(io.milton.resource.ReplaceableResource) BadRequestException(io.milton.http.exceptions.BadRequestException) DeletableResource(io.milton.resource.DeletableResource)

Example 5 with DeletableResource

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

the class DeleteHandler method processExistingResource.

@Override
public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException {
    log.debug("DELETE: " + request.getAbsoluteUrl());
    DeletableResource r = (DeletableResource) resource;
    if (deleteHelper.isLockedOut(request, r)) {
        log.info("Could not delete. Is locked");
        responseHandler.respondDeleteFailed(request, response, r, Status.SC_LOCKED);
        return;
    }
    deleteHelper.delete(r, manager.getEventManager());
    log.debug("deleted ok");
    responseHandler.respondNoContent(resource, response, request);
}
Also used : DeletableResource(io.milton.resource.DeletableResource)

Aggregations

DeletableResource (io.milton.resource.DeletableResource)5 Resource (io.milton.resource.Resource)4 CollectionResource (io.milton.resource.CollectionResource)3 ConflictException (io.milton.http.exceptions.ConflictException)2 MoveEvent (io.milton.event.MoveEvent)1 PutEvent (io.milton.event.PutEvent)1 BadRequestException (io.milton.http.exceptions.BadRequestException)1 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)1 CopyableResource (io.milton.resource.CopyableResource)1 DeletableCollectionResource (io.milton.resource.DeletableCollectionResource)1 MoveableResource (io.milton.resource.MoveableResource)1 PostableResource (io.milton.resource.PostableResource)1 PutableResource (io.milton.resource.PutableResource)1 ReplaceableResource (io.milton.resource.ReplaceableResource)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1