Search in sources :

Example 41 with Response

use of io.crnk.core.engine.dispatcher.Response 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 42 with Response

use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.

the class CollectionGet method handle.

@Override
@SuppressWarnings("unchecked")
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    String resourceName = jsonPath.getElementName();
    RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
    if (registryEntry == null) {
        throw new ResourceNotFoundException(resourceName);
    }
    Document responseDocument;
    ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
    JsonApiResponse entities;
    if (jsonPath.getIds() == null || jsonPath.getIds().getIds().isEmpty()) {
        entities = resourceRepository.findAll(queryAdapter);
    } else {
        Class<? extends Serializable> idType = (Class<? extends Serializable>) registryEntry.getResourceInformation().getIdField().getType();
        Iterable<? extends Serializable> parsedIds = typeParser.parse((Iterable<String>) jsonPath.getIds().getIds(), idType);
        entities = resourceRepository.findAll(parsedIds, queryAdapter);
    }
    responseDocument = documentMapper.toDocument(entities, queryAdapter, parameterProvider);
    return new Response(responseDocument, 200);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Serializable(java.io.Serializable) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 43 with Response

use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.

the class RelationshipsResourceGet method handle.

@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    String resourceName = jsonPath.getResourceType();
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
    Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
    String elementName = jsonPath.getElementName();
    ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(elementName);
    verifyFieldNotNull(relationshipField, elementName);
    boolean isCollection = Iterable.class.isAssignableFrom(relationshipField.getType());
    RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry.getRelationshipRepository(relationshipField, parameterProvider);
    JsonApiResponse entities;
    if (isCollection) {
        entities = relationshipRepositoryForClass.findManyTargets(castedResourceId, relationshipField, queryAdapter);
    } else {
        entities = relationshipRepositoryForClass.findOneTarget(castedResourceId, relationshipField, queryAdapter);
    }
    Document responseDocument = documentMapper.toDocument(entities, queryAdapter, parameterProvider);
    // return explicit { data : null } if values found
    if (!responseDocument.getData().isPresent()) {
        responseDocument.setData(Nullable.nullValue());
    }
    return new Response(responseDocument, 200);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) Serializable(java.io.Serializable) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)

Example 44 with Response

use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.

the class ResourceDelete method handle.

@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    String resourceName = jsonPath.getElementName();
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
    if (registryEntry == null) {
        // TODO: Add JsonPath toString and provide to exception?
        throw new ResourceNotFoundException(resourceName);
    }
    for (String id : resourceIds.getIds()) {
        Serializable castedId = registryEntry.getResourceInformation().parseIdString(id);
        // noinspection unchecked
        registryEntry.getResourceRepository(parameterProvider).delete(castedId, queryAdapter);
    }
    return new Response(null, 204);
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Serializable(java.io.Serializable) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 45 with Response

use of io.crnk.core.engine.dispatcher.Response in project crnk-framework by crnk-project.

the class ResourceGet method handle.

/**
 * {@inheritDoc}
 * <p>
 * Passes the request to controller method.
 */
@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    String resourceType = jsonPath.getElementName();
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry registryEntry = getRegistryEntry(resourceType);
    String id = resourceIds.getIds().get(0);
    @SuppressWarnings("unchecked") Class<? extends Serializable> idClass = (Class<? extends Serializable>) registryEntry.getResourceInformation().getIdField().getType();
    Serializable castedId = typeParser.parse(id, idClass);
    ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
    JsonApiResponse entities = resourceRepository.findOne(castedId, queryAdapter);
    Document responseDocument = documentMapper.toDocument(entities, queryAdapter);
    // return explicit { data : null } if values found
    if (!responseDocument.getData().isPresent()) {
        responseDocument.setData(Nullable.nullValue());
    }
    return new Response(responseDocument, 200);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Serializable(java.io.Serializable) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Aggregations

Response (io.crnk.core.engine.dispatcher.Response)72 Document (io.crnk.core.engine.document.Document)56 Test (org.junit.Test)54 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)52 Resource (io.crnk.core.engine.document.Resource)43 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)41 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)30 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)18 Project (io.crnk.core.mock.models.Project)15 Relationship (io.crnk.core.engine.document.Relationship)14 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)14 ResourceField (io.crnk.core.engine.information.resource.ResourceField)12 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)12 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)12 Task (io.crnk.core.mock.models.Task)12 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)11 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)9 QueryParams (io.crnk.legacy.queryParams.QueryParams)9 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)8