private Object doConvertInput(ConversionService<?> conversionService, Argument<?> arg, Object object) { ArgumentConversionContext conversionContext = ConversionContext.of(arg); Optional<?> convert = conversionService.convert(object, conversionContext); if (convert.isPresent()) { return convert.get(); } else { Optional<ConversionError> lastError = conversionContext.getLastError(); if (lastError.isPresent()) { throw new ConversionErrorException(arg, lastError.get()); } } return null; } }
@Override default AnnotationMetadata getAnnotationMetadata() { return getArgument().getAnnotationMetadata(); }
@Override public BindingResult<T> bind(ArgumentConversionContext<T> argument, HttpRequest<?> source) { MutableConvertibleValues<Object> parameters = source.getAttributes(); AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata(); String parameterName = annotationMetadata.getValue(RequestAttribute.class, String.class).orElse(argument.getArgument().getName()); return doBind(argument, parameters, parameterName); }
@Override protected void doOnComplete() { Optional<Argument<?>> firstTypeParameter = context.getFirstTypeVariable(); Optional body = nettyHttpRequest.getBody(); if (body.isPresent()) { if (firstTypeParameter.isPresent()) { Argument<?> arg = firstTypeParameter.get(); Class targetType = arg.getType(); Optional converted = conversionService.convert(body.get(), context.with(arg)); if (converted.isPresent()) { future.complete(converted.get()); } else { future.completeExceptionally(new IllegalArgumentException("Cannot bind JSON to argument type: " + targetType.getName())); } } else { future.complete(body.get()); } } else { future.complete(null); } } });
if (nativeRequest instanceof StreamedHttpRequest) { Optional<MediaType> contentType = source.getContentType(); Argument<?> targetType = context.getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT); HttpContentProcessor<?> processor = contentType .flatMap(type -> beanLocator.findBean(HttpContentSubscriberFactory.class, new ConsumesMediaTypeQualifier<>(type))) return () -> Optional.of(subscriber -> processor.subscribe(new TypedSubscriber<Object>((Argument) context.getArgument()) {
private static String buildConversionMessage(String property, ArgumentConversionContext conversionContext) { Optional<ConversionError> lastError = conversionContext.getLastError(); if (lastError.isPresent()) { ConversionError conversionError = lastError.get(); return "Error resolving property value [" + property + "]. Unable to convert value " + conversionError.getOriginalValue().map(o -> "[" + o + "]").orElse("") + " to target type [" + conversionContext.getArgument().getTypeString(true) + "] due to: " + conversionError.getCause().getMessage(); } else { return "Error resolving property value [" + property + "]. Property doesn't exist"; } } }
/** * @param name The property name * @param entries The entries * @param conversionContext The conversion context * @return The submap */ protected Map<String, Object> resolveSubMap(String name, Map<String, Object> entries, ArgumentConversionContext<?> conversionContext) { // special handling for maps for resolving sub keys AnnotationMetadata annotationMetadata = conversionContext.getAnnotationMetadata(); StringConvention keyConvention = annotationMetadata.getValue(MapFormat.class, "keyFormat", StringConvention.class).orElse(StringConvention.RAW); MapFormat.MapTransformation transformation = annotationMetadata.getValue( MapFormat.class, "transformation", MapFormat.MapTransformation.class) .orElse(conversionContext.isAnnotationPresent(Property.class) ? MapFormat.MapTransformation.FLAT : MapFormat.MapTransformation.NESTED); return resolveSubMap(name, entries, conversionContext, keyConvention, transformation); }
@Override public BindingResult<Object> bind(ArgumentConversionContext<Object> context, Map<CharSequence, ? super Object> source) { try { ObjectNode objectNode = buildSourceObjectNode(source.entrySet()); Object result = objectMapper.treeToValue(objectNode, context.getArgument().getType()); return () -> Optional.of(result); } catch (Exception e) { context.reject(e); return BindingResult.EMPTY; } }
/** * @param name The property name * @param entries The entries * @param conversionContext The conversion context * @return The subproperties */ protected Properties resolveSubProperties(String name, Map<String, Object> entries, ArgumentConversionContext<?> conversionContext) { // special handling for maps for resolving sub keys Properties properties = new Properties(); AnnotationMetadata annotationMetadata = conversionContext.getAnnotationMetadata(); StringConvention keyConvention = annotationMetadata.getValue(MapFormat.class, "keyFormat", StringConvention.class) .orElse(StringConvention.RAW); String prefix = name + '.'; entries.entrySet().stream() .filter(map -> map.getKey().startsWith(prefix)) .forEach(entry -> { Object value = entry.getValue(); if (value != null) { String key = entry.getKey().substring(prefix.length()); key = keyConvention.format(key); properties.put(key, resolvePlaceHoldersIfNecessary(value.toString())); } }); return properties; }
final Object v = paramMap.get(argumentName); if (v != null) { paramMap.put(argumentName, conversionService.convert(v, ConversionContext.of(String.class).with(argument.getAnnotationMetadata()))); conversionService.convert(definedValue, ConversionContext.of(String.class).with(annotationMetadata)).ifPresent(o -> { if (!StringUtils.isEmpty(parameterName)) { paramMap.put(parameterName, o); } else if (annotationMetadata.isAnnotationPresent(PathVariable.class)) { String parameterName = annotationMetadata.getValue(PathVariable.class, String.class).orElse(null); conversionService.convert(definedValue, ConversionContext.of(String.class).with(annotationMetadata)).ifPresent(o -> { if (!StringUtils.isEmpty(o)) { paramMap.put(parameterName, o);
private static String buildConversionMessage(String property, ArgumentConversionContext conversionContext) { Optional<ConversionError> lastError = conversionContext.getLastError(); if (lastError.isPresent()) { ConversionError conversionError = lastError.get(); return "Error resolving property value [" + property + "]. Unable to convert value " + conversionError.getOriginalValue().map(o -> "[" + o + "]").orElse("") + " to target type [" + conversionContext.getArgument().getTypeString(true) + "] due to: " + conversionError.getCause().getMessage(); } else { return "Error resolving property value [" + property + "]. Property doesn't exist"; } } }
AnnotationMetadata annotationMetadata = conversionContext.getAnnotationMetadata(); StringConvention keyConvention = annotationMetadata.getValue(MapFormat.class, "keyFormat", StringConvention.class).orElse(StringConvention.RAW); String prefix = name + '.'; "transformation", MapFormat.MapTransformation.class) .orElse(conversionContext.isAnnotationPresent(Property.class) ? MapFormat.MapTransformation.FLAT : MapFormat.MapTransformation.NESTED);
/** * @param name The property name * @param entries The entries * @param conversionContext The conversion context * @return The subproperties */ protected Properties resolveSubProperties(String name, Map<String, Object> entries, ArgumentConversionContext<?> conversionContext) { // special handling for maps for resolving sub keys Properties properties = new Properties(); AnnotationMetadata annotationMetadata = conversionContext.getAnnotationMetadata(); StringConvention keyConvention = annotationMetadata.getValue(MapFormat.class, "keyFormat", StringConvention.class) .orElse(StringConvention.RAW); String prefix = name + '.'; entries.entrySet().stream() .filter(map -> map.getKey().startsWith(prefix)) .forEach(entry -> { Object value = entry.getValue(); if (value != null) { String key = entry.getKey().substring(prefix.length()); key = keyConvention.format(key); properties.put(key, resolvePlaceHoldersIfNecessary(value.toString())); } }); return properties; }
final Object v = paramMap.get(argumentName); if (v != null) { paramMap.put(argumentName, conversionService.convert(v, ConversionContext.of(String.class).with(argument.getAnnotationMetadata()))); conversionService.convert(definedValue, ConversionContext.of(String.class).with(annotationMetadata)).ifPresent(o -> { if (!StringUtils.isEmpty(parameterName)) { paramMap.put(parameterName, o); } else if (annotationMetadata.isAnnotationPresent(PathVariable.class)) { String parameterName = annotationMetadata.getValue(PathVariable.class, String.class).orElse(null); conversionService.convert(definedValue, ConversionContext.of(String.class).with(annotationMetadata)).ifPresent(o -> { if (!StringUtils.isEmpty(o)) { paramMap.put(parameterName, o);
@Override public <T> Optional<T> get(K name, ArgumentConversionContext<T> conversionContext) { Object v = map.get(name); if (v == null) { return Optional.empty(); } Argument<T> argument = conversionContext.getArgument(); if (argument.getType().isInstance(v)) { return Optional.of((T) v); } return ConversionService.SHARED.convert(v, conversionContext); }
@Override public BindingResult<T> bind(ArgumentConversionContext<T> argument, HttpRequest<?> source) { ConvertibleMultiValues<String> parameters = source.getHeaders(); AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata(); String parameterName = annotationMetadata.getValue(Header.class, String.class).orElse(argument.getArgument().getName()); return doBind(argument, parameters, parameterName); }
@Override public BindingResult<T> bind(ArgumentConversionContext<T> context, HttpRequest<?> source) { Optional<?> body = source.getBody(); if (!body.isPresent()) { //noinspection unchecked return BindingResult.EMPTY; } else { Object o = body.get(); Optional<T> converted = conversionService.convert(o, context); final Optional<ConversionError> lastError = context.getLastError(); //noinspection OptionalIsPresent if (lastError.isPresent()) { return new BindingResult<T>() { @Override public Optional<T> getValue() { return Optional.empty(); } @Override public List<ConversionError> getConversionErrors() { return Collections.singletonList(lastError.get()); } }; } else { return () -> converted; } } } }
final Object v = paramMap.get(argumentName); if (v != null) { paramMap.put(argumentName, conversionService.convert(v, ConversionContext.of(String.class).with(argument.getAnnotationMetadata()))); conversionService.convert(definedValue, ConversionContext.of(String.class).with(annotationMetadata)).ifPresent(o -> { if (!StringUtils.isEmpty(parameterName)) { paramMap.put(parameterName, o);
/** * Convert the value and return a binding result. * * @param value The value to convert * @param context The conversion context * @return The binding result */ protected BindingResult<T> doConvert(Object value, ArgumentConversionContext<T> context) { Optional<T> result = conversionService.convert(value, context); if (result.isPresent() && context.getArgument().getType() == Optional.class) { return () -> (Optional<T>) result.get(); } return () -> result; } }
@Override public BindingResult<T> bind(ArgumentConversionContext<T> argument, HttpRequest<?> source) { ConvertibleValues<io.micronaut.http.cookie.Cookie> parameters = source.getCookies(); AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata(); String parameterName = annotationMetadata.getValue(CookieValue.class, String.class) .orElse(argument.getArgument().getName()); return doBind(argument, parameters, parameterName); }