001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.configuration.web;
019
020import java.util.ArrayList;
021import java.util.Collection;
022import java.util.Iterator;
023import java.util.List;
024import java.util.Map;
025
026import javax.servlet.ServletRequest;
027
028/**
029 * A configuration wrapper to read the parameters of a servlet request. This
030 * configuration is read only, adding or removing a property will throw an
031 * UnsupportedOperationException.
032 *
033 * @author <a href="mailto:ebourg@apache.org">Emmanuel Bourg</a>
034 * @version $Id: ServletRequestConfiguration.java 1211131 2011-12-06 20:57:37Z oheger $
035 * @since 1.1
036 */
037public class ServletRequestConfiguration extends BaseWebConfiguration
038{
039    /** Stores the wrapped request.*/
040    protected ServletRequest request;
041
042    /**
043     * Create a ServletRequestConfiguration using the request parameters.
044     *
045     * @param request the servlet request
046     */
047    public ServletRequestConfiguration(ServletRequest request)
048    {
049        this.request = request;
050    }
051
052    public Object getProperty(String key)
053    {
054        String[] values = request.getParameterValues(key);
055
056        if (values == null || values.length == 0)
057        {
058            return null;
059        }
060        else if (values.length == 1)
061        {
062            return handleDelimiters(values[0]);
063        }
064        else
065        {
066            // ensure that escape characters in all list elements are removed
067            List<Object> result = new ArrayList<Object>(values.length);
068            for (int i = 0; i < values.length; i++)
069            {
070                Object val = handleDelimiters(values[i]);
071                if (val instanceof Collection)
072                {
073                    result.addAll((Collection<?>) val);
074                }
075                else
076                {
077                    result.add(val);
078                }
079            }
080            return result;
081        }
082    }
083
084    public Iterator<String> getKeys()
085    {
086        // According to the documentation of getParameterMap(), keys are Strings.
087        @SuppressWarnings("unchecked")
088        Map<String, ?> parameterMap = request.getParameterMap();
089        return parameterMap.keySet().iterator();
090    }
091}