/** * Decode the given type from the given bytes. * * @param type The type * @param bytes The bytes * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Class<T> type, byte[] bytes) throws CodecException { return decode(type, new ByteArrayInputStream(bytes)); }
/** * Decode the given type from the given bytes. * * @param type The type * @param bytes The bytes * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Argument<T> type, byte[] bytes) throws CodecException { return decode(type, new ByteArrayInputStream(bytes)); }
/** * Decode the given type from the given bytes. * * @param type The type * @param data The data as a string * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Class<T> type, String data) throws CodecException { return decode(type, new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8))); }
/** * Decode the given type from the given bytes. * * @param type The type * @param data The data as a string * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Argument<T> type, String data) throws CodecException { return decode(type, new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8))); }
/** * Decode the given type from the given buffer. Implementations optimized to handle {@link ByteBuffer} instances * should override this method. * * @param type The type * @param buffer the buffer * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Class<T> type, ByteBuffer<?> buffer) throws CodecException { return decode(type, buffer.toInputStream()); }
/** * Decode the given type from the given {@link InputStream}. * * @param type The type * @param inputStream The input stream * @param <T> The generic type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Class<T> type, InputStream inputStream) throws CodecException { return decode(Argument.of(type), inputStream); }
/** * Decode the given type from the given buffer. Implementations optimized to handle {@link ByteBuffer} instances * should override this method. * * @param type The type * @param buffer the buffer * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Argument<T> type, ByteBuffer<?> buffer) throws CodecException { return decode(type, buffer.toInputStream()); }
MediaTypeCodec codec = foundCodec.get(); try { return Optional.of(codec.decode(type, byteBufferFactory.wrap(content))); } catch (CodecException e) { if (LOG.isDebugEnabled()) {
/** * Get. * * @param type type * @param <T> generic return type * @return Type */ public final <T> T get(Class<T> type) { if (ClassUtils.isJavaLangType(type)) { return applicationContext .getConversionService() .convert(data, type).orElseThrow(() -> newIllegalArgument(type, data)); } else { MediaTypeCodecRegistry codecRegistry = applicationContext.getBean(MediaTypeCodecRegistry.class); return codecRegistry.findCodec(MediaType.APPLICATION_JSON_TYPE) .map(codec -> { if (data != null) { return codec.decode(type, data); } else { // try System.in return codec.decode(type, System.in); } }) .orElseThrow(() -> newIllegalArgument(type, data)); } }
@Override public <I, B> Flowable<Event<B>> eventStream(io.micronaut.http.HttpRequest<I> request, Argument<B> eventType) { return eventStream(request).map(byteBufferEvent -> { ByteBuffer<?> data = byteBufferEvent.getData(); Optional<MediaTypeCodec> registeredCodec; if (mediaTypeCodecRegistry != null) { registeredCodec = mediaTypeCodecRegistry.findCodec(MediaType.APPLICATION_JSON_TYPE); } else { registeredCodec = Optional.empty(); } if (registeredCodec.isPresent()) { B decoded = registeredCodec.get().decode(eventType, data); return Event.of(byteBufferEvent, decoded); } else { throw new CodecException("JSON codec not present"); } }); }
private Object decodeInputArgument( ConversionService<?> conversionService, LocalFunctionRegistry localFunctionRegistry, Argument<?> arg, InputStream input) { Class<?> argType = arg.getType(); ClassLoadingReporter.reportBeanPresent(argType); if (ClassUtils.isJavaLangType(argType)) { Object converted = doConvertInput(conversionService, arg, input); if (converted != null) { return converted; } } else if (argType.isInstance(input)) { return input; } else { if (localFunctionRegistry instanceof MediaTypeCodecRegistry) { Optional<MediaTypeCodec> registeredDecoder = ((MediaTypeCodecRegistry) localFunctionRegistry).findCodec(MediaType.APPLICATION_JSON_TYPE); if (registeredDecoder.isPresent()) { MediaTypeCodec decoder = registeredDecoder.get(); return decoder.decode(arg, input); } } } throw new CodecException("Unable to decode argument from stream: " + arg); }
@Override public Optional<Object> convert(FileUpload object, Class<Object> targetType, ConversionContext context) { try { if (!object.isCompleted()) { return Optional.empty(); } String contentType = object.getContentType(); ByteBuf byteBuf = object.getByteBuf(); if (StringUtils.isNotEmpty(contentType)) { MediaType mediaType = new MediaType(contentType); Optional<MediaTypeCodec> registered = decoderRegistryProvider.get().findCodec(mediaType); if (registered.isPresent()) { MediaTypeCodec decoder = registered.get(); Object val = decoder.decode(targetType, new ByteBufInputStream(byteBuf)); return Optional.of(val); } else { return conversionService.convert(byteBuf, targetType, context); } } return conversionService.convert(byteBuf, targetType, context); } catch (Exception e) { context.reject(e); return Optional.empty(); } } }
MediaType mediaType = messageHandler.getValue(Consumes.class, MediaType.class).orElse(MediaType.APPLICATION_JSON_TYPE); try { converted = mediaTypeCodecRegistry.findCodec(mediaType).map(codec -> codec.decode(bodyArgument, new NettyByteBufferFactory(ctx.alloc()).wrap(msg.content()))); } catch (CodecException e) { if (LOG.isErrorEnabled()) {
/** * Decode the given type from the given bytes. * * @param type The type * @param bytes The bytes * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Class<T> type, byte[] bytes) throws CodecException { return decode(type, new ByteArrayInputStream(bytes)); }
/** * Decode the given type from the given bytes. * * @param type The type * @param bytes The bytes * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Argument<T> type, byte[] bytes) throws CodecException { return decode(type, new ByteArrayInputStream(bytes)); }
/** * Decode the given type from the given bytes. * * @param type The type * @param data The data as a string * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Class<T> type, String data) throws CodecException { return decode(type, new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8))); }
/** * Decode the given type from the given bytes. * * @param type The type * @param data The data as a string * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Argument<T> type, String data) throws CodecException { return decode(type, new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8))); }
/** * Decode the given type from the given {@link InputStream}. * * @param type The type * @param inputStream The input stream * @param <T> The generic type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Class<T> type, InputStream inputStream) throws CodecException { return decode(Argument.of(type), inputStream); }
/** * Decode the given type from the given buffer. Implementations optimized to handle {@link ByteBuffer} instances * should override this method. * * @param type The type * @param buffer the buffer * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Class<T> type, ByteBuffer<?> buffer) throws CodecException { return decode(type, buffer.toInputStream()); }
/** * Decode the given type from the given buffer. Implementations optimized to handle {@link ByteBuffer} instances * should override this method. * * @param type The type * @param buffer the buffer * @param <T> The decoded type * @return The decoded result * @throws CodecException When the result cannot be decoded */ default <T> T decode(Argument<T> type, ByteBuffer<?> buffer) throws CodecException { return decode(type, buffer.toInputStream()); }