Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Filter annotations to be stacked in a third one #4771

Merged
merged 3 commits into from
Jan 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add tests to illustrate how to use several filters with annotations
  • Loading branch information
ggerbaud committed Jan 6, 2021
commit 40f9fd1bb76027a86235155aea078c93874e2be3
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 @@ -58,12 +58,19 @@ class ClientFilterStereotypeSpec extends Specification {
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 Post URL"
indirectlyMarkedClient.echo() == "echo Intercepted URL"
indirectlyMarkedClient.echoPost() == "echo Intercepted Twice Post URL"
indirectlyMarkedClient.echo() == "echo Intercepted Twice URL"
}

void "low-level client filter matching"() {
Expand Down Expand Up @@ -100,6 +107,17 @@ 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 {
Expand Down Expand Up @@ -148,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 @@ -172,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
Expand Up @@ -21,5 +21,6 @@
@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,17 @@ 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()
Expand All @@ -112,6 +123,7 @@ class HttpFilterSpec extends Specification {
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")
Expand Down Expand Up @@ -143,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 @@ -164,6 +189,13 @@ class HttpFilterSpec extends Specification {
HttpResponse.ok()
}

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

@Get("/indirectlymatched")
@IndirectMarkerStereotypeAnnotation
HttpResponse indirectlyMatched() {
Expand All @@ -180,6 +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 {
}