JsonNode node = null;
try {
node = JsonFormat.deserializeNonStd(entityStream);
} catch (JsonParseException e) {
throw new ResourceException(e, BAD_REQUEST.getStatusCode());
}
if (!(node instanceof ObjectNode)) {
throw new ResourceException("Request body should be a JSON object.", BAD_REQUEST.getStatusCode());
}
ObjectNode postNode = (ObjectNode)node;
String action = JsonUtil.getString(postNode, "action");
List<MutationCondition> conditions;
Object entity = null;
try {
Namespaces namespaces = NamespacesConverter.fromContextJsonIfAvailable(postNode);
conditions = readMutationConditions(postNode, namespaces);
// Hardcoded behavior that action 'delete' does not need a submitted entity (and any other does)
if (!action.equals("delete")) {
EntityRegistry.RegistryEntry registryEntry = EntityRegistry.findReaderRegistryEntry((Class)entityType);
ObjectNode objectNode = JsonUtil.getObject(postNode, registryEntry.getPropertyName());
// Multiple repositories: ok to use public repo since only non-repository-specific things are needed
entity = EntityRegistry.findReader((Class)entityType).fromJson(objectNode, namespaces,
repositoryMgr.getDefaultRepository(), linkTransformer);
}
} catch (JsonFormatException e) {
throw new ResourceException("Error in submitted JSON.", e, BAD_REQUEST.getStatusCode());
} catch (Exception e) {
throw new ResourceException("Error reading submitted JSON.", e, INTERNAL_SERVER_ERROR.getStatusCode());
}
return new PostAction(action, entity, conditions);
}