codec.get().encode(result, output); } else { byte[] bytes = environment
/** * 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)); }
/** * @param codecs The media type codecs */ DefaultMediaTypeCodecRegistry(Collection<MediaTypeCodec> codecs) { if (codecs != null) { this.codecs = Collections.unmodifiableCollection(codecs); for (MediaTypeCodec decoder : codecs) { Collection<MediaType> mediaTypes = decoder.getMediaTypes(); for (MediaType mediaType : mediaTypes) { if (mediaType != null) { decodersByExtension.put(mediaType.getExtension(), decoder); decodersByType.put(mediaType, decoder); } } } } else { this.codecs = Collections.emptyList(); } }
@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; }
@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)); }
LOG.debug("Encoding emitted response object [{}] using codec: {}", body, codec); byteBuf = (ByteBuf) codec.encode(body, new NettyByteBufferFactory(context.alloc())).asNativeBuffer();
/** * @param codecs The media type codecs */ DefaultMediaTypeCodecRegistry(Collection<MediaTypeCodec> codecs) { if (codecs != null) { this.codecs = Collections.unmodifiableCollection(codecs); for (MediaTypeCodec decoder : codecs) { Collection<MediaType> mediaTypes = decoder.getMediaTypes(); for (MediaType mediaType : mediaTypes) { if (mediaType != null) { decodersByExtension.put(mediaType.getExtension(), decoder); decodersByType.put(mediaType, decoder); } } } } else { this.codecs = 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))); }
LOG.debug("Encoding emitted response object [{}] using codec: {}", body, codec); byteBuf = (ByteBuf) codec.encode(body, new NettyByteBufferFactory(context.alloc())).asNativeBuffer();
/** * 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))); }
/** * Encode the given message with the given media type. * @param message The message * @param mediaType The media type * @return The encoded frame */ public WebSocketFrame encodeMessage(Object message, MediaType mediaType) { if (message instanceof byte[]) { return new BinaryWebSocketFrame(Unpooled.wrappedBuffer((byte[]) message)); } else if (ClassUtils.isJavaLangType(message.getClass())) { String s = message.toString(); return new TextWebSocketFrame(s); } else if (message instanceof ByteBuf) { return new BinaryWebSocketFrame((ByteBuf) message); } else if (message instanceof ByteBuffer) { return new BinaryWebSocketFrame(Unpooled.wrappedBuffer((ByteBuffer) message)); } else { Optional<MediaTypeCodec> codec = codecRegistry.findCodec(mediaType != null ? mediaType : MediaType.APPLICATION_JSON_TYPE); if (codec.isPresent()) { io.micronaut.core.io.buffer.ByteBuffer encoded = codec.get().encode(message, new NettyByteBufferFactory(UnpooledByteBufAllocator.DEFAULT)); return new TextWebSocketFrame((ByteBuf) encoded.asNativeBuffer()); } } throw new WebSocketSessionException("Unable to encode WebSocket message: " + message); } }
/** * 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()); }
@Override public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) { Optional<Class> viewClass = request.getAttribute(HttpAttributes.ROUTE_MATCH, AnnotationMetadata.class) .flatMap(ann -> ann.classValue(JsonView.class)); final Publisher<MutableHttpResponse<?>> responsePublisher = chain.proceed(request); if (viewClass.isPresent()) { return Flowable.fromPublisher(responsePublisher).switchMap(response -> { final Optional<?> body = response.getBody(); if (body.isPresent()) { MediaTypeCodec codec = codecFactory.resolveJsonViewCodec(viewClass.get()); return Flowable.fromCallable(() -> { final byte[] encoded = codec.encode(body.get()); ((MutableHttpResponse) response).body(encoded); return response; }).subscribeOn(Schedulers.from(executorService)); } else { return Flowable.just(response); } }); } else { return responsePublisher; } } }
/** * 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); }
} else if (mediaTypeCodecRegistry != null) { Optional<MediaTypeCodec> registeredCodec = mediaTypeCodecRegistry.findCodec(requestContentType); ByteBuf encoded = registeredCodec.map(codec -> (ByteBuf) codec.encode(o, byteBufferFactory).asNativeBuffer()) .orElse(null); if (encoded != null) { } else if (mediaTypeCodecRegistry != null) { Optional<MediaTypeCodec> registeredCodec = mediaTypeCodecRegistry.findCodec(requestContentType); bodyContent = registeredCodec.map(codec -> (ByteBuf) codec.encode(bodyValue, byteBufferFactory).asNativeBuffer()) .orElse(null);
/** * 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 jsonCodec = resolveMediaTypeCodecRegistry().findCodec(MediaType.APPLICATION_JSON_TYPE) .orElseThrow(() -> new CodecException("No possible JSON encoders found!")); body = jsonCodec.encode(data, allocator);
MediaTypeCodec codec = foundCodec.get(); try { return Optional.of(codec.decode(type, byteBufferFactory.wrap(content))); } catch (CodecException e) { if (LOG.isDebugEnabled()) {
private byte[] encodeInternal(MediaTypeCodec codec) throws InvalidResponseObjectException { byte[] encoded = null; try { if (body != null) { if (body instanceof ByteBuffer) { encoded = ((ByteBuffer) body).toByteArray(); } else if (body instanceof byte[]) { encoded = (byte[]) body; } else { final Optional<MediaType> contentType = getContentType(); if (!contentType.isPresent()) { contentType(MediaType.APPLICATION_JSON_TYPE); } encoded = codec.encode(body); } } } catch (Exception e) { throw new InvalidResponseObjectException("Invalid Response: " + e.getMessage() , e); } return encoded; }