Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for URI's path prefix modification #952

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +143,10 @@ 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,
Expand Down Expand Up @@ -177,6 +194,8 @@ private static final class UriModifyingContentModifier implements ContentModifie

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,21 +227,26 @@ 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 == null ? "" : this.pathPrefix.startsWith("/") ? this.pathPrefix : "/" + this.pathPrefix);

int previous = 0;

Matcher matcher = SCHEME_HOST_PORT_PATTERN.matcher(input);
StringBuilder builder = new StringBuilder();
while (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); i++) {
int matcherGroupCount = matcher.groupCount();
for (int i = 1; i <= matcherGroupCount; i++) {
if (matcher.start(i) >= 0) {
builder.append(input, previous, matcher.start(i));
}
if (matcher.start(i) >= 0) {
previous = matcher.end(i);
}
builder.append(getReplacement(matcher.group(i), replacements.get(i - 1)));
if (i == matcherGroupCount) {
builder.append(replacements.get(i));
}
}
}

Expand Down