Skip to content

Commit

Permalink
GUACAMOLE-1976: Add OPTIONAL token modifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
necouchman committed Oct 16, 2024
1 parent bbede31 commit f027623
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Filtering object which replaces tokens of the form "${TOKEN_NAME}" with
Expand All @@ -33,6 +35,11 @@
*/
public class TokenFilter {

/**
* The logger for this class.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(TokenFilter.class);

/**
* Regular expression which matches individual tokens, with additional
* capturing groups for convenient retrieval of leading text, the possible
Expand Down Expand Up @@ -225,15 +232,25 @@ private String filter(String input, boolean strict)
// strict mode is enabled
if (tokenValue == null) {

// Token marked as optional, so just skip it and update
// last match.
if (modifier != null && modifier.equals("OPTIONAL")) {
LOGGER.warn("The token \"{}\" has no value and has been "
+ "marked as optional, so it will be treated "
+ "as a blank value instead of a literal.",
tokenName);
endOfLastMatch = tokenMatcher.end();
continue;
}

// Fail outright if strict mode is enabled
if (strict)
throw new GuacamoleTokenUndefinedException("Token "
+ "has no defined value.", tokenName);

// If strict mode is NOT enabled, simply interpret as
// a literal
String notToken = tokenMatcher.group(TOKEN_GROUP);
output.append(notToken);
output.append(tokenMatcher.group(TOKEN_GROUP));

}

Expand Down

0 comments on commit f027623

Please sign in to comment.