/*
 * Copyright (c) 2022, WSO2 LLC. (http://www.wso2.com).
 *
 * WSO2 LLC. 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.wso2.carbon.identity.application.authentication.endpoint.util.client.model;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * Authentication endpoint request wrapper.
 */
public class AuthenticationRequestWrapper extends HttpServletRequestWrapper {

    private Map<String, Object> authParams;

    public AuthenticationRequestWrapper(HttpServletRequest request, Map<String, Object> authParams) {

        super(request);
        this.authParams = authParams;
        this.authParams.putAll(super.getParameterMap());
    }

    /**
     * Get the parameter from the map.
     *
     * @param name Name of the parameter.
     * @return
     */
    public String getParameter(String name) {

        if (authParams != null) {
            return (String) authParams.get(name);
        } else {
            return super.getParameter(name);
        }
    }

    /**
     * Get the parameter map.
     *
     * @return
     */
    public Map<String, Object> getParameterMap() {

        return authParams;
    }

    /**
     * Get the parameter names.
     *
     * @return
     */
    public Enumeration<String> getParameterNames() {

        return Collections.enumeration(authParams.keySet());
    }


    /**
     * Get values of a parameter from the map.
     *
     * @param name Name of the parameter.
     * @return
     */
    public String[] getParameterValues(String name) {


        return (String[]) authParams.get(name);
    }
}

@@ -794,16 +794,42 @@
 ration;%0A
+import java.util.HashMap;%0A
 import j
@@ -842,16 +842,16 @@
 il.Map;%0A
-
 %0Aimport 
@@ -1246,16 +1246,54 @@
 quest);%0A
+        if (authParams != null) %7B%0A    
         
@@ -1334,56 +1334,439 @@
     
-this.authParams.putAll(super.getParameterMap());
+%7D else %7B%0A            this.authParams = new HashMap%3C%3E();%0A        %7D%0A%0A        if (super.getParameterMap() != null) %7B%0A            Map%3CString, Object%3E superParams = super.getParameterMap();%0A            for (String paramName : superParams.keySet()) %7B%0A                if (super.getParameter(paramName) != null) %7B%0A                    this.authParams.put(paramName, super.getParameter(paramName));%0A                %7D%0A            %7D%0A        %7D%0A
 %0A   
@@ -1993,16 +1993,8 @@
 rn (
-String) 
 auth
@@ -1995,32 +1995,100 @@
  (authParams.get
+(name) != null) ? (String) authParams.get(name) : super.getParameter
 (name);%0A        
@@ -2290,25 +2290,424 @@
     
-return authParams
+Map%3CString, Object%3E paramMap;%0A        if (authParams != null) %7B%0A            paramMap = authParams;%0A            Map%3CString, Object%3E superParams = super.getParameterMap();%0A            for (String paramName : superParams.keySet()) %7B%0A                authParams.put(paramName, super.getParameter(paramName));%0A            %7D%0A        %7D else %7B%0A            paramMap = super.getParameterMap();%0A        %7D%0A        return paramMap
 ;%0A  

add request parameters to auth param map