    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 * @author <a href="mailto:ebourg@apache.org">Emmanuel Bourg</a>
    033 * @version $Id: ServletRequestConfiguration.java 1842194 2018-09-27 22:24:23Z ggregory $
    034 * @since 1.1
    035 */
    036public class ServletRequestConfiguration extends BaseWebConfiguration
    037{
    038    /** Stores the wrapped request.*/
    039    protected ServletRequest request;
    040
    041    /**
    042     * Create a ServletRequestConfiguration using the request parameters.
    043     *
    044     * @param request the servlet request
    045     */
    046    public ServletRequestConfiguration(final ServletRequest request)
    047    {
    048        this.request = request;
    049    }
    050
    051    @Override
    052    protected Object getPropertyInternal(final String key)
    053    {
    054        final 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            final List<Object> result = new ArrayList<>(values.length);
    068            for (final String value : values)
    069            {
    070                final Object val = handleDelimiters(value);
    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    @Override
    085    protected Iterator<String> getKeysInternal()
    086    {
    087        // According to the documentation of getParameterMap(), keys are Strings.
    088        final Map<String, ?> parameterMap = request.getParameterMap();
    089        return parameterMap.keySet().iterator();
    090    }
    091}