Skip to content

Commit

Permalink
Add magic function replace function for replacing [a-z0-9-_.:;] with …
Browse files Browse the repository at this point in the history
…something similar or empty
  • Loading branch information
vmarian2 committed Jan 3, 2024
1 parent 177c8ef commit ed33069
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/main/java/com/mimecast/robin/util/Magic.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Magic {
/**
* Magic variable pattern.
*/
protected final static Pattern magicVariablePattern = Pattern.compile("\\{([a-z]+)?(\\([a-z0-9-_.:;]+\\))?\\$([a-z0-9-_.]+)(\\[([0-9?]+)](\\[([a-z0-9]+)])?)?}", Pattern.CASE_INSENSITIVE);
protected final static Pattern magicVariablePattern = Pattern.compile("\\{([a-z]+)?(\\([a-z0-9-_.:;|]+\\))?\\$([a-z0-9-_.]+)(\\[([0-9?]+)](\\[([a-z0-9]+)])?)?}", Pattern.CASE_INSENSITIVE);

/**
* Transaction response pattern.
Expand Down Expand Up @@ -99,8 +99,11 @@ public static String magicReplace(String magicString, Session session, boolean n
while (matcher.find()) {
String magicVariable = matcher.group();

String magicfunction = matcher.group(1);
String magicarg = matcher.group(2);
String magicFunction = matcher.group(1);
String magicArgs = matcher.group(2);
if (magicArgs != null) {
magicArgs = magicArgs.replaceAll("[()]", "");
}
String magicName = matcher.group(3);
String resultColumn = matcher.group(7);
String value = null;
Expand Down Expand Up @@ -135,19 +138,30 @@ public static String magicReplace(String magicString, Session session, boolean n
}

// Magic functions.
if (magicfunction != null && value != null) {
if ("dateToMillis".equals(magicfunction)) {
if (magicFunction != null && value != null) {
if ("dateToMillis".equals(magicFunction)) {
value = dateToMillis(value);
} else if ("millisToDate".equals(magicfunction)) {
} else if ("millisToDate".equals(magicFunction)) {
value = millisToDate(value);
} else if ("toLowerCase".equals(magicfunction)) {
} else if ("toLowerCase".equals(magicFunction)) {
value = value.toLowerCase();
} else if ("toUpperCase".equals(magicfunction)) {
} else if ("toUpperCase".equals(magicFunction)) {
value = value.toUpperCase();
} else if ("patternQuote".equals(magicfunction)) {
} else if ("patternQuote".equals(magicFunction)) {
value = Pattern.quote(value);
} else if ("strip".equals(magicfunction)) {
value = value.replaceAll(magicarg, "");
} else if ("strip".equals(magicFunction)) {
if (magicArgs != null) {
value = value.replaceAll(magicArgs, "");
} else {
log.warn("Magic strip function requires an argument got: {}", magicArgs);
}
} else if ("replace".equals(magicFunction)) {
if (magicArgs != null && magicArgs.contains("|")) {
String[] replaceArgs = magicArgs.split("\\|", 2);
value = value.replaceAll(replaceArgs[0], replaceArgs[1]);
} else {
log.warn("Magic replace function requires two arguments separated by | but got: {}", magicArgs);
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/mimecast/robin/util/MagicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ void magicFunctionStrip() {

assertEquals("https://example.com", test);
}

@Test
void magicFunctionRepalce() {
Session session = new Session();
session.putMagic("host", "example.com:8080");

String test = Magic.magicReplace("https://{replace(:8080|)$host}", session, false);

assertEquals("https://example.com", test);
}
}

0 comments on commit ed33069

Please sign in to comment.