Skip to content

Commit

Permalink
#550 converted postfixFromUrl in QueueSplitterConfiguration in Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
zorba71 committed Jan 25, 2024
1 parent 57c3987 commit a834584
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public class QueueSplitterConfiguration {
private final String postfixFromHeader;

@Nullable
private final String postfixFromUrl;
private final Pattern postfixFromUrl;


public QueueSplitterConfiguration(
Pattern queue,
String postfixDelimiter,
@Nullable List<String> postfixFromStatic,
@Nullable String postfixFromHeader,
@Nullable String postfixFromUrl) {
@Nullable Pattern postfixFromUrl) {
this.queue = queue;
this.postfixDelimiter = postfixDelimiter;
this.postfixFromStatic = postfixFromStatic;
Expand All @@ -59,7 +59,7 @@ public String getPostfixFromHeader() {
}

@Nullable
public String getPostfixFromUrl() {
public Pattern getPostfixFromUrl() {
return postfixFromUrl;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -45,32 +46,36 @@ static List<QueueSplitterConfiguration> parse(Buffer configurationResourceBuffer
}

for (String queuePattern : config.fieldNames()) {
Pattern pattern = Pattern.compile(queuePattern);
JsonObject queueConfig = config.getJsonObject(queuePattern);
JsonArray postfixFromStatic = queueConfig.getJsonArray(POSTFIX_FROM_STATIC_KEY);
if (postfixFromStatic != null) {
List<String> staticPostfixes = postfixFromStatic.stream().map(Object::toString).collect(Collectors.toList());
queueSplitterConfigurations.add(new QueueSplitterConfiguration(
pattern,
queueConfig.getString("postfixDelimiter", DEFAULT_POSTFIX_DELIMITER),
staticPostfixes,
null,
null
));
continue;
}
String postfixFromHeader = queueConfig.getString(POSTFIX_FROM_HEADER_KEY);
String postfixFromUrl = queueConfig.getString(POSTFIX_FROM_URL_KEY);
if (postfixFromHeader != null || postfixFromUrl != null) {
queueSplitterConfigurations.add(new QueueSplitterConfiguration(
pattern,
queueConfig.getString(POSTFIX_DELIMITER_KEY, DEFAULT_POSTFIX_DELIMITER),
null,
postfixFromHeader,
postfixFromUrl
));
} else {
log.warn("Queue splitter configuration without a postfix definition");
try {
Pattern pattern = Pattern.compile(queuePattern);
JsonObject queueConfig = config.getJsonObject(queuePattern);
JsonArray postfixFromStatic = queueConfig.getJsonArray(POSTFIX_FROM_STATIC_KEY);
if (postfixFromStatic != null) {
List<String> staticPostfixes = postfixFromStatic.stream().map(Object::toString).collect(Collectors.toList());
queueSplitterConfigurations.add(new QueueSplitterConfiguration(
pattern,
queueConfig.getString("postfixDelimiter", DEFAULT_POSTFIX_DELIMITER),
staticPostfixes,
null,
null
));
continue;
}
String postfixFromHeader = queueConfig.getString(POSTFIX_FROM_HEADER_KEY);
String postfixFromUrl = queueConfig.getString(POSTFIX_FROM_URL_KEY);
if (postfixFromHeader != null || postfixFromUrl != null) {
queueSplitterConfigurations.add(new QueueSplitterConfiguration(
pattern,
queueConfig.getString(POSTFIX_DELIMITER_KEY, DEFAULT_POSTFIX_DELIMITER),
null,
postfixFromHeader,
postfixFromUrl != null ? Pattern.compile(postfixFromUrl) : null
));
} else {
log.warn("Queue splitter configuration without a postfix definition");
}
} catch (PatternSyntaxException patternException) {
log.warn("Queue splitter '{}' is not a valid regex pattern. Discarding this queue splitter configuration", queuePattern);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public String executeSplit(String queue, HttpServerRequest request) {
StringBuilder stringBuilder = new StringBuilder(queue);
if (matches(queue)) {
if (configuration.getPostfixFromUrl() != null) {
Pattern pattern = Pattern.compile(configuration.getPostfixFromUrl());
Matcher matcher = pattern.matcher(request.uri());
Matcher matcher = configuration.getPostfixFromUrl().matcher(request.uri());
if (matcher.matches()) {
for (int i = 0; i < matcher.groupCount(); i++) {
stringBuilder.append(configuration.getPostfixDelimiter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public void parseWithAllValid(TestContext context) {
context.assertEquals("_", config_3.getPostfixDelimiter());
context.assertNull(config_3.getPostfixFromStatic());
context.assertNull(config_3.getPostfixFromHeader());
context.assertEquals(".*/path1/(.*)/path3/path4/.*", config_3.getPostfixFromUrl());
context.assertEquals(
Pattern.compile(".*/path1/(.*)/path3/path4/.*").pattern(),
config_3.getPostfixFromUrl().pattern());
context.assertFalse(config_3.isSplitStatic());
context.assertTrue(config_3.isSplitFromRequest());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void testExecuteSplitWithUrl() {
"-",
null,
null,
"/path1/(.+)/path3/(.+)"
Pattern.compile("/path1/(.+)/path3/(.+)")
));
HttpServerRequest request = mock(HttpServerRequest.class);
when(request.headers()).thenReturn(new HeadersMultiMap());
Expand All @@ -112,7 +112,7 @@ public void testExecuteSplitWithUrlAndHeader() {
"-",
null,
"x-rp-deviceid",
"/path1/(.+)/path3/(.+)"
Pattern.compile("/path1/(.+)/path3/(.+)")
));
HttpServerRequest request = mock(HttpServerRequest.class);
when(request.headers()).thenReturn(new HeadersMultiMap().add("x-rp-deviceid", "A1B2C3D4E5F6"));
Expand Down

0 comments on commit a834584

Please sign in to comment.