feat(gateway): User defined endpoint may be a simple path instead of a complete URI.
Closes gravitee-io/issues#1065
@@ -179,7 +179,7 @@ public abstract class DefaultInvoker implements Invoker {
String targetUri = (String) executionContext.getAttribute(ExecutionContext.ATTR_REQUEST_ENDPOINT);
return (targetUri != null)
- ? selectUserDefinedEndpoint(serverRequest, targetUri)
+ ? selectUserDefinedEndpoint(serverRequest, targetUri, executionContext)
: selectLoadBalancedEndpoint(serverRequest, executionContext);
}
@@ -191,13 +191,13 @@ public abstract class DefaultInvoker implements Invoker {
return new TargetEndpoint(
endpoint,
- (endpoint != null) ? rewriteURI(serverRequest, endpoint.target()) : null);
+ (endpoint != null) ? endpoint.target() + serverRequest.pathInfo() : null);
}
/**
* Select an endpoint according to the URI passed in the execution request attribute.
*/
- private TargetEndpoint selectUserDefinedEndpoint(Request serverRequest, String target) {
+ private TargetEndpoint selectUserDefinedEndpoint(Request serverRequest, String target, ExecutionContext executionContext) {
QueryStringDecoder decoder = new QueryStringDecoder(target);
Map<String, List<String>> queryParameters = decoder.parameters();
@@ -216,6 +216,14 @@ public abstract class DefaultInvoker implements Invoker {
String encodedTarget = builder.substring(0, builder.length() - 1);
+ // Do we have a single path or a plain URI ?
+ if (encodedTarget.startsWith("/")) {
+ Endpoint endpoint = nextEndpoint(serverRequest, executionContext);
+
+ return new TargetEndpoint(
+ endpoint,
+ (endpoint != null) ? endpoint.target() + encodedTarget : null);
+ } else {
Endpoint endpoint = endpointManager.endpoints()
.stream()
.filter(endpointEntry -> encodedTarget.startsWith(endpointEntry.target()))
@@ -224,13 +232,10 @@ public abstract class DefaultInvoker implements Invoker {
return new TargetEndpoint(endpoint, encodedTarget);
}
+ }
public abstract Endpoint nextEndpoint(Request serverRequest, ExecutionContext executionContext);
- private String rewriteURI(Request request, String endpointUri) {
- return endpointUri + request.pathInfo();
- }
-
private URI encodeQueryParameters(Request request, String endpointUri) throws MalformedURLException, URISyntaxException {
if (request.parameters() != null && !request.parameters().isEmpty()) {
QueryStringEncoder encoder = new QueryStringEncoder(endpointUri);
