package io.micronaut.http.netty;
import edu.umd.cs.findbugs.annotations.NonNull;
import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.http.HttpResponseWrapper;
import io.micronaut.http.netty.stream.DefaultStreamedHttpResponse;
import io.micronaut.http.netty.stream.StreamedHttpResponse;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.*;
import java.util.Objects;
public interface NettyHttpResponseBuilder {
@NonNull FullHttpResponse toFullHttpResponse();
default @NonNull StreamedHttpResponse toStreamHttpResponse() {
FullHttpResponse fullHttpResponse = toFullHttpResponse();
DefaultStreamedHttpResponse streamedHttpResponse = new DefaultStreamedHttpResponse(
fullHttpResponse.protocolVersion(),
fullHttpResponse.status(),
true,
Publishers.just(new DefaultLastHttpContent(fullHttpResponse.content()))
);
streamedHttpResponse.headers().setAll(fullHttpResponse.headers());
return streamedHttpResponse;
}
default @NonNull HttpResponse toHttpResponse() {
if (isStream()) {
return toStreamHttpResponse();
} else {
return toFullHttpResponse();
}
}
boolean isStream();
static @NonNull HttpResponse toHttpResponse(@NonNull io.micronaut.http.HttpResponse<?> response) {
Objects.requireNonNull(response, "The response cannot be null");
while (response instanceof HttpResponseWrapper) {
response = ((HttpResponseWrapper<?>) response).getDelegate();
}
if (response instanceof NettyHttpResponseBuilder) {
return ((NettyHttpResponseBuilder) response).toHttpResponse();
}
HttpResponse fullHttpResponse;
ByteBuf byteBuf = response.getBody(ByteBuf.class).orElse(null);
if (byteBuf != null) {
fullHttpResponse = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.valueOf(response.code(), response.reason()),
byteBuf
);
} else {
fullHttpResponse = new DefaultHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.valueOf(response.code(), response.reason())
);
}
response.getHeaders()
.forEach((s, strings) -> fullHttpResponse.headers().add(s, strings));
return fullHttpResponse;
}
static @NonNull StreamedHttpResponse toStreamResponse(@NonNull io.micronaut.http.HttpResponse<?> response) {
Objects.requireNonNull(response, "The response cannot be null");
while (response instanceof HttpResponseWrapper) {
response = ((HttpResponseWrapper<?>) response).getDelegate();
}
if (response instanceof NettyHttpResponseBuilder) {
NettyHttpResponseBuilder builder = (NettyHttpResponseBuilder) response;
if (builder.isStream()) {
return builder.toStreamHttpResponse();
} else {
FullHttpResponse fullHttpResponse = builder.toFullHttpResponse();
return new DefaultStreamedHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.valueOf(response.code(), response.reason()),
Publishers.just(new DefaultLastHttpContent(fullHttpResponse.content()))
);
}
}
StreamedHttpResponse fullHttpResponse;
ByteBuf byteBuf = response.getBody(ByteBuf.class).orElse(null);
if (byteBuf != null) {
fullHttpResponse = new DefaultStreamedHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.valueOf(response.code(), response.reason()),
Publishers.just(new DefaultLastHttpContent(byteBuf))
);
} else {
fullHttpResponse = new DefaultStreamedHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.valueOf(response.code(), response.reason()),
Publishers.empty()
);
}
response.getHeaders()
.forEach((s, strings) -> fullHttpResponse.headers().add(s, strings));
return fullHttpResponse;
}
}