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.configuration2.web;
019
020import javax.servlet.ServletRequest;
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.Iterator;
024import java.util.List;
025import java.util.Map;
026
027/**
028 * A configuration wrapper to read the parameters of a servlet request. This
029 * configuration is read only, adding or removing a property will throw an
030 * UnsupportedOperationException.
031 *
032 * @since 1.1
033 */
034public class ServletRequestConfiguration extends BaseWebConfiguration
035{
036    /** Stores the wrapped request.*/
037    protected ServletRequest request;
038
039    /**
040     * Create a ServletRequestConfiguration using the request parameters.
041     *
042     * @param request the servlet request
043     */
044    public ServletRequestConfiguration(final ServletRequest request)
045    {
046        this.request = request;
047    }
048
049    @Override
050    protected Object getPropertyInternal(final String key)
051    {
052        final String[] values = request.getParameterValues(key);
053
054        if (values == null || values.length == 0)
055        {
056            return null;
057        }
058        else if (values.length == 1)
059        {
060            return handleDelimiters(values[0]);
061        }
062        else
063        {
064            // ensure that escape characters in all list elements are removed
065            final List<Object> result = new ArrayList<>(values.length);
066            for (final String value : values)
067            {
068                final Object val = handleDelimiters(value);
069                if (val instanceof Collection)
070                {
071                    result.addAll((Collection<?>) val);
072                }
073                else
074                {
075                    result.add(val);
076                }
077            }
078            return result;
079        }
080    }
081
082    @Override
083    protected Iterator<String> getKeysInternal()
084    {
085        // According to the documentation of getParameterMap(), keys are Strings.
086        final Map<String, ?> parameterMap = request.getParameterMap();
087        return parameterMap.keySet().iterator();
088    }
089}