Search in sources :

Example 1 with PropertySetException

use of io.milton.property.PropertySource.PropertySetException in project lobcder by skoulouzis.

the class PropertySourcePatchSetter method setProperties.

@Override
public PropFindResponse setProperties(String href, ParseResult parseResult, Resource r) throws NotAuthorizedException, BadRequestException {
    log.trace("setProperties: resource type: {}", r.getClass());
    Map<QName, ValueAndType> knownProps = new HashMap<QName, ValueAndType>();
    Map<Status, List<NameAndError>> errorProps = new EnumMap<Status, List<NameAndError>>(Status.class);
    for (Entry<QName, String> entry : parseResult.getFieldsToSet().entrySet()) {
        QName name = entry.getKey();
        boolean found = false;
        for (PropertySource source : propertySources) {
            PropertyMetaData meta = source.getPropertyMetaData(entry.getKey(), r);
            if (meta != null && !meta.isUnknown()) {
                found = true;
                if (meta.isWritable()) {
                    Object val = parse(name, entry.getValue(), meta.getValueType());
                    try {
                        log.trace("setProperties: name: {} source: {}", name, source.getClass());
                        source.setProperty(name, val, r);
                        knownProps.put(name, new ValueAndType(null, meta.getValueType()));
                        break;
                    } catch (NotAuthorizedException e) {
                        log.warn("setProperties: NotAuthorised to write property: {}", name, e);
                        addErrorProp(errorProps, Response.Status.SC_UNAUTHORIZED, name, "Not authorised");
                        break;
                    } catch (PropertySetException ex) {
                        log.warn("setProperties: PropertySetException when writing property {}", name, ex);
                        addErrorProp(errorProps, ex.getStatus(), name, ex.getErrorNotes());
                        break;
                    }
                } else {
                    log.warn("property is not writable in source: " + source.getClass());
                    addErrorProp(errorProps, Response.Status.SC_FORBIDDEN, name, "Property is read only");
                    break;
                }
            } else {
            // log.debug( "not found in: " + source.getClass().getCanonicalName() );
            }
        }
        if (!found) {
            log.warn("property not found: " + entry.getKey() + " on resource: " + r.getClass());
            addErrorProp(errorProps, Status.SC_NOT_FOUND, entry.getKey(), "Unknown property");
        }
    }
    if (parseResult.getFieldsToRemove() != null) {
        for (QName name : parseResult.getFieldsToRemove()) {
            boolean found = false;
            for (PropertySource source : propertySources) {
                PropertyMetaData meta = source.getPropertyMetaData(name, r);
                if (meta != null && !meta.isUnknown()) {
                    found = true;
                    if (meta.isWritable()) {
                        try {
                            log.trace("clearProperty");
                            source.clearProperty(name, r);
                            knownProps.put(name, new ValueAndType(null, meta.getValueType()));
                            break;
                        } catch (NotAuthorizedException e) {
                            addErrorProp(errorProps, Response.Status.SC_UNAUTHORIZED, name, "Not authorised");
                            break;
                        } catch (PropertySetException ex) {
                            addErrorProp(errorProps, ex.getStatus(), name, ex.getErrorNotes());
                            break;
                        }
                    } else {
                        log.warn("property is not writable in source: " + source.getClass());
                        addErrorProp(errorProps, Response.Status.SC_FORBIDDEN, name, "Property is read only");
                        break;
                    }
                } else {
                // log.debug( "not found in: " + source.getClass().getCanonicalName() );
                }
            }
            if (!found) {
                log.warn("property not found to remove: " + name);
                addErrorProp(errorProps, Status.SC_NOT_FOUND, name, "Unknown property");
            }
        }
    }
    if (log.isDebugEnabled()) {
        if (errorProps.size() > 0) {
            log.debug("errorProps: " + errorProps.size() + " listing property sources:");
            for (PropertySource s : propertySources) {
                log.debug("  source: " + s.getClass().getCanonicalName());
            }
        }
    }
    if (r instanceof CommitableResource) {
        log.trace("resource is commitable, call doCommit");
        CommitableResource cr = (CommitableResource) r;
        cr.doCommit(knownProps, errorProps);
    } else {
        log.trace("resource is not commitable");
    }
    PropFindResponse resp = new PropFindResponse(href, knownProps, errorProps);
    return resp;
}
Also used : Status(io.milton.http.Response.Status) ValueAndType(io.milton.http.values.ValueAndType) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) PropertyMetaData(io.milton.property.PropertySource.PropertyMetaData) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) PropertySource(io.milton.property.PropertySource) NameAndError(io.milton.http.webdav.PropFindResponse.NameAndError) PropertySetException(io.milton.property.PropertySource.PropertySetException) ArrayList(java.util.ArrayList) List(java.util.List) EnumMap(java.util.EnumMap)

Aggregations

Status (io.milton.http.Response.Status)1 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)1 ValueAndType (io.milton.http.values.ValueAndType)1 NameAndError (io.milton.http.webdav.PropFindResponse.NameAndError)1 PropertySource (io.milton.property.PropertySource)1 PropertyMetaData (io.milton.property.PropertySource.PropertyMetaData)1 PropertySetException (io.milton.property.PropertySource.PropertySetException)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 QName (javax.xml.namespace.QName)1