Search in sources :

Example 1 with RequestBodyException

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);
}
Also used : Serializable(java.io.Serializable) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) RequestBodyException(io.crnk.core.exception.RequestBodyException) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds)

Example 2 with RequestBodyException

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;
}
Also used : RequestBodyException(io.crnk.core.exception.RequestBodyException) Resource(io.crnk.core.engine.document.Resource) RepositoryNotFoundException(io.crnk.core.exception.RepositoryNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Aggregations

RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)2 RequestBodyException (io.crnk.core.exception.RequestBodyException)2 Response (io.crnk.core.engine.dispatcher.Response)1 Document (io.crnk.core.engine.document.Document)1 Resource (io.crnk.core.engine.document.Resource)1 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 PathIds (io.crnk.core.engine.internal.dispatcher.path.PathIds)1 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)1 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)1 RepositoryNotFoundException (io.crnk.core.exception.RepositoryNotFoundException)1 Serializable (java.io.Serializable)1