Skip to content

Commit

Permalink
Merge pull request #14711 from VasilisAndritsoudis/restclient-providers
Browse files Browse the repository at this point in the history
Support more than one custom annotation which registers MP REST Client provider
  • Loading branch information
sberyozkin authored Feb 11, 2021
2 parents 4aeba34 + d46d35b commit 9e4b7f5
Show file tree
Hide file tree
Showing 28 changed files with 762 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import org.jboss.jandex.DotName;

import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.builder.item.MultiBuildItem;

/**
* Used to mark a custom annotation and its associated JAX-RS client provider
*/
public final class RestClientAnnotationProviderBuildItem extends SimpleBuildItem {
public final class RestClientAnnotationProviderBuildItem extends MultiBuildItem {

private final DotName annotationName;
private final Class<?> providerClass;
Expand All @@ -17,6 +17,11 @@ public RestClientAnnotationProviderBuildItem(DotName annotationName, Class<?> pr
this.providerClass = providerClass;
}

public RestClientAnnotationProviderBuildItem(String annotationName, Class<?> providerClass) {
this.annotationName = DotName.createSimple(annotationName);
this.providerClass = providerClass;
}

public DotName getAnnotationName() {
return annotationName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void processInterfaces(
BeanArchiveIndexBuildItem beanArchiveIndexBuildItem,
Capabilities capabilities,
PackageConfig packageConfig,
Optional<RestClientAnnotationProviderBuildItem> restClientAnnotationProvider,
List<RestClientAnnotationProviderBuildItem> restClientAnnotationProviders,
BuildProducer<NativeImageProxyDefinitionBuildItem> proxyDefinition,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<ReflectiveHierarchyBuildItem> reflectiveHierarchy,
Expand Down Expand Up @@ -237,20 +237,28 @@ public void register(RegistrationContext registrationContext) {
configurator.addQualifier(REST_CLIENT);
final String configPrefix = computeConfigPrefix(restClientName.toString(), entry.getValue());
final ScopeInfo scope = computeDefaultScope(capabilities, config, entry, configPrefix);
final Class<?> annotationProvider = checkAnnotationProviders(entry.getValue(),
restClientAnnotationProvider.orElse(null));
final List<Class<?>> annotationProviders = checkAnnotationProviders(entry.getValue(),
restClientAnnotationProviders);
configurator.scope(scope);
configurator.creator(m -> {
// return new RestClientBase(proxyType, baseUri).create();
ResultHandle interfaceHandle = m.loadClass(restClientName.toString());
ResultHandle baseUriHandle = m.load(getAnnotationParameter(entry.getValue(), "baseUri"));
ResultHandle configPrefixHandle = m.load(configPrefix);
ResultHandle annotationProviderHandle = annotationProvider == null ? m.loadNull()
: m.loadClass(annotationProvider);
ResultHandle annotationProvidersHandle = null;
if (!annotationProviders.isEmpty()) {
annotationProvidersHandle = m.newArray(Class.class, annotationProviders.size());
for (int i = 0; i < annotationProviders.size(); i++) {
m.writeArrayValue(annotationProvidersHandle, i, m.loadClass(annotationProviders.get(i)));
}
} else {
annotationProvidersHandle = m.loadNull();
}
ResultHandle baseHandle = m.newInstance(
MethodDescriptor.ofConstructor(RestClientBase.class, Class.class, String.class, String.class,
Class.class),
interfaceHandle, baseUriHandle, configPrefixHandle, annotationProviderHandle);
MethodDescriptor.ofConstructor(RestClientBase.class, Class.class, String.class,
String.class,
Class[].class),
interfaceHandle, baseUriHandle, configPrefixHandle, annotationProvidersHandle);
ResultHandle ret = m.invokeVirtualMethod(
MethodDescriptor.ofMethod(RestClientBase.class, "create", Object.class), baseHandle);
m.returnValue(ret);
Expand All @@ -262,13 +270,10 @@ public void register(RegistrationContext registrationContext) {
}));
}

private static Class<?> checkAnnotationProviders(ClassInfo classInfo,
RestClientAnnotationProviderBuildItem restClientAnnotationProvider) {
if (restClientAnnotationProvider != null
&& classInfo.classAnnotation(restClientAnnotationProvider.getAnnotationName()) != null) {
return restClientAnnotationProvider.getProviderClass();
}
return null;
private static List<Class<?>> checkAnnotationProviders(ClassInfo classInfo,
List<RestClientAnnotationProviderBuildItem> restClientAnnotationProviders) {
return restClientAnnotationProviders.stream().filter(p -> (classInfo.classAnnotation(p.getAnnotationName()) != null))
.map(p -> p.getProviderClass()).collect(Collectors.toList());
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public class RestClientBase {
private final Class<?> proxyType;
private final String baseUriFromAnnotation;
private final String propertyPrefix;
private final Class<?> annotationProvider;
private final Class<?>[] annotationProviders;

public RestClientBase(Class<?> proxyType, String baseUriFromAnnotation, String propertyPrefix,
Class<?> annotationProvider) {
Class<?>[] annotationProviders) {
this.proxyType = proxyType;
this.baseUriFromAnnotation = baseUriFromAnnotation;
this.propertyPrefix = propertyPrefix;
this.annotationProvider = annotationProvider;
this.annotationProviders = annotationProviders;
}

public Object create() {
Expand Down Expand Up @@ -202,8 +202,10 @@ private void configureProviders(RestClientBuilder builder) {
if (maybeProviders.isPresent()) {
registerProviders(builder, maybeProviders.get());
}
if (annotationProvider != null) {
builder.register(annotationProvider);
if (annotationProviders != null) {
for (Class<?> annotationProvider : annotationProviders) {
builder.register(annotationProvider);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,24 @@ public void testProjectWithExtension() throws MavenInvocationException, IOExcept
assertEquals(3, extDepWarnings.size());
}

@Test
public void testRestClientCustomHeadersExtension() throws MavenInvocationException, IOException {
testDir = getTargetDir("projects/rest-client-custom-headers-extension");
runAndCheck();

final List<String> extDepWarnings = Files
.readAllLines(testDir.toPath().resolve("build-rest-client-custom-headers-extension.log"))
.stream()
.filter(s -> s.startsWith(
"[WARNING] [io.quarkus.bootstrap.devmode.DependenciesFilter] Local Quarkus extension dependency "))
.collect(Collectors.toList());
assertTrue(extDepWarnings
.contains(
"[WARNING] [io.quarkus.bootstrap.devmode.DependenciesFilter] Local Quarkus extension dependency org.acme:rest-client-custom-headers will not be hot-reloadable"));

assertThat(DevModeTestUtils.getHttpResponse("/app/frontend")).isEqualTo("CustomValue1 CustomValue2");
}

@Test
public void testThatTheApplicationIsReloadedMultiModule() throws MavenInvocationException, IOException {
testDir = initProject("projects/multimodule", "projects/multimodule-with-deps");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>org.acme</groupId>
<artifactId>quarkus-extensions-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.version>@project.version@</quarkus.platform.version>
<quarkus-plugin.version>@project.version@</quarkus-plugin.version>
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modules>
<module>rest-client-custom-headers</module>
<module>runner</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>\${quarkus-plugin.version}</version>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.acme</groupId>
<artifactId>rest-client-custom-headers</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.acme</groupId>
<artifactId>rest-client-custom-headers-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>rest-client-custom-headers-deployment</artifactId>
<name>Rest Client Custom Headers - Deployment</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.acme</groupId>
<artifactId>rest-client-custom-headers</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>\${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.acme.deployment;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.restclient.deployment.RestClientAnnotationProviderBuildItem;
import org.acme.CustomHeader1Filter;
import org.acme.CustomHeader2Filter;


public class ExtensionBuildStep {

@BuildStep
void registerProvider(BuildProducer<AdditionalBeanBuildItem> additionalBeans,
BuildProducer<RestClientAnnotationProviderBuildItem> restAnnotationProvider) {
additionalBeans.produce(AdditionalBeanBuildItem.unremovableOf(CustomHeader1Filter.class));
additionalBeans.produce(AdditionalBeanBuildItem.unremovableOf(CustomHeader2Filter.class));

restAnnotationProvider.produce(new RestClientAnnotationProviderBuildItem("org.acme.CustomHeader1",
CustomHeader1Filter.class));
restAnnotationProvider.produce(new RestClientAnnotationProviderBuildItem("org.acme.CustomHeader2",
CustomHeader2Filter.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.acme</groupId>
<artifactId>quarkus-extensions-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>rest-client-custom-headers-parent</artifactId>
<name>Rest Client Custom Headers - Parent</name>

<packaging>pom</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.parameters>true</maven.compiler.parameters>
<quarkus.version>@project.version@</quarkus.version>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
</properties>

<modules>
<module>runtime</module>
<module>deployment</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>\${compiler-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.acme</groupId>
<artifactId>rest-client-custom-headers-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>rest-client-custom-headers</artifactId>
<name>Rest Client Custom Headers - Runtime</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bootstrap-maven-plugin</artifactId>
<version>\${quarkus.version}</version>
<executions>
<execution>
<goals>
<goal>extension-descriptor</goal>
</goals>
<phase>compile</phase>
<configuration>
<deployment>org.acme:rest-client-custom-headers-deployment:1.0-SNAPSHOT</deployment>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>\${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.acme;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CustomHeader1 {
}
Loading

0 comments on commit 9e4b7f5

Please sign in to comment.