/*
 * Copyright 2003,2004 The Apache Software Foundation.
 * 
 * Licensed 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.pluto.driver.core;

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

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

import org.apache.pluto.PortletWindow;
import org.apache.pluto.driver.url.PortalURLParameter;
import org.apache.pluto.driver.url.PortalURL;

public class PortalServletRequest extends HttpServletRequestWrapper {

    private PortletWindow portletWindow = null;

    private PortalURL url;
    private Map portletParameters;

    public PortalServletRequest(HttpServletRequest request,
                                PortletWindow window) {
        super(request);
        this.portletWindow = window;

        url =
        PortalRequestContext.getContext(request).getRequestedPortalURL();
    }


// HttpServletRequestWrapper overlay

    public java.lang.String getContentType() {
        String contentType = super.getContentType();
        return contentType;
    }

// ServletRequestWrapper overlay

    public String getParameter(String name) {
        String[] values = (String[]) this.getParameterMap().get(name);
        if (values != null) {
            return values[0];
        }
        return null;
    }

    /**
     * Retreive the Parameters.
     * @return Map of parameters targeted to the window associated with this
     *         request.
     */
    public Map getParameterMap() {
        if (portletParameters == null) {
            initParameterMap();
        }
        return Collections.unmodifiableMap(portletParameters);
    }

    /**
     * Initialize parameters for this request.  We must be careful to make sure
     * that render parameters are only made available if they were targeted for
     * this specific window.
     */
    private void initParameterMap() {
        portletParameters = new HashMap();

        Iterator iterator = url.getParameters().iterator();
        while (iterator.hasNext()) {
            PortalURLParameter param = (PortalURLParameter) iterator.next();
            String name = param.getName();
            String[] values = param.getValues();
            if (param.getWindowId().equals(portletWindow.getId().getStringId())) {
                portletParameters.put(name, values);
            }
        }

        String id = portletWindow.getId().getStringId();
        if (portletWindow.getId().getStringId().equals(id)) {
            Enumeration params = super.getParameterNames();
            while (params.hasMoreElements()) {
                String name = params.nextElement().toString();
                String[] values = super.getParameterValues(name);
                if (portletParameters.containsKey(name)) {
                    String[] temp = (String[]) portletParameters.get(name);
                    String[] all = new String[values.length + temp.length];
                    System.arraycopy(values, 0, all, 0, values.length);
                    System.arraycopy(temp, 0, all, values.length, temp.length);
                }
                portletParameters.put(name, values);
            }
        }
    }

    /**
     * Get an enumeration which contains each of the names for which parameters
     * exist.
     * @return an enumeration of all names bound as parameters.
     */
    public Enumeration getParameterNames() {
        return Collections.enumeration(getParameterMap().keySet());
    }

    /**
     * Get the values associated with the given parameter key.
     * @param name the Parameter name used to key the parameter.
     * @return a String[] of all values bound to the given name as a parameter.
     */
    public String[] getParameterValues(String name) {
        return (String[]) getParameterMap().get(name);
    }

}



@@ -1065,79 +1065,251 @@
 L;%0D%0A
-%0D%0Apublic class PortalServletRequest extends HttpServletRequestWrapper %7B
+import org.apache.commons.logging.Log;%0D%0Aimport org.apache.commons.logging.LogFactory;%0D%0A%0D%0Apublic class PortalServletRequest extends HttpServletRequestWrapper %7B%0D%0A%0D%0A    private static final Log LOG = LogFactory.getLog(PortalServletRequest.class);
 %0D%0A%0D%0A
@@ -3241,53 +3241,261 @@
     
-String id = portletWindow.getId().getStringId
+// Currently this request is only used for rendering.%0D%0A        // Because of that, this will never be used, however, it's%0D%0A        // being left in since this request's scope may expand at some%0D%0A        // point!%0D%0A        String id = url.getActionWindow
 ();%0D
@@ -4260,24 +4260,206 @@
 %0D%0A        %7D%0D
+%0A%0D%0A        if(LOG.isDebugEnabled()) %7B%0D%0A            LOG.debug(%22Initialized request parameter map for window: '%22%0D%0A                +portletWindow.getId().getStringId()+%22'%22);%0D%0A        %7D%0D
 %0A    %7D%0D%0A%0D%0A  

Cleaning up evaluation which is always true to express the original intent. PLUTO-261