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 java.util.Collections;
018    import java.util.Enumeration;
019    import java.util.Map;
020    
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.http.HttpServletRequestWrapper;
023    
024    import org.apache.hivemind.util.Defense;
025    
026    /**
027     * {@link javax.servlet.http.HttpServletRequest}  wrapper that provides access to the form
028     * field values uploaded in a multipart request.
029     * 
030     * @author Howard M. Lewis Ship
031     * @since 4.0
032     */
033    public class UploadFormParametersWrapper extends HttpServletRequestWrapper
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 arrays of Strings.
043         */
044        public UploadFormParametersWrapper(HttpServletRequest request, Map parameterMap)
045        {
046            super(request);
047    
048            Defense.notNull(parameterMap, "parameterMap");
049    
050            _parameterMap = Collections.unmodifiableMap(parameterMap);
051        }
052    
053        public String getParameter(String name)
054        {
055            String[] values = getParameterValues(name);
056    
057            return (values == null || values.length == 0) ? null : values[0];
058        }
059    
060        public Map getParameterMap()
061        {
062            return _parameterMap;
063        }
064    
065        public Enumeration getParameterNames()
066        {
067            return Collections.enumeration(_parameterMap.keySet());
068        }
069    
070        public String[] getParameterValues(String name)
071        {
072            return (String[]) _parameterMap.get(name);
073        }
074    
075        public String toString()
076        {
077            return "<UploadFormPartWrapper for " + getRequest() + ">";
078        }
079    }