Search in sources :

Example 16 with Resource

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

the class PutJsonResource method getName.

/**
 * We dont return anything, so best not use json
 *
 * @param accepts
 * @return
 */
// @Override
// public String getContentType(String accepts) {
// return "text/html";
// }
private String getName(String filename, Map<String, String> parameters) throws ConflictException, NotAuthorizedException, BadRequestException {
    String initialName = filename;
    if (parameters.containsKey(PARAM_NAME)) {
        initialName = parameters.get(PARAM_NAME);
    }
    boolean nonBlankName = initialName != null && initialName.trim().length() > 0;
    boolean autoname = (parameters.get(PARAM_AUTONAME) != null);
    boolean overwrite = (parameters.get(PARAM_OVERWRITE) != null);
    if (nonBlankName) {
        Resource child = wrapped.child(initialName);
        if (child == null) {
            log.trace("no existing file with that name");
            return initialName;
        } else {
            if (overwrite) {
                log.trace("file exists, and overwrite parameters is set, so allow overwrite: " + initialName);
                return initialName;
            } else {
                if (!autoname) {
                    log.warn("Conflict: Can't create resource with name " + initialName + " because it already exists. To rename automatically use request parameter: " + PARAM_AUTONAME + ", or to overwrite use " + PARAM_OVERWRITE);
                    throw new ConflictException(this);
                } else {
                    log.trace("file exists and autoname is set, so will find acceptable name");
                }
            }
        }
    } else {
        initialName = getDateAsName("upload");
        log.trace("no name given in request");
    }
    return findAcceptableName(initialName);
}
Also used : ConflictException(io.milton.http.exceptions.ConflictException) 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)

Example 17 with Resource

use of io.milton.resource.Resource 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 18 with Resource

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

the class AnnoPrincipalResource method getCalendarHomeSet.

@Override
public HrefList getCalendarHomeSet() {
    try {
        HrefList list = new HrefList();
        for (Resource r : getChildren()) {
            if (r instanceof AnnoCollectionResource) {
                AnnoCollectionResource col = (AnnoCollectionResource) r;
                if (annoFactory.calendarsAnnotationHandler.hasCalendars(col.getSource())) {
                    list.add(col.getHref());
                }
            }
        }
        if (list.isEmpty()) {
            ResourceList topDirs = getChildren().getDirs();
            if (topDirs.isEmpty()) {
                log.warn("Could not find any calendar home directories for user type: " + getSource().getClass() + ". In fact there are no child diretories at all!");
            } else {
                log.warn("Could not find any calendar home directories for user type: " + getSource().getClass() + " You should have a @" + Calendars.class + " annotation on one of the following methods");
                for (Resource r : topDirs) {
                    if (r instanceof AnnoCollectionResource) {
                        AnnoCollectionResource col = (AnnoCollectionResource) r;
                        List<ControllerMethod> candMethods = annoFactory.calendarsAnnotationHandler.getMethods(col.getSource().getClass());
                        if (candMethods.isEmpty()) {
                            log.info("	- inspecting: " + col.getName() + " for source: " + col.getSource().getClass() + " - has NO child methods");
                        } else {
                            log.info("	- inspecting: " + col.getName() + " for source: " + col.getSource().getClass());
                            for (ControllerMethod cm : candMethods) {
                                log.warn("	- candidate method: " + cm.controller.getClass() + "::" + cm.method.getName());
                            }
                        }
                    } else {
                        log.warn("	- found a directory which is not a AnnoCollectionResource: " + r.getClass() + " which cannot be inspected");
                    }
                }
            }
        }
        return list;
    } catch (NotAuthorizedException e) {
        throw new RuntimeException(e);
    } catch (BadRequestException e) {
        throw new RuntimeException(e);
    }
}
Also used : Resource(io.milton.resource.Resource) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) HrefList(io.milton.http.values.HrefList)

Example 19 with Resource

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

the class AnnotationResourceFactory method findFromRoot.

public Resource findFromRoot(AnnoCollectionResource rootFolder, Path p) throws NotAuthorizedException, BadRequestException {
    CollectionResource col = rootFolder;
    Resource r = null;
    for (String s : p.getParts()) {
        if (col == null) {
            if (log.isTraceEnabled()) {
                log.trace("findFromRoot: collection is null, can't look for child: " + s);
            }
            return null;
        }
        r = col.child(s);
        if (r == null) {
            if (log.isTraceEnabled()) {
                log.trace("findFromRoot: Couldnt find child: " + s + " of parent: " + col.getName() + " with type: " + col.getClass());
            }
            return null;
        } else {
            if (log.isTraceEnabled()) {
                if (r instanceof AnnoResource) {
                    AnnoResource ar = (AnnoResource) r;
                    log.trace("findFromRoot: found a child: " + r.getName() + " with source type: " + ar.getSource().getClass());
                } else {
                    log.trace("findFromRoot: found a child: " + r.getName() + " of type: " + r.getClass());
                }
            }
        }
        if (r instanceof CollectionResource) {
            col = (CollectionResource) r;
        } else {
            col = null;
        }
    }
    return r;
}
Also used : CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource) PropFindableResource(io.milton.resource.PropFindableResource) CollectionResource(io.milton.resource.CollectionResource)

Example 20 with Resource

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

the class AnnotationResourceFactory method getResource.

@Override
public Resource getResource(String host, String url) throws NotAuthorizedException, BadRequestException {
    log.info("getResource: host: " + host + " - url:" + url);
    AnnoCollectionResource hostRoot = locateHostRoot(host, HttpManager.request());
    if (hostRoot == null) {
        log.warn("Could not find a root resource for host: " + host + " Using " + rootAnnotationHandler.controllerMethods.size() + " root methods");
        return null;
    }
    Resource r;
    url = stripContext(url);
    if (url.equals("/") || url.equals("")) {
        r = hostRoot;
    } else {
        Path path = Path.path(url);
        r = findFromRoot(hostRoot, path);
        if (r == null) {
            log.info("Resource not found: host=" + host + " path=" + path);
        } else {
            if (r instanceof AnnoResource) {
                AnnoResource ar = (AnnoResource) r;
                log.info("Found AnnoResource: " + r.getClass() + "  for path=" + path + "  with source: " + ar.getSource());
            } else {
                log.info("Found resource: " + r.getClass() + "  for path=" + path);
            }
        }
    }
    return r;
}
Also used : Path(io.milton.common.Path) Resource(io.milton.resource.Resource) PropFindableResource(io.milton.resource.PropFindableResource) CollectionResource(io.milton.resource.CollectionResource)

Aggregations

Resource (io.milton.resource.Resource)37 CollectionResource (io.milton.resource.CollectionResource)23 BadRequestException (io.milton.http.exceptions.BadRequestException)11 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)10 Path (io.milton.common.Path)9 MakeCollectionableResource (io.milton.resource.MakeCollectionableResource)8 PutableResource (io.milton.resource.PutableResource)8 GetableResource (io.milton.resource.GetableResource)7 ConflictException (io.milton.http.exceptions.ConflictException)6 ReplaceableResource (io.milton.resource.ReplaceableResource)6 DeletableResource (io.milton.resource.DeletableResource)5 PostableResource (io.milton.resource.PostableResource)4 IOException (java.io.IOException)4 PutEvent (io.milton.event.PutEvent)3 LockableResource (io.milton.resource.LockableResource)3 PropFindableResource (io.milton.resource.PropFindableResource)3 NewFolderEvent (io.milton.event.NewFolderEvent)2 CopyableResource (io.milton.resource.CopyableResource)2 LockingCollectionResource (io.milton.resource.LockingCollectionResource)2 MoveableResource (io.milton.resource.MoveableResource)2