GUACAMOLE-38: Rearrange logic in QCParser class.

This commit is contained in:
Nick Couchman
2018-05-09 08:40:55 -04:00
parent 7df88cdfbd
commit 2a9c7fe0b7

View File

@@ -85,6 +85,7 @@ public class QCParser {
public static GuacamoleConfiguration getConfiguration(String uri) public static GuacamoleConfiguration getConfiguration(String uri)
throws GuacamoleException { throws GuacamoleException {
// Parse the URI object from provided string.
URI qcUri; URI qcUri;
try { try {
qcUri = new URI(uri); qcUri = new URI(uri);
@@ -100,22 +101,32 @@ public class QCParser {
String userInfo = qcUri.getUserInfo(); String userInfo = qcUri.getUserInfo();
String query = qcUri.getQuery(); String query = qcUri.getQuery();
String username = null; // Generate a new GuacamoleConfiguration
String password = null; GuacamoleConfiguration qcConfig = new GuacamoleConfiguration();
Map<String, String> queryParams = null;
// Assign default protocol if one is not found in the URI. // Check for provided protocol or use default
if (protocol == null || protocol.isEmpty()) if (protocol != null && !protocol.isEmpty())
protocol = DEFAULT_URI_PROTOCOL; qcConfig.setProtocol(protocol);
else
qcConfig.setProtocol(DEFAULT_URI_PROTOCOL);
// Assign default host if one is not found in the URI. // Check for provided port number
if (host == null || host.isEmpty()) if (port > 0)
host = DEFAULT_URI_HOST; qcConfig.setParameter("port", Integer.toString(port));
// Check for provided host or use default
if (host != null && !host.isEmpty())
qcConfig.setParameter("hostname", host);
else
qcConfig.setParameter("hostname", DEFAULT_URI_HOST);
// Look for extra query parameters and parse them out. // Look for extra query parameters and parse them out.
if (query != null && !query.isEmpty()) { if (query != null && !query.isEmpty()) {
try { try {
queryParams = parseQueryString(query); Map<String, String> queryParams = parseQueryString(query);
if (queryParams != null)
for (Map.Entry<String, String> entry: queryParams.entrySet())
qcConfig.setParameter(entry.getKey(), entry.getValue());
} }
catch (UnsupportedEncodingException e) { catch (UnsupportedEncodingException e) {
throw new GuacamoleServerException("Unexpected lack of UTF-8 encoding support.", e); throw new GuacamoleServerException("Unexpected lack of UTF-8 encoding support.", e);
@@ -127,30 +138,18 @@ public class QCParser {
Matcher userinfoMatcher = userinfoPattern.matcher(userInfo); Matcher userinfoMatcher = userinfoPattern.matcher(userInfo);
if (userinfoMatcher.matches()) { if (userinfoMatcher.matches()) {
username = userinfoMatcher.group(USERNAME_GROUP); String username = userinfoMatcher.group(USERNAME_GROUP);
password = userinfoMatcher.group(PASSWORD_GROUP); String password = userinfoMatcher.group(PASSWORD_GROUP);
if (username != null && !username.isEmpty())
qcConfig.setParameter("username", username);
if (password != null && !password.isEmpty())
qcConfig.setParameter("password", password);
} }
} }
// Generate a new GuacamoleConfiguration and set parameters.
GuacamoleConfiguration qcConfig = new GuacamoleConfiguration();
qcConfig.setProtocol(protocol);
qcConfig.setParameter("hostname",host);
if (port > 0)
qcConfig.setParameter("port", Integer.toString(port));
if (username != null && !username.isEmpty())
qcConfig.setParameter("username", username);
if (password != null && !password.isEmpty())
qcConfig.setParameter("password", password);
if (queryParams != null)
for (Map.Entry<String, String> entry : queryParams.entrySet())
qcConfig.setParameter(entry.getKey(), entry.getValue());
return qcConfig; return qcConfig;
} }