Search in sources :

Example 1 with ArgumentBinder

use of io.micronaut.core.bind.ArgumentBinder in project micronaut-rabbitmq by micronaut-projects.

the class RabbitBinderRegistry method findArgumentBinder.

@Override
public <T> Optional<ArgumentBinder<T, RabbitConsumerState>> findArgumentBinder(Argument<T> argument, RabbitConsumerState source) {
    Optional<Class<? extends Annotation>> opt = argument.getAnnotationMetadata().getAnnotationTypeByStereotype(Bindable.class);
    if (opt.isPresent()) {
        Class<? extends Annotation> annotationType = opt.get();
        ArgumentBinder binder = byAnnotation.get(annotationType);
        if (binder != null) {
            return Optional.of(binder);
        }
    } else {
        ArgumentBinder binder = byType.get(argument.typeHashCode());
        if (binder != null) {
            return Optional.of(binder);
        }
    }
    return Optional.of((ArgumentBinder<T, RabbitConsumerState>) defaultBinder);
}
Also used : ArgumentBinder(io.micronaut.core.bind.ArgumentBinder) Annotation(java.lang.annotation.Annotation)

Example 2 with ArgumentBinder

use of io.micronaut.core.bind.ArgumentBinder in project micronaut-kafka by micronaut-projects.

the class BatchConsumerRecordsBinderRegistry method findArgumentBinder.

@SuppressWarnings("unchecked")
@Override
public <T> Optional<ArgumentBinder<T, ConsumerRecords<?, ?>>> findArgumentBinder(Argument<T> argument, ConsumerRecords<?, ?> source) {
    Class<T> argType = argument.getType();
    if (Iterable.class.isAssignableFrom(argType) || argType.isArray() || Publishers.isConvertibleToPublisher(argType)) {
        Argument<?> batchType = argument.getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT);
        List bound = new ArrayList();
        return Optional.of((context, consumerRecords) -> {
            for (ConsumerRecord<?, ?> consumerRecord : consumerRecords) {
                Optional<ArgumentBinder<?, ConsumerRecord<?, ?>>> binder = consumerRecordBinderRegistry.findArgumentBinder((Argument) argument, consumerRecord);
                binder.ifPresent(b -> {
                    Argument<?> newArg = Argument.of(batchType.getType(), argument.getName(), argument.getAnnotationMetadata(), batchType.getTypeParameters());
                    ArgumentConversionContext conversionContext = ConversionContext.of(newArg);
                    ArgumentBinder.BindingResult<?> result = b.bind(conversionContext, consumerRecord);
                    if (result.isPresentAndSatisfied()) {
                        bound.add(result.get());
                    }
                });
            }
            return () -> {
                if (Publisher.class.isAssignableFrom(argument.getType())) {
                    return ConversionService.SHARED.convert(Flux.fromIterable(bound), argument);
                } else {
                    return ConversionService.SHARED.convert(bound, argument);
                }
            };
        });
    }
    return Optional.empty();
}
Also used : ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ArgumentBinder(io.micronaut.core.bind.ArgumentBinder) Publisher(org.reactivestreams.Publisher)

Example 3 with ArgumentBinder

use of io.micronaut.core.bind.ArgumentBinder in project micronaut-nats by micronaut-projects.

the class NatsBinderRegistry method findArgumentBinder.

@Override
public <T> Optional<ArgumentBinder<T, Message>> findArgumentBinder(Argument<T> argument, Message source) {
    Optional<Class<? extends Annotation>> opt = argument.getAnnotationMetadata().getAnnotationTypeByStereotype(Bindable.class);
    if (opt.isPresent()) {
        Class<? extends Annotation> annotationType = opt.get();
        ArgumentBinder binder = byAnnotation.get(annotationType);
        if (binder != null) {
            return Optional.of(binder);
        }
    } else {
        ArgumentBinder binder = byType.get(argument.typeHashCode());
        if (binder != null) {
            return Optional.of(binder);
        }
    }
    return Optional.of((ArgumentBinder<T, Message>) defaultBinder);
}
Also used : Message(io.nats.client.Message) ArgumentBinder(io.micronaut.core.bind.ArgumentBinder) Annotation(java.lang.annotation.Annotation)

Example 4 with ArgumentBinder

use of io.micronaut.core.bind.ArgumentBinder in project micronaut-gcp by micronaut-projects.

the class PubSubBinderRegistry method findArgumentBinder.

@Override
public <T> Optional<ArgumentBinder<T, PubSubConsumerState>> findArgumentBinder(Argument<T> argument, PubSubConsumerState source) {
    Optional<Class<? extends Annotation>> opt = argument.getAnnotationMetadata().getAnnotationTypeByStereotype(Bindable.class);
    if (opt.isPresent()) {
        Class<? extends Annotation> annotationType = opt.get();
        ArgumentBinder binder = byAnnotation.get(annotationType);
        if (binder != null) {
            return Optional.of(binder);
        }
    } else {
        ArgumentBinder binder = byType.get(argument.typeHashCode());
        if (binder != null) {
            return Optional.of(binder);
        }
    }
    return Optional.of((ArgumentBinder<T, PubSubConsumerState>) defaultBinder);
}
Also used : ArgumentBinder(io.micronaut.core.bind.ArgumentBinder) Annotation(java.lang.annotation.Annotation)

Example 5 with ArgumentBinder

use of io.micronaut.core.bind.ArgumentBinder in project micronaut-core by micronaut-projects.

the class RequestArgumentSatisfier method getValueForArgument.

/**
 * @param argument         The argument
 * @param request          The HTTP request
 * @param satisfyOptionals Whether to satisfy optionals
 * @return An {@link Optional} for the value
 */
protected Optional<Object> getValueForArgument(Argument argument, HttpRequest<?> request, boolean satisfyOptionals) {
    Object value = null;
    Optional<ArgumentBinder> registeredBinder = binderRegistry.findArgumentBinder(argument, request);
    if (registeredBinder.isPresent()) {
        ArgumentBinder argumentBinder = registeredBinder.get();
        ArgumentConversionContext conversionContext = ConversionContext.of(argument, request.getLocale().orElse(null), request.getCharacterEncoding());
        if (argumentBinder instanceof BodyArgumentBinder) {
            if (argumentBinder instanceof NonBlockingBodyArgumentBinder) {
                ArgumentBinder.BindingResult bindingResult = argumentBinder.bind(conversionContext, request);
                if (bindingResult.isPresentAndSatisfied()) {
                    value = bindingResult.get();
                } else if (bindingResult.isSatisfied() && argument.isNullable()) {
                    value = NullArgument.INSTANCE;
                }
            } else {
                value = getValueForBlockingBodyArgumentBinder(request, argumentBinder, conversionContext);
            }
        } else if (argumentBinder instanceof RequestBeanAnnotationBinder) {
            // Resolve RequestBean after filters since some field types may depend on filters, i.e. Authentication
            value = (UnresolvedArgument<?>) () -> argumentBinder.bind(conversionContext, request);
        } else {
            ArgumentBinder.BindingResult bindingResult = argumentBinder.bind(conversionContext, request);
            if (argument.getType() == Optional.class) {
                if (bindingResult.isSatisfied() || satisfyOptionals) {
                    Optional optionalValue = bindingResult.getValue();
                    if (optionalValue.isPresent()) {
                        value = optionalValue.get();
                    } else {
                        value = optionalValue;
                    }
                }
            } else if (bindingResult.isPresentAndSatisfied()) {
                value = bindingResult.get();
            } else if (bindingResult.isSatisfied() && argument.isNullable()) {
                value = NullArgument.INSTANCE;
            } else if (HttpMethod.requiresRequestBody(request.getMethod()) || argument.isNullable() || conversionContext.hasErrors()) {
                value = (UnresolvedArgument) () -> {
                    ArgumentBinder.BindingResult result = argumentBinder.bind(conversionContext, request);
                    Optional<ConversionError> lastError = conversionContext.getLastError();
                    if (lastError.isPresent()) {
                        return (ArgumentBinder.BindingResult) () -> lastError;
                    }
                    return result;
                };
            }
        }
    }
    return Optional.ofNullable(value);
}
Also used : NonBlockingBodyArgumentBinder(io.micronaut.http.bind.binders.NonBlockingBodyArgumentBinder) UnresolvedArgument(io.micronaut.web.router.UnresolvedArgument) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) Optional(java.util.Optional) BodyArgumentBinder(io.micronaut.http.bind.binders.BodyArgumentBinder) NonBlockingBodyArgumentBinder(io.micronaut.http.bind.binders.NonBlockingBodyArgumentBinder) RequestBeanAnnotationBinder(io.micronaut.http.bind.binders.RequestBeanAnnotationBinder) BodyArgumentBinder(io.micronaut.http.bind.binders.BodyArgumentBinder) ArgumentBinder(io.micronaut.core.bind.ArgumentBinder) NonBlockingBodyArgumentBinder(io.micronaut.http.bind.binders.NonBlockingBodyArgumentBinder)

Aggregations

ArgumentBinder (io.micronaut.core.bind.ArgumentBinder)6 Annotation (java.lang.annotation.Annotation)3 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)2 AnnotatedArgumentBinder (io.micronaut.core.bind.annotation.AnnotatedArgumentBinder)1 HttpRequest (io.micronaut.http.HttpRequest)1 QueryValue (io.micronaut.http.annotation.QueryValue)1 BodyArgumentBinder (io.micronaut.http.bind.binders.BodyArgumentBinder)1 NonBlockingBodyArgumentBinder (io.micronaut.http.bind.binders.NonBlockingBodyArgumentBinder)1 QueryValueArgumentBinder (io.micronaut.http.bind.binders.QueryValueArgumentBinder)1 RequestBeanAnnotationBinder (io.micronaut.http.bind.binders.RequestBeanAnnotationBinder)1 UnresolvedArgument (io.micronaut.web.router.UnresolvedArgument)1 Message (io.nats.client.Message)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Publisher (org.reactivestreams.Publisher)1