io.micronaut.core.convert.ArgumentConversionContext Java Examples
The following examples show how to use
io.micronaut.core.convert.ArgumentConversionContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequestAttributeArgumentBinder.java From micronaut-spring with Apache License 2.0 | 6 votes |
@Override public BindingResult<Object> bind(ArgumentConversionContext<Object> context, HttpRequest<?> source) { final AnnotationMetadata annotationMetadata = context.getAnnotationMetadata(); final boolean required = annotationMetadata.getValue("required", boolean.class).orElse(true); final String name = annotationMetadata.getValue(RequestAttribute.class, String.class).orElseGet(() -> annotationMetadata.getValue(RequestAttribute.class, "name", String.class).orElse(context.getArgument().getName()) ); return new BindingResult<Object>() { @Override public Optional<Object> getValue() { return source.getAttributes().get(name, context); } @Override public boolean isSatisfied() { return !required; } }; }
Example #2
Source File: KafkaHeaderBinder.java From micronaut-kafka with Apache License 2.0 | 6 votes |
@Override public BindingResult<T> bind(ArgumentConversionContext<T> context, ConsumerRecord<?, ?> source) { Headers headers = source.headers(); AnnotationMetadata annotationMetadata = context.getAnnotationMetadata(); String name = annotationMetadata.getValue(Header.class, "name", String.class) .orElseGet(() -> annotationMetadata.getValue(Header.class, String.class) .orElse(context.getArgument().getName())); Iterable<org.apache.kafka.common.header.Header> value = headers.headers(name); if (value.iterator().hasNext()) { Optional<T> converted = ConversionService.SHARED.convert(value, context); return () -> converted; } else if (context.getArgument().getType() == Optional.class) { //noinspection unchecked return () -> (Optional<T>) Optional.of(Optional.empty()); } else { //noinspection unchecked return BindingResult.EMPTY; } }
Example #3
Source File: GoogleResponseBinder.java From micronaut-gcp with Apache License 2.0 | 5 votes |
@Override public BindingResult<HttpResponse> bind(ArgumentConversionContext<HttpResponse> context, HttpRequest<?> source) { if (source instanceof GoogleFunctionHttpRequest) { GoogleFunctionHttpRequest googleFunctionHttpRequest = (GoogleFunctionHttpRequest) source; return () -> Optional.of(googleFunctionHttpRequest.getGoogleResponse().getNativeResponse()); } return BindingResult.UNSATISFIED; }
Example #4
Source File: ModelMapRequestArgumentBinder.java From micronaut-spring with Apache License 2.0 | 5 votes |
@Override public BindingResult<ModelMap> bind(ArgumentConversionContext<ModelMap> context, HttpRequest<?> source) { final Optional<ModelMap> attribute = source.getAttribute(ATTRIBUTE, ModelMap.class); if (!attribute.isPresent()) { final ModelMap modelMap = new ModelMap(); source.setAttribute(ATTRIBUTE, modelMap); return () -> Optional.of(modelMap); } return () -> attribute; }
Example #5
Source File: ModelRequestArgumentBinder.java From micronaut-spring with Apache License 2.0 | 5 votes |
@Override public BindingResult<Model> bind(ArgumentConversionContext<Model> context, HttpRequest<?> source) { final Optional<Model> attribute = source.getAttribute(ATTRIBUTE, Model.class); if (!attribute.isPresent()) { final ConcurrentModel concurrentModel = new ConcurrentModel(); source.setAttribute(ATTRIBUTE, concurrentModel); return () -> Optional.of(concurrentModel); } return () -> attribute; }
Example #6
Source File: ServerHttpRequestBinder.java From micronaut-spring with Apache License 2.0 | 5 votes |
@Override public BindingResult<ServerHttpRequest> bind(ArgumentConversionContext<ServerHttpRequest> context, HttpRequest<?> source) { return () -> Optional.of(new MicronautServerHttpRequest( source, channelResolver )); }
Example #7
Source File: SpringPageableRequestArgumentBinder.java From micronaut-data with Apache License 2.0 | 5 votes |
@Override public BindingResult<Pageable> bind(ArgumentConversionContext<Pageable> context, HttpRequest<?> source) { HttpParameters parameters = source.getParameters(); int page = Math.max(parameters.getFirst(configuration.getPageParameterName(), Integer.class) .orElse(0), 0); final int configuredMaxSize = configuration.getMaxPageSize(); final int defaultSize = configuration.getDefaultPageSize(); int size = Math.min(parameters.getFirst(configuration.getSizeParameterName(), Integer.class) .orElse(defaultSize), configuredMaxSize); String sortParameterName = configuration.getSortParameterName(); boolean hasSort = parameters.contains(sortParameterName); Pageable pageable; Sort sort; if (hasSort) { List<String> sortParams = parameters.getAll(sortParameterName); List<Sort.Order> orders = sortParams.stream() .map(sortMapper) .collect(Collectors.toList()); sort = Sort.by(orders); } else { sort = Sort.unsorted(); } if (size < 1) { if (page == 0 && configuredMaxSize < 1 && sort.isUnsorted()) { pageable = Pageable.unpaged(); } else { pageable = PageRequest.of(page, defaultSize, sort); } } else { pageable = PageRequest.of(page, size, sort); } return () -> Optional.of(pageable); }
Example #8
Source File: PageableRequestArgumentBinder.java From micronaut-data with Apache License 2.0 | 5 votes |
@Override public BindingResult<Pageable> bind(ArgumentConversionContext<Pageable> context, HttpRequest<?> source) { HttpParameters parameters = source.getParameters(); int page = Math.max(parameters.getFirst(configuration.getPageParameterName(), Integer.class) .orElse(0), 0); final int configuredMaxSize = configuration.getMaxPageSize(); final int defaultSize = configuration.getDefaultPageSize(); int size = Math.min(parameters.getFirst(configuration.getSizeParameterName(), Integer.class) .orElse(defaultSize), configuredMaxSize); String sortParameterName = configuration.getSortParameterName(); boolean hasSort = parameters.contains(sortParameterName); Pageable pageable; Sort sort = null; if (hasSort) { List<String> sortParams = parameters.getAll(sortParameterName); List<Sort.Order> orders = sortParams.stream() .map(sortMapper) .collect(Collectors.toList()); sort = Sort.of(orders); } if (size < 1) { if (page == 0 && configuredMaxSize < 1 && sort == null) { pageable = Pageable.UNPAGED; } else { pageable = Pageable.from(page, defaultSize, sort); } } else { pageable = Pageable.from(page, size, sort); } return () -> Optional.of(pageable); }
Example #9
Source File: BatchConsumerRecordsBinderRegistry.java From micronaut-kafka with Apache License 2.0 | 5 votes |
@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(Flowable.fromIterable(bound), argument); } else { return ConversionService.SHARED.convert(bound, argument); } }; }); } return Optional.empty(); }
Example #10
Source File: KafkaHeaders.java From micronaut-kafka with Apache License 2.0 | 5 votes |
@Override public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) { String v = get(name); if (v != null) { return ConversionService.SHARED.convert(v, conversionContext); } return Optional.empty(); }
Example #11
Source File: MicronautAwsProxyResponse.java From micronaut-aws with Apache License 2.0 | 5 votes |
@Override public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) { final String v = get(name); if (v != null) { return ConversionService.SHARED.convert(v, conversionContext); } return Optional.empty(); }
Example #12
Source File: GoogleRequestBinder.java From micronaut-gcp with Apache License 2.0 | 5 votes |
@Override public BindingResult<HttpRequest> bind(ArgumentConversionContext<HttpRequest> context, io.micronaut.http.HttpRequest<?> source) { if (source instanceof GoogleFunctionHttpRequest) { GoogleFunctionHttpRequest googleFunctionHttpRequest = (GoogleFunctionHttpRequest) source; return () -> Optional.of(googleFunctionHttpRequest.getNativeRequest()); } return BindingResult.UNSATISFIED; }
Example #13
Source File: GoogleMultiValueMap.java From micronaut-gcp with Apache License 2.0 | 5 votes |
@Override public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) { final String v = get(name); if (v != null) { return ConversionService.SHARED.convert(v, conversionContext); } return Optional.empty(); }
Example #14
Source File: MicronautAwsProxyRequest.java From micronaut-aws with Apache License 2.0 | 5 votes |
@Override public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) { final String v = get(name); if (v != null) { return ConversionService.SHARED.convert(v, conversionContext); } return Optional.empty(); }
Example #15
Source File: MicronautAwsProxyRequest.java From micronaut-aws with Apache License 2.0 | 5 votes |
@Override public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) { final String v = get(name); if (v != null) { return ConversionService.SHARED.convert(v, conversionContext); } return Optional.empty(); }
Example #16
Source File: AwsProxyRequestContextArgumentBinder.java From micronaut-aws with Apache License 2.0 | 5 votes |
@Override public BindingResult<AwsProxyRequestContext> bind(ArgumentConversionContext<AwsProxyRequestContext> context, HttpRequest<?> source) { if (source instanceof MicronautAwsProxyRequest) { final AwsProxyRequest awsProxyRequest = ((MicronautAwsProxyRequest<?>) source).getAwsProxyRequest(); return () -> Optional.ofNullable(awsProxyRequest.getRequestContext()); } return BindingResult.UNSATISFIED; }
Example #17
Source File: AwsProxyRequestArgumentBinder.java From micronaut-aws with Apache License 2.0 | 5 votes |
@Override public BindingResult<AwsProxyRequest> bind(ArgumentConversionContext<AwsProxyRequest> context, HttpRequest<?> source) { if (source instanceof MicronautAwsProxyRequest) { final AwsProxyRequest awsProxyRequest = ((MicronautAwsProxyRequest<?>) source).getAwsProxyRequest(); return () -> Optional.ofNullable(awsProxyRequest); } return BindingResult.UNSATISFIED; }
Example #18
Source File: MicronautRequestHandler.java From micronaut-aws with Apache License 2.0 | 5 votes |
/** * Converts the input the required type. Subclasses can override to provide custom conversion. * * @param input The input * @return The converted input * @throws IllegalArgumentException If input cannot be converted */ protected I convertInput(Object input) { final ArgumentConversionContext<I> cc = ConversionContext.of(inputType); final Optional<I> converted = applicationContext.getConversionService().convert( input, cc ); return converted.orElseThrow(() -> new IllegalArgumentException("Unconvertible input: " + input, cc.getLastError().map(ConversionError::getCause).orElse(null)) ); }
Example #19
Source File: KafkaBodyBinder.java From micronaut-kafka with Apache License 2.0 | 4 votes |
@Override public BindingResult<T> bind(ArgumentConversionContext<T> context, ConsumerRecord<?, ?> source) { Object value = source.value(); Optional<T> converted = ConversionService.SHARED.convert(value, context); return () -> converted; }
Example #20
Source File: KafkaMessagingBodyBinder.java From micronaut-kafka with Apache License 2.0 | 4 votes |
@Override public BindingResult<T> bind(ArgumentConversionContext<T> context, ConsumerRecord<?, ?> source) { Object value = source.value(); Optional<T> converted = ConversionService.SHARED.convert(value, context); return () -> converted; }
Example #21
Source File: AwsProxyPrincipalBinder.java From micronaut-aws with Apache License 2.0 | 4 votes |
@Override public BindingResult<Principal> bind(ArgumentConversionContext<Principal> context, HttpRequest<?> source) { return source::getUserPrincipal; }
Example #22
Source File: BeanIntrospectionMapper.java From micronaut-data with Apache License 2.0 | 4 votes |
@Override default @NonNull R map(@NonNull D object, @NonNull Class<R> type) throws InstantiationException { ArgumentUtils.requireNonNull("resultSet", object); ArgumentUtils.requireNonNull("type", type); try { BeanIntrospection<R> introspection = BeanIntrospection.getIntrospection(type); ConversionService<?> conversionService = getConversionService(); Argument<?>[] arguments = introspection.getConstructorArguments(); R instance; if (ArrayUtils.isEmpty(arguments)) { instance = introspection.instantiate(); } else { Object[] args = new Object[arguments.length]; for (int i = 0; i < arguments.length; i++) { Argument<?> argument = arguments[i]; Object o = read(object, argument); if (o == null) { args[i] = o; } else { if (argument.getType().isInstance(o)) { args[i] = o; } else { ArgumentConversionContext<?> acc = ConversionContext.of(argument); args[i] = conversionService.convert(o, acc).orElseThrow(() -> { Optional<ConversionError> lastError = acc.getLastError(); return lastError.<RuntimeException>map(conversionError -> new ConversionErrorException(argument, conversionError)) .orElseGet(() -> new IllegalArgumentException("Cannot convert object type " + o.getClass() + " to required type: " + argument.getType()) ); } ); } } } instance = introspection.instantiate(args); } Collection<BeanProperty<R, Object>> properties = introspection.getBeanProperties(); for (BeanProperty<R, Object> property : properties) { if (property.isReadOnly()) { continue; } Object v = read(object, property.getName()); if (property.getType().isInstance(v)) { property.set(instance, v); } else { property.convertAndSet(instance, v); } } return instance; } catch (IntrospectionException | InstantiationException e) { throw new DataAccessException("Error instantiating type [" + type.getName() + "] from introspection: " + e.getMessage(), e); } }
Example #23
Source File: KafkaKeyBinder.java From micronaut-kafka with Apache License 2.0 | 4 votes |
@Override public BindingResult<T> bind(ArgumentConversionContext<T> context, ConsumerRecord<?, ?> source) { Object key = source.key(); Optional<T> converted = ConversionService.SHARED.convert(key, context); return () -> converted; }
Example #24
Source File: MicronautBeanProcessor.java From micronaut-spring with Apache License 2.0 | 4 votes |
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (environment != null) { String[] profiles = getProfiles(); micronautContext = new DefaultApplicationContext(profiles) { DefaultEnvironment env = new DefaultEnvironment(() -> Arrays.asList(profiles)) { @Override public io.micronaut.context.env.Environment start() { return this; } @Override public io.micronaut.context.env.Environment stop() { return this; } @Override public boolean containsProperty(String name) { return environment.containsProperty(name); } @Override public boolean containsProperties(String name) { return environment.containsProperty(name); } @Override public <T> Optional<T> getProperty(String name, ArgumentConversionContext<T> conversionContext) { return Optional.ofNullable(environment.getProperty(name, conversionContext.getArgument().getType())); } }; @Override public io.micronaut.context.env.Environment getEnvironment() { return env; } }; } else { micronautContext = new DefaultApplicationContext(); } micronautContext.start(); micronautBeanQualifierTypes .forEach(micronautBeanQualifierType -> { Qualifier<Object> micronautBeanQualifier; if (micronautBeanQualifierType.isAnnotation()) { micronautBeanQualifier = Qualifiers.byStereotype((Class<? extends Annotation>) micronautBeanQualifierType); } else { micronautBeanQualifier = Qualifiers.byType(micronautBeanQualifierType); } micronautContext.getBeanDefinitions(micronautBeanQualifier) .forEach(definition -> { final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder .rootBeanDefinition(MicronautSpringBeanFactory.class.getName()); beanDefinitionBuilder.addPropertyValue(MICRONAUT_BEAN_TYPE_PROPERTY_NAME, definition.getBeanType()); beanDefinitionBuilder.addPropertyValue(MICRONAUT_CONTEXT_PROPERTY_NAME, micronautContext); beanDefinitionBuilder.addPropertyValue(MICRONAUT_SINGLETON_PROPERTY_NAME, definition.isSingleton()); ((DefaultListableBeanFactory) beanFactory).registerBeanDefinition(definition.getName(), beanDefinitionBuilder.getBeanDefinition()); }); }); }
Example #25
Source File: ContextArgumentBinder.java From micronaut-aws with Apache License 2.0 | 4 votes |
@Override public BindingResult<Context> bind(ArgumentConversionContext<Context> context, HttpRequest<?> source) { return () -> source.getAttribute(RequestReader.LAMBDA_CONTEXT_PROPERTY).map(Context.class::cast); }
Example #26
Source File: HttpMethodArgumentBinder.java From micronaut-spring with Apache License 2.0 | 4 votes |
@Override public BindingResult<HttpMethod> bind(ArgumentConversionContext<HttpMethod> context, HttpRequest<?> source) { return () -> Optional.of(HttpMethod.valueOf(source.getMethod().name())); }
Example #27
Source File: KafkaHeadersBinder.java From micronaut-kafka with Apache License 2.0 | 4 votes |
@Override public BindingResult<MessageHeaders> bind(ArgumentConversionContext<MessageHeaders> context, ConsumerRecord<?, ?> source) { KafkaHeaders kafkaHeaders = new KafkaHeaders(source.headers()); return () -> Optional.of(kafkaHeaders); }