use of io.crnk.core.engine.internal.dispatcher.path.PathBuilder in project crnk-framework by crnk-project.
the class HttpRequestProcessorImpl method dispatchRequest.
/**
* Dispatch the request from a client
*
* @param path built represents the URI sent in the request
* @param method type of the request e.g. POST, GET, PATCH
* @param parameterProvider repository method legacy provider
* @param requestBody deserialized body of the client request
* @return the response form the Crnk
*/
@Override
public Response dispatchRequest(String path, String method, Map<String, Set<String>> parameters, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
JsonPath jsonPath = new PathBuilder(moduleRegistry.getResourceRegistry()).build(path);
try {
BaseController controller = controllerRegistry.getController(jsonPath, method);
ResourceInformation resourceInformation = getRequestedResource(jsonPath);
QueryAdapter queryAdapter = queryAdapterBuilder.build(resourceInformation, parameters);
DefaultFilterRequestContext context = new DefaultFilterRequestContext(jsonPath, queryAdapter, parameterProvider, requestBody, method);
DefaultFilterChain chain = new DefaultFilterChain(controller);
return chain.doFilter(context);
} catch (Exception e) {
Optional<JsonApiExceptionMapper> exceptionMapper = exceptionMapperRegistry.findMapperFor(e.getClass());
if (exceptionMapper.isPresent()) {
// noinspection unchecked
logger.debug("dispatching exception to mapper", e);
return exceptionMapper.get().toErrorResponse(e).toResponse();
} else {
logger.error("failed to process request", e);
throw e;
}
}
}
use of io.crnk.core.engine.internal.dispatcher.path.PathBuilder 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.core.engine.internal.dispatcher.path.PathBuilder in project crnk-framework by crnk-project.
the class HomeModule method isHomeRequest.
private boolean isHomeRequest(HttpRequestContext requestContext) {
String path = requestContext.getPath();
if (!path.endsWith("/") || !requestContext.getMethod().equalsIgnoreCase(HttpMethod.GET.toString())) {
return false;
}
boolean acceptsHome = requestContext.accepts(JSON_HOME_CONTENT_TYPE);
boolean acceptsJsonApi = requestContext.accepts(HttpHeaders.JSONAPI_CONTENT_TYPE);
boolean acceptsJson = requestContext.accepts(HttpHeaders.JSON_CONTENT_TYPE);
boolean acceptsAny = requestContext.acceptsAny();
if (!(acceptsHome || acceptsAny || acceptsJson || acceptsJsonApi)) {
return false;
}
ResourceRegistry resourceRegistry = moduleContext.getResourceRegistry();
JsonPath jsonPath = new PathBuilder(resourceRegistry).build(path);
// no repository with that path
return jsonPath == null;
}
Aggregations