001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.multipart;
016    
017    import org.apache.hivemind.util.Defense;
018    
019    import javax.servlet.http.HttpServletRequest;
020    import javax.servlet.http.HttpServletRequestWrapper;
021    import java.util.Collections;
022    import java.util.Enumeration;
023    import java.util.Map;
024    
025    /**
026     * {@link javax.servlet.http.HttpServletRequest}  wrapper that provides
027     * access to the form field values uploaded in a multipart request.
028     * 
029     * @author Howard M. Lewis Ship
030     * @since 4.0
031     */
032    public class UploadFormParametersWrapper extends HttpServletRequestWrapper
033    {
034    
035        /**
036         * Map of {@link ValuePart} keyed on parameter name.
037         */
038        private Map _parameterMap;
039    
040        /**
041         * @param parameterMap
042         *            a map whose keys are parameter names and whose values are
043         *            arrays of Strings.
044         */
045        public UploadFormParametersWrapper(HttpServletRequest request, Map parameterMap)
046        {
047            super(request);
048    
049            Defense.notNull(parameterMap, "parameterMap");
050    
051            // add Parameter from the URL, typically added by JavaScript-URL-manipulation
052    
053            if (request.getParameterMap() != null)
054            {
055                parameterMap.putAll(request.getParameterMap());
056            }
057            
058            _parameterMap = Collections.unmodifiableMap(parameterMap );
059        }
060    
061        public String getParameter(String name)
062        {
063            String[] values = getParameterValues(name);
064    
065            return (values == null || values.length == 0) ? null : values[0];
066        }
067    
068        public Map getParameterMap()
069        {
070            return _parameterMap;
071        }
072    
073        public Enumeration getParameterNames()
074        {
075            return Collections.enumeration(_parameterMap.keySet());
076        }
077    
078        public String[] getParameterValues(String name)
079        {
080            return (String[]) _parameterMap.get(name);
081        }
082    
083        public String toString()
084        {
085            return "<UploadFormPartWrapper for " + getRequest() + ">";
086        }
087    }