/** * Create a simple {@link ConversionContext} for the given generic type variables. * * @param <T> type Generic * @param argument The argument * @return The conversion context */ static <T> ArgumentConversionContext<T> of(Argument<T> argument) { return of(argument, null, null); }
@Override public void reject(Exception exception) { thisContext.reject(exception); }
private SimpleDateFormat resolveFormat(ConversionContext context) { AnnotationMetadata annotationMetadata = context.getAnnotationMetadata(); Optional<String> format = annotationMetadata.getValue(Format.class, String.class); return format .map((pattern) -> new SimpleDateFormat(pattern, context.getLocale())) .orElse(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", context.getLocale())); }
/** * Augment this context with annotation metadata. * * @param annotationMetadata The annotation metadata * @return The conversion context */ @SuppressWarnings("unchecked") default ArgumentConversionContext<T> with(AnnotationMetadata annotationMetadata) { ConversionContext thisContext = this; return new DefaultArgumentConversionContext(getArgument(), thisContext.getLocale(), thisContext.getCharset()) { @Override public AnnotationMetadata getAnnotationMetadata() { return annotationMetadata; } }; } }
default <T> ArgumentConversionContext<T> with(Argument<T> argument) { ConversionContext childContext = ConversionContext.of(argument); ConversionContext thisContext = this; return new DefaultArgumentConversionContext(argument, thisContext.getLocale(), thisContext.getCharset()) { @Override public <T extends Annotation> T synthesize(Class<T> annotationClass) {
Optional<Argument<?>> firstTypeVariable = context.getFirstTypeVariable(); Argument<?> argument = firstTypeVariable.orElse(Argument.OBJECT_ARGUMENT); Optional converted = DefaultConversionService.this.convert(object, context.with(argument)); if (converted.isPresent()) { return Optional.of(Collections.singletonList(converted.get())); return Optional.of(IOUtils.readText(reader)); } catch (IOException e) { context.reject(e); return Optional.empty(); addConverter(CharSequence.class, byte[].class, (object, targetType, context) -> Optional.of(object.toString().getBytes(context.getCharset()))); addConverter(Integer.class, byte[].class, (object, targetType, context) -> Optional.of(ByteBuffer.allocate(Integer.BYTES).putInt(object).array())); addConverter(Character.class, byte[].class, (object, targetType, context) -> Optional.of(ByteBuffer.allocate(Integer.BYTES).putChar(object).array())); return Optional.of(IOUtils.readText(reader)); } catch (IOException e) { context.reject(e); return Optional.empty(); return Optional.of(format.parse(object.toString())); } catch (ParseException e) { context.reject(object, e); return Optional.empty(); return Optional.ofNullable(Paths.get(object.toString())); } catch (Exception e) { context.reject("Invalid path [" + object + " ]: " + e.getMessage(), e); return Optional.empty();
/** * @return A converter that converts bytebufs to strings */ @Singleton @Bean TypeConverter<ByteBuf, CharSequence> byteBufCharSequenceTypeConverter() { return (object, targetType, context) -> Optional.of(object.toString(context.getCharset())); }
@SuppressWarnings("unchecked") @Override public <T> Optional<T> convert(Object object, Class<T> targetType, ConversionContext context) { if (object == null || targetType == null || context == null) { return Optional.empty(); } if (targetType == Object.class) { return Optional.of((T) object); } Class<?> sourceType = object.getClass(); targetType = ReflectionUtils.getWrapperType(targetType); if (targetType.isInstance(object) && !Iterable.class.isInstance(object) && !Map.class.isInstance(object)) { return Optional.of((T) object); } Optional<? extends Class<? extends Annotation>> formattingAnn = context.getAnnotationMetadata().getAnnotationTypeByStereotype(Format.class); Class<? extends Annotation> formattingAnnotation = formattingAnn.orElse(null); ConvertiblePair pair = new ConvertiblePair(sourceType, targetType, formattingAnnotation); TypeConverter typeConverter = converterCache.get(pair); if (typeConverter == null) { typeConverter = findTypeConverter(sourceType, targetType, formattingAnnotation); if (typeConverter == null) { return Optional.empty(); } else { converterCache.put(pair, typeConverter); } } return typeConverter.convert(object, targetType, context); }
/** * @return A converter that converts composite bytebufs to strings */ @Singleton @Bean TypeConverter<CompositeByteBuf, CharSequence> compositeByteBufCharSequenceTypeConverter() { return (object, targetType, context) -> Optional.of(object.toString(context.getCharset())); }
/** * Create a simple {@link ConversionContext} for the given generic type variables. * * @param <T> type Generic * @param argument The argument * @param locale The locale * @return The conversion context */ static <T> ArgumentConversionContext of(Argument<T> argument, @Nullable Locale locale) { return of(argument, locale, null); }
@Override public Optional<byte[]> convert(NettyPartData object, Class<byte[]> targetType, ConversionContext context) { try { return Optional.of(object.getBytes()); } catch (IOException e) { context.reject(e); return Optional.empty(); } } }
private DateTimeFormatter resolveFormatter(ConversionContext context) { Optional<String> format = context.getAnnotationMetadata().getValue(Format.class, String.class); return format .map((pattern) -> DateTimeFormatter.ofPattern(pattern, context.getLocale())) .orElse(DateTimeFormatter.RFC_1123_DATE_TIME); } }
/** * Get the value of the {@code value} member of the annotation. * * @param argument The argument * @param <T> The type * @return The result */ public final <T> Optional<T> getValue(Argument<T> argument) { return getValue(ConversionContext.of(argument)); }
@Override public Optional<CompletedFileUpload> convert(FileUpload upload, Class<CompletedFileUpload> targetType, ConversionContext context) { try { if (!upload.isCompleted()) { return Optional.empty(); } return Optional.of(new NettyCompletedFileUpload(upload)); } catch (Exception e) { context.reject(e); return Optional.empty(); } } }
/** * Get the value of the {@code value} member of the annotation. * * @param member The member * @param type The type * @param <T> The type * @throws IllegalStateException If no member is available that conforms to the given name and type * @return The result */ public @Nonnull final <T> T getRequiredValue(String member, Class<T> type) { return get(member, ConversionContext.of(type)).orElseThrow(() -> new IllegalStateException("No value available for annotation member @" + annotationName + "[" + member + "] of type: " + type)); }
@Override public Optional<Object> convert(Attribute object, Class<Object> targetType, ConversionContext context) { try { return conversionService.convert(object.getValue(), targetType, context); } catch (IOException e) { context.reject(e); return Optional.empty(); } } }
/** * Returns a submap for all the keys with the given prefix. * * @param prefix The prefix * @param valueType The value type * @return The submap */ @SuppressWarnings("unchecked") default Map<String, V> subMap(String prefix, Argument<V> valueType) { return subMap(prefix, ConversionContext.of(valueType)); }
@Override public Optional<Number> convert(CharSequence object, Class<Number> targetType, ConversionContext context) { String value = object.toString().toUpperCase(Locale.ENGLISH); try { if (value.endsWith("KB")) { long size = Long.valueOf(value.substring(0, value.length() - 2)) * KB_UNIT; return ConversionService.SHARED.convert(size, targetType); } if (value.endsWith("MB")) { long size = Long.valueOf(value.substring(0, value.length() - 2)) * KB_UNIT * KB_UNIT; return ConversionService.SHARED.convert(size, targetType); } if (value.endsWith("GB")) { long size = Long.valueOf(value.substring(0, value.length() - 2)) * KB_UNIT * KB_UNIT * KB_UNIT; return ConversionService.SHARED.convert(size, targetType); } Long size = Long.valueOf(value); return ConversionService.SHARED.convert(size, targetType); } catch (NumberFormatException e) { context.reject(value, e); return Optional.empty(); } } }
/** * Create a simple {@link ConversionContext} for the given generic type variables. * * @param <T> type Generic * @param argument The argument * @return The conversion context */ static <T> ArgumentConversionContext<T> of(Class<T> argument) { return of(Argument.of(argument), null, null); }
@Override public Optional<CharSequence> convert(HttpData upload, Class<CharSequence> targetType, ConversionContext context) { try { if (!upload.isCompleted()) { return Optional.empty(); } ByteBuf byteBuf = upload.getByteBuf(); return conversionService.convert(byteBuf, targetType, context); } catch (Exception e) { context.reject(e); return Optional.empty(); } } }