Search in sources :

Example 1 with ReplaceableResource

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

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

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

the class MiltonFtpFile method isWritable.

/**
 * Check file write permission.
 */
@Override
public boolean isWritable() {
    try {
        log.debug("isWritable: " + getAbsolutePath());
        if (path.isRoot())
            return false;
        Auth auth = new Auth(user.getName(), user.getUser());
        FtpRequest request = new FtpRequest(Method.DELETE, auth, path.toString());
        if (r != null) {
            if (r instanceof ReplaceableResource) {
                return r.authorise(request, Method.PUT, auth);
            }
        }
        if (getParent() instanceof PutableResource) {
            return getParent().authorise(request, Method.PUT, auth);
        } else {
            return false;
        }
    } catch (NotAuthorizedException ex) {
        throw new RuntimeException(ex);
    } catch (BadRequestException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Auth(io.milton.http.Auth) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) ReplaceableResource(io.milton.resource.ReplaceableResource)

Example 4 with ReplaceableResource

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

the class MiltonFtpFile method createOutputStream.

@Override
public OutputStream createOutputStream(long offset) throws IOException {
    log.debug("createOutputStream: " + offset);
    final BufferingOutputStream out = new BufferingOutputStream(50000);
    if (r instanceof ReplaceableResource) {
        log.debug("resource is replaceable");
        final ReplaceableResource rr = (ReplaceableResource) r;
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    rr.replaceContent(out.getInputStream(), out.getSize());
                } catch (BadRequestException ex) {
                    throw new RuntimeException(ex);
                } catch (ConflictException ex) {
                    throw new RuntimeException(ex);
                } catch (NotAuthorizedException ex) {
                    throw new RuntimeException(ex);
                }
            }
        };
        out.setOnClose(runnable);
        return out;
    } else {
        CollectionResource col;
        try {
            col = getParent();
        } catch (NotAuthorizedException ex) {
            throw new RuntimeException(ex);
        } catch (BadRequestException ex) {
            throw new RuntimeException(ex);
        }
        if (col == null) {
            throw new IOException("parent not found");
        } else if (col instanceof PutableResource) {
            final PutableResource putableResource = (PutableResource) col;
            final String newName = path.getName();
            Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    try {
                        putableResource.createNew(newName, out.getInputStream(), out.getSize(), null);
                    } catch (BadRequestException ex) {
                        throw new RuntimeException(ex);
                    } catch (NotAuthorizedException ex) {
                        throw new RuntimeException(ex);
                    } catch (ConflictException ex) {
                        throw new RuntimeException(ex);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            };
            out.setOnClose(runnable);
            return out;
        } else {
            throw new IOException("folder doesnt support PUT, and the resource is not replaceable");
        }
    }
}
Also used : ConflictException(io.milton.http.exceptions.ConflictException) BufferingOutputStream(io.milton.common.BufferingOutputStream) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) IOException(java.io.IOException) ReplaceableResource(io.milton.resource.ReplaceableResource)

Example 5 with ReplaceableResource

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

Aggregations

ReplaceableResource (io.milton.resource.ReplaceableResource)5 BadRequestException (io.milton.http.exceptions.BadRequestException)3 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)3 PutableResource (io.milton.resource.PutableResource)3 Resource (io.milton.resource.Resource)3 Path (io.milton.common.Path)2 PutEvent (io.milton.event.PutEvent)2 ConflictException (io.milton.http.exceptions.ConflictException)2 CollectionResource (io.milton.resource.CollectionResource)2 GetableResource (io.milton.resource.GetableResource)2 MakeCollectionableResource (io.milton.resource.MakeCollectionableResource)2 IOException (java.io.IOException)2 BufferingOutputStream (io.milton.common.BufferingOutputStream)1 Auth (io.milton.http.Auth)1 StorageErrorReason (io.milton.http.quota.StorageChecker.StorageErrorReason)1 DeletableResource (io.milton.resource.DeletableResource)1 PostableResource (io.milton.resource.PostableResource)1 InputStream (java.io.InputStream)1