use of io.milton.http.webdav.PropFindResponse.NameAndError in project lobcder by skoulouzis.
the class JsonPropPatchHandler method process.
public PropFindResponse process(Resource wrappedResource, String encodedUrl, Map<String, String> params) throws NotAuthorizedException, ConflictException, BadRequestException {
log.trace("process");
Map<QName, String> fields = new HashMap<QName, String>();
for (String fieldName : params.keySet()) {
String sFieldValue = params.get(fieldName);
QName qn;
if (fieldName.contains(":")) {
// name is of form uri:local E.g. MyDav:authorName
String[] parts = fieldName.split(":");
String nsUri = parts[0];
String localName = parts[1];
qn = new QName(nsUri, localName);
} else {
// name is simple form E.g. displayname, default nsUri to DAV
qn = new QName(WebDavProtocol.NS_DAV.getPrefix(), fieldName);
}
log.debug("field: " + qn);
fields.put(qn, sFieldValue);
}
ParseResult parseResult = new ParseResult(fields, null);
if (log.isTraceEnabled()) {
log.trace("check permissions with: " + permissionService.getClass());
}
Set<PropertyAuthoriser.CheckResult> errorFields = permissionService.checkPermissions(HttpManager.request(), Method.PROPPATCH, PropertyAuthoriser.PropertyPermission.WRITE, fields.keySet(), wrappedResource);
if (errorFields != null && errorFields.size() > 0) {
log.info("authorisation errors: " + errorFields.size() + " from permissionService: " + permissionService.getClass());
if (log.isTraceEnabled()) {
for (CheckResult e : errorFields) {
LogUtils.trace(log, " - field error: ", e.getField(), e.getStatus(), e.getDescription());
}
}
throw new NotAuthorizedException(wrappedResource);
} else {
LogUtils.trace(log, "setting properties with", patchSetter.getClass());
PropFindResponse resp = patchSetter.setProperties(encodedUrl, parseResult, wrappedResource);
if (eventManager != null) {
log.trace("fire event");
eventManager.fireEvent(new PropPatchEvent(wrappedResource, resp));
} else {
log.trace("no event manager");
}
if (resp.getErrorProperties().size() > 0) {
LogUtils.warn(log, "Encountered errors setting fields with patch setter", patchSetter.getClass());
}
if (log.isTraceEnabled()) {
if (resp.getErrorProperties().size() > 0) {
for (List<NameAndError> e : resp.getErrorProperties().values()) {
for (NameAndError ne : e) {
LogUtils.trace(log, " - field error setting properties: ", ne.getName(), ne.getError());
}
}
}
}
return resp;
}
}
use of io.milton.http.webdav.PropFindResponse.NameAndError in project lobcder by skoulouzis.
the class PropFindXmlGeneratorHelper method appendResponse.
public void appendResponse(XmlWriter writer, PropFindResponse r, Map<String, String> mapOfNamespaces) {
XmlWriter.Element el = writer.begin(WebDavProtocol.NS_DAV.getPrefix(), "response");
el.open();
writer.writeProperty(WebDavProtocol.NS_DAV.getPrefix(), "href", r.getHref());
sendKnownProperties(writer, mapOfNamespaces, r.getKnownProperties(), r.getHref());
if (r.getErrorProperties() != null) {
for (Status status : r.getErrorProperties().keySet()) {
List<NameAndError> props = r.getErrorProperties().get(status);
sendErrorProperties(status, writer, mapOfNamespaces, props);
}
}
el.close();
}
use of io.milton.http.webdav.PropFindResponse.NameAndError in project lobcder by skoulouzis.
the class PropertySourcePatchSetter method addErrorProp.
private void addErrorProp(Map<Status, List<NameAndError>> errorProps, Status stat, QName name, String err) {
List<NameAndError> list = errorProps.get(stat);
if (list == null) {
list = new ArrayList<NameAndError>();
errorProps.put(stat, list);
}
NameAndError ne = new NameAndError(name, err);
list.add(ne);
}
use of io.milton.http.webdav.PropFindResponse.NameAndError in project lobcder by skoulouzis.
the class PropFindXmlGeneratorHelper method sendErrorProperties.
private void sendErrorProperties(Response.Status status, XmlWriter writer, Map<String, String> mapOfNamespaces, List<NameAndError> properties) {
// log.debug( "sendUnknownProperties: " + properties.size() );
if (!properties.isEmpty()) {
XmlWriter.Element elPropStat = writer.begin(WebDavProtocol.NS_DAV.getPrefix(), "propstat").open();
XmlWriter.Element elProp = writer.begin(WebDavProtocol.NS_DAV.getPrefix(), "prop").open();
for (NameAndError ne : properties) {
QName qname = ne.getName();
String prefix = mapOfNamespaces.get(qname.getNamespaceURI());
writer.writeProperty(prefix, qname.getLocalPart());
}
elProp.close();
writer.writeProperty(WebDavProtocol.NS_DAV.getPrefix(), "status", status.toString());
elPropStat.close();
}
}
use of io.milton.http.webdav.PropFindResponse.NameAndError 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;
}
Aggregations