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);
}
}
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);
}
}
}
}
Aggregations