-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for conditional routing using expressions (#9094)
Adds a new `@RequestCondition` annotation which can make the route execution logic conditional.
- Loading branch information
1 parent
2004749
commit 0c8c44f
Showing
22 changed files
with
261 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
http-server-tck/src/main/java/io/micronaut/http/server/tck/tests/ExpressionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2017-2023 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.server.tck.tests; | ||
|
||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.http.HttpHeaders; | ||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.http.HttpStatus; | ||
import io.micronaut.http.MediaType; | ||
import io.micronaut.http.annotation.Controller; | ||
import io.micronaut.http.annotation.Get; | ||
import io.micronaut.http.annotation.RouteCondition; | ||
import io.micronaut.http.tck.AssertionUtils; | ||
import io.micronaut.http.tck.HttpResponseAssertion; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static io.micronaut.http.tck.TestScenario.asserts; | ||
|
||
@SuppressWarnings({ | ||
"java:S5960", // We're allowed assertions, as these are used in tests only | ||
"checkstyle:MissingJavadocType", | ||
"checkstyle:DesignForExtension" | ||
}) | ||
public class ExpressionTest { | ||
public static final String SPEC_NAME = "ExpressionTest"; | ||
|
||
@Test | ||
void testConditionalGetRequest() throws IOException { | ||
asserts(SPEC_NAME, | ||
HttpRequest.GET("/expr/test"), | ||
(server, request) -> AssertionUtils.assertThrows(server, request, | ||
HttpResponseAssertion.builder() | ||
.status(HttpStatus.NOT_FOUND) | ||
.build())); | ||
|
||
asserts(SPEC_NAME, | ||
HttpRequest.GET("/expr/test") | ||
.header(HttpHeaders.AUTHORIZATION, "foo"), | ||
(server, request) -> AssertionUtils.assertDoesNotThrow(server, request, | ||
HttpResponseAssertion.builder() | ||
.status(HttpStatus.OK) | ||
.body("ok") | ||
.build())); | ||
} | ||
|
||
@Controller("/expr") | ||
@Requires(property = "spec.name", value = SPEC_NAME) | ||
static class ExpressionController { | ||
@Get(value = "/test") | ||
@RouteCondition("#{request.headers.getFirst('Authorization')?.contains('foo')}") | ||
String testGet() { | ||
return "ok"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
http/src/main/java/io/micronaut/http/annotation/RouteCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2017-2023 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.annotation; | ||
|
||
import io.micronaut.context.annotation.AnnotationExpressionContext; | ||
import io.micronaut.core.annotation.Experimental; | ||
import io.micronaut.http.expression.RequestConditionContext; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Allows defining a condition for this route to match using an expression. | ||
* | ||
* <p>Within the scope of the expression a {@code request} variable is available that references the {@link io.micronaut.http.HttpRequest}.</p> | ||
* | ||
* <p>When added to a method the condition will be evaluated during route matching and if the condition | ||
* does not evaluate to {@code true} the route will not be matched resulting in a {@link io.micronaut.http.HttpStatus#NOT_FOUND} response. </p> | ||
* | ||
* <p>Note that this annotation only applies to the server and is ignored when placed on declarative HTTP client routes.</p> | ||
* | ||
* @see io.micronaut.http.expression.RequestConditionContext | ||
* @since 4.0.0 | ||
*/ | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@AnnotationExpressionContext(RequestConditionContext.class) | ||
@Experimental | ||
public @interface RouteCondition { | ||
/** | ||
* An expression that evalutes to {@code true} or {@code false}. | ||
* @return The expression | ||
* @since 4.0.0 | ||
*/ | ||
String value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.