Search in sources :

Example 1 with HttpClientFilter

use of io.micronaut.http.filter.HttpClientFilter 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)

Example 2 with HttpClientFilter

use of io.micronaut.http.filter.HttpClientFilter in project micronaut-core by micronaut-projects.

the class DefaultHttpClientFilterResolver method resolveFilterEntries.

@Override
public List<FilterEntry<HttpClientFilter>> resolveFilterEntries(ClientFilterResolutionContext context) {
    return clientFilters.stream().map(httpClientFilter -> {
        AnnotationMetadata annotationMetadata = annotationMetadataResolver.resolveMetadata(httpClientFilter);
        HttpMethod[] methods = annotationMetadata.enumValues(Filter.class, "methods", HttpMethod.class);
        FilterPatternStyle patternStyle = annotationMetadata.enumValue(Filter.class, "patternStyle", FilterPatternStyle.class).orElse(FilterPatternStyle.ANT);
        final Set<HttpMethod> httpMethods = new HashSet<>(Arrays.asList(methods));
        if (annotationMetadata.hasStereotype(FilterMatcher.class)) {
            httpMethods.addAll(Arrays.asList(annotationMetadata.enumValues(FilterMatcher.class, "methods", HttpMethod.class)));
        }
        return FilterEntry.of(httpClientFilter, annotationMetadata, httpMethods, patternStyle, annotationMetadata.stringValues(Filter.class));
    }).filter(entry -> {
        AnnotationMetadata annotationMetadata = entry.getAnnotationMetadata();
        boolean matches = !annotationMetadata.hasStereotype(FilterMatcher.class);
        String filterAnnotation = annotationMetadata.getAnnotationNameByStereotype(FilterMatcher.class).orElse(null);
        if (filterAnnotation != null && !matches) {
            matches = context.getAnnotationMetadata().hasStereotype(filterAnnotation);
        }
        if (matches) {
            String[] clients = annotationMetadata.stringValues(Filter.class, "serviceId");
            boolean hasClients = ArrayUtils.isNotEmpty(clients);
            if (hasClients) {
                matches = containsIdentifier(context.getClientIds(), clients);
            }
        }
        return matches;
    }).collect(Collectors.toList());
}
Also used : HttpMethod(io.micronaut.http.HttpMethod) Filter(io.micronaut.http.annotation.Filter) Arrays(java.util.Arrays) Collection(java.util.Collection) ArrayUtils(io.micronaut.core.util.ArrayUtils) AnnotationMetadataResolver(io.micronaut.core.annotation.AnnotationMetadataResolver) Singleton(jakarta.inject.Singleton) Set(java.util.Set) HttpClientFilterResolver(io.micronaut.http.filter.HttpClientFilterResolver) Internal(io.micronaut.core.annotation.Internal) FilterPatternStyle(io.micronaut.http.filter.FilterPatternStyle) Toggleable(io.micronaut.core.util.Toggleable) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StringUtils(io.micronaut.core.util.StringUtils) HttpClientFilter(io.micronaut.http.filter.HttpClientFilter) List(java.util.List) FilterMatcher(io.micronaut.http.annotation.FilterMatcher) BootstrapContextCompatible(io.micronaut.context.annotation.BootstrapContextCompatible) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) HttpRequest(io.micronaut.http.HttpRequest) Filter(io.micronaut.http.annotation.Filter) HttpClientFilter(io.micronaut.http.filter.HttpClientFilter) FilterPatternStyle(io.micronaut.http.filter.FilterPatternStyle) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) HttpMethod(io.micronaut.http.HttpMethod) HashSet(java.util.HashSet)

Example 3 with HttpClientFilter

use of io.micronaut.http.filter.HttpClientFilter in project micronaut-core by micronaut-projects.

the class DefaultHttpClientFilterResolver method resolveFilters.

@Override
public List<HttpClientFilter> resolveFilters(HttpRequest<?> request, List<FilterEntry<HttpClientFilter>> filterEntries) {
    String requestPath = StringUtils.prependUri("/", request.getUri().getPath());
    io.micronaut.http.HttpMethod method = request.getMethod();
    List<HttpClientFilter> filterList = new ArrayList<>(filterEntries.size());
    for (FilterEntry<HttpClientFilter> filterEntry : filterEntries) {
        final HttpClientFilter filter = filterEntry.getFilter();
        if (filter instanceof Toggleable && !((Toggleable) filter).isEnabled()) {
            continue;
        }
        boolean matches = true;
        if (filterEntry.hasMethods()) {
            matches = anyMethodMatches(method, filterEntry.getFilterMethods());
        }
        if (filterEntry.hasPatterns()) {
            matches = matches && anyPatternMatches(requestPath, filterEntry.getPatterns(), filterEntry.getPatternStyle());
        }
        if (matches) {
            filterList.add(filter);
        }
    }
    return filterList;
}
Also used : HttpMethod(io.micronaut.http.HttpMethod) HttpClientFilter(io.micronaut.http.filter.HttpClientFilter) Toggleable(io.micronaut.core.util.Toggleable) ArrayList(java.util.ArrayList)

Example 4 with HttpClientFilter

use of io.micronaut.http.filter.HttpClientFilter in project micronaut-core by micronaut-projects.

the class DefaultHttpClient method buildChain.

private ClientFilterChain buildChain(AtomicReference<io.micronaut.http.HttpRequest> requestWrapper, List<HttpClientFilter> filters) {
    AtomicInteger integer = new AtomicInteger();
    int len = filters.size();
    return new ClientFilterChain() {

        @Override
        public Publisher<? extends io.micronaut.http.HttpResponse<?>> proceed(MutableHttpRequest<?> request) {
            int pos = integer.incrementAndGet();
            if (pos > len) {
                throw new IllegalStateException("The FilterChain.proceed(..) method should be invoked exactly once per filter execution. The method has instead been invoked multiple times by an erroneous filter definition.");
            }
            HttpClientFilter httpFilter = filters.get(pos);
            try {
                return httpFilter.doFilter(requestWrapper.getAndSet(request), this);
            } catch (Throwable t) {
                return Flux.error(t);
            }
        }
    };
}
Also used : HttpClientFilter(io.micronaut.http.filter.HttpClientFilter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientFilterChain(io.micronaut.http.filter.ClientFilterChain) io.netty.handler.codec.http(io.netty.handler.codec.http) MutableHttpRequest(io.micronaut.http.MutableHttpRequest)

Aggregations

HttpClientFilter (io.micronaut.http.filter.HttpClientFilter)4 Toggleable (io.micronaut.core.util.Toggleable)2 HttpMethod (io.micronaut.http.HttpMethod)2 MutableHttpRequest (io.micronaut.http.MutableHttpRequest)2 ClientFilterChain (io.micronaut.http.filter.ClientFilterChain)2 io.netty.handler.codec.http (io.netty.handler.codec.http)2 ArrayList (java.util.ArrayList)2 BootstrapContextCompatible (io.micronaut.context.annotation.BootstrapContextCompatible)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AnnotationMetadataResolver (io.micronaut.core.annotation.AnnotationMetadataResolver)1 Internal (io.micronaut.core.annotation.Internal)1 ArrayUtils (io.micronaut.core.util.ArrayUtils)1 StringUtils (io.micronaut.core.util.StringUtils)1 HttpRequest (io.micronaut.http.HttpRequest)1 HttpResponse (io.micronaut.http.HttpResponse)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 Filter (io.micronaut.http.annotation.Filter)1 FilterMatcher (io.micronaut.http.annotation.FilterMatcher)1 ClientServerContextFilter (io.micronaut.http.client.filters.ClientServerContextFilter)1 FilterPatternStyle (io.micronaut.http.filter.FilterPatternStyle)1