use of io.crnk.core.utils.Optional in project crnk-framework by crnk-project.
the class MetaPartitionBase method allocateCollectionType.
private Optional<MetaElement> allocateCollectionType(Type type) {
if (type instanceof Class && ((Class) type).isArray()) {
Class<?> elementClass = ((Class<?>) type).getComponentType();
Optional<MetaElement> elementType = allocateMetaElement(elementClass);
if (elementType.isPresent()) {
MetaArrayType arrayType = new MetaArrayType();
arrayType.setName(elementType.get().getName() + "$array");
arrayType.setId(elementType.get().getId() + "$array");
arrayType.setImplementationType(type);
arrayType.setElementType((MetaType) elementType.get());
return addElement(type, arrayType);
}
}
if (type instanceof ParameterizedType && ((ParameterizedType) type).getActualTypeArguments().length == 1) {
ParameterizedType paramType = (ParameterizedType) type;
Optional<MetaType> elementType = (Optional) allocateMetaElement(paramType.getActualTypeArguments()[0]);
if (!elementType.isPresent()) {
return Optional.empty();
}
boolean isSet = Set.class.isAssignableFrom((Class<?>) paramType.getRawType());
boolean isList = List.class.isAssignableFrom((Class<?>) paramType.getRawType());
if (isSet) {
MetaSetType metaSet = new MetaSetType();
metaSet.setId(elementType.get().getId() + "$set");
metaSet.setName(elementType.get().getName() + "$set");
metaSet.setImplementationType(paramType);
metaSet.setElementType(elementType.get());
return addElement(type, metaSet);
}
if (isList) {
PreconditionUtil.assertTrue("expected a list type", isList);
MetaListType metaList = new MetaListType();
metaList.setId(elementType.get().getId() + "list");
metaList.setName(elementType.get().getName() + "$list");
metaList.setImplementationType(paramType);
metaList.setElementType(elementType.get());
return addElement(type, metaList);
}
}
return Optional.empty();
}
use of io.crnk.core.utils.Optional in project crnk-framework by crnk-project.
the class ResourceRegistryImpl method getResourceClass.
public Optional<Class<?>> getResourceClass(Class<?> resourceClass) {
Class<?> currentClass = resourceClass;
while (currentClass != null && currentClass != Object.class) {
RegistryEntry entry = rootPart.getEntry(currentClass);
if (entry != null) {
return (Optional) Optional.of(currentClass);
}
currentClass = currentClass.getSuperclass();
}
return Optional.empty();
}
use of io.crnk.core.utils.Optional 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.utils.Optional in project crnk-framework by crnk-project.
the class MetaLookup method addProvider.
public void addProvider(MetaProvider provider) {
providers.add(provider);
provider.init(new MetaProviderContext() {
@Override
public ModuleContext getModuleContext() {
return moduleContext;
}
@Override
public Optional<MetaElement> getMetaElement(String id) {
return Optional.ofNullable(idElementMap.get(id));
}
@Override
public void checkInitialized() {
MetaLookup.this.checkInitialized();
}
@Override
public <T> T runDiscovery(Callable<T> callable) {
return discover(callable);
}
});
for (MetaFilter filter : provider.getFilters()) {
addFilter(filter);
}
for (MetaPartition partition : provider.getPartitions()) {
addPartition(partition);
}
metaTypes.addAll(provider.getMetaTypes());
}
use of io.crnk.core.utils.Optional in project crnk-framework by crnk-project.
the class ClientStubBase method handleError.
protected RuntimeException handleError(HttpAdapterResponse response) throws IOException {
ErrorResponse errorResponse = null;
String body = response.body();
String contentType = response.getResponseHeader(HttpHeaders.HTTP_CONTENT_TYPE);
if (body.length() > 0 && contentType != null && contentType.toLowerCase().contains(HttpHeaders.JSONAPI_CONTENT_TYPE)) {
ObjectMapper objectMapper = client.getObjectMapper();
Document document = objectMapper.readValue(body, Document.class);
if (document.getErrors() != null && !document.getErrors().isEmpty()) {
errorResponse = new ErrorResponse(document.getErrors(), response.code());
}
}
if (errorResponse == null) {
errorResponse = new ErrorResponse(null, response.code());
}
ExceptionMapperRegistry exceptionMapperRegistry = client.getExceptionMapperRegistry();
Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(errorResponse);
if (mapper.isPresent()) {
Throwable throwable = mapper.get().fromErrorResponse(errorResponse);
if (throwable instanceof RuntimeException) {
return (RuntimeException) throwable;
} else {
return new ClientException(response.code(), response.message(), throwable);
}
} else {
return new ClientException(response.code(), response.message());
}
}
Aggregations