/*
 * 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.auth.quickconnect.utility;

import java.lang.StringBuilder;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.guacamole.GuacamoleClientException;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.protocol.GuacamoleConfiguration;

/**
 * A utility class to parse out a URI into the settings necessary
 * to create and establish a Guacamole connection.
 */
public class QCParser {

    /**
     * The default protocol to parse to if one is provided in
     * the incoming URI..
     */
    public static final String DEFAULT_URI_PROTOCOL = "ssh";

    /**
     * The default host to use if one is not defined.
     */
    public static final String DEFAULT_URI_HOST = "localhost";

    /**
     * The regex to use to split username and password.
     */
    private static final Pattern userinfoPattern = Pattern.compile("(^[^:]+):?(.*)");

    /**
     * The regex group of the username.
     */
    private static final int USERNAME_GROUP = 1;

    /**
     * THe regex group of the password.
     */
    private static final int PASSWORD_GROUP = 2;

    /**
     * Parse out a URI string and get a connection from that string,
     * or an exception if the parsing fails.
     *
     * @param uri
     *     The string form of the URI to be parsed.
     *
     * @return
     *     A GuacamoleConfiguration using a combination of the parsed
     *     URI values and default values when not specified in the
     *     URI.
     *
     * @throws GuacamoleException
     *     When an error occurs parsing the URI.
     */
    public static GuacamoleConfiguration getConfiguration(String uri)
            throws GuacamoleException {

        URI qcUri;
        try {
            qcUri = new URI(uri);
        }
        catch (URISyntaxException e) {
            throw new GuacamoleClientException("Invalid URI Syntax", e);
        }
        String protocol = qcUri.getScheme();
        String host = qcUri.getHost();
        int port = qcUri.getPort();
        String userInfo = qcUri.getUserInfo();
        String query = qcUri.getQuery();
        String username = null;
        String password = null;
        List<String> paramList = null;

        if (protocol == null || protocol.isEmpty())
            protocol = DEFAULT_URI_PROTOCOL;

        if (host == null || host.isEmpty())
            host = DEFAULT_URI_HOST;

        if (query != null && !query.isEmpty())
            paramList = Arrays.asList(query.split("&"));

        if (userInfo != null && !userInfo.isEmpty()) {

            Matcher userinfoMatcher = userinfoPattern.matcher(userInfo);
            if (userinfoMatcher.matches()) {
                username = userinfoMatcher.group(USERNAME_GROUP);
                password = userinfoMatcher.group(PASSWORD_GROUP);
            }

        }

        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 (paramList != null) {
            for (String parameter : paramList) {
                String[] paramArray = parameter.split("=", 2);
                qcConfig.setParameter(paramArray[0],paramArray[1]);
            }
        }

        return qcConfig;
        
    }

    /**
     * Given a GuacamoleConfiguration object, generate a name
     * for the configuration based on the protocol, host, user
     * and port in the configuration, and return the string value.
     *
     * @param config
     *     The GuacamoleConfiguration object to use to generate
     *     the name.
     *
     * @return
     *     The String value of the name that is generated.
     *
     * @throws GuacamoleException
     *     If an error occurs getting items in the configuration.
     */
    public static String getName(GuacamoleConfiguration config)
            throws GuacamoleException {

        if (config == null)
            return null;

        String protocol = config.getProtocol();
        String host = config.getParameter("hostname");
        String port = config.getParameter("port");
        String user = config.getParameter("username");

        StringBuilder name = new StringBuilder("");

        if (protocol != null && !protocol.isEmpty())
            name.append(protocol).append("://");

        if (user != null && !user.isEmpty())
            name.append(user).append("@");

        if (host != null && !host.isEmpty())
            name.append(host);

        if (port != null && !port.isEmpty())
            name.append(":").append(port);

        name.append("/");

        return name.toString();
    }

}

@@ -859,16 +859,61 @@
 ility;%0A%0A
+import java.io.UnsupportedEncodingException;%0A
 import j
@@ -932,24 +932,24 @@
 ingBuilder;%0A
-
 import java.
@@ -985,32 +985,60 @@
 yntaxException;%0A
+import java.net.URLDecoder;%0A
 import java.util
@@ -1179,32 +1179,86 @@
 lientException;%0A
+import org.apache.guacamole.GuacamoleServerException;%0A
 import org.apach
@@ -1349,16 +1349,73 @@
 uration;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0A/**%0A *
@@ -1533,16 +1533,16 @@
 on.%0A */%0A
-
 public c
@@ -1554,24 +1554,153 @@
 QCParser %7B%0A%0A
+    /**%0A     * Logger for this class.%0A     */%0A    private final static Logger logger = LoggerFactory.getLogger(QCParser.class);%0A%0A
     /**%0A    
@@ -4707,59 +4707,462 @@
     
-qcConfig.setParameter(paramArray%5B0%5D,paramArray%5B1%5D);
+try %7B%0A                    qcConfig.setParameter(URLDecoder.decode(paramArray%5B0%5D, %22UTF-8%22),%0A                                          URLDecoder.decode(paramArray%5B1%5D, %22UTF-8%22));%0A                %7D%0A                catch (UnsupportedEncodingException e) %7B%0A                    logger.error(%22Unexpected lack of UTF-8 encoding support.%22);%0A                    throw new GuacamoleServerException(%22Unexpected lack of UTF-8 encoding support.%22, e);%0A                %7D
 %0A   

Handle encoded query parameters.