Search in sources :

Example 1 with NewFolderEvent

use of io.milton.event.NewFolderEvent 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 2 with NewFolderEvent

use of io.milton.event.NewFolderEvent in project lobcder by skoulouzis.

the class MkColHandler method processMakeCol.

private void processMakeCol(HttpManager manager, Request request, Response response, CollectionResource resource, String newName, CollectionResourceCreator creator) throws ConflictException, NotAuthorizedException, BadRequestException, IOException {
    if (!handlerHelper.checkAuthorisation(manager, resource, request)) {
        log.info("not authorised");
        responseHandler.respondUnauthorised(resource, response, request);
        return;
    }
    handlerHelper.checkExpects(responseHandler, request, response);
    MakeCollectionableResource existingCol = (MakeCollectionableResource) resource;
    if (!isCompatible(existingCol)) {
        log.info("not compatible");
        responseHandler.respondMethodNotImplemented(existingCol, response, request);
        return;
    }
    Resource existingChild = existingCol.child(newName);
    if (existingChild != null) {
        log.warn("found already existing item: " + newName + " of type: " + existingChild.getClass() + " with actual name: " + existingChild.getName());
        // throw new ConflictException( existingChild );
        // See http://www.ettrema.com:8080/browse/MIL-86
        // 405 (Method Not Allowed) - MKCOL can only be executed on a deleted/non-existent resource.
        responseHandler.respondMethodNotAllowed(existingChild, response, request);
        return;
    }
    CollectionResource made = creator.createResource(existingCol, newName, request);
    if (made == null) {
        log.warn("createCollection returned null. In resource class: " + existingCol.getClass());
        response.setStatus(Response.Status.SC_METHOD_NOT_ALLOWED);
    } else {
        log.info("created item ok: " + made.getClass());
        manager.getEventManager().fireEvent(new NewFolderEvent(resource));
        response.setStatus(Response.Status.SC_CREATED);
    }
}
Also used : CollectionResource(io.milton.resource.CollectionResource) NewFolderEvent(io.milton.event.NewFolderEvent) MakeCollectionableResource(io.milton.resource.MakeCollectionableResource) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource) MakeCollectionableResource(io.milton.resource.MakeCollectionableResource)

Aggregations

NewFolderEvent (io.milton.event.NewFolderEvent)2 CollectionResource (io.milton.resource.CollectionResource)2 MakeCollectionableResource (io.milton.resource.MakeCollectionableResource)2 Resource (io.milton.resource.Resource)2 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)1 GetableResource (io.milton.resource.GetableResource)1 PutableResource (io.milton.resource.PutableResource)1 ReplaceableResource (io.milton.resource.ReplaceableResource)1