Skip to content

Commit

Permalink
Allow Filter annotations to be stacked in a third one (micronaut-proj…
Browse files Browse the repository at this point in the history
…ects#4771)

* Allow client filter annotations to be matched through another one

Signed-off-by: Guillaume GERBAUD <ggerbaud@mabreizh.fr>

* Allow server filter annotations to be matched through another one

* Add tests to illustrate how to use several filters with annotations
  • Loading branch information
ggerbaud authored Jan 7, 2021
1 parent 2df2a53 commit deb4596
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public List<FilterEntry<HttpClientFilter>> resolveFilterEntries(ClientFilterReso
boolean matches = !annotationMetadata.hasStereotype(FilterMatcher.class);
String filterAnnotation = annotationMetadata.getAnnotationNameByStereotype(FilterMatcher.class).orElse(null);
if (filterAnnotation != null && !matches) {
matches = context.getAnnotationMetadata().hasAnnotation(filterAnnotation);
matches = context.getAnnotationMetadata().getAnnotationNamesByStereotype(FilterMatcher.class).contains(filterAnnotation);
}

if (matches) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.http.client.filter;

import io.micronaut.context.annotation.AliasFor;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.annotation.FilterMatcher;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.PARAMETER})
@FilterMatcher
public @interface AnotherMarkerStereotypeAnnotation {

@AliasFor(member = "methods", annotation = FilterMatcher.class)
HttpMethod[] methods() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ import io.micronaut.http.MutableHttpRequest
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Filter
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Header
import io.micronaut.http.annotation.Post
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.http.filter.ClientFilterChain
import io.micronaut.http.filter.HttpClientFilter
import io.micronaut.runtime.server.EmbeddedServer
import io.reactivex.Flowable
import io.reactivex.Single
import org.reactivestreams.Publisher
import spock.lang.AutoCleanup
import spock.lang.Shared
Expand All @@ -59,6 +57,20 @@ class ClientFilterStereotypeSpec extends Specification {
then:
markedClient.echoPost() == "echo Intercepted Post URL"
markedClient.echo() == "echo Intercepted URL"

when:
MarkedTwiceClient markedTwiceClient = ctx.getBean(MarkedTwiceClient)

then:
markedTwiceClient.echoPost() == "echo Intercepted Twice Post URL"
markedTwiceClient.echo() == "echo Intercepted Twice URL"

when:
IndirectlyMarkedClient indirectlyMarkedClient = ctx.getBean(IndirectlyMarkedClient)

then:
indirectlyMarkedClient.echoPost() == "echo Intercepted Twice Post URL"
indirectlyMarkedClient.echo() == "echo Intercepted Twice URL"
}

void "low-level client filter matching"() {
Expand Down Expand Up @@ -95,6 +107,27 @@ class ClientFilterStereotypeSpec extends Specification {
String echoPost()
}

@Client("/filters/marked")
@AnotherMarkerStereotypeAnnotation
@MarkerStereotypeAnnotation
static interface MarkedTwiceClient {
@Get("/")
String echo()

@Post("/")
String echoPost()
}

@Client("/filters/marked")
@IndirectMarkerStereotypeAnnotation
static interface IndirectlyMarkedClient {
@Get("/")
String echo()

@Post("/")
String echoPost()
}

@Client("/filters/marked")
static interface UnmarkedClient {
@Get("/")
Expand Down Expand Up @@ -133,13 +166,31 @@ class ClientFilterStereotypeSpec extends Specification {
}
}

@AnotherMarkerStereotypeAnnotation
@Singleton
static class AnotherMarkerFilter implements HttpClientFilter {

@Override
int getOrder() {
1
}

@Override
Publisher<? extends HttpResponse<?>> doFilter(MutableHttpRequest<?> request, ClientFilterChain chain) {
return Flowable.fromPublisher(chain.proceed(request))
.map({ HttpResponse response ->
HttpResponse.ok(response.body().toString() + " Twice")
})
}
}

@MarkerStereotypeAnnotation(methods = HttpMethod.POST)
@Singleton
static class MarkerPostFilter implements HttpClientFilter {

@Override
int getOrder() {
1
2
}

@Override
Expand All @@ -157,7 +208,7 @@ class ClientFilterStereotypeSpec extends Specification {

@Override
int getOrder() {
2
3
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.http.client.filter;

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.PARAMETER})
@MarkerStereotypeAnnotation
@AnotherMarkerStereotypeAnnotation
public @interface IndirectMarkerStereotypeAnnotation {
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import io.micronaut.http.client.annotation.Client
import io.micronaut.http.client.exceptions.HttpClientResponseException
import io.micronaut.http.filter.HttpServerFilter
import io.micronaut.http.filter.ServerFilterChain
import io.micronaut.test.annotation.MicronautTest
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import io.reactivex.Flowable
import io.reactivex.Single
import org.reactivestreams.Publisher
Expand Down Expand Up @@ -104,6 +104,28 @@ class HttpFilterSpec extends Specification {
response.headers.contains("X-Matched-Filter")
}

void "test two filters on matched with filter matcher URI"() {
when:
HttpResponse response = rxClient.exchange("/matchedtwice").blockingFirst()

then:
response.status == HttpStatus.OK
response.headers.get("X-Root-Filter") == "processed"
response.headers.contains("X-Matched-Filter")
response.headers.contains("X-Another-Matched-Filter")
}

void "test a filter on indirect matched with filter matcher URI"() {
when:
HttpResponse response = rxClient.exchange("/indirectlymatched").blockingFirst()

then:
response.status == HttpStatus.OK
response.headers.get("X-Root-Filter") == "processed"
response.headers.contains("X-Matched-Filter")
response.headers.contains("X-Another-Matched-Filter")
}

@Requires(property = 'spec.name', value = "HttpFilterSpec")
@Filter("/**")
static class RootFilter implements HttpServerFilter {
Expand Down Expand Up @@ -133,6 +155,19 @@ class HttpFilterSpec extends Specification {
}
}

@Filter("/**")
@AnotherMarkerStereotypeAnnotation
@Requires(property = 'spec.name', value = "HttpFilterSpec")
static class AnotherMatchedFilter implements HttpServerFilter {

@Override
Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
return Flowable.fromPublisher(chain.proceed(request)).doOnNext({ response ->
response.header("X-Another-Matched-Filter", "processed")
})
}
}


@Controller
@Requires(property = 'spec.name', value = "HttpFilterSpec")
Expand All @@ -154,6 +189,19 @@ class HttpFilterSpec extends Specification {
HttpResponse.ok()
}

@Get("/matchedtwice")
@MarkerStereotypeAnnotation
@AnotherMarkerStereotypeAnnotation
HttpResponse matchedTwice() {
HttpResponse.ok()
}

@Get("/indirectlymatched")
@IndirectMarkerStereotypeAnnotation
HttpResponse indirectlyMatched() {
HttpResponse.ok()
}

}


Expand All @@ -164,3 +212,13 @@ class HttpFilterSpec extends Specification {
@AliasFor(member = "methods", annotation = FilterMatcher.class)
HttpMethod[] methods() default [];
}
@FilterMatcher
@interface AnotherMarkerStereotypeAnnotation {

@AliasFor(member = "methods", annotation = FilterMatcher.class)
HttpMethod[] methods() default [];
}
@MarkerStereotypeAnnotation
@AnotherMarkerStereotypeAnnotation
@interface IndirectMarkerStereotypeAnnotation {
}
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ private Stream<FilterRoute> filterRouteStream(RouteMatch<?> context) {
if (!matches) {
String filterAnnotation = annotationMetadata.getAnnotationNameByStereotype(FilterMatcher.NAME).orElse(null);
if (filterAnnotation != null) {
matches = context.getAnnotationMetadata().hasAnnotation(filterAnnotation);
matches = context.getAnnotationMetadata().getAnnotationNamesByStereotype(FilterMatcher.class).contains(filterAnnotation);
}
}
return matches;
Expand Down

0 comments on commit deb4596

Please sign in to comment.