Search in sources :

Example 1 with CollectionResource

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

the class PutHandler method process.

@Override
public void process(HttpManager manager, Request request, Response response) throws NotAuthorizedException, ConflictException, BadRequestException, NotFoundException {
    if (!handlerHelper.checkExpects(responseHandler, request, response)) {
        return;
    }
    String host = request.getHostHeader();
    String urlToCreateOrUpdate = HttpManager.decodeUrl(request.getAbsolutePath());
    LogUtils.debug(log, "PUT request. Host:", host, " Url:", urlToCreateOrUpdate, " content length header:", request.getContentLengthHeader());
    Path path = Path.path(urlToCreateOrUpdate);
    urlToCreateOrUpdate = path.toString();
    Resource existingResource = manager.getResourceFactory().getResource(host, urlToCreateOrUpdate);
    StorageErrorReason storageErr = null;
    if (existingResource != null) {
        // Make sure the parent collection is not locked by someone else
        if (handlerHelper.isLockedOut(request, existingResource)) {
            log.warn("resource is locked, but not by the current user");
            String value = request.getIfHeader();
            if (value != null) {
                response.setStatus(Status.SC_PRECONDITION_FAILED);
                return;
            } else {
                respondLocked(request, response, existingResource);
                return;
            }
        }
        // Check if the resource has been modified based on etags
        if (!matchHelper.checkIfMatch(existingResource, request)) {
            log.info("if-match comparison failed, aborting PUT request");
            responseHandler.respondPreconditionFailed(request, response, existingResource);
            return;
        }
        if (matchHelper.checkIfNoneMatch(existingResource, request)) {
            log.info("if-none-match comparison failed, aborting PUT request");
            responseHandler.respondPreconditionFailed(request, response, existingResource);
            return;
        }
        Resource parent = manager.getResourceFactory().getResource(host, path.getParent().toString());
        // }
        if (parent instanceof CollectionResource) {
            CollectionResource parentCol = (CollectionResource) parent;
            storageErr = handlerHelper.checkStorageOnReplace(request, parentCol, existingResource, host);
        } else {
            log.warn("parent exists but is not a collection resource: " + path.getParent());
        }
    } else {
        if (!matchHelper.checkIfMatch(null, request)) {
            log.info("if-match comparison failed on null resource, aborting PUT request");
            responseHandler.respondPreconditionFailed(request, response, existingResource);
            return;
        }
        if (matchHelper.checkIfNoneMatch(null, request)) {
            log.info("if-none-match comparison failed on null resource, aborting PUT request");
            responseHandler.respondPreconditionFailed(request, response, existingResource);
            return;
        }
        CollectionResource parentCol = putHelper.findNearestParent(manager, host, path);
        storageErr = handlerHelper.checkStorageOnAdd(request, parentCol, path.getParent(), host);
    }
    if (storageErr != null) {
        respondInsufficientStorage(request, response, storageErr);
        return;
    }
    ReplaceableResource replacee;
    if (existingResource != null && existingResource instanceof ReplaceableResource) {
        replacee = (ReplaceableResource) existingResource;
    } else {
        replacee = null;
    }
    if (replacee != null) {
        if (log.isTraceEnabled()) {
            log.trace("replacing content in: " + replacee.getName() + " - " + replacee.getClass());
        }
        long t = System.currentTimeMillis();
        try {
            manager.onProcessResourceStart(request, response, replacee);
            processReplace(manager, request, response, replacee);
            manager.getEventManager().fireEvent(new PutEvent(replacee));
        } finally {
            t = System.currentTimeMillis() - t;
            manager.onProcessResourceFinish(request, response, replacee, t);
        }
    } else {
        // either no existing resource, or its not replaceable. check for folder
        String nameToCreate = path.getName();
        CollectionResource folderResource = findOrCreateFolders(manager, host, path.getParent(), request);
        if (folderResource != null) {
            long t = System.currentTimeMillis();
            try {
                if (folderResource instanceof PutableResource) {
                    // Make sure the parent collection is not locked by someone else
                    if (handlerHelper.isLockedOut(request, folderResource)) {
                        respondLocked(request, response, folderResource);
                        return;
                    }
                    PutableResource putableResource = (PutableResource) folderResource;
                    processCreate(manager, request, response, putableResource, nameToCreate);
                } else {
                    LogUtils.debug(log, "method not implemented: PUT on class: ", folderResource.getClass(), folderResource.getName());
                    manager.getResponseHandler().respondMethodNotImplemented(folderResource, response, request);
                }
            } finally {
                t = System.currentTimeMillis() - t;
                manager.onProcessResourceFinish(request, response, folderResource, t);
            }
        } else {
            responseHandler.respondNotFound(response, request);
        }
    }
}
Also used : Path(io.milton.common.Path) CollectionResource(io.milton.resource.CollectionResource) StorageErrorReason(io.milton.http.quota.StorageChecker.StorageErrorReason) PutEvent(io.milton.event.PutEvent) PutableResource(io.milton.resource.PutableResource) ReplaceableResource(io.milton.resource.ReplaceableResource) Resource(io.milton.resource.Resource) GetableResource(io.milton.resource.GetableResource) MakeCollectionableResource(io.milton.resource.MakeCollectionableResource) CollectionResource(io.milton.resource.CollectionResource) PutableResource(io.milton.resource.PutableResource) ReplaceableResource(io.milton.resource.ReplaceableResource)

Example 2 with CollectionResource

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

the class PutHandler method processExistingResource.

public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException, NotFoundException {
    String host = request.getHostHeader();
    String urlToCreateOrUpdate = HttpManager.decodeUrl(request.getAbsolutePath());
    log.debug("process request: host: " + host + " url: " + urlToCreateOrUpdate);
    Path path = Path.path(urlToCreateOrUpdate);
    urlToCreateOrUpdate = path.toString();
    Resource existingResource = manager.getResourceFactory().getResource(host, urlToCreateOrUpdate);
    ReplaceableResource replacee;
    if (existingResource != null) {
        // Make sure the parent collection is not locked by someone else
        if (handlerHelper.isLockedOut(request, existingResource)) {
            log.warn("resource is locked, but not by the current user");
            // 423
            response.setStatus(Status.SC_LOCKED);
            return;
        }
    }
    if (existingResource != null && existingResource instanceof ReplaceableResource) {
        replacee = (ReplaceableResource) existingResource;
    } else {
        replacee = null;
    }
    if (replacee != null) {
        processReplace(manager, request, response, (ReplaceableResource) existingResource);
    } else {
        // either no existing resource, or its not replaceable. check for folder
        String urlFolder = path.getParent().toString();
        String nameToCreate = path.getName();
        CollectionResource folderResource = findOrCreateFolders(manager, host, path.getParent(), request);
        if (folderResource != null) {
            if (log.isDebugEnabled()) {
                log.debug("found folder: " + urlFolder + " - " + folderResource.getClass());
            }
            if (folderResource instanceof PutableResource) {
                // Make sure the parent collection is not locked by someone else
                if (handlerHelper.isLockedOut(request, folderResource)) {
                    // 423
                    response.setStatus(Status.SC_LOCKED);
                    return;
                }
                PutableResource putableResource = (PutableResource) folderResource;
                processCreate(manager, request, response, putableResource, nameToCreate);
            } else {
                responseHandler.respondMethodNotImplemented(folderResource, response, request);
            }
        } else {
            responseHandler.respondNotFound(response, request);
        }
    }
}
Also used : Path(io.milton.common.Path) CollectionResource(io.milton.resource.CollectionResource) PutableResource(io.milton.resource.PutableResource) ReplaceableResource(io.milton.resource.ReplaceableResource) Resource(io.milton.resource.Resource) GetableResource(io.milton.resource.GetableResource) MakeCollectionableResource(io.milton.resource.MakeCollectionableResource) CollectionResource(io.milton.resource.CollectionResource) PutableResource(io.milton.resource.PutableResource) ReplaceableResource(io.milton.resource.ReplaceableResource)

Example 3 with CollectionResource

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

the class JsonPropFindHandler method sendContent.

public void sendContent(PropFindableResource wrappedResource, String encodedUrl, OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException, NotAuthorizedException, BadRequestException {
    log.debug("sendContent: " + encodedUrl);
    JsonConfig cfg = new JsonConfig();
    cfg.setIgnoreTransientFields(true);
    cfg.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
    JSON json;
    Writer writer = new PrintWriter(out);
    String[] arr;
    if (propertyBuilder == null) {
        if (wrappedResource instanceof CollectionResource) {
            List<? extends Resource> children = ((CollectionResource) wrappedResource).getChildren();
            json = JSONSerializer.toJSON(toSimpleList(children), cfg);
        } else {
            json = JSONSerializer.toJSON(toSimple(wrappedResource), cfg);
        }
    } else {
        // use propfind handler
        String sFields = params.get("fields");
        Set<QName> fields = new HashSet<QName>();
        Map<QName, String> aliases = new HashMap<QName, String>();
        if (sFields != null && sFields.length() > 0) {
            arr = sFields.split(",");
            for (String s : arr) {
                parseField(s, fields, aliases);
            }
        }
        String sDepth = params.get("depth");
        int depth = 1;
        if (sDepth != null && sDepth.trim().length() > 0) {
            depth = Integer.parseInt(sDepth);
        }
        String href = encodedUrl.replace("/_DAV/PROPFIND", "");
        PropertiesRequest parseResult = new PropertiesRequest(toProperties(fields));
        LogUtils.debug(log, "prop builder: ", propertyBuilder.getClass(), "href", href);
        List<PropFindResponse> props;
        try {
            props = propertyBuilder.buildProperties(wrappedResource, depth, parseResult, href);
        } catch (URISyntaxException ex) {
            throw new RuntimeException("Requested url is not properly encoded: " + href, ex);
        }
        String where = params.get("where");
        filterResults(props, where);
        List<Map<String, Object>> list = helper.toMap(props, aliases);
        json = JSONSerializer.toJSON(list, cfg);
    }
    json.write(writer);
    writer.flush();
}
Also used : CollectionResource(io.milton.resource.CollectionResource) HashMap(java.util.HashMap) JsonConfig(net.sf.json.JsonConfig) QName(javax.xml.namespace.QName) JSON(net.sf.json.JSON) URISyntaxException(java.net.URISyntaxException) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Example 4 with CollectionResource

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

the class PutHandler method findOrCreateFolders.

private CollectionResource findOrCreateFolders(HttpManager manager, String host, Path path, Request request) throws NotAuthorizedException, ConflictException, BadRequestException {
    if (path == null) {
        return null;
    }
    Resource thisResource = manager.getResourceFactory().getResource(host, path.toString());
    if (thisResource != null) {
        // return the wrong resource for a given path
        if (thisResource.getName() != null && !thisResource.getName().equals(path.getName())) {
            log.warn("Your resource factory returned a resource with a different name to that requested!!! Requested: " + path.getName() + " returned: " + thisResource.getName() + " - resource factory: " + manager.getResourceFactory().getClass());
        }
        if (thisResource instanceof CollectionResource) {
            return (CollectionResource) thisResource;
        } else {
            log.warn("parent is not a collection: " + path);
            return null;
        }
    }
    CollectionResource parent = findOrCreateFolders(manager, host, path.getParent(), request);
    if (parent == null) {
        log.warn("couldnt find parent: " + path);
    // return null;
    }
    Resource r = parent.child(path.getName());
    if (r == null) {
        log.info("Could not find child: " + path.getName() + " in parent: " + parent.getName() + " - " + parent.getClass());
        if (parent instanceof MakeCollectionableResource) {
            MakeCollectionableResource mkcol = (MakeCollectionableResource) parent;
            if (!handlerHelper.checkAuthorisation(manager, mkcol, request)) {
                throw new NotAuthorizedException(mkcol);
            }
            log.info("autocreating new folder: " + path.getName());
            CollectionResource newCol = mkcol.createCollection(path.getName());
            manager.getEventManager().fireEvent(new NewFolderEvent(newCol));
            return newCol;
        } else {
            log.info("parent folder isnt a MakeCollectionableResource: " + parent.getName() + " - " + parent.getClass());
            return null;
        }
    } else if (r instanceof CollectionResource) {
        return (CollectionResource) r;
    } else {
        log.info("parent in URL is not a collection: " + r.getName());
        return null;
    }
}
Also used : CollectionResource(io.milton.resource.CollectionResource) NewFolderEvent(io.milton.event.NewFolderEvent) PutableResource(io.milton.resource.PutableResource) ReplaceableResource(io.milton.resource.ReplaceableResource) Resource(io.milton.resource.Resource) GetableResource(io.milton.resource.GetableResource) MakeCollectionableResource(io.milton.resource.MakeCollectionableResource) CollectionResource(io.milton.resource.CollectionResource) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) MakeCollectionableResource(io.milton.resource.MakeCollectionableResource)

Example 5 with CollectionResource

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

the class FckQuickUploaderResource method processFileUpload.

private void processFileUpload(FileItem f, Map<String, String> params) throws BadRequestException, NotAuthorizedException {
    CollectionResource target = null;
    if (wrappedResource == null) {
        throw new BadRequestException(this, "collection not found");
    }
    target = (CollectionResource) wrappedResource.child("uploads");
    if (target == null) {
        try {
            if (wrappedResource instanceof MakeCollectionableResource) {
                MakeCollectionableResource mk = (MakeCollectionableResource) wrappedResource;
                target = mk.createCollection("uploads");
            } else {
                throw new BadRequestException(target, "Cant create subfolder");
            }
        } catch (ConflictException ex) {
            throw new RuntimeException(ex);
        } catch (NotAuthorizedException ex) {
            throw new RuntimeException(ex);
        } catch (BadRequestException ex) {
            throw new RuntimeException(ex);
        }
    }
    String name = FileUtils.sanitiseName(f.getName());
    log.debug("processFileUpload: " + name);
    boolean isFirst = true;
    String newName = null;
    while (target.child(name) != null) {
        name = FileUtils.incrementFileName(name, isFirst);
        newName = name;
        isFirst = false;
    }
    long size = f.getSize();
    try {
        if (target instanceof PutableResource) {
            PutableResource putable = (PutableResource) target;
            Resource newRes = putable.createNew(name, f.getInputStream(), size, null);
            if (newRes != null) {
                log.trace("created: " + newRes.getName() + " of type: " + newRes.getClass());
            } else {
                log.trace("createNew returned null");
            }
        } else {
            throw new BadRequestException(target, "Does not implement PutableResource");
        }
    } catch (ConflictException ex) {
        throw new RuntimeException(ex);
    } catch (NotAuthorizedException ex) {
        throw new RuntimeException(ex);
    } catch (BadRequestException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    try {
        if (newName != null) {
            // we renamed the file
            uploadResponseOk(name);
        } else {
            uploadResponseOk();
        }
    } catch (Throwable ex) {
        log.error("Exception saving new file", ex);
        uploadResponseFailed(ex.getMessage());
    }
}
Also used : CollectionResource(io.milton.resource.CollectionResource) ConflictException(io.milton.http.exceptions.ConflictException) PutableResource(io.milton.resource.PutableResource) MakeCollectionableResource(io.milton.resource.MakeCollectionableResource) Resource(io.milton.resource.Resource) CollectionResource(io.milton.resource.CollectionResource) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) IOException(java.io.IOException) BadRequestException(io.milton.http.exceptions.BadRequestException) MakeCollectionableResource(io.milton.resource.MakeCollectionableResource) PutableResource(io.milton.resource.PutableResource)

Aggregations

CollectionResource (io.milton.resource.CollectionResource)19 Resource (io.milton.resource.Resource)16 MakeCollectionableResource (io.milton.resource.MakeCollectionableResource)7 Path (io.milton.common.Path)6 BadRequestException (io.milton.http.exceptions.BadRequestException)5 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)5 GetableResource (io.milton.resource.GetableResource)5 PutableResource (io.milton.resource.PutableResource)5 ConflictException (io.milton.http.exceptions.ConflictException)4 DeletableResource (io.milton.resource.DeletableResource)3 ReplaceableResource (io.milton.resource.ReplaceableResource)3 QName (javax.xml.namespace.QName)3 NewFolderEvent (io.milton.event.NewFolderEvent)2 CopyableResource (io.milton.resource.CopyableResource)2 MoveableResource (io.milton.resource.MoveableResource)2 PostableResource (io.milton.resource.PostableResource)2 PropFindableResource (io.milton.resource.PropFindableResource)2 ArrayList (java.util.ArrayList)2 FtpException (org.apache.ftpserver.ftplet.FtpException)2 MoveEvent (io.milton.event.MoveEvent)1