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