Search in sources :

Example 1 with ClientServerContextFilter

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;
}
Also used : StreamedHttpResponse(io.micronaut.http.netty.stream.StreamedHttpResponse) DefaultStreamedHttpResponse(io.micronaut.http.netty.stream.DefaultStreamedHttpResponse) HttpResponse(io.micronaut.http.HttpResponse) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) ClientFilterChain(io.micronaut.http.filter.ClientFilterChain) MutableHttpRequest(io.micronaut.http.MutableHttpRequest) Publisher(org.reactivestreams.Publisher) HttpClientFilter(io.micronaut.http.filter.HttpClientFilter) io.netty.handler.codec.http(io.netty.handler.codec.http) Supplier(java.util.function.Supplier) ClientServerContextFilter(io.micronaut.http.client.filters.ClientServerContextFilter)

Aggregations

HttpResponse (io.micronaut.http.HttpResponse)1 MutableHttpRequest (io.micronaut.http.MutableHttpRequest)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 ClientServerContextFilter (io.micronaut.http.client.filters.ClientServerContextFilter)1 ClientFilterChain (io.micronaut.http.filter.ClientFilterChain)1 HttpClientFilter (io.micronaut.http.filter.HttpClientFilter)1 DefaultStreamedHttpResponse (io.micronaut.http.netty.stream.DefaultStreamedHttpResponse)1 StreamedHttpResponse (io.micronaut.http.netty.stream.StreamedHttpResponse)1 io.netty.handler.codec.http (io.netty.handler.codec.http)1 Supplier (java.util.function.Supplier)1 Publisher (org.reactivestreams.Publisher)1