use of io.micronaut.http.client.filters.ClientServerContextFilter in project micronaut-core by micronaut-projects.
the class DefaultHttpClient method applyFilterToResponsePublisher.
/**
* @param parentRequest The parent request
* @param request The request
* @param requestURI The URI of the request
* @param requestWrapper The request wrapper
* @param responsePublisher The response publisher
* @param <I> The input type
* @param <O> The output type
* @return The {@link Publisher} for the response
*/
protected <I, O> Publisher<io.micronaut.http.HttpResponse<O>> applyFilterToResponsePublisher(io.micronaut.http.HttpRequest<?> parentRequest, io.micronaut.http.HttpRequest<I> request, URI requestURI, AtomicReference<io.micronaut.http.HttpRequest> requestWrapper, Publisher<io.micronaut.http.HttpResponse<O>> responsePublisher) {
if (request instanceof MutableHttpRequest) {
((MutableHttpRequest<I>) request).uri(requestURI);
List<HttpClientFilter> filters = filterResolver.resolveFilters(request, clientFilterEntries);
if (parentRequest != null) {
filters.add(new ClientServerContextFilter(parentRequest));
}
OrderUtil.reverseSort(filters);
Publisher<io.micronaut.http.HttpResponse<O>> finalResponsePublisher = responsePublisher;
filters.add((req, chain) -> finalResponsePublisher);
ClientFilterChain filterChain = buildChain(requestWrapper, filters);
if (parentRequest != null) {
responsePublisher = ServerRequestContext.with(parentRequest, (Supplier<Publisher<io.micronaut.http.HttpResponse<O>>>) () -> {
try {
return Flux.from((Publisher<io.micronaut.http.HttpResponse<O>>) filters.get(0).doFilter(request, filterChain)).contextWrite(ctx -> ctx.put(ServerRequestContext.KEY, parentRequest));
} catch (Throwable t) {
return Flux.error(t);
}
});
} else {
try {
responsePublisher = (Publisher<io.micronaut.http.HttpResponse<O>>) filters.get(0).doFilter(request, filterChain);
} catch (Throwable t) {
responsePublisher = Flux.error(t);
}
}
}
return responsePublisher;
}
Aggregations