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

GUACAMOLE-1976: Add OPTIONAL token modifier. #1004

Merged
merged 1 commit into from
Oct 18, 2024
Merged
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 @@ -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")) {
necouchman marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.debug("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
Loading