Search in sources :

Example 1 with BaseController

use of io.crnk.core.engine.internal.dispatcher.controller.BaseController 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)

Aggregations

ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 BaseController (io.crnk.core.engine.internal.dispatcher.controller.BaseController)1 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)1 PathBuilder (io.crnk.core.engine.internal.dispatcher.path.PathBuilder)1 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 ResourceFieldNotFoundException (io.crnk.core.exception.ResourceFieldNotFoundException)1 Optional (io.crnk.core.utils.Optional)1 IOException (java.io.IOException)1