GUACAMOLE-1006: Merge support for reading any property value as a Collection.

This commit is contained in:
Mike Jumper
2024-08-30 10:44:51 -07:00
committed by GitHub
26 changed files with 517 additions and 143 deletions

View File

@@ -20,6 +20,7 @@
package org.apache.guacamole.environment;
import java.io.File;
import java.util.Collection;
import java.util.Map;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.auth.GuacamoleProxyConfiguration;
@@ -73,11 +74,35 @@ public class DelegatingEnvironment implements Environment {
public <Type> Type getProperty(GuacamoleProperty<Type> property, Type defaultValue) throws GuacamoleException {
return environment.getProperty(property, defaultValue);
}
@Override
public <Type> Collection<Type> getPropertyCollection(GuacamoleProperty<Type> property)
throws GuacamoleException {
return environment.getPropertyCollection(property);
}
@Override
public <Type> Collection<Type> getPropertyCollection(GuacamoleProperty<Type> property,
Type defaultValue) throws GuacamoleException {
return environment.getPropertyCollection(property, defaultValue);
}
@Override
public <Type> Collection<Type> getPropertyCollection(GuacamoleProperty<Type> property,
Collection<Type> defaultValue) throws GuacamoleException {
return environment.getPropertyCollection(property, defaultValue);
}
@Override
public <Type> Type getRequiredProperty(GuacamoleProperty<Type> property) throws GuacamoleException {
return environment.getRequiredProperty(property);
}
@Override
public <Type> Collection<Type> getRequiredPropertyCollection(GuacamoleProperty<Type> property)
throws GuacamoleException {
return environment.getRequiredPropertyCollection(property);
}
@Override
public GuacamoleProxyConfiguration getDefaultGuacamoleProxyConfiguration() throws GuacamoleException {

View File

@@ -21,6 +21,8 @@ package org.apache.guacamole.environment;
import org.apache.guacamole.properties.GuacamoleProperties;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleUnsupportedException;
@@ -102,13 +104,18 @@ public interface Environment {
* Given a GuacamoleProperty, parses and returns the value set for that
* property in guacamole.properties, if any.
*
* @param <Type> The type that the given property is parsed into.
* @param property The property to read from guacamole.properties.
* @return The parsed value of the property as read from
* guacamole.properties.
* @throws GuacamoleException If an error occurs while parsing the value
* for the given property in
* guacamole.properties.
* @param <Type>
* The type that the given property is parsed into.
*
* @param property
* The property to read from guacamole.properties.
*
* @return
* The parsed value of the property as read from guacamole.properties.
*
* @throws GuacamoleException
* If an error occurs while parsing the value for the given property in
* guacamole.properties.
*/
public <Type> Type getProperty(GuacamoleProperty<Type> property)
throws GuacamoleException;
@@ -118,20 +125,161 @@ public interface Environment {
* property in guacamole.properties, if any. If no value is found, the
* provided default value is returned.
*
* @param <Type> The type that the given property is parsed into.
* @param property The property to read from guacamole.properties.
* @param defaultValue The value to return if no value was given in
* guacamole.properties.
* @return The parsed value of the property as read from
* guacamole.properties, or the provided default value if no value
* was found.
* @throws GuacamoleException If an error occurs while parsing the value
* for the given property in
* guacamole.properties.
* @param <Type>
* The type that the given property is parsed into.
*
* @param property
* The property to read from guacamole.properties.
*
* @param defaultValue
* The value to return if no value was given in guacamole.properties.
*
* @return
* The parsed value of the property as read from guacamole.properties,
* or the provided default value if no value was found.
*
* @throws GuacamoleException
* If an error occurs while parsing the value for the given property in
* guacamole.properties.
*/
public <Type> Type getProperty(GuacamoleProperty<Type> property,
Type defaultValue) throws GuacamoleException;
/**
* Given a GuacamoleProperty, parses and returns a sorted Collection of the
* value set for that property in guacamole.properties, if any. The
* implementation of parsing and returning a collection of multiple
* values is up to the individual property implementations, and not all
* implementations will support reading and returning multiple values.
*
* @param <Type>
* The type that the given property is parsed into.
*
* @param property
* The property to read from guacamole.properties.
*
* @return
* A sorted collection of the the parsed values of the property as read
* from guacamole.properties.
*
* @throws GuacamoleException
* If an error occurs while parsing the value for the given property in
* guacamole.properties.
*/
public default <Type> Collection<Type> getPropertyCollection(
GuacamoleProperty<Type> property) throws GuacamoleException {
/* Pull the given property as a string. */
StringGuacamoleProperty stringProperty = new StringGuacamoleProperty() {
@Override
public String getName() { return property.getName(); }
};
/* Parse the string to a Collection of the desired type. */
return property.parseValueCollection(getProperty(stringProperty));
}
/**
* Given a GuacamoleProperty, parses and returns the value set for that
* property in guacamole.properties, if any. If no value is found, a
* Collection is returned with the provided default value. The
* implementation of parsing and returning a collection of multiple
* values is up to the individual property implementations, and not all
* implementations will support reading and returning multiple values.
*
* @param <Type>
* The type that the given property is parsed into.
*
* @param property
* The property to read from guacamole.properties.
*
* @param defaultValue
* The single value to return in the Collection if no value was given
* in guacamole.properties.
*
* @return
* A sorted collection of the the parsed values of the property as read
* from guacamole.properties, or a Collection with the single default
* value provided.
*
* @throws GuacamoleException
* If an error occurs while parsing the value for the given property in
* guacamole.properties.
*/
public default <Type> Collection<Type> getPropertyCollection(
GuacamoleProperty<Type> property, Type defaultValue)
throws GuacamoleException {
/* Pull the given property as a string. */
StringGuacamoleProperty stringProperty = new StringGuacamoleProperty() {
@Override
public String getName() { return property.getName(); }
};
/* Check the value and return the default if null. */
String stringValue = getProperty(stringProperty);
if (stringValue == null)
return Collections.singletonList(defaultValue);
/* Parse the string and return the collection. */
return property.parseValueCollection(stringValue);
}
/**
* Given a GuacamoleProperty, parses and returns the value set for that
* property in guacamole.properties, if any. If no value is found, the
* provided Collection of default values is returned. The
* implementation of parsing and returning a collection of multiple
* values is up to the individual property implementations, and not all
* implementations will support reading and returning multiple values.
*
* @param <Type>
* The type that the given property is parsed into.
*
* @param property
* The property to read from guacamole.properties.
*
* @param defaultValue
* The Collection of values to return in the Collection if no value was
* given in guacamole.properties.
*
* @return
* A sorted collection of the the parsed values of the property as read
* from guacamole.properties, or a Collection with the single default
* value provided.
*
* @throws GuacamoleException
* If an error occurs while parsing the value for the given property in
* guacamole.properties.
*/
public default <Type> Collection<Type> getPropertyCollection(
GuacamoleProperty<Type> property, Collection<Type> defaultValue)
throws GuacamoleException {
/* Pull the given property as a string. */
StringGuacamoleProperty stringProperty = new StringGuacamoleProperty() {
@Override
public String getName() { return property.getName(); }
};
/* Check the value and return the default if null. */
String stringValue = getProperty(stringProperty);
if (stringValue == null)
return defaultValue;
/* Parse the string and return the collection. */
return property.parseValueCollection(stringValue);
}
/**
* Given a GuacamoleProperty, parses and returns the value set for that
* property in guacamole.properties. An exception is thrown if the value
@@ -148,6 +296,43 @@ public interface Environment {
*/
public <Type> Type getRequiredProperty(GuacamoleProperty<Type> property)
throws GuacamoleException;
/**
* Given a GuacamoleProperty, parses and returns a sorted Collection of
* values for that property in guacamole.properties. An exception is thrown
* if the value is not provided. The implementation of parsing and returning
* a collection of multiple values is up to the individual property
* implementations, and not all implementations will support reading and
* returning multiple values.
*
* @param <Type>
* The type that the given property is parsed into.
*
* @param property
* The property to read from guacamole.properties.
*
* @return
* A sorted Collection of the property as read from guacamole.properties.
*
* @throws GuacamoleException
* If an error occurs while parsing the value for the given property in
* guacamole.properties, or if the property is not specified.
*/
public default <Type> Collection<Type> getRequiredPropertyCollection(
GuacamoleProperty<Type> property) throws GuacamoleException {
/* Pull the given property as a string. */
StringGuacamoleProperty stringProperty = new StringGuacamoleProperty() {
@Override
public String getName() { return property.getName(); }
};
/* Parse the string to a Collection of the desired type. */
return property.parseValueCollection(getRequiredProperty(stringProperty));
}
/**
* Returns the connection information which should be used, by default, to

View File

@@ -25,6 +25,8 @@ import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -353,6 +355,38 @@ public class LocalEnvironment implements Environment {
return value;
}
@Override
public <Type> Collection<Type> getPropertyCollection(GuacamoleProperty<Type> property)
throws GuacamoleException {
return property.parseValueCollection(getPropertyValue(property.getName()));
}
@Override
public <Type> Collection<Type> getPropertyCollection(GuacamoleProperty<Type> property,
Type defaultValue) throws GuacamoleException {
Collection<Type> value = getPropertyCollection(property);
if (value == null)
return Collections.singletonList(defaultValue);
return value;
}
@Override
public <Type> Collection<Type> getPropertyCollection(GuacamoleProperty<Type> property,
Collection<Type> defaultValue) throws GuacamoleException {
Collection<Type> value = getPropertyCollection(property);
if (value == null)
return defaultValue;
return value;
}
@Override
public <Type> Type getRequiredProperty(GuacamoleProperty<Type> property)
@@ -365,6 +399,18 @@ public class LocalEnvironment implements Environment {
return value;
}
@Override
public <Type> Collection<Type> getRequiredPropertyCollection(GuacamoleProperty<Type> property)
throws GuacamoleException {
Collection<Type> value = getPropertyCollection(property);
if (value == null)
throw new GuacamoleServerException("Property " + property.getName() + " is required.");
return value;
}
@Override
public Map<String, ProtocolInfo> getProtocols() {

View File

@@ -20,6 +20,9 @@
package org.apache.guacamole.properties;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.guacamole.GuacamoleException;
/**
@@ -37,5 +40,27 @@ public abstract class FileGuacamoleProperty implements GuacamoleProperty<File> {
return new File(value);
}
@Override
public List<File> parseValueCollection(String value) throws GuacamoleException {
// If no property is provided, return null.
if (value == null)
return null;
// Split string into a list of individual values
List<String> stringValues = Arrays.asList(DELIMITER_PATTERN.split(value));
if (stringValues.isEmpty())
return null;
// Translate values to Files and add to result array.
List<File> fileValues = new ArrayList<>();
for (String stringFile : stringValues) {
fileValues.add(new File(stringFile));
}
return fileValues;
}
}

View File

@@ -19,6 +19,9 @@
package org.apache.guacamole.properties;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Pattern;
import org.apache.guacamole.GuacamoleException;
/**
@@ -30,12 +33,21 @@ import org.apache.guacamole.GuacamoleException;
*/
public interface GuacamoleProperty<Type> {
/**
* A pattern which matches against the delimiters between values. This is
* currently simply a semicolon and any following whitespace. Parts of the
* input string which match this pattern will not be included in the parsed
* result.
*/
static final Pattern DELIMITER_PATTERN = Pattern.compile(";\\s*");
/**
* Returns the name of the property in guacamole.properties that this
* GuacamoleProperty will parse.
*
* @return The name of the property in guacamole.properties that this
* GuacamoleProperty will parse.
* @return
* The name of the property in guacamole.properties that this
* GuacamoleProperty will parse.
*/
public String getName();
@@ -43,11 +55,37 @@ public interface GuacamoleProperty<Type> {
* Parses the given string value into the type associated with this
* GuacamoleProperty.
*
* @param value The string value to parse.
* @return The parsed value.
* @throws GuacamoleException If an error occurs while parsing the
* provided value.
* @param value
* The string value to parse.
*
* @return
* The parsed value.
*
* @throws GuacamoleException
* If an error occurs while parsing the provided value.
*/
public Type parseValue(String value) throws GuacamoleException;
/**
* Parses the given string value into a Collection of values of the type
* associated with this GuacamoleProperty. The default implementation
* simply returns a list containing a single item as parsed by the
* parseValue method.
*
* @param value
* The string value to parse.
*
* @return
* A sorted Collection of the parsed values.
*
* @throws GuacamoleException
* If an error occurs while parsing the provided value.
*/
default public Collection<Type> parseValueCollection(String value)
throws GuacamoleException {
return Collections.singletonList(parseValue(value));
}
}

View File

@@ -19,6 +19,9 @@
package org.apache.guacamole.properties;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleServerException;
@@ -42,5 +45,26 @@ public abstract class IntegerGuacamoleProperty implements GuacamoleProperty<Inte
}
}
@Override
public List<Integer> parseValueCollection(String value) throws GuacamoleException {
if (value == null)
return null;
// Split string into a list of individual values
List<String> stringValues = Arrays.asList(DELIMITER_PATTERN.split(value));
if (stringValues.isEmpty())
return null;
// Translate values to Integers, validating along the way.
List<Integer> intValues = new ArrayList<>();
for (String stringInt : stringValues) {
intValues.add(parseValue(stringInt));
}
return intValues;
}
}

View File

@@ -19,6 +19,9 @@
package org.apache.guacamole.properties;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleServerException;
@@ -42,5 +45,26 @@ public abstract class LongGuacamoleProperty implements GuacamoleProperty<Long> {
}
}
@Override
public List<Long> parseValueCollection(String value) throws GuacamoleException {
if (value == null)
return null;
// Split string into a list of individual values
List<String> stringValues = Arrays.asList(DELIMITER_PATTERN.split(value));
if (stringValues.isEmpty())
return null;
// Translate values to Longs, validating along the way.
List<Long> longValues = new ArrayList<>();
for (String stringLong : stringValues) {
longValues.add(parseValue(stringLong));
}
return longValues;
}
}

View File

@@ -19,6 +19,8 @@
package org.apache.guacamole.properties;
import java.util.Arrays;
import java.util.List;
import org.apache.guacamole.GuacamoleException;
/**
@@ -30,5 +32,20 @@ public abstract class StringGuacamoleProperty implements GuacamoleProperty<Strin
public String parseValue(String value) throws GuacamoleException {
return value;
}
@Override
public List<String> parseValueCollection(String value) throws GuacamoleException {
if (value == null)
return null;
// Split string into a list of individual values
List<String> stringValues = Arrays.asList(DELIMITER_PATTERN.split(value));
if (stringValues.isEmpty())
return null;
return stringValues;
}
}

View File

@@ -31,17 +31,14 @@ import org.apache.guacamole.GuacamoleException;
* compatibility with the behavior of Java properties in general, only
* whitespace at the beginning of each value is ignored; trailing whitespace
* becomes part of the value.
*
* @deprecated
* This class is now deprecated in favor of using the StringGuacamoleProperty
* class with the parseValueCollection method.
*/
@Deprecated
public abstract class StringListProperty implements GuacamoleProperty<List<String>> {
/**
* A pattern which matches against the delimiters between values. This is
* currently simply a comma and any following whitespace. Parts of the
* input string which match this pattern will not be included in the parsed
* result.
*/
private static final Pattern DELIMITER_PATTERN = Pattern.compile(",\\s*");
@Override
public List<String> parseValue(String values) throws GuacamoleException {

View File

@@ -19,6 +19,9 @@
package org.apache.guacamole.properties;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Pattern;
import org.apache.guacamole.GuacamoleException;
@@ -57,4 +60,25 @@ public abstract class TimeZoneGuacamoleProperty
}
@Override
public List<TimeZone> parseValueCollection(String value) throws GuacamoleException {
if (value == null)
return null;
// Split string into a list of individual values
List<String> stringValues = Arrays.asList(DELIMITER_PATTERN.split(value));
if (stringValues.isEmpty())
return null;
// Translate values to Integers, validating along the way.
List<TimeZone> tzValues = new ArrayList<>();
for (String stringTz : stringValues) {
tzValues.add(parseValue(stringTz));
}
return tzValues;
}
}

View File

@@ -21,6 +21,9 @@ package org.apache.guacamole.properties;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleServerException;
@@ -46,4 +49,26 @@ public abstract class URIGuacamoleProperty implements GuacamoleProperty<URI> {
}
@Override
public List<URI> parseValueCollection(String value) throws GuacamoleException {
// Nothing provided, return nothing.
if (value == null)
return null;
// Split string into a list of individual values
List<String> stringValues = Arrays.asList(DELIMITER_PATTERN.split(value));
if (stringValues.isEmpty())
return null;
// Translate values to URIs, validating along the way.
List<URI> uriValues = new ArrayList<>();
for (String stringUri : stringValues) {
uriValues.add(parseValue(stringUri));
}
return uriValues;
}
}