-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: RequestScope bean implementing JAX-RS ResourceInfo (#348)
- Loading branch information
Showing
5 changed files
with
122 additions
and
0 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
61 changes: 61 additions & 0 deletions
61
jaxrs-server/src/main/java/io/micronaut/jaxrs/runtime/core/JaxRsResourceInfo.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,61 @@ | ||
/* | ||
* 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.jaxrs.runtime.core; | ||
|
||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.http.HttpAttributes; | ||
import io.micronaut.http.HttpRequest; | ||
import io.micronaut.runtime.http.scope.RequestAware; | ||
import io.micronaut.runtime.http.scope.RequestScope; | ||
import io.micronaut.web.router.MethodBasedRouteInfo; | ||
import io.micronaut.web.router.RouteInfo; | ||
import io.micronaut.web.router.RouteMatch; | ||
import jakarta.ws.rs.container.ResourceInfo; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* A {@link RequestScope} bean implementing the JAX-RS ResourceInfo to access the resource class and resource method matched by the current request. | ||
* Methods in this class MAY return <code>null</code> if a resource class and method have not been matched. | ||
* | ||
* @author Tim Yates | ||
* @since 4.1.0 | ||
*/ | ||
@RequestScope | ||
public class JaxRsResourceInfo implements RequestAware, ResourceInfo { | ||
|
||
private RouteInfo<?> routeInfo; | ||
|
||
@Override | ||
public void setRequest(HttpRequest<?> request) { | ||
routeInfo = request.getAttribute(HttpAttributes.ROUTE_MATCH, RouteMatch.class).map(RouteMatch::getRouteInfo).orElse(null); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Method getResourceMethod() { | ||
if (routeInfo instanceof MethodBasedRouteInfo<?, ?> methodBasedRouteInfo) { | ||
return methodBasedRouteInfo.getTargetMethod().getTargetMethod(); | ||
} | ||
return null; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Class<?> getResourceClass() { | ||
return routeInfo == null ? null : routeInfo.getDeclaringType(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
jaxrs-server/src/test/groovy/io/micronaut/jaxrs/runtime/core/ResourceInfoSpec.groovy
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,52 @@ | ||
package io.micronaut.jaxrs.runtime.core | ||
|
||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.http.client.annotation.Client | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import jakarta.ws.rs.GET | ||
import jakarta.ws.rs.POST | ||
import jakarta.ws.rs.Path | ||
import jakarta.ws.rs.container.ResourceInfo | ||
import spock.lang.Specification | ||
|
||
@MicronautTest | ||
@Property(name = "spec.name", value = "ResourceInfoSpec") | ||
class ResourceInfoSpec extends Specification { | ||
|
||
@Inject | ||
@Client("/") | ||
HttpClient client | ||
|
||
void "resource info is correctly populated"() { | ||
expect: | ||
client.toBlocking().retrieve("/api/info/test", String) == 'io.micronaut.jaxrs.runtime.core.ResourceInfoSpec$ResourceInfoResource:getTest' | ||
client.toBlocking().retrieve(HttpRequest.POST("/api/info/test", "")) == 'io.micronaut.jaxrs.runtime.core.ResourceInfoSpec$ResourceInfoResource:postTest' | ||
} | ||
|
||
@Path("/info") | ||
@Requires(property = "spec.name", value = "ResourceInfoSpec") | ||
static class ResourceInfoResource { | ||
|
||
private final ResourceInfo resourceInfo | ||
|
||
ResourceInfoResource(JaxRsResourceInfo resourceInfo) { | ||
this.resourceInfo = resourceInfo | ||
} | ||
|
||
@Path("/test") | ||
@GET | ||
String getTest() { | ||
"${resourceInfo.resourceClass.name}:${resourceInfo.resourceMethod.name}" | ||
} | ||
|
||
@Path("/test") | ||
@POST | ||
String postTest() { | ||
"${resourceInfo.resourceClass.name}:${resourceInfo.resourceMethod.name}" | ||
} | ||
} | ||
} |
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