/** * Create a new registry from the given codecs. * * @param codecs The decoders * @return The registry */ static MediaTypeCodecRegistry of(Collection<MediaTypeCodec> codecs) { return new DefaultMediaTypeCodecRegistry(codecs); } }
@Override public Collection<MediaTypeCodec> getCodecs() { return decoderRegistry.getCodecs(); }
/** * Constructor. * @param decoders decoders */ public DefaultLocalFunctionRegistry(MediaTypeCodec... decoders) { this.decoderRegistry = MediaTypeCodecRegistry.of(decoders); }
@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"); } }); }
/** * 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)); }
@Override public Optional<MediaTypeCodec> findCodec(MediaType mediaType) { return decoderRegistry.findCodec(mediaType); }
@Override public <T> void encode(T object, OutputStream outputStream) throws CodecException { try { outputStream.write(encode(object)); } catch (IOException e) { throw new CodecException("I/O error occurred encoding object to output stream: " + e.getMessage(), e); } }
@Override public Optional<MediaTypeCodec> findCodec(MediaType mediaType, Class<?> type) { Optional<MediaTypeCodec> codec = findCodec(mediaType); if (codec.isPresent()) { MediaTypeCodec mediaTypeCodec = codec.get(); if (mediaTypeCodec.supportsType(type)) { return codec; } else { return Optional.empty(); } } return codec; }
/** * 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)); }
@Override public Optional<MediaTypeCodec> findCodec(MediaType mediaType, Class<?> type) { return decoderRegistry.findCodec(mediaType, type); }
/** * Constructor. * @param decoders decoders */ @Inject public DefaultLocalFunctionRegistry(List<MediaTypeCodec> decoders) { this.decoderRegistry = MediaTypeCodecRegistry.of(decoders); }
/** * Create a new registry from the given codecs. * * @param codecs The decoders * @return The registry */ static MediaTypeCodecRegistry of(MediaTypeCodec... codecs) { return new DefaultMediaTypeCodecRegistry(codecs); }
@Override public Collection<MediaTypeCodec> getCodecs() { if (localFunctionRegistry instanceof MediaTypeCodecRegistry) { return ((MediaTypeCodecRegistry) localFunctionRegistry).getCodecs(); } return Collections.emptyList(); } }
/** * 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))); }
@Override public Optional<MediaTypeCodec> findCodec(MediaType mediaType) { if (localFunctionRegistry instanceof MediaTypeCodecRegistry) { return ((MediaTypeCodecRegistry) localFunctionRegistry).findCodec(mediaType); } return Optional.empty(); }
/** * 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))); }
@Override public Optional<MediaTypeCodec> findCodec(MediaType mediaType, Class<?> type) { if (localFunctionRegistry instanceof MediaTypeCodecRegistry) { return ((MediaTypeCodecRegistry) localFunctionRegistry).findCodec(mediaType, type); } return Optional.empty(); }
/** * 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()); }