001// Copyright 2007, 2008 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
015package org.apache.tapestry5.upload.internal.services;
016
017import static org.apache.tapestry5.ioc.internal.util.CollectionFactory.newMap;
018
019import javax.servlet.http.HttpServletRequest;
020import javax.servlet.http.HttpServletRequestWrapper;
021import java.io.UnsupportedEncodingException;
022import java.util.Collections;
023import java.util.Enumeration;
024import java.util.Map;
025
026/**
027 * Wrapper for HttpServletRequest that overrides the parameter methods of the wrapped request. i.e. parameters are
028 * retrieved from the wrapper rather than the real request.
029 */
030public class ParametersServletRequestWrapper extends HttpServletRequestWrapper
031{
032    private final Map<String, ParameterValue> parameters = newMap();
033
034    public ParametersServletRequestWrapper(HttpServletRequest httpServletRequest)
035    {
036        super(httpServletRequest);
037    }
038
039    @Override
040    public String getParameter(String name)
041    {
042        return getValueFor(name).single();
043    }
044
045    @Override
046    public Map<String, Object> getParameterMap()
047    {
048        Map<String, Object> paramMap = newMap();
049
050        for (Map.Entry<String, ParameterValue> e : parameters.entrySet())
051        {
052            ParameterValue value = e.getValue();
053
054            paramMap.put(e.getKey(), value.isMulti() ? value.multi() : value.single());
055        }
056
057        return paramMap;
058    }
059
060    @Override
061    public Enumeration getParameterNames()
062    {
063        return Collections.enumeration(parameters.keySet());
064    }
065
066    @Override
067    public String[] getParameterValues(String name)
068    {
069        return getValueFor(name).multi();
070    }
071
072    public void addParameter(String name, String value)
073    {
074        ParameterValue pv = parameters.get(name);
075        if (pv == null)
076        {
077            pv = new ParameterValue(value);
078            parameters.put(name, pv);
079        }
080        else
081        {
082            pv.add(value);
083        }
084    }
085
086    ParameterValue getValueFor(String name)
087    {
088        ParameterValue value = parameters.get(name);
089
090        return value == null ? ParameterValue.NULL : value;
091    }
092
093    /**
094     * Ignores any attempt to set the character encoding, as it already has been set before the request content was
095     * parsed.
096     *
097     * @see org.apache.tapestry5.SymbolConstants#CHARSET
098     */
099    @Override
100    public void setCharacterEncoding(String enc) throws UnsupportedEncodingException
101    {
102
103    }
104}