Search in sources :

Example 6 with PathBuilder

use of io.crnk.core.engine.internal.dispatcher.path.PathBuilder 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 7 with PathBuilder

use of io.crnk.core.engine.internal.dispatcher.path.PathBuilder in project crnk-framework by crnk-project.

the class JsonApiRequestProcessor method process.

@Override
public void process(HttpRequestContext requestContext) throws IOException {
    if (isJsonApiRequest(requestContext, isAcceptingPlainJson())) {
        ResourceRegistry resourceRegistry = moduleContext.getResourceRegistry();
        RequestDispatcher requestDispatcher = moduleContext.getRequestDispatcher();
        String path = requestContext.getPath();
        JsonPath jsonPath = new PathBuilder(resourceRegistry).build(path);
        Map<String, Set<String>> parameters = requestContext.getRequestParameters();
        String method = requestContext.getMethod();
        if (jsonPath instanceof ActionPath) {
            // inital implementation, has to improve
            requestDispatcher.dispatchAction(path, method, parameters);
        } else if (jsonPath != null) {
            byte[] requestBody = requestContext.getRequestBody();
            Document document = null;
            if (requestBody != null && requestBody.length > 0) {
                ObjectMapper objectMapper = moduleContext.getObjectMapper();
                try {
                    document = objectMapper.readerFor(Document.class).readValue(requestBody);
                } catch (JsonProcessingException e) {
                    final String message = "Json Parsing failed";
                    setResponse(requestContext, buildBadRequestResponse(message, e.getMessage()));
                    LOGGER.error(message, e);
                    return;
                }
            }
            RepositoryMethodParameterProvider parameterProvider = requestContext.getRequestParameterProvider();
            Response crnkResponse = requestDispatcher.dispatchRequest(path, method, parameters, parameterProvider, document);
            setResponse(requestContext, crnkResponse);
        } else {
        // no repositories invoked, we do nothing
        }
    }
}
Also used : Set(java.util.Set) ActionPath(io.crnk.core.engine.internal.dispatcher.path.ActionPath) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher) Response(io.crnk.core.engine.dispatcher.Response) PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 8 with PathBuilder

use of io.crnk.core.engine.internal.dispatcher.path.PathBuilder in project crnk-framework by crnk-project.

the class HomeModule method isHomeRequest.

private boolean isHomeRequest(HttpRequestContext requestContext) {
    String path = requestContext.getPath();
    if (!path.endsWith("/") || !requestContext.getMethod().equalsIgnoreCase(HttpMethod.GET.toString())) {
        return false;
    }
    boolean acceptsHome = requestContext.accepts(JSON_HOME_CONTENT_TYPE);
    boolean acceptsJsonApi = requestContext.accepts(HttpHeaders.JSONAPI_CONTENT_TYPE);
    boolean acceptsJson = requestContext.accepts(HttpHeaders.JSON_CONTENT_TYPE);
    boolean acceptsAny = requestContext.acceptsAny();
    if (!(acceptsHome || acceptsAny || acceptsJson || acceptsJsonApi)) {
        return false;
    }
    ResourceRegistry resourceRegistry = moduleContext.getResourceRegistry();
    JsonPath jsonPath = new PathBuilder(resourceRegistry).build(path);
    // no repository with that path
    return jsonPath == null;
}
Also used : PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath)

Aggregations

PathBuilder (io.crnk.core.engine.internal.dispatcher.path.PathBuilder)8 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)6 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)3 CrnkBoot (io.crnk.core.boot.CrnkBoot)2 ControllerRegistry (io.crnk.core.engine.internal.dispatcher.ControllerRegistry)2 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)2 ReflectionsServiceDiscovery (io.crnk.core.module.discovery.ReflectionsServiceDiscovery)2 Before (org.junit.Before)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)1 Response (io.crnk.core.engine.dispatcher.Response)1 Document (io.crnk.core.engine.document.Document)1 Resource (io.crnk.core.engine.document.Resource)1 ResourceModificationFilterBase (io.crnk.core.engine.filter.ResourceModificationFilterBase)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 BaseController (io.crnk.core.engine.internal.dispatcher.controller.BaseController)1 CollectionGet (io.crnk.core.engine.internal.dispatcher.controller.CollectionGet)1 ActionPath (io.crnk.core.engine.internal.dispatcher.path.ActionPath)1 HttpRequestProcessorImpl (io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)1