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