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);
}
}
}
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;
}
}
}
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;
}
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);
}
}
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);
}
Aggregations