/*
 * 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").isEmpty()) {
            this.remoteHost = request.getHeader("X-Guacamole-Client-Hostname");
        } else if(request.getHeader("X-Forwarded-For") != null && !request.getHeader("X-Forwarded-For").isEmpty()) {
            this.remoteHost = request.getHeader("X-Forwarded-For");
        } else if(request.getRemoteHost() != null && !request.getRemoteHost().isEmpty()) {
            this.remoteHost = request.getRemoteHost();
        } else {
            this.remoteHost = null;
        }

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

        // 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;
    }

}

@@ -2280,32 +2280,33 @@
 info.%0A        if
+ 
 (request.getHead
@@ -2402,34 +2402,32 @@
 ame%22).isEmpty())
- %7B
 %0A            thi
@@ -2494,34 +2494,32 @@
 e%22);%0A       
- %7D
  else if
 (request.get
@@ -2498,32 +2498,33 @@
 %0A        else if
+ 
 (request.getHead
@@ -2596,34 +2596,32 @@
 For%22).isEmpty())
- %7B
 %0A            thi
@@ -2676,34 +2676,32 @@
 r%22);%0A       
- %7D
  else if
 (request.get
@@ -2680,32 +2680,33 @@
 %0A        else if
+ 
 (request.getRemo
@@ -2752,34 +2752,32 @@
 ost().isEmpty())
- %7B
 %0A            thi
@@ -2819,33 +2819,29 @@
 t();%0A       
- %7D
  else
- %7B
 %0A           
@@ -2860,34 +2860,24 @@
 Host = null;
-%0A        %7D
 %0A%0A        if
@@ -2876,16 +2876,17 @@
       if
+ 
 (request
@@ -2978,34 +2978,32 @@
 -IP%22).isEmpty())
- %7B
 %0A            thi
@@ -3060,34 +3060,32 @@
 nt-IP%22);%0A       
- %7D
  else if(request
@@ -3165,34 +3165,32 @@
 For%22).isEmpty())
- %7B
 %0A            thi
@@ -3245,26 +3245,24 @@
 r%22);%0A       
- %7D
  else if(req
@@ -3328,18 +3328,16 @@
 Empty())
- %7B
 %0A       
@@ -3395,17 +3395,13 @@
     
- %7D
  else
- %7B
 %0A   
@@ -3432,26 +3432,16 @@
  = null;
-%0A        %7D
 %0A%0A      

Fix code style issues.