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());
}
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);
}
}
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);
}
Aggregations