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