Skip to content

Commit

Permalink
Fix StorkClientRequestFilter exception handling
Browse files Browse the repository at this point in the history
Exceptions were not caught properly because a Uni subscription was done
outside a try-catch block.
  • Loading branch information
damianorenfer committed Mar 27, 2024
1 parent 826fb7e commit d40f0e8
Showing 1 changed file with 51 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestFilter;

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import io.smallrye.stork.Stork;
import io.smallrye.stork.api.ServiceInstance;

@Priority(Priorities.AUTHENTICATION)
@Provider
Expand All @@ -34,67 +32,64 @@ public void filter(ResteasyReactiveClientRequestContext requestContext) {
}

requestContext.suspend();
Uni<ServiceInstance> serviceInstance;
boolean measureTime = shouldMeasureTime(requestContext.getResponseType());
try {
serviceInstance = Stork.getInstance()
Stork.getInstance()
.getService(serviceName)
.selectInstanceAndRecordStart(measureTime);
.selectInstanceAndRecordStart(measureTime)
.subscribe()
.with(instance -> {
boolean isHttps = instance.isSecure() || "storks".equals(uri.getScheme());
String scheme = isHttps ? "https" : "http";
try {
// In the case the service instance does not set the host and/or port
String host = instance.getHost() == null ? "localhost" : instance.getHost();
int port = instance.getPort();
if (instance.getPort() == 0) {
if (isHttps) {
port = 433;
} else {
port = 80;
}
}
// Service instance can also contain an optional path.
Optional<String> path = instance.getPath();
String actualPath = uri.getRawPath();
if (path.isPresent()) {
var p = path.get();
if (!p.startsWith("/")) {
p = "/" + p;
}
if (actualPath == null) {
actualPath = p;
} else {
// Append both.
if (actualPath.startsWith("/") || p.endsWith("/")) {
actualPath = p + actualPath;
} else {
actualPath = p + "/" + actualPath;
}
}
}
//To avoid the path double encoding we create uri with path=null and set the path after
URI newUri = new URI(scheme,
uri.getUserInfo(), host, port,
null, uri.getQuery(), uri.getFragment());
URI build = UriBuilder.fromUri(newUri).path(actualPath).build();
requestContext.setUri(build);
if (measureTime && instance.gatherStatistics()) {
requestContext.setCallStatsCollector(instance);
}
requestContext.resume();
} catch (URISyntaxException e) {
requestContext.resume(new IllegalArgumentException("Invalid URI", e));
}
},
requestContext::resume);
} catch (Throwable e) {
log.error("Error selecting service instance for serviceName: " + serviceName, e);
requestContext.resume(e);
return;
}

serviceInstance.subscribe()
.with(instance -> {
boolean isHttps = instance.isSecure() || "storks".equals(uri.getScheme());
String scheme = isHttps ? "https" : "http";
try {
// In the case the service instance does not set the host and/or port
String host = instance.getHost() == null ? "localhost" : instance.getHost();
int port = instance.getPort();
if (instance.getPort() == 0) {
if (isHttps) {
port = 433;
} else {
port = 80;
}
}
// Service instance can also contain an optional path.
Optional<String> path = instance.getPath();
String actualPath = uri.getRawPath();
if (path.isPresent()) {
var p = path.get();
if (!p.startsWith("/")) {
p = "/" + p;
}
if (actualPath == null) {
actualPath = p;
} else {
// Append both.
if (actualPath.startsWith("/") || p.endsWith("/")) {
actualPath = p + actualPath;
} else {
actualPath = p + "/" + actualPath;
}
}
}
//To avoid the path double encoding we create uri with path=null and set the path after
URI newUri = new URI(scheme,
uri.getUserInfo(), host, port,
null, uri.getQuery(), uri.getFragment());
URI build = UriBuilder.fromUri(newUri).path(actualPath).build();
requestContext.setUri(build);
if (measureTime && instance.gatherStatistics()) {
requestContext.setCallStatsCollector(instance);
}
requestContext.resume();
} catch (URISyntaxException e) {
requestContext.resume(new IllegalArgumentException("Invalid URI", e));
}
},
requestContext::resume);
}

}
Expand Down

0 comments on commit d40f0e8

Please sign in to comment.