Search in sources :

Example 1 with RepositoryMethodParameterProvider

use of io.crnk.legacy.internal.RepositoryMethodParameterProvider in project crnk-framework by crnk-project.

the class OperationsModule method fetchUpToDateResponses.

protected void fetchUpToDateResponses(List<OrderedOperation> orderedOperations, OperationResponse[] responses) {
    RequestDispatcher requestDispatcher = moduleContext.getRequestDispatcher();
    // get current set of resources after all the updates have been applied
    for (OrderedOperation orderedOperation : orderedOperations) {
        Operation operation = orderedOperation.getOperation();
        OperationResponse operationResponse = responses[orderedOperation.getOrdinal()];
        boolean isPost = operation.getOp().equalsIgnoreCase(HttpMethod.POST.toString());
        boolean isPatch = operation.getOp().equalsIgnoreCase(HttpMethod.PATCH.toString());
        if (isPost || isPatch) {
            Resource resource = operationResponse.getSingleData().get();
            String path = resource.getType() + "/" + resource.getId();
            String method = HttpMethod.GET.toString();
            RepositoryMethodParameterProvider parameterProvider = null;
            Map<String, Set<String>> parameters = new HashMap<>();
            parameters.put("include", getLoadedRelationshipNames(resource));
            Response response = requestDispatcher.dispatchRequest(path, method, parameters, parameterProvider, null);
            copyDocument(operationResponse, response.getDocument());
            operationResponse.setIncluded(null);
        }
    }
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) OperationResponse(io.crnk.operations.OperationResponse) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Resource(io.crnk.core.engine.document.Resource) OrderedOperation(io.crnk.operations.server.order.OrderedOperation) OrderedOperation(io.crnk.operations.server.order.OrderedOperation) Operation(io.crnk.operations.Operation) OperationResponse(io.crnk.operations.OperationResponse) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher)

Example 2 with RepositoryMethodParameterProvider

use of io.crnk.legacy.internal.RepositoryMethodParameterProvider in project crnk-framework by crnk-project.

the class OperationsModule method executeOperation.

protected OperationResponse executeOperation(Operation operation) {
    RequestDispatcher requestDispatcher = moduleContext.getRequestDispatcher();
    String path = OperationParameterUtils.parsePath(operation.getPath());
    Map<String, Set<String>> parameters = OperationParameterUtils.parseParameters(operation.getPath());
    String method = operation.getOp();
    RepositoryMethodParameterProvider parameterProvider = null;
    Document requestBody = new Document();
    requestBody.setData((Nullable) Nullable.of(operation.getValue()));
    Response response = requestDispatcher.dispatchRequest(path, method, parameters, parameterProvider, requestBody);
    OperationResponse operationResponse = new OperationResponse();
    operationResponse.setStatus(response.getHttpStatus());
    copyDocument(operationResponse, response.getDocument());
    return operationResponse;
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) OperationResponse(io.crnk.operations.OperationResponse) HashSet(java.util.HashSet) Set(java.util.Set) Document(io.crnk.core.engine.document.Document) OperationResponse(io.crnk.operations.OperationResponse) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher)

Example 3 with RepositoryMethodParameterProvider

use of io.crnk.legacy.internal.RepositoryMethodParameterProvider in project crnk-framework by crnk-project.

the class IncludeLookupSetter method setIncludedElements.

public void setIncludedElements(Document document, Object entity, QueryAdapter queryAdapter, DocumentMappingConfig mappingConfig) {
    QueryAdapter inclusionQueryAdapter = queryAdapter;
    if (!allowPagination && !(queryAdapter instanceof QueryParamsAdapter) && queryAdapter != null) {
        // offset/limit cannot properly work for nested inclusions if becomes cyclic
        inclusionQueryAdapter = queryAdapter.duplicate();
        if (queryAdapter.getResourceInformation().getPagingBehavior() != null) {
            inclusionQueryAdapter.setPagingSpec(queryAdapter.getResourceInformation().getPagingBehavior().createEmptyPagingSpec());
        }
    }
    List<Object> entityList = DocumentMapperUtil.toList(entity);
    List<Resource> dataList = DocumentMapperUtil.toList(document.getData().get());
    Map<ResourceIdentifier, Resource> dataMap = new HashMap<>();
    Map<ResourceIdentifier, Object> entityMap = new HashMap<>();
    for (int i = 0; i < dataList.size(); i++) {
        Resource dataElement = dataList.get(i);
        ResourceIdentifier id = dataElement.toIdentifier();
        entityMap.put(id, entityList.get(i));
        dataMap.put(id, dataElement);
    }
    Map<ResourceIdentifier, Resource> resourceMap = new HashMap<>();
    resourceMap.putAll(dataMap);
    Set<ResourceIdentifier> inclusions = new HashSet<>();
    PopulatedCache populatedCache = new PopulatedCache();
    RepositoryMethodParameterProvider parameterProvider = mappingConfig.getParameterProvider();
    Set<String> fieldsWithEnforcedIdSerialization = mappingConfig.getFieldsWithEnforcedIdSerialization();
    ResourceMappingConfig resourceMappingConfig = mappingConfig.getResourceMapping();
    ArrayList<ResourceField> stack = new ArrayList<>();
    populate(dataList, inclusions, resourceMap, entityMap, stack, inclusionQueryAdapter, parameterProvider, fieldsWithEnforcedIdSerialization, populatedCache, resourceMappingConfig);
    // no need to include resources included in the data section
    inclusions.removeAll(dataMap.keySet());
    // setup included section
    ArrayList<Resource> included = new ArrayList<>();
    for (ResourceIdentifier inclusionId : inclusions) {
        Resource includedResource = resourceMap.get(inclusionId);
        PreconditionUtil.assertNotNull("resource not found", includedResource);
        included.add(includedResource);
    }
    Collections.sort(included);
    LOGGER.debug("Extracted included resources {}", included.toString());
    document.setIncluded(included);
}
Also used : HashMap(java.util.HashMap) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ResourceField(io.crnk.core.engine.information.resource.ResourceField) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) HashSet(java.util.HashSet)

Example 4 with RepositoryMethodParameterProvider

use of io.crnk.legacy.internal.RepositoryMethodParameterProvider 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 5 with RepositoryMethodParameterProvider

use of io.crnk.legacy.internal.RepositoryMethodParameterProvider in project crnk-framework by crnk-project.

the class DocumentMapper method addRelationDataAndInclusions.

private void addRelationDataAndInclusions(Document doc, Object entity, QueryAdapter queryAdapter, DocumentMappingConfig mappingConfig) {
    if (doc.getData().isPresent() && !client) {
        RepositoryMethodParameterProvider parameterProvider = mappingConfig.getParameterProvider();
        Set<String> fieldsWithEnforceIdSerialization = mappingConfig.getFieldsWithEnforcedIdSerialization();
        includeLookupSetter.setIncludedElements(doc, entity, queryAdapter, parameterProvider, fieldsWithEnforceIdSerialization);
    }
}
Also used : RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider)

Aggregations

RepositoryMethodParameterProvider (io.crnk.legacy.internal.RepositoryMethodParameterProvider)7 Set (java.util.Set)4 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)3 Response (io.crnk.core.engine.dispatcher.Response)3 Document (io.crnk.core.engine.document.Document)3 Resource (io.crnk.core.engine.document.Resource)3 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)2 OperationResponse (io.crnk.operations.OperationResponse)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)1 DocumentFilterChain (io.crnk.core.engine.filter.DocumentFilterChain)1 DocumentFilterContext (io.crnk.core.engine.filter.DocumentFilterContext)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ActionPath (io.crnk.core.engine.internal.dispatcher.path.ActionPath)1 PathBuilder (io.crnk.core.engine.internal.dispatcher.path.PathBuilder)1 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)1