Search in sources :

Example 1 with OperationResponse

use of io.crnk.operations.OperationResponse 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 OperationResponse

use of io.crnk.operations.OperationResponse in project crnk-framework by crnk-project.

the class OperationsModule method fillinIgnoredOperations.

protected void fillinIgnoredOperations(OperationResponse[] responses) {
    for (int i = 0; i < responses.length; i++) {
        if (responses[i] == null) {
            OperationResponse operationResponse = new OperationResponse();
            operationResponse.setStatus(HttpStatus.PRECONDITION_FAILED_412);
            responses[i] = operationResponse;
        }
    }
}
Also used : OperationResponse(io.crnk.operations.OperationResponse)

Example 3 with OperationResponse

use of io.crnk.operations.OperationResponse 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 4 with OperationResponse

use of io.crnk.operations.OperationResponse in project crnk-framework by crnk-project.

the class OperationsCall method execute.

public void execute() {
    List<Operation> operations = new ArrayList<>();
    for (QueuedOperation queuedOperation : queuedOperations) {
        operations.add(queuedOperation.operation);
    }
    HttpAdapter adapter = client.getCrnk().getHttpAdapter();
    ObjectMapper mapper = client.getCrnk().getObjectMapper();
    try {
        String operationsJson = mapper.writer().writeValueAsString(operations.toArray(new Operation[operations.size()]));
        String url = client.getCrnk().getServiceUrlProvider().getUrl() + "/operations";
        HttpAdapterRequest request = adapter.newRequest(url, HttpMethod.PATCH, operationsJson);
        request.header(HttpHeaders.HTTP_CONTENT_TYPE, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
        request.header(HttpHeaders.HTTP_HEADER_ACCEPT, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
        HttpAdapterResponse response = request.execute();
        int status = response.code();
        if (status != 200) {
            // general issue, status of individual operations is important.
            throw new InternalServerErrorException("patch execution failed with status " + status);
        }
        String json = response.body();
        responses = Arrays.asList(mapper.readValue(json, OperationResponse[].class));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : HttpAdapterRequest(io.crnk.client.http.HttpAdapterRequest) HttpAdapter(io.crnk.client.http.HttpAdapter) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) ArrayList(java.util.ArrayList) Operation(io.crnk.operations.Operation) IOException(java.io.IOException) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) OperationResponse(io.crnk.operations.OperationResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with OperationResponse

use of io.crnk.operations.OperationResponse in project crnk-framework by crnk-project.

the class OperationsModule method executeOperations.

protected List<OperationResponse> executeOperations(List<OrderedOperation> orderedOperations) {
    OperationResponse[] responses = new OperationResponse[orderedOperations.size()];
    boolean successful = true;
    for (OrderedOperation orderedOperation : orderedOperations) {
        OperationResponse operationResponse = executeOperation(orderedOperation.getOperation());
        responses[orderedOperation.getOrdinal()] = operationResponse;
        int status = operationResponse.getStatus();
        if (status >= 400) {
            successful = false;
            break;
        }
    }
    if (orderedOperations.size() > 1 && successful) {
        fetchUpToDateResponses(orderedOperations, responses);
    }
    fillinIgnoredOperations(responses);
    return Arrays.asList(responses);
}
Also used : OrderedOperation(io.crnk.operations.server.order.OrderedOperation) OperationResponse(io.crnk.operations.OperationResponse)

Aggregations

OperationResponse (io.crnk.operations.OperationResponse)7 Operation (io.crnk.operations.Operation)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)2 Response (io.crnk.core.engine.dispatcher.Response)2 RepositoryMethodParameterProvider (io.crnk.legacy.internal.RepositoryMethodParameterProvider)2 OrderedOperation (io.crnk.operations.server.order.OrderedOperation)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 HttpAdapter (io.crnk.client.http.HttpAdapter)1 HttpAdapterRequest (io.crnk.client.http.HttpAdapterRequest)1 HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)1 Document (io.crnk.core.engine.document.Document)1 Resource (io.crnk.core.engine.document.Resource)1 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1