Skip to content

Commit

Permalink
Fix #28292
Browse files Browse the repository at this point in the history
Registers the StorkClientRequestFilter when we detect a URI starting with stork:// or storks://.
  • Loading branch information
cescoffier committed Sep 30, 2022
1 parent 8da3049 commit 1563b88
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.net.URI;
import java.net.URISyntaxException;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.UriBuilder;

import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.inject.RestClient;
Expand Down Expand Up @@ -34,6 +38,19 @@ void shouldDetermineUrlViaStork() {
assertThat(greeting).isEqualTo("hello, black and white bird");
}

@Test
void shouldDetermineUrlViaStorkWhenUsingTarget() throws URISyntaxException {
String greeting = ClientBuilder.newClient().target("stork://hello-service/hello").request().get(String.class);
assertThat(greeting).isEqualTo("Hello");

greeting = ClientBuilder.newClient().target(new URI("stork://hello-service/hello")).request().get(String.class);
assertThat(greeting).isEqualTo("Hello");

greeting = ClientBuilder.newClient().target(UriBuilder.fromUri("stork://hello-service/hello")).request()
.get(String.class);
assertThat(greeting).isEqualTo("Hello");
}

@Test
void shouldDetermineUrlViaStorkCDI() {
String greeting = client.echo("big bird");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,45 @@ private void abortIfClosed() {

protected InvocationBuilderImpl createQuarkusRestInvocationBuilder(HttpClient client, UriBuilder uri,
ConfigurationImpl configuration) {
return new InvocationBuilderImpl(uri.build(), restClient, client, this, configuration,
URI actualUri = uri.build();
registerStorkFilterIfNeeded(configuration, actualUri);
return new InvocationBuilderImpl(actualUri, restClient, client, this, configuration,
handlerChain.setPreClientSendHandler(preClientSendHandler), requestContext);
}

/**
* If the URI starts with stork:// or storks://, then register the StorkClientRequestFilter automatically.
*
* @param configuration the configuration
* @param actualUri the uri
*/
private static void registerStorkFilterIfNeeded(ConfigurationImpl configuration, URI actualUri) {
if (actualUri.getScheme() != null && actualUri.getScheme().startsWith("stork")
&& !isStorkAlreadyRegistered(configuration)) {
configuration.register(StorkClientRequestFilter.class);
}
}

/**
* Checks if the Stork request filter is already registered.
* We cannot use configuration.isRegistered, as the user registration uses a subclass, and so fail the equality
* expectation.
* <p>
* This method prevents having the stork filter registered twice: once because the uri starts with stork:// and,
* once from the user.
*
* @param configuration the configuration
* @return {@code true} if stork is already registered.
*/
private static boolean isStorkAlreadyRegistered(ConfigurationImpl configuration) {
for (Class<?> clazz : configuration.getClasses()) {
if (clazz.getName().startsWith(StorkClientRequestFilter.class.getName())) {
return true;
}
}
return false;
}

@Override
public WebTargetImpl property(String name, Object value) {
abortIfClosed();
Expand Down

0 comments on commit 1563b88

Please sign in to comment.