Search in sources :

Example 36 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class HttpRequestProcessorImpl method dispatchRequest.

/**
 * Dispatch the request from a client
 *
 * @param path              built represents the URI sent in the request
 * @param method            type of the request e.g. POST, GET, PATCH
 * @param parameterProvider repository method legacy provider
 * @param requestBody       deserialized body of the client request
 * @return the response form the Crnk
 */
@Override
public Response dispatchRequest(String path, String method, Map<String, Set<String>> parameters, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    JsonPath jsonPath = new PathBuilder(moduleRegistry.getResourceRegistry()).build(path);
    try {
        BaseController controller = controllerRegistry.getController(jsonPath, method);
        ResourceInformation resourceInformation = getRequestedResource(jsonPath);
        QueryAdapter queryAdapter = queryAdapterBuilder.build(resourceInformation, parameters);
        DefaultFilterRequestContext context = new DefaultFilterRequestContext(jsonPath, queryAdapter, parameterProvider, requestBody, method);
        DefaultFilterChain chain = new DefaultFilterChain(controller);
        return chain.doFilter(context);
    } catch (Exception e) {
        Optional<JsonApiExceptionMapper> exceptionMapper = exceptionMapperRegistry.findMapperFor(e.getClass());
        if (exceptionMapper.isPresent()) {
            // noinspection unchecked
            logger.debug("dispatching exception to mapper", e);
            return exceptionMapper.get().toErrorResponse(e).toResponse();
        } else {
            logger.error("failed to process request", e);
            throw e;
        }
    }
}
Also used : PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Optional(io.crnk.core.utils.Optional) BaseController(io.crnk.core.engine.internal.dispatcher.controller.BaseController) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourceFieldNotFoundException(io.crnk.core.exception.ResourceFieldNotFoundException) IOException(java.io.IOException)

Example 37 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class DocumentMapperUtil method toResourceId.

public ResourceIdentifier toResourceId(Object entity) {
    if (entity == null) {
        return null;
    }
    RegistryEntry entry = resourceRegistry.findEntry(entity.getClass());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    return resourceInformation.toResourceIdentifier(entity);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 38 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class IncludeLookupUtil method isInclusionRequestedForQueryParams.

private boolean isInclusionRequestedForQueryParams(QueryAdapter queryAdapter, List<ResourceField> fieldPath) {
    Map<String, IncludedRelationsParams> params = queryAdapter.getIncludedRelations().getParams();
    // we have to possibilities for inclusion: by type or dot notation
    for (int i = fieldPath.size() - 1; i >= 0; i--) {
        String path = toPath(fieldPath, i);
        ResourceInformation rootInformation = fieldPath.get(i).getParentResourceInformation();
        IncludedRelationsParams includedRelationsParams = params.get(rootInformation.getResourceType());
        if (includedRelationsParams != null && contains(includedRelationsParams, path)) {
            return true;
        }
    }
    return false;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) IncludedRelationsParams(io.crnk.legacy.queryParams.params.IncludedRelationsParams)

Example 39 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class IncludeLookupUtil method process.

private void process(String type, Set<String> processedTypes, Set<ResourceField> fields) {
    if (!processedTypes.contains(type)) {
        processedTypes.add(type);
        RegistryEntry entry = resourceRegistry.getEntry(type);
        ResourceInformation information = entry.getResourceInformation();
        ResourceInformation superInformation = getSuperInformation(information);
        if (superInformation != null) {
            process(superInformation.getResourceType(), processedTypes, fields);
        }
        // TODO same relationship on multiple children
        for (ResourceField field : information.getRelationshipFields()) {
            boolean existsOnSuperType = superInformation != null && superInformation.findRelationshipFieldByName(field.getJsonName()) != null;
            if (!existsOnSuperType) {
                fields.add(field);
            }
        }
    }
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 40 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class RegistryEntryTest method newRepositoryInformation.

private <T> ResourceRepositoryInformation newRepositoryInformation(Class<T> repositoryClass, String path) {
    ModuleRegistry moduleRegistry = new ModuleRegistry();
    TypeParser typeParser = moduleRegistry.getTypeParser();
    return new ResourceRepositoryInformationImpl(path, new ResourceInformation(typeParser, Task.class, path, null, null, null, null), RepositoryMethodAccess.ALL);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Task(io.crnk.core.mock.models.Task) TypeParser(io.crnk.core.engine.parser.TypeParser) ResourceRepositoryInformationImpl(io.crnk.core.engine.internal.information.repository.ResourceRepositoryInformationImpl) ModuleRegistry(io.crnk.core.module.ModuleRegistry)

Aggregations

ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)167 Test (org.junit.Test)79 ResourceField (io.crnk.core.engine.information.resource.ResourceField)76 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)60 QuerySpec (io.crnk.core.queryspec.QuerySpec)16 Resource (io.crnk.core.engine.document.Resource)15 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)15 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)9 Task (io.crnk.core.mock.models.Task)9 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)9 HashSet (java.util.HashSet)9 Collection (java.util.Collection)8 Relationship (io.crnk.core.engine.document.Relationship)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)7 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)7 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)7 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)7 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)7 Set (java.util.Set)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6