in juneau-core/juneau-marshall/src/main/java/org/apache/juneau/objecttools/ObjectRest.java [760:894]
private Object service(int method, String url, Object val) throws ObjectRestException {
url = normalizeUrl(url);
if (method == GET) {
JsonNode p = getNode(url, root);
return p == null ? null : p.o;
}
// Get the url of the parent and the property name of the addressed object.
int i = url.lastIndexOf('/');
String parentUrl = (i == -1 ? null : url.substring(0, i));
String childKey = (i == -1 ? url : url.substring(i + 1));
if (method == PUT) {
if (url.isEmpty()) {
if (rootLocked)
throw new ObjectRestException(HTTP_FORBIDDEN, "Cannot overwrite root object");
Object o = root.o;
root = new JsonNode(null, null, val, session.object());
return o;
}
JsonNode n = (parentUrl == null ? root : getNode(parentUrl, root));
if (n == null)
throw new ObjectRestException(HTTP_NOT_FOUND, "Node at URL ''{0}'' not found.", parentUrl);
ClassMeta cm = n.cm;
Object o = n.o;
if (cm.isMap())
return ((Map)o).put(childKey, convert(val, cm.getValueType()));
if (cm.isCollection() && o instanceof List)
return ((List)o).set(parseInt(childKey), convert(val, cm.getElementType()));
if (cm.isArray()) {
o = setArrayEntry(n.o, parseInt(childKey), val, cm.getElementType());
ClassMeta pct = n.parent.cm;
Object po = n.parent.o;
if (pct.isMap()) {
((Map)po).put(n.keyName, o);
return url;
}
if (pct.isBean()) {
BeanMap m = session.toBeanMap(po);
m.put(n.keyName, o);
return url;
}
throw new ObjectRestException(HTTP_BAD_REQUEST, "Cannot perform PUT on ''{0}'' with parent node type ''{1}''", url, pct);
}
if (cm.isBean())
return session.toBeanMap(o).put(childKey, val);
throw new ObjectRestException(HTTP_BAD_REQUEST, "Cannot perform PUT on ''{0}'' whose parent is of type ''{1}''", url, cm);
}
if (method == POST) {
// Handle POST to root special
if (url.isEmpty()) {
ClassMeta cm = root.cm;
Object o = root.o;
if (cm.isCollection()) {
Collection c = (Collection)o;
c.add(convert(val, cm.getElementType()));
return (c instanceof List ? url + "/" + (c.size()-1) : null);
}
if (cm.isArray()) {
Object[] o2 = addArrayEntry(o, val, cm.getElementType());
root = new JsonNode(null, null, o2, null);
return url + "/" + (o2.length-1);
}
throw new ObjectRestException(HTTP_BAD_REQUEST, "Cannot perform POST on ''{0}'' of type ''{1}''", url, cm);
}
JsonNode n = getNode(url, root);
if (n == null)
throw new ObjectRestException(HTTP_NOT_FOUND, "Node at URL ''{0}'' not found.", url);
ClassMeta cm = n.cm;
Object o = n.o;
if (cm.isArray()) {
Object[] o2 = addArrayEntry(o, val, cm.getElementType());
ClassMeta pct = n.parent.cm;
Object po = n.parent.o;
if (pct.isMap()) {
((Map)po).put(childKey, o2);
return url + "/" + (o2.length-1);
}
if (pct.isBean()) {
BeanMap m = session.toBeanMap(po);
m.put(childKey, o2);
return url + "/" + (o2.length-1);
}
throw new ObjectRestException(HTTP_BAD_REQUEST, "Cannot perform POST on ''{0}'' with parent node type ''{1}''", url, pct);
}
if (cm.isCollection()) {
Collection c = (Collection)o;
c.add(convert(val, cm.getElementType()));
return (c instanceof List ? url + "/" + (c.size()-1) : null);
}
throw new ObjectRestException(HTTP_BAD_REQUEST, "Cannot perform POST on ''{0}'' of type ''{1}''", url, cm);
}
if (method == DELETE) {
if (url.isEmpty()) {
if (rootLocked)
throw new ObjectRestException(HTTP_FORBIDDEN, "Cannot overwrite root object");
Object o = root.o;
root = new JsonNode(null, null, null, session.object());
return o;
}
JsonNode n = (parentUrl == null ? root : getNode(parentUrl, root));
ClassMeta cm = n.cm;
Object o = n.o;
if (cm.isMap())
return ((Map)o).remove(childKey);
if (cm.isCollection() && o instanceof List)
return ((List)o).remove(parseInt(childKey));
if (cm.isArray()) {
int index = parseInt(childKey);
Object old = ((Object[])o)[index];
Object[] o2 = removeArrayEntry(o, index);
ClassMeta pct = n.parent.cm;
Object po = n.parent.o;
if (pct.isMap()) {
((Map)po).put(n.keyName, o2);
return old;
}
if (pct.isBean()) {
BeanMap m = session.toBeanMap(po);
m.put(n.keyName, o2);
return old;
}
throw new ObjectRestException(HTTP_BAD_REQUEST, "Cannot perform POST on ''{0}'' with parent node type ''{1}''", url, pct);
}
if (cm.isBean())
return session.toBeanMap(o).put(childKey, null);
throw new ObjectRestException(HTTP_BAD_REQUEST, "Cannot perform PUT on ''{0}'' whose parent is of type ''{1}''", url, cm);
}
return null; // Never gets here.
}