forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#19839 from geoand/rest-links-handler
Replace runtime reflection usage in rest-links with build time metadata capturing
- Loading branch information
Showing
6 changed files
with
163 additions
and
86 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...inks/deployment/src/main/java/io/quarkus/resteasy/reactive/links/deployment/DotNames.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,15 @@ | ||
package io.quarkus.resteasy.reactive.links.deployment; | ||
|
||
import org.jboss.jandex.DotName; | ||
|
||
import io.quarkus.resteasy.reactive.links.InjectRestLinks; | ||
import io.quarkus.resteasy.reactive.links.RestLink; | ||
|
||
final class DotNames { | ||
|
||
static final DotName INJECT_REST_LINKS_ANNOTATION = DotName.createSimple(InjectRestLinks.class.getName()); | ||
static final DotName REST_LINK_ANNOTATION = DotName.createSimple(RestLink.class.getName()); | ||
|
||
private DotNames() { | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
...yment/src/main/java/io/quarkus/resteasy/reactive/links/deployment/LinksMethodScanner.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,59 @@ | ||
package io.quarkus.resteasy.reactive.links.deployment; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.AnnotationValue; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.MethodInfo; | ||
import org.jboss.resteasy.reactive.server.model.FixedHandlerChainCustomizer; | ||
import org.jboss.resteasy.reactive.server.model.HandlerChainCustomizer; | ||
import org.jboss.resteasy.reactive.server.processor.scanning.MethodScanner; | ||
|
||
import io.quarkus.resteasy.reactive.links.RestLinkType; | ||
import io.quarkus.resteasy.reactive.links.RestLinksHandler; | ||
|
||
public class LinksMethodScanner implements MethodScanner { | ||
|
||
@Override | ||
public List<HandlerChainCustomizer> scan(MethodInfo method, ClassInfo actualEndpointClass, | ||
Map<String, Object> methodContext) { | ||
AnnotationInstance injectRestLinksInstance = getInjectRestLinksAnnotation(method, actualEndpointClass); | ||
if (injectRestLinksInstance == null) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
RestLinkType restLinkType = RestLinkType.TYPE; | ||
AnnotationValue injectRestLinksValue = injectRestLinksInstance.value(); | ||
if (injectRestLinksValue != null) { | ||
restLinkType = RestLinkType.valueOf(injectRestLinksValue.asEnum()); | ||
} | ||
|
||
AnnotationInstance restLinkInstance = method.annotation(DotNames.REST_LINK_ANNOTATION); | ||
String entityType = null; | ||
if (restLinkInstance != null) { | ||
AnnotationValue restInstanceValue = restLinkInstance.value("entityType"); | ||
if (restInstanceValue != null) { | ||
entityType = restInstanceValue.asClass().name().toString(); | ||
} | ||
} | ||
|
||
RestLinksHandler handler = new RestLinksHandler(); | ||
handler.setRestLinkData(new RestLinksHandler.RestLinkData(restLinkType, entityType)); | ||
return Collections.singletonList(new FixedHandlerChainCustomizer(handler, | ||
HandlerChainCustomizer.Phase.AFTER_RESPONSE_CREATED)); | ||
} | ||
|
||
private AnnotationInstance getInjectRestLinksAnnotation(MethodInfo method, ClassInfo actualEndpointClass) { | ||
AnnotationInstance annotationInstance = method.annotation(DotNames.INJECT_REST_LINKS_ANNOTATION); | ||
if (annotationInstance == null) { | ||
annotationInstance = method.declaringClass().classAnnotation(DotNames.INJECT_REST_LINKS_ANNOTATION); | ||
if ((annotationInstance == null) && !actualEndpointClass.equals(method.declaringClass())) { | ||
annotationInstance = actualEndpointClass.classAnnotation(DotNames.INJECT_REST_LINKS_ANNOTATION); | ||
} | ||
} | ||
return annotationInstance; | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
...tive-links/runtime/src/main/java/io/quarkus/resteasy/reactive/links/RestLinksHandler.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,82 @@ | ||
package io.quarkus.resteasy.reactive.links; | ||
|
||
import java.util.Collection; | ||
|
||
import javax.ws.rs.core.Link; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler; | ||
|
||
import io.quarkus.arc.Arc; | ||
|
||
public class RestLinksHandler implements ServerRestHandler { | ||
|
||
private RestLinkData restLinkData; | ||
|
||
public RestLinkData getRestLinkData() { | ||
return restLinkData; | ||
} | ||
|
||
public void setRestLinkData(RestLinkData restLinkData) { | ||
this.restLinkData = restLinkData; | ||
} | ||
|
||
@Override | ||
public void handle(ResteasyReactiveRequestContext context) { | ||
Response response = context.getResponse().get(); | ||
for (Link link : getLinks(response)) { | ||
response.getHeaders().add("Link", link); | ||
} | ||
} | ||
|
||
private Collection<Link> getLinks(Response response) { | ||
if ((restLinkData.getRestLinkType() == RestLinkType.INSTANCE) && response.hasEntity()) { | ||
return getTestLinksProvider().getInstanceLinks(response.getEntity()); | ||
} | ||
return getTestLinksProvider() | ||
.getTypeLinks(restLinkData.getEntityType() != null ? entityTypeClass() : response.getEntity().getClass()); | ||
} | ||
|
||
private Class<?> entityTypeClass() { | ||
try { | ||
return Thread.currentThread().getContextClassLoader().loadClass(restLinkData.getEntityType()); | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalStateException("Unable load class '" + restLinkData.getEntityType() + "'", e); | ||
} | ||
} | ||
|
||
private RestLinksProvider getTestLinksProvider() { | ||
return Arc.container().instance(RestLinksProvider.class).get(); | ||
} | ||
|
||
public static class RestLinkData { | ||
|
||
public RestLinkData(RestLinkType restLinkType, String entityType) { | ||
this.restLinkType = restLinkType; | ||
this.entityType = entityType; | ||
} | ||
|
||
public RestLinkData() { | ||
} | ||
|
||
private RestLinkType restLinkType; | ||
private String entityType; | ||
|
||
public RestLinkType getRestLinkType() { | ||
return restLinkType; | ||
} | ||
|
||
public void setRestLinkType(RestLinkType restLinkType) { | ||
this.restLinkType = restLinkType; | ||
} | ||
|
||
public String getEntityType() { | ||
return entityType; | ||
} | ||
|
||
public void setEntityType(String entityType) { | ||
this.entityType = entityType; | ||
} | ||
} | ||
} |
76 changes: 0 additions & 76 deletions
76
...nks/runtime/src/main/java/io/quarkus/resteasy/reactive/links/RestLinksResponseFilter.java
This file was deleted.
Oops, something went wrong.