GUACAMOLE-1: Refactor org.glyptodon package/groupId to org.apache.

This commit is contained in:
Michael Jumper
2016-03-22 14:05:53 -07:00
parent 2358d88683
commit 6990344697
402 changed files with 1536 additions and 1536 deletions

View File

@@ -0,0 +1,126 @@
/*
* Copyright (C) 2016 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.apache.guacamole.token;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.guacamole.net.auth.Credentials;
/**
* Utility class which provides access to standardized token names, as well as
* facilities for generating those tokens from common objects.
*
* @author Michael Jumper
*/
public class StandardTokens {
/**
* The name of the username token added via addStandardTokens().
*/
public static final String USERNAME_TOKEN = "GUAC_USERNAME";
/**
* The name of the password token added via addStandardTokens().
*/
public static final String PASSWORD_TOKEN = "GUAC_PASSWORD";
/**
* The name of the date token (server-local time) added via
* addStandardTokens().
*/
public static final String DATE_TOKEN = "GUAC_DATE";
/**
* The name of the time token (server-local time) added via
* addStandardTokens().
*/
public static final String TIME_TOKEN = "GUAC_TIME";
/**
* The date format that should be used for the date token. This format must
* be compatible with Java's SimpleDateFormat.
*/
private static final String DATE_FORMAT = "yyyyMMdd";
/**
* The date format that should be used for the time token. This format must
* be compatible with Java's SimpleDateFormat.
*/
private static final String TIME_FORMAT = "HHmmss";
/**
* This utility class should not be instantiated.
*/
private StandardTokens() {}
/**
* Adds tokens which are standardized by guacamole-ext to the given
* TokenFilter and which do not require a corresponding Credentials object.
* These the server date and time (GUAC_DATE and GUAC_TIME respectively).
*
* @param filter
* The TokenFilter to add standard tokens to.
*/
public static void addStandardTokens(TokenFilter filter) {
// Add date/time tokens (server-local time)
Date currentTime = new Date();
filter.setToken(DATE_TOKEN, new SimpleDateFormat(DATE_FORMAT).format(currentTime));
filter.setToken(TIME_TOKEN, new SimpleDateFormat(TIME_FORMAT).format(currentTime));
}
/**
* Adds tokens which are standardized by guacamole-ext to the given
* TokenFilter using the values from the given Credentials object. These
* standardized tokens include the current username (GUAC_USERNAME),
* password (GUAC_PASSWORD), and the server date and time (GUAC_DATE and
* GUAC_TIME respectively). If either the username or password are not set
* within the given credentials, the corresponding token(s) will remain
* unset.
*
* @param filter
* The TokenFilter to add standard tokens to.
*
* @param credentials
* The Credentials to use when populating the GUAC_USERNAME and
* GUAC_PASSWORD tokens.
*/
public static void addStandardTokens(TokenFilter filter, Credentials credentials) {
// Add username token
String username = credentials.getUsername();
if (username != null)
filter.setToken(USERNAME_TOKEN, username);
// Add password token
String password = credentials.getPassword();
if (password != null)
filter.setToken(PASSWORD_TOKEN, password);
// Add any tokens which do not require credentials
addStandardTokens(filter);
}
}

View File

@@ -0,0 +1,234 @@
/*
* Copyright (C) 2015 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.apache.guacamole.token;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Filtering object which replaces tokens of the form "${TOKEN_NAME}" with
* their corresponding values. Unknown tokens are not replaced. If TOKEN_NAME
* is a valid token, the literal value "${TOKEN_NAME}" can be included by using
* "$${TOKEN_NAME}".
*
* @author Michael Jumper
*/
public class TokenFilter {
/**
* Regular expression which matches individual tokens, with additional
* capturing groups for convenient retrieval of leading text, the possible
* escape character preceding the token, the name of the token, and the
* entire token itself.
*/
private final Pattern tokenPattern = Pattern.compile("(.*?)(^|.)(\\$\\{([A-Za-z0-9_]*)\\})");
/**
* The index of the capturing group within tokenPattern which matches
* non-token text preceding a possible token.
*/
private static final int LEADING_TEXT_GROUP = 1;
/**
* The index of the capturing group within tokenPattern which matches the
* character immediately preceding a possible token, possibly denoting that
* the token should instead be interpreted as a literal.
*/
private static final int ESCAPE_CHAR_GROUP = 2;
/**
* The index of the capturing group within tokenPattern which matches the
* entire token, including the leading "${" and terminating "}" strings.
*/
private static final int TOKEN_GROUP = 3;
/**
* The index of the capturing group within tokenPattern which matches only
* the token name contained within the "${" and "}" strings.
*/
private static final int TOKEN_NAME_GROUP = 4;
/**
* The values of all known tokens.
*/
private final Map<String, String> tokenValues = new HashMap<String, String>();
/**
* Sets the token having the given name to the given value. Any existing
* value for that token is replaced.
*
* @param name
* The name of the token to set.
*
* @param value
* The value to set the token to.
*/
public void setToken(String name, String value) {
tokenValues.put(name, value);
}
/**
* Returns the value of the token with the given name, or null if no such
* token has been set.
*
* @param name
* The name of the token to return.
*
* @return
* The value of the token with the given name, or null if no such
* token exists.
*/
public String getToken(String name) {
return tokenValues.get(name);
}
/**
* Removes the value of the token with the given name. If no such token
* exists, this function has no effect.
*
* @param name
* The name of the token whose value should be removed.
*/
public void unsetToken(String name) {
tokenValues.remove(name);
}
/**
* Returns a map of all tokens, with each key being a token name, and each
* value being the corresponding token value. Changes to this map will
* directly affect the tokens associated with this filter.
*
* @return
* A map of all token names and their corresponding values.
*/
public Map<String, String> getTokens() {
return tokenValues;
}
/**
* Replaces all current token values with the contents of the given map,
* where each map key represents a token name, and each map value
* represents a token value.
*
* @param tokens
* A map containing the token names and corresponding values to
* assign.
*/
public void setTokens(Map<String, String> tokens) {
tokenValues.clear();
tokenValues.putAll(tokens);
}
/**
* Filters the given string, replacing any tokens with their corresponding
* values.
*
* @param input
* The string to filter.
*
* @return
* A copy of the input string, with any tokens replaced with their
* corresponding values.
*/
public String filter(String input) {
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);
// 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, interpret as literal
if (tokenValue == null) {
String notToken = tokenMatcher.group(TOKEN_GROUP);
output.append(notToken);
}
// Otherwise, substitute 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();
}
/**
* Given an arbitrary map containing String values, replace each non-null
* value with the corresponding filtered value.
*
* @param map
* The map whose values should be filtered.
*/
public void filterValues(Map<?, String> map) {
// For each map entry
for (Map.Entry<?, String> entry : map.entrySet()) {
// If value is non-null, filter value through this TokenFilter
String value = entry.getValue();
if (value != null)
entry.setValue(filter(value));
}
}
}