Search in sources :

Example 16 with CollectionResource

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

the class DefaultPropFindPropertyBuilder method processResource.

@Override
public void processResource(List<PropFindResponse> responses, PropFindableResource resource, PropertiesRequest parseResult, String href, int requestedDepth, int currentDepth, String collectionHref) throws NotAuthorizedException, BadRequestException {
    final LinkedHashMap<QName, ValueAndType> knownProperties = new LinkedHashMap<QName, ValueAndType>();
    final ArrayList<NameAndError> unknownProperties = new ArrayList<NameAndError>();
    if (resource instanceof CollectionResource) {
        if (!href.endsWith("/")) {
            href = href + "/";
        }
    }
    Set<QName> requestedFields;
    if (parseResult.isAllProp()) {
        requestedFields = findAllProps(resource);
    } else {
        requestedFields = parseResult.getNames();
    }
    Iterator<QName> it = requestedFields.iterator();
    while (it.hasNext()) {
        QName field = it.next();
        LogUtils.trace(log, "processResoource: find property:", field);
        if (field.getLocalPart().equals("href")) {
            knownProperties.put(field, new ValueAndType(href, String.class));
        } else {
            boolean found = false;
            for (PropertySource source : propertySources) {
                LogUtils.trace(log, "look for field", field, " in property source", source.getClass());
                PropertyMetaData meta = source.getPropertyMetaData(field, resource);
                if (meta != null && !meta.isUnknown()) {
                    Object val;
                    try {
                        val = source.getProperty(field, resource);
                        LogUtils.trace(log, "processResource: got value", val, "from source", source.getClass());
                        if (val == null) {
                            // null, but we still need type information to write it so use meta
                            knownProperties.put(field, new ValueAndType(val, meta.getValueType()));
                        } else {
                            // non-null, so use more robust class info
                            knownProperties.put(field, new ValueAndType(val, val.getClass()));
                        }
                    } catch (NotAuthorizedException ex) {
                        unknownProperties.add(new NameAndError(field, "Not authorised"));
                    }
                    found = true;
                    break;
                }
            }
            if (!found) {
                if (log.isDebugEnabled()) {
                    log.debug("property not found in any property source: " + field.toString());
                }
                unknownProperties.add(new NameAndError(field, null));
            }
        }
    }
    if (log.isDebugEnabled()) {
        if (unknownProperties.size() > 0) {
            log.debug("some properties could not be resolved. Listing property sources:");
            for (PropertySource ps : propertySources) {
                log.debug(" - " + ps.getClass().getCanonicalName());
            }
        }
    }
    // Map<Status, List<NameAndError>> errorProperties = new HashMap<Status, List<NameAndError>>();
    Map<Status, List<NameAndError>> errorProperties = new EnumMap<Status, List<NameAndError>>(Status.class);
    errorProperties.put(Status.SC_NOT_FOUND, unknownProperties);
    PropFindResponse r = new PropFindResponse(href, knownProperties, errorProperties);
    responses.add(r);
    if (requestedDepth > currentDepth && resource instanceof CollectionResource) {
        CollectionResource col = (CollectionResource) resource;
        List<? extends Resource> list = col.getChildren();
        list = new ArrayList<Resource>(list);
        for (Resource child : list) {
            if (child instanceof PropFindableResource) {
                String childName = child.getName();
                if (childName == null) {
                    log.warn("null name for resource of type: " + child.getClass() + " in folder: " + href + " WILL NOT be returned in PROPFIND response!!");
                } else {
                    String childHref = href + Utils.percentEncode(childName);
                    // Note that the new collection href, is just the current href
                    processResource(responses, (PropFindableResource) child, parseResult, childHref, requestedDepth, currentDepth + 1, href);
                }
            }
        }
    }
}
Also used : CollectionResource(io.milton.resource.CollectionResource) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) PropertySource(io.milton.property.PropertySource) PropFindableResource(io.milton.resource.PropFindableResource) Status(io.milton.http.Response.Status) ValueAndType(io.milton.http.values.ValueAndType) QName(javax.xml.namespace.QName) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource) PropFindableResource(io.milton.resource.PropFindableResource) PropertyMetaData(io.milton.property.PropertySource.PropertyMetaData) NameAndError(io.milton.http.webdav.PropFindResponse.NameAndError)

Example 17 with CollectionResource

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

the class CopyJsonResource method processForm.

public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws BadRequestException, NotAuthorizedException {
    String dest = parameters.get("destination");
    Path pDest = Path.path(dest);
    Resource rDestParent = resourceFactory.getResource(host, pDest.getParent().toString());
    if (rDestParent == null)
        throw new BadRequestException(wrapped, "The destination parent does not exist");
    if (rDestParent instanceof CollectionResource) {
        CollectionResource colDestParent = (CollectionResource) rDestParent;
        if (colDestParent.child(pDest.getName()) == null) {
            try {
                wrapped.copyTo(colDestParent, pDest.getName());
            } catch (ConflictException ex) {
                log.warn("Exception copying to: " + pDest.getName(), ex);
                throw new BadRequestException(rDestParent, "conflict: " + ex.getMessage());
            }
            return null;
        } else {
            log.warn("destination already exists: " + pDest.getName());
            throw new BadRequestException(rDestParent, "File already exists");
        }
    } else {
        throw new BadRequestException(wrapped, "The destination parent is not a collection resource");
    }
}
Also used : Path(io.milton.common.Path) CollectionResource(io.milton.resource.CollectionResource) ConflictException(io.milton.http.exceptions.ConflictException) CopyableResource(io.milton.resource.CopyableResource) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource) PostableResource(io.milton.resource.PostableResource) BadRequestException(io.milton.http.exceptions.BadRequestException)

Example 18 with CollectionResource

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

the class PutHelper method findNearestParent.

public CollectionResource findNearestParent(HttpManager manager, String host, Path path) throws NotAuthorizedException, ConflictException, BadRequestException {
    if (path == null) {
        return null;
    }
    Resource thisResource = manager.getResourceFactory().getResource(host, path.toString());
    if (thisResource != null) {
        if (thisResource instanceof CollectionResource) {
            return (CollectionResource) thisResource;
        } else {
            log.warn("parent is not a collection: " + path);
            return null;
        }
    }
    CollectionResource parent = findNearestParent(manager, host, path.getParent());
    return parent;
}
Also used : CollectionResource(io.milton.resource.CollectionResource) GetableResource(io.milton.resource.GetableResource) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource)

Example 19 with CollectionResource

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

the class FckFileManagerResource method find.

private Resource find(CollectionResource wrappedResource, Path p) throws NotAuthorizedException, BadRequestException {
    Resource r = wrappedResource;
    for (String s : p.getParts()) {
        if (r instanceof CollectionResource) {
            CollectionResource col = (CollectionResource) r;
            r = col.child(s);
            if (r == null) {
                log.trace("not found: " + s + " in path: " + p);
                return null;
            }
        } else {
            log.trace("not a collection: " + r.getName() + " in path: " + p);
            return null;
        }
    }
    return r;
}
Also used : CollectionResource(io.milton.resource.CollectionResource) PutableResource(io.milton.resource.PutableResource) GetableResource(io.milton.resource.GetableResource) MakeCollectionableResource(io.milton.resource.MakeCollectionableResource) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource)

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