use of io.micronaut.http.HttpResponseWrapper in project micronaut-core by micronaut-projects.
the class DefaultHttpClient method buildExchangeStreamPublisher.
/**
* @param request The request
* @param errorType The error type
* @param <I> The input type
* @return A {@link Function}
*/
protected <I> Function<URI, Publisher<HttpResponse<ByteBuffer<?>>>> buildExchangeStreamPublisher(@NonNull io.micronaut.http.HttpRequest<I> request, @NonNull Argument<?> errorType) {
final io.micronaut.http.HttpRequest<Object> parentRequest = ServerRequestContext.currentRequest().orElse(null);
return requestURI -> {
Flux<io.micronaut.http.HttpResponse<Object>> streamResponsePublisher = Flux.from(buildStreamExchange(parentRequest, request, requestURI, errorType));
return streamResponsePublisher.switchMap(response -> {
StreamedHttpResponse streamedHttpResponse = NettyHttpResponseBuilder.toStreamResponse(response);
Flux<HttpContent> httpContentReactiveSequence = Flux.from(streamedHttpResponse);
return httpContentReactiveSequence.filter(message -> !(message.content() instanceof EmptyByteBuf)).map(message -> {
ByteBuf byteBuf = message.content();
if (log.isTraceEnabled()) {
log.trace("HTTP Client Streaming Response Received Chunk (length: {}) for Request: {} {}", byteBuf.readableBytes(), request.getMethodName(), request.getUri());
traceBody("Response", byteBuf);
}
ByteBuffer<?> byteBuffer = byteBufferFactory.wrap(byteBuf);
NettyStreamedHttpResponse<ByteBuffer<?>> thisResponse = new NettyStreamedHttpResponse<>(streamedHttpResponse, response.status());
thisResponse.setBody(byteBuffer);
return (HttpResponse<ByteBuffer<?>>) new HttpResponseWrapper<>(thisResponse);
});
}).doOnTerminate(() -> {
final Object o = request.getAttribute(NettyClientHttpRequest.CHANNEL).orElse(null);
if (o instanceof Channel) {
final Channel c = (Channel) o;
if (c.isOpen()) {
c.close();
}
}
});
};
}
use of io.micronaut.http.HttpResponseWrapper in project micronaut-core by micronaut-projects.
the class NettyHttpResponseBuilder method toStreamResponse.
/**
* Convert the given response to a full http response.
* @param response The response
* @return The full response.
*/
@NonNull
static 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())));
}
}
// manual conversion
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;
}
use of io.micronaut.http.HttpResponseWrapper in project micronaut-core by micronaut-projects.
the class NettyHttpResponseBuilder method toHttpResponse.
/**
* Convert the given response to a full http response.
* @param response The response
* @return The full response.
*/
@NonNull
static 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();
}
// manual conversion
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;
}
Aggregations