use of io.micronaut.http.filter.HttpFilter in project micronaut-core by micronaut-projects.
the class DefaultFilterRoute method match.
@Override
public Optional<HttpFilter> match(HttpMethod method, URI uri) {
if (httpMethods != null && !httpMethods.contains(method)) {
return Optional.empty();
}
String uriStr = uri.getPath();
PathMatcher matcher = getPatternStyle().getPathMatcher();
for (String pattern : patterns) {
if (matcher.matches(pattern, uriStr)) {
HttpFilter filter = getFilter();
if (filter instanceof Toggleable && !((Toggleable) filter).isEnabled()) {
return Optional.empty();
}
return Optional.of(filter);
}
}
return Optional.empty();
}
use of io.micronaut.http.filter.HttpFilter in project micronaut-core by micronaut-projects.
the class DefaultFilterRoute method getFilter.
@Override
public HttpFilter getFilter() {
HttpFilter filter = this.filter;
if (filter == null) {
synchronized (this) {
// double check
filter = this.filter;
if (filter == null) {
filter = filterSupplier.get();
this.filter = filter;
}
}
}
return filter;
}
use of io.micronaut.http.filter.HttpFilter in project micronaut-core by micronaut-projects.
the class RouteExecutor method filterPublisher.
/**
* Applies server filters to a request/response.
*
* @param requestReference The request reference
* @param upstreamResponsePublisher The original response publisher
* @return A new response publisher that executes server filters
*/
public Publisher<MutableHttpResponse<?>> filterPublisher(AtomicReference<HttpRequest<?>> requestReference, Publisher<MutableHttpResponse<?>> upstreamResponsePublisher) {
List<HttpFilter> httpFilters = router.findFilters(requestReference.get());
if (httpFilters.isEmpty()) {
return upstreamResponsePublisher;
}
List<HttpFilter> filters = new ArrayList<>(httpFilters);
AtomicInteger integer = new AtomicInteger();
int len = filters.size();
final Function<MutableHttpResponse<?>, Publisher<MutableHttpResponse<?>>> handleStatusException = (response) -> handleStatusException(requestReference.get(), response);
final Function<Throwable, Publisher<MutableHttpResponse<?>>> onError = (t) -> onError(t, requestReference.get());
ServerFilterChain filterChain = new ServerFilterChain() {
@SuppressWarnings("unchecked")
@Override
public Publisher<MutableHttpResponse<?>> proceed(io.micronaut.http.HttpRequest<?> 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.");
}
if (pos == len) {
return upstreamResponsePublisher;
}
HttpFilter httpFilter = filters.get(pos);
HttpRequest<?> requestForFilter = requestReference.getAndSet(request);
try {
return Flux.from((Publisher<MutableHttpResponse<?>>) httpFilter.doFilter(requestForFilter, this)).flatMap(handleStatusException).onErrorResume(onError);
} catch (Throwable t) {
return onError.apply(t);
}
}
};
HttpFilter httpFilter = filters.get(0);
HttpRequest<?> request = requestReference.get();
try {
return Flux.from((Publisher<MutableHttpResponse<?>>) httpFilter.doFilter(request, filterChain)).flatMap(handleStatusException).onErrorResume(onError);
} catch (Throwable t) {
return onError.apply(t);
}
}
use of io.micronaut.http.filter.HttpFilter 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.HttpFilter 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