Skip to content

Commit

Permalink
Add support for URI path prefix modification
Browse files Browse the repository at this point in the history
  • Loading branch information
big-cir committed Dec 27, 2024
1 parent dfa9496 commit a52798c
Showing 1 changed file with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class UriModifyingOperationPreprocessor implements OperationPreprocessor

private String port;

private String pathPrefix;

/**
* Modifies the URI to use the given {@code scheme}. {@code null}, the default, will
* leave the scheme unchanged.
Expand Down Expand Up @@ -99,6 +101,17 @@ public UriModifyingOperationPreprocessor port(int port) {
return port(Integer.toString(port));
}

/**
* Modifies the URI to add the given path prefix.
* @param pathPrefix the path prefix to add
* @return {@code this}
*/
public UriModifyingOperationPreprocessor pathPrefix(String pathPrefix) {
this.pathPrefix = pathPrefix;
this.contentModifier.setPathPrefix(pathPrefix);
return this;
}

/**
* Removes the port from the URI.
* @return {@code this}
Expand Down Expand Up @@ -130,13 +143,17 @@ public OperationRequest preprocess(OperationRequest request) {
uriBuilder.port(null);
}
}
if (this.pathPrefix != null) {
String rawPath = request.getUri().getRawPath();
uriBuilder.replacePath(this.pathPrefix + ((rawPath != null) ? rawPath : ""));
}
URI modifiedUri = uriBuilder.build(true).toUri();
HttpHeaders modifiedHeaders = modify(request.getHeaders());
modifiedHeaders.set(HttpHeaders.HOST,
modifiedUri.getHost() + ((modifiedUri.getPort() != -1) ? ":" + modifiedUri.getPort() : ""));
return this.contentModifyingDelegate
.preprocess(new OperationRequestFactory().create(uriBuilder.build(true).toUri(), request.getMethod(),
request.getContent(), modifiedHeaders, modify(request.getParts()), request.getCookies()));
.preprocess(new OperationRequestFactory().create(uriBuilder.build(true).toUri(), request.getMethod(),
request.getContent(), modifiedHeaders, modify(request.getParts()), request.getCookies()));
}

@Override
Expand Down Expand Up @@ -169,14 +186,16 @@ private Collection<OperationRequestPart> modify(Collection<OperationRequestPart>
private static final class UriModifyingContentModifier implements ContentModifier {

private static final Pattern SCHEME_HOST_PORT_PATTERN = Pattern
.compile("(http[s]?)://([a-zA-Z0-9-\\.]+)(:[0-9]+)?");
.compile("(http[s]?)://([a-zA-Z0-9-\\.]+)(:[0-9]+)?");

private String scheme;

private String host;

private String port;

private String pathPrefix;

private void setScheme(String scheme) {
this.scheme = scheme;
}
Expand All @@ -189,6 +208,10 @@ private void setPort(String port) {
this.port = port;
}

private void setPathPrefix(String pathPrefix) {
this.pathPrefix = pathPrefix;
}

@Override
public byte[] modifyContent(byte[] content, MediaType contentType) {
String input;
Expand All @@ -204,7 +227,8 @@ public byte[] modifyContent(byte[] content, MediaType contentType) {

private String modify(String input) {
List<String> replacements = Arrays.asList(this.scheme, this.host,
StringUtils.hasText(this.port) ? ":" + this.port : this.port);
StringUtils.hasText(this.port) ? ":" + this.port : this.port,
this.pathPrefix);

int previous = 0;

Expand Down

0 comments on commit a52798c

Please sign in to comment.