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
028     * access to the form 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        /**
037         * Map of {@link ValuePart} keyed on parameter name.
038         */
039        private Map _parameterMap;
040    
041        /**
042         * @param parameterMap
043         *            a map whose keys are parameter names and whose values are
044         *            arrays of Strings.
045         */
046        public UploadFormParametersWrapper(HttpServletRequest request,
047                Map parameterMap)
048        {
049            super(request);
050    
051            Defense.notNull(parameterMap, "parameterMap");
052    
053            _parameterMap = Collections.unmodifiableMap(parameterMap);
054        }
055    
056        public String getParameter(String name)
057        {
058            String[] values = getParameterValues(name);
059    
060            return (values == null || values.length == 0) ? null : values[0];
061        }
062    
063        public Map getParameterMap()
064        {
065            return _parameterMap;
066        }
067    
068        public Enumeration getParameterNames()
069        {
070            return Collections.enumeration(_parameterMap.keySet());
071        }
072    
073        public String[] getParameterValues(String name)
074        {
075            return (String[]) _parameterMap.get(name);
076        }
077    
078        public String toString()
079        {
080            return "<UploadFormPartWrapper for " + getRequest() + ">";
081        }
082    }