Skip to content

Commit

Permalink
GUACAMOLE-1006: Provide implementations for List properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
necouchman committed Apr 2, 2020
1 parent 1ede126 commit 4d49d06
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.guacamole.properties;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.guacamole.GuacamoleException;

/**
* A GuacamoleProperty whose value is a List of Strings. The string value
* parsed to produce this list is a semicolon-delimited list. Duplicate values
* are ignored, as is any whitespace following delimiters. To maintain
* 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.
*/
public abstract class StringListGuacamoleProperty
implements GuacamoleProperty<List<String>> {

/**
* 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.
*/
private static final Pattern DELIMITER_PATTERN = Pattern.compile(";\\s*");

@Override
public List<String> parseValue(String values) throws GuacamoleException {

// If no property provided, return null.
if (values == null)
return null;

// Split string into a list of individual values
List<String> stringValues = Arrays.asList(DELIMITER_PATTERN.split(values));
if (stringValues.isEmpty())
return null;

return stringValues;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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 java.util.regex.Pattern;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleServerException;

/**
* A GuacamoleProperty whose value is a List of URIs. The string value
* parsed to produce this list is a semicolon-delimited list.
* Duplicate values are ignored, as is any whitespace following delimiters.
* To maintain 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.
*/
public abstract class URIListGuacamoleProperty
implements GuacamoleProperty<List<URI>> {

/**
* 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.
*/
private static final Pattern DELIMITER_PATTERN = Pattern.compile(";\\s*");

@Override
public List<URI> parseValue(String values) throws GuacamoleException {

// If no property provided, return null.
if (values == null)
return null;

// Split string into a list of individual values
List<String> stringValues = Arrays.asList(DELIMITER_PATTERN.split(values));
if (stringValues.isEmpty())
return null;

List<URI> uriValues = new ArrayList<>();
for (String stringUri : stringValues) {
try {
uriValues.add(new URI(stringUri));
}
catch (URISyntaxException e) {
throw new GuacamoleServerException("Value \"" + stringUri +
"\" is not a valid URI.");
}
}

return uriValues;

}

}

0 comments on commit 4d49d06

Please sign in to comment.