Search in sources :

Example 1 with NettyCustomizableResponseTypeHandler

use of io.micronaut.http.server.netty.types.NettyCustomizableResponseTypeHandler in project micronaut-core by micronaut-projects.

the class DefaultNettyEmbeddedServerFactory method buildInternal.

@NotNull
private NettyEmbeddedServer buildInternal(@NonNull NettyHttpServerConfiguration configuration, boolean isDefaultServer, @Nullable ServerSslConfiguration sslConfiguration) {
    Objects.requireNonNull(configuration, "Netty HTTP server configuration cannot be null");
    List<NettyCustomizableResponseTypeHandler<?>> handlers = Arrays.asList(new FileTypeHandler(configuration.getFileTypeHandlerConfiguration()), new StreamTypeHandler());
    if (isDefaultServer) {
        return new NettyHttpServer(configuration, this, new DefaultCustomizableResponseTypeHandlerRegistry(handlers.toArray(new NettyCustomizableResponseTypeHandler[0])), true);
    } else {
        NettyEmbeddedServices embeddedServices = resolveNettyEmbeddedServices(configuration, sslConfiguration);
        return new NettyHttpServer(configuration, embeddedServices, new DefaultCustomizableResponseTypeHandlerRegistry(handlers.toArray(new NettyCustomizableResponseTypeHandler[0])), false);
    }
}
Also used : DefaultCustomizableResponseTypeHandlerRegistry(io.micronaut.http.server.netty.types.DefaultCustomizableResponseTypeHandlerRegistry) FileTypeHandler(io.micronaut.http.server.netty.types.files.FileTypeHandler) NettyCustomizableResponseTypeHandler(io.micronaut.http.server.netty.types.NettyCustomizableResponseTypeHandler) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with NettyCustomizableResponseTypeHandler

use of io.micronaut.http.server.netty.types.NettyCustomizableResponseTypeHandler in project micronaut-core by micronaut-projects.

the class RoutingInBoundHandler method encodeResponseBody.

private void encodeResponseBody(ChannelHandlerContext context, HttpRequest<?> request, MutableHttpResponse<?> message, @Nullable Argument<Object> bodyType, Object body) {
    if (body == null) {
        return;
    }
    Optional<NettyCustomizableResponseTypeHandler> typeHandler = customizableResponseTypeHandlerRegistry.findTypeHandler(body.getClass());
    if (typeHandler.isPresent()) {
        NettyCustomizableResponseTypeHandler th = typeHandler.get();
        setBodyContent(message, new NettyCustomizableResponseTypeHandlerInvoker(th, body));
    } else {
        MediaType mediaType = message.getContentType().orElse(null);
        if (mediaType == null) {
            mediaType = message.getAttribute(HttpAttributes.ROUTE_INFO, RouteInfo.class).map(routeInfo -> routeExecutor.resolveDefaultResponseContentType(request, routeInfo)).orElse(MediaType.APPLICATION_JSON_TYPE);
            message.contentType(mediaType);
        }
        if (body instanceof CharSequence) {
            ByteBuf byteBuf = Unpooled.wrappedBuffer(body.toString().getBytes(message.getCharacterEncoding()));
            setResponseBody(message, byteBuf);
        } else if (body instanceof byte[]) {
            ByteBuf byteBuf = Unpooled.wrappedBuffer((byte[]) body);
            setResponseBody(message, byteBuf);
        } else if (body instanceof ByteBuffer) {
            ByteBuffer<?> byteBuffer = (ByteBuffer) body;
            Object nativeBuffer = byteBuffer.asNativeBuffer();
            if (nativeBuffer instanceof ByteBuf) {
                setResponseBody(message, (ByteBuf) nativeBuffer);
            } else if (nativeBuffer instanceof java.nio.ByteBuffer) {
                ByteBuf byteBuf = Unpooled.wrappedBuffer((java.nio.ByteBuffer) nativeBuffer);
                setResponseBody(message, byteBuf);
            }
        } else if (body instanceof ByteBuf) {
            setResponseBody(message, (ByteBuf) body);
        } else {
            Optional<MediaTypeCodec> registeredCodec = mediaTypeCodecRegistry.findCodec(mediaType, body.getClass());
            if (registeredCodec.isPresent()) {
                MediaTypeCodec codec = registeredCodec.get();
                encodeBodyWithCodec(message, bodyType, body, codec, context, request);
            } else {
                MediaTypeCodec defaultCodec = new TextPlainCodec(serverConfiguration.getDefaultCharset());
                encodeBodyWithCodec(message, bodyType, body, defaultCodec, context, request);
            }
        }
    }
}
Also used : Optional(java.util.Optional) NettyCustomizableResponseTypeHandler(io.micronaut.http.server.netty.types.NettyCustomizableResponseTypeHandler) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(io.micronaut.core.io.buffer.ByteBuffer) TextPlainCodec(io.micronaut.runtime.http.codec.TextPlainCodec) MediaType(io.micronaut.http.MediaType) RouteInfo(io.micronaut.web.router.RouteInfo) MediaTypeCodec(io.micronaut.http.codec.MediaTypeCodec)

Aggregations

NettyCustomizableResponseTypeHandler (io.micronaut.http.server.netty.types.NettyCustomizableResponseTypeHandler)2 ByteBuffer (io.micronaut.core.io.buffer.ByteBuffer)1 MediaType (io.micronaut.http.MediaType)1 MediaTypeCodec (io.micronaut.http.codec.MediaTypeCodec)1 DefaultCustomizableResponseTypeHandlerRegistry (io.micronaut.http.server.netty.types.DefaultCustomizableResponseTypeHandlerRegistry)1 FileTypeHandler (io.micronaut.http.server.netty.types.files.FileTypeHandler)1 TextPlainCodec (io.micronaut.runtime.http.codec.TextPlainCodec)1 RouteInfo (io.micronaut.web.router.RouteInfo)1 ByteBuf (io.netty.buffer.ByteBuf)1 Optional (java.util.Optional)1 NotNull (org.jetbrains.annotations.NotNull)1