Search in sources :

Example 1 with StorageErrorReason

use of io.milton.http.quota.StorageChecker.StorageErrorReason 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)

Aggregations

Path (io.milton.common.Path)1 PutEvent (io.milton.event.PutEvent)1 StorageErrorReason (io.milton.http.quota.StorageChecker.StorageErrorReason)1 CollectionResource (io.milton.resource.CollectionResource)1 GetableResource (io.milton.resource.GetableResource)1 MakeCollectionableResource (io.milton.resource.MakeCollectionableResource)1 PutableResource (io.milton.resource.PutableResource)1 ReplaceableResource (io.milton.resource.ReplaceableResource)1 Resource (io.milton.resource.Resource)1