/*
 * 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.rest;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.ws.rs.core.MultivaluedMap;

/**
 * Wrapper for HttpServletRequest which uses a given MultivaluedMap to provide
 * the values of all request parameters.
 * 
 * @author Michael Jumper
 */
public class APIRequest extends HttpServletRequestWrapper {

    /**
     * Map of all request parameter names to their corresponding values.
     */
    private final Map<String, String[]> parameters;

    /**
     * The remote hostname that initiated the request.
     */
    private final String remoteHost;

    /**
     * The remote ip address that initiated the request.
     */
    private final String remoteAddr;

    /**
     * Wraps the given HttpServletRequest, using the given MultivaluedMap to
     * provide all request parameters. All HttpServletRequest functions which
     * do not deal with parameter names and values are delegated to the wrapped
     * request.
     *
     * @param request
     *     The HttpServletRequest to wrap.
     *
     * @param parameters
     *     All request parameters.
     */
    public APIRequest(HttpServletRequest request,
            MultivaluedMap<String, String> parameters) {

        super(request);

        // Try a few methods to get client info.
        if(request.getHeader("X-Guacamole-Client-Hostname") != null && request.getHeader("X-Guacamole-Client-Hostname") != "") {
            this.remoteHost = request.getHeader("X-Guacamole-Client-Hostname");
        } else if(request.getHeader("X-Forwarded-For") != null && request.getHeader("X-Forwarded-For") != "") {
            this.remoteHost = request.getHeader("X-Forwarded-For");
        } else if(request.getRemoteHost() != null && request.getRemoteHost() != "") {
            this.remoteHost = request.getRemoteHost();
        } else {
            this.remoteHost = "";
        }

        if(request.getHeader("X-Guacamole-Client-IP") != null && request.getHeader("X-Guacamole-Client-IP") != "") {
            this.remoteAddr = request.getHeader("X-Guacamole-Client-IP");
        } else if(request.getHeader("X-Forwarded-For") != null && request.getHeader("X-Forwarded-For") != "") {
            this.remoteAddr = request.getHeader("X-Forwarded-For");
        } else if(request.getRemoteHost() != null && request.getRemoteAddr() != "") {
            this.remoteAddr = request.getRemoteAddr();
        } else {
            this.remoteAddr = "";
        }

        // Copy parameters from given MultivaluedMap 
        this.parameters = new HashMap<String, String[]>(parameters.size());
        for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {

            // Get parameter name and all corresponding values
            String name = entry.getKey();
            List<String> values = entry.getValue();

            // Add parameters to map
            this.parameters.put(name, values.toArray(new String[values.size()]));
            
        }
        
    }

    @Override
    public String[] getParameterValues(String name) {
        return parameters.get(name);
    }

    @Override
    public Enumeration<String> getParameterNames() {
        return Collections.enumeration(parameters.keySet());
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return Collections.unmodifiableMap(parameters);
    }

    @Override
    public String getParameter(String name) {

        // If no such parameter exists, just return null
        String[] values = getParameterValues(name);
        if (values == null)
            return null;

        // Otherwise, return first value
        return values[0];

    }

    @Override
    public String getRemoteHost() {
        return this.remoteHost;
    }

    @Override
    public String getRemoteAddr() {
        return this.remoteAddr;
    }

}

@@ -2341,32 +2341,33 @@
 me%22) != null && 
+!
 request.getHeade
@@ -2398,22 +2398,26 @@
 stname%22)
- != %22%22
+.isEmpty()
 ) %7B%0A    
@@ -2550,32 +2550,33 @@
 or%22) != null && 
+!
 request.getHeade
@@ -2587,38 +2587,42 @@
 -Forwarded-For%22)
- != %22%22
+.isEmpty()
 ) %7B%0A            
@@ -2722,32 +2722,33 @@
 st() != null && 
+!
 request.getRemot
@@ -2754,22 +2754,26 @@
 teHost()
- != %22%22
+.isEmpty()
 ) %7B%0A    
@@ -2870,18 +2870,20 @@
 eHost = 
-%22%22
+null
 ;%0A      
@@ -2944,32 +2944,33 @@
 IP%22) != null && 
+!
 request.getHeade
@@ -2995,22 +2995,26 @@
 ent-IP%22)
- != %22%22
+.isEmpty()
 ) %7B%0A    
@@ -3141,32 +3141,33 @@
 or%22) != null && 
+!
 request.getHeade
@@ -3186,22 +3186,26 @@
 ed-For%22)
- != %22%22
+.isEmpty()
 ) %7B%0A    
@@ -3321,16 +3321,17 @@
 null && 
+!
 request.
@@ -3349,14 +3349,18 @@
 dr()
- != %22%22
+.isEmpty()
 ) %7B%0A
@@ -3465,10 +3465,12 @@
 r = 
-%22%22
+null
 ;%0A  

Fix checking for empty strings; Set tokens to null if nothing found.