Search in sources :

Example 1 with Optional

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();
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Optional(io.crnk.core.utils.Optional)

Example 2 with Optional

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();
}
Also used : Optional(io.crnk.core.utils.Optional) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 3 with Optional

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;
        }
    }
}
Also used : PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Optional(io.crnk.core.utils.Optional) BaseController(io.crnk.core.engine.internal.dispatcher.controller.BaseController) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourceFieldNotFoundException(io.crnk.core.exception.ResourceFieldNotFoundException) IOException(java.io.IOException)

Example 4 with Optional

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());
}
Also used : Optional(io.crnk.core.utils.Optional) ModuleContext(io.crnk.core.module.Module.ModuleContext) BaseMetaPartition(io.crnk.meta.internal.BaseMetaPartition)

Example 5 with Optional

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());
    }
}
Also used : ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) ClientException(io.crnk.client.ClientException) Document(io.crnk.core.engine.document.Document) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ErrorResponse(io.crnk.core.engine.error.ErrorResponse)

Aggregations

Optional (io.crnk.core.utils.Optional)9 ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)5 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)4 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)4 Test (org.junit.Test)3 ErrorData (io.crnk.core.engine.document.ErrorData)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ClientException (io.crnk.client.ClientException)1 Document (io.crnk.core.engine.document.Document)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 BaseController (io.crnk.core.engine.internal.dispatcher.controller.BaseController)1 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)1 PathBuilder (io.crnk.core.engine.internal.dispatcher.path.PathBuilder)1 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)1 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)1 ResourceFieldNotFoundException (io.crnk.core.exception.ResourceFieldNotFoundException)1 ModuleContext (io.crnk.core.module.Module.ModuleContext)1 BaseMetaPartition (io.crnk.meta.internal.BaseMetaPartition)1 IOException (java.io.IOException)1