use of io.crnk.core.exception.RequestBodyException in project crnk-framework by crnk-project.
the class RelationshipsResourceUpsert method handle.
@Override
public final Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
String resourceName = jsonPath.getResourceType();
PathIds resourceIds = jsonPath.getIds();
RegistryEntry registryEntry = getRegistryEntry(resourceName);
assertRequestDocument(requestBody, HttpMethod.POST, resourceName);
Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(jsonPath.getElementName());
verifyFieldNotNull(relationshipField, jsonPath.getElementName());
ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
@SuppressWarnings("unchecked") Object resource = resourceRepository.findOne(castedResourceId, queryAdapter).getEntity();
ResourceInformation targetInformation = getRegistryEntry(relationshipField.getOppositeResourceType()).getResourceInformation();
@SuppressWarnings("unchecked") RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry.getRelationshipRepository(relationshipField, parameterProvider);
if (Iterable.class.isAssignableFrom(relationshipField.getType())) {
Iterable<ResourceIdentifier> dataBodies = (Iterable<ResourceIdentifier>) (requestBody.isMultiple() ? requestBody.getData().get() : Collections.singletonList(requestBody.getData().get()));
processToManyRelationship(resource, targetInformation, relationshipField, dataBodies, queryAdapter, relationshipRepositoryForClass);
} else {
if (requestBody.isMultiple()) {
throw new RequestBodyException(HttpMethod.POST, resourceName, "Multiple data in body");
}
ResourceIdentifier dataBody = (ResourceIdentifier) requestBody.getData().get();
processToOneRelationship(resource, targetInformation, relationshipField, dataBody, queryAdapter, relationshipRepositoryForClass);
}
return new Response(new Document(), HttpStatus.NO_CONTENT_204);
}
use of io.crnk.core.exception.RequestBodyException in project crnk-framework by crnk-project.
the class ResourceUpsert method getRequestBody.
protected Resource getRequestBody(Document requestDocument, JsonPath path, HttpMethod method) {
String resourceType = path.getResourceType();
assertRequestDocument(requestDocument, method, resourceType);
if (!requestDocument.getData().isPresent() || requestDocument.getData().get() == null) {
throw new RequestBodyException(method, resourceType, "No data field in the body.");
}
if (requestDocument.getData().get() instanceof Collection) {
throw new RequestBodyException(method, resourceType, "Multiple data in body");
}
Resource resourceBody = (Resource) requestDocument.getData().get();
RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType());
if (bodyRegistryEntry == null) {
throw new RepositoryNotFoundException(resourceBody.getType());
}
return resourceBody;
}
Aggregations