Skip to content

Commit

Permalink
Handle baseUri value of @RegisterRestClient
Browse files Browse the repository at this point in the history
Fixes: #2687
  • Loading branch information
geoand committed Jun 2, 2019
1 parent bd1326f commit 97bb88a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
import org.eclipse.microprofile.rest.client.annotation.RegisterProviders;
import org.eclipse.microprofile.rest.client.ext.DefaultClientHeadersFactoryImpl;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
Expand Down Expand Up @@ -79,6 +81,7 @@
class SmallRyeRestClientProcessor {

private static final DotName REST_CLIENT = DotName.createSimple(RestClient.class.getName());
private static final DotName REGISTER_REST_CLIENT = DotName.createSimple(RegisterRestClient.class.getName());

private static final DotName PATH = DotName.createSimple(Path.class.getName());

Expand Down Expand Up @@ -209,10 +212,12 @@ public void register(RegistrationContext registrationContext) {
configurator.scope(BuiltinScope.SINGLETON.getInfo());
configurator.addQualifier(REST_CLIENT);
configurator.creator(m -> {
// return new RestClientBase(proxyType).create();
// return new RestClientBase(proxyType, baseUri).create();
ResultHandle interfaceHandle = m.loadClass(entry.getKey().toString());
ResultHandle baseUriHandle = m.load(getBaseUri(entry.getValue()));
ResultHandle baseHandle = m.newInstance(
MethodDescriptor.ofConstructor(RestClientBase.class, Class.class), interfaceHandle);
MethodDescriptor.ofConstructor(RestClientBase.class, Class.class, String.class),
interfaceHandle, baseUriHandle);
ResultHandle ret = m.invokeVirtualMethod(
MethodDescriptor.ofMethod(RestClientBase.class, "create", Object.class), baseHandle);
m.returnValue(ret);
Expand All @@ -228,6 +233,20 @@ public void register(RegistrationContext registrationContext) {
smallRyeRestClientTemplate.setSslEnabled(sslNativeConfig.isEnabled());
}

private String getBaseUri(ClassInfo classInfo) {
AnnotationInstance instance = classInfo.classAnnotation(REGISTER_REST_CLIENT);
if (instance == null) {
return "";
}

AnnotationValue value = instance.value("baseUri");
if (value == null) {
return "";
}

return value.asString();
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
void registerProviders(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ public class RestClientBase {
public static final String REST_URL_FORMAT = "%s/mp-rest/url";

private final Class<?> proxyType;
private final String baseUriFromAnnotation;

private final Config config;

public RestClientBase(Class<?> proxyType) {
public RestClientBase(Class<?> proxyType, String baseUriFromAnnotation) {
this.proxyType = proxyType;
this.baseUriFromAnnotation = baseUriFromAnnotation;
this.config = ConfigProvider.getConfig();
}

Expand All @@ -53,7 +55,10 @@ public Object create() {
}

private String getBaseUrl() {
String property = String.format(REST_URL_FORMAT, proxyType.getName());
return config.getValue(property, String.class);
if ((baseUriFromAnnotation == null) || baseUriFromAnnotation.isEmpty()) {
String property = String.format(REST_URL_FORMAT, proxyType.getName());
return config.getValue(property, String.class);
}
return baseUriFromAnnotation;
}
}

0 comments on commit 97bb88a

Please sign in to comment.