Search in sources :

Example 1 with NettyMutableHttpResponse

use of io.micronaut.http.netty.NettyMutableHttpResponse in project micronaut-core by micronaut-projects.

the class NettyHttpResponseFactory method getOr.

/**
 * Lookup the response from the context.
 *
 * @param request     The context
 * @param alternative The alternative HttpResponse
 * @return The {@link NettyMutableHttpResponse}
 */
@Internal
public static NettyMutableHttpResponse getOr(NettyHttpRequest<?> request, io.micronaut.http.HttpResponse<?> alternative) {
    Attribute<NettyMutableHttpResponse> attr = request.attr(ServerAttributeKeys.RESPONSE_KEY);
    NettyMutableHttpResponse nettyHttpResponse = attr.get();
    if (nettyHttpResponse == null) {
        nettyHttpResponse = (NettyMutableHttpResponse) alternative;
        attr.set(nettyHttpResponse);
    }
    return nettyHttpResponse;
}
Also used : NettyMutableHttpResponse(io.micronaut.http.netty.NettyMutableHttpResponse) Internal(io.micronaut.core.annotation.Internal)

Example 2 with NettyMutableHttpResponse

use of io.micronaut.http.netty.NettyMutableHttpResponse in project micronaut-core by micronaut-projects.

the class RoutingInBoundHandler method createNettyResponse.

@NonNull
private NettyMutableHttpResponse<?> createNettyResponse(HttpResponse<?> message) {
    HttpStatus httpStatus = message.status();
    Object body = message.body();
    io.netty.handler.codec.http.HttpHeaders nettyHeaders = new DefaultHttpHeaders(serverConfiguration.isValidateHeaders());
    message.getHeaders().forEach((BiConsumer<String, List<String>>) nettyHeaders::set);
    return new NettyMutableHttpResponse<>(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(httpStatus.getCode(), httpStatus.getReason()), body instanceof ByteBuf ? body : null, ConversionService.SHARED);
}
Also used : NettyMutableHttpResponse(io.micronaut.http.netty.NettyMutableHttpResponse) HttpStatus(io.micronaut.http.HttpStatus) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ArrayList(java.util.ArrayList) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) NonNull(io.micronaut.core.annotation.NonNull)

Example 3 with NettyMutableHttpResponse

use of io.micronaut.http.netty.NettyMutableHttpResponse in project micronaut-core by micronaut-projects.

the class HttpResponseEncoder method encode.

@Override
protected void encode(ChannelHandlerContext context, MutableHttpResponse<?> response, List<Object> out) {
    Optional<MediaType> specifiedMediaType = response.getContentType();
    MediaType responseMediaType = specifiedMediaType.orElse(MediaType.APPLICATION_JSON_TYPE);
    applyConfiguredHeaders(response.getHeaders());
    Optional<?> responseBody = response.getBody();
    if (responseBody.isPresent()) {
        Object body = responseBody.get();
        if (specifiedMediaType.isPresent()) {
            Optional<MediaTypeCodec> registeredCodec = mediaTypeCodecRegistry.findCodec(responseMediaType, body.getClass());
            if (registeredCodec.isPresent()) {
                MediaTypeCodec codec = registeredCodec.get();
                response = encodeBodyWithCodec(response, body, codec, responseMediaType, context);
            }
        }
        Optional<MediaTypeCodec> registeredCodec = mediaTypeCodecRegistry.findCodec(MediaType.APPLICATION_JSON_TYPE, body.getClass());
        if (registeredCodec.isPresent()) {
            MediaTypeCodec codec = registeredCodec.get();
            response = encodeBodyWithCodec(response, body, codec, responseMediaType, context);
        }
        MediaTypeCodec defaultCodec = new TextPlainCodec(serverConfiguration.getDefaultCharset());
        response = encodeBodyWithCodec(response, body, defaultCodec, responseMediaType, context);
    }
    if (response instanceof NettyMutableHttpResponse) {
        out.add(((NettyMutableHttpResponse) response).toHttpResponse());
    } else {
        io.netty.handler.codec.http.HttpHeaders nettyHeaders = new DefaultHttpHeaders();
        for (Map.Entry<String, List<String>> header : response.getHeaders()) {
            nettyHeaders.add(header.getKey(), header.getValue());
        }
        Object b = response.getBody().orElse(null);
        ByteBuf body = b instanceof ByteBuf ? (ByteBuf) b : Unpooled.buffer(0);
        FullHttpResponse nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(response.status().getCode(), response.status().getReason()), body, nettyHeaders, EmptyHttpHeaders.INSTANCE);
        out.add(nettyResponse);
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) TextPlainCodec(io.micronaut.runtime.http.codec.TextPlainCodec) NettyMutableHttpResponse(io.micronaut.http.netty.NettyMutableHttpResponse) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) MediaType(io.micronaut.http.MediaType) List(java.util.List) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Map(java.util.Map) MediaTypeCodec(io.micronaut.http.codec.MediaTypeCodec)

Example 4 with NettyMutableHttpResponse

use of io.micronaut.http.netty.NettyMutableHttpResponse in project micronaut-core by micronaut-projects.

the class NettyStreamedCustomizableResponseType method write.

@Override
default void write(HttpRequest<?> request, MutableHttpResponse<?> response, ChannelHandlerContext context) {
    if (response instanceof NettyMutableHttpResponse) {
        NettyMutableHttpResponse nettyResponse = ((NettyMutableHttpResponse) response);
        // Write the request data
        final DefaultHttpResponse finalResponse = new DefaultHttpResponse(nettyResponse.getNettyHttpVersion(), nettyResponse.getNettyHttpStatus(), nettyResponse.getNettyHeaders());
        final io.micronaut.http.HttpVersion httpVersion = request.getHttpVersion();
        final boolean isHttp2 = httpVersion == io.micronaut.http.HttpVersion.HTTP_2_0;
        if (request instanceof NettyHttpRequest) {
            ((NettyHttpRequest<?>) request).prepareHttp2ResponseIfNecessary(finalResponse);
        }
        InputStream inputStream = getInputStream();
        // can be null if the stream was closed
        context.write(finalResponse, context.voidPromise());
        if (inputStream != null) {
            ChannelFutureListener closeListener = (future) -> {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    LOG.warn("An error occurred closing an input stream", e);
                }
            };
            final HttpChunkedInput chunkedInput = new HttpChunkedInput(new ChunkedStream(inputStream));
            context.writeAndFlush(chunkedInput).addListener(closeListener);
        } else {
            context.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
        }
    } else {
        throw new IllegalArgumentException("Unsupported response type. Not a Netty response: " + response);
    }
}
Also used : NettyMutableHttpResponse(io.micronaut.http.netty.NettyMutableHttpResponse) Logger(org.slf4j.Logger) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) LoggerFactory(org.slf4j.LoggerFactory) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) IOException(java.io.IOException) NettyCustomizableResponseType(io.micronaut.http.server.netty.types.NettyCustomizableResponseType) ChunkedStream(io.netty.handler.stream.ChunkedStream) Internal(io.micronaut.core.annotation.Internal) HttpChunkedInput(io.netty.handler.codec.http.HttpChunkedInput) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) ChannelFutureListener(io.netty.channel.ChannelFutureListener) HttpRequest(io.micronaut.http.HttpRequest) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) NettyHttpRequest(io.micronaut.http.server.netty.NettyHttpRequest) InputStream(java.io.InputStream) InputStream(java.io.InputStream) ChunkedStream(io.netty.handler.stream.ChunkedStream) IOException(java.io.IOException) ChannelFutureListener(io.netty.channel.ChannelFutureListener) NettyMutableHttpResponse(io.micronaut.http.netty.NettyMutableHttpResponse) HttpChunkedInput(io.netty.handler.codec.http.HttpChunkedInput) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) NettyHttpRequest(io.micronaut.http.server.netty.NettyHttpRequest)

Example 5 with NettyMutableHttpResponse

use of io.micronaut.http.netty.NettyMutableHttpResponse in project micronaut-core by micronaut-projects.

the class FileTypeHandler method notModified.

private FullHttpResponse notModified(MutableHttpResponse<?> originalResponse) {
    MutableHttpResponse response = HttpResponse.notModified();
    copyNonEntityHeaders(originalResponse, response);
    setDateHeader(response);
    return ((NettyMutableHttpResponse) response).toFullHttpResponse();
}
Also used : NettyMutableHttpResponse(io.micronaut.http.netty.NettyMutableHttpResponse) NettyMutableHttpResponse(io.micronaut.http.netty.NettyMutableHttpResponse) MutableHttpResponse(io.micronaut.http.MutableHttpResponse)

Aggregations

NettyMutableHttpResponse (io.micronaut.http.netty.NettyMutableHttpResponse)6 Internal (io.micronaut.core.annotation.Internal)2 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)2 NettyHttpRequest (io.micronaut.http.server.netty.NettyHttpRequest)2 ByteBuf (io.netty.buffer.ByteBuf)2 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)2 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)2 HttpChunkedInput (io.netty.handler.codec.http.HttpChunkedInput)2 IOException (java.io.IOException)2 List (java.util.List)2 NonNull (io.micronaut.core.annotation.NonNull)1 HttpRequest (io.micronaut.http.HttpRequest)1 HttpStatus (io.micronaut.http.HttpStatus)1 MediaType (io.micronaut.http.MediaType)1 MediaTypeCodec (io.micronaut.http.codec.MediaTypeCodec)1 NettyCustomizableResponseType (io.micronaut.http.server.netty.types.NettyCustomizableResponseType)1 CustomizableResponseTypeException (io.micronaut.http.server.types.CustomizableResponseTypeException)1 TextPlainCodec (io.micronaut.runtime.http.codec.TextPlainCodec)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1