private String filter(String input, boolean strict)
            throws GuacamoleTokenUndefinedException {

        StringBuilder output = new StringBuilder();
        Matcher tokenMatcher = tokenPattern.matcher(input);

        // Track last regex match
        int endOfLastMatch = 0;

        // For each possible token
        while (tokenMatcher.find()) {

            // Pull possible leading text and first char before possible token
            String literal = tokenMatcher.group(LEADING_TEXT_GROUP);
            String escape = tokenMatcher.group(ESCAPE_CHAR_GROUP);
            String modifier = tokenMatcher.group(TOKEN_MODIFIER);

            // Append leading non-token text
            output.append(literal);

            // If char before token is '$', the token itself is escaped
            if ("$".equals(escape)) {
                String notToken = tokenMatcher.group(TOKEN_GROUP);
                output.append(notToken);
            }

            // If char is not '$', interpret as a token
            else {

                // The char before the token, if any, is a literal
                output.append(escape);

                // Pull token value
                String tokenName = tokenMatcher.group(TOKEN_NAME_GROUP);
                String tokenValue = getToken(tokenName);

                // If token is unknown, interpretation depends on whether
                // 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.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
                    output.append(tokenMatcher.group(TOKEN_GROUP));

                }

                // Otherwise, check for modifiers and substitute value appropriately
                else {
                    
                    // If a modifier is present, try to use it.
                    if (modifier != null && !modifier.isEmpty()) {
                        switch (modifier) {
                            // Switch token to upper-case
                            case "UPPER":
                                output.append(tokenValue.toUpperCase());
                                break;
                                
                            // Switch token to lower case
                            case "LOWER":
                                output.append(tokenValue.toLowerCase());
                                break;
                                
                            // Just append the token value
                            default:
                                output.append(tokenValue);
                        }
                    }
                    
                    // No modifier present, so just append token value.
                    else
                        output.append(tokenValue);
                    
                }

            }

            // Update last regex match
            endOfLastMatch = tokenMatcher.end();

        }

        // Append any remaining non-token text
        output.append(input.substring(endOfLastMatch));

        return output.toString();

    }
Filters the given string, replacing any tokens with their corresponding
values. Handling of undefined tokens depends on the value given for the
strict flag.

@param input
    The string to filter.

@param strict
    Whether to disallow tokens which lack values from existing in the
    string. If true, an exception will be thrown if any tokens in the
    string lack corresponding values. If false, tokens which lack values
    will be interpreted as literals.

@return
    A copy of the input string, with any tokens replaced with their
    corresponding values.

@throws GuacamoleTokenUndefinedException
    If the strict flag is set to true and at least one token in the
    given string has no corresponding value.
guacamole-ext/src/main/java/org/apache/guacamole/token/TokenFilter.java