Search in sources :

Example 1 with FilterPatternStyle

use of io.micronaut.http.filter.FilterPatternStyle 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 2 with FilterPatternStyle

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

the class AnnotatedFilterRouteBuilder method process.

@Override
public void process(BeanDefinition<?> beanDefinition, BeanContext beanContext) {
    if (HttpClientFilter.class.isAssignableFrom(beanDefinition.getBeanType())) {
        // ignore http client filters
        return;
    }
    String[] patterns = getPatterns(beanDefinition);
    if (ArrayUtils.isNotEmpty(patterns)) {
        HttpMethod[] methods = beanDefinition.enumValues(Filter.class, "methods", HttpMethod.class);
        FilterPatternStyle patternStyle = beanDefinition.enumValue(Filter.class, "patternStyle", FilterPatternStyle.class).orElse(FilterPatternStyle.ANT);
        String first = patterns[0];
        @SuppressWarnings("unchecked") FilterRoute filterRoute = addFilter(first, beanContext, (BeanDefinition<? extends HttpFilter>) beanDefinition);
        if (patterns.length > 1) {
            for (int i = 1; i < patterns.length; i++) {
                String pattern = patterns[i];
                filterRoute.pattern(pattern);
            }
        }
        if (ArrayUtils.isNotEmpty(methods)) {
            filterRoute.methods(methods);
        }
        filterRoute.patternStyle(patternStyle);
    }
}
Also used : Filter(io.micronaut.http.annotation.Filter) HttpFilter(io.micronaut.http.filter.HttpFilter) HttpClientFilter(io.micronaut.http.filter.HttpClientFilter) FilterPatternStyle(io.micronaut.http.filter.FilterPatternStyle) HttpMethod(io.micronaut.http.HttpMethod)

Example 3 with FilterPatternStyle

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

the class DefaultRouter method resolveFilters.

@Override
public List<HttpFilter> resolveFilters(HttpRequest<?> request, List<FilterEntry<HttpFilter>> filterEntries) {
    List<HttpFilter> httpFilters = new ArrayList<>(filterEntries.size());
    for (FilterEntry<HttpFilter> entry : filterEntries) {
        if (entry.hasMethods() && !entry.getFilterMethods().contains(request.getMethod())) {
            continue;
        }
        if (entry.hasPatterns()) {
            String path = request.getPath();
            String[] patterns = entry.getPatterns();
            FilterPatternStyle patternStyle = entry.getAnnotationMetadata().enumValue("patternStyle", FilterPatternStyle.class).orElse(FilterPatternStyle.ANT);
            boolean matches = true;
            for (String pattern : patterns) {
                if (!matches) {
                    break;
                }
                matches = Filter.MATCH_ALL_PATTERN.equals(pattern) || patternStyle.getPathMatcher().matches(pattern, path);
            }
            if (!matches) {
                continue;
            }
        }
        httpFilters.add(entry.getFilter());
    }
    httpFilters.sort(OrderUtil.COMPARATOR);
    return Collections.unmodifiableList(httpFilters);
}
Also used : FilterPatternStyle(io.micronaut.http.filter.FilterPatternStyle) HttpFilter(io.micronaut.http.filter.HttpFilter)

Aggregations

FilterPatternStyle (io.micronaut.http.filter.FilterPatternStyle)3 HttpMethod (io.micronaut.http.HttpMethod)2 Filter (io.micronaut.http.annotation.Filter)2 HttpClientFilter (io.micronaut.http.filter.HttpClientFilter)2 HttpFilter (io.micronaut.http.filter.HttpFilter)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 Toggleable (io.micronaut.core.util.Toggleable)1 HttpRequest (io.micronaut.http.HttpRequest)1 FilterMatcher (io.micronaut.http.annotation.FilterMatcher)1 HttpClientFilterResolver (io.micronaut.http.filter.HttpClientFilterResolver)1 Singleton (jakarta.inject.Singleton)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1