Source code
Description: source: package: org.apache.commons.configuration2.web, class: JakartaServletRequestConfiguration

    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 *     https://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 java.util.ArrayList;
    021import java.util.Collection;
    022import java.util.Iterator;
    023import java.util.List;
    024import java.util.Map;
    025import java.util.Objects;
    026
    027import jakarta.servlet.ServletRequest;
    028
    029/**
    030 * A configuration wrapper to read the parameters of a servlet request. This configuration is read only, adding or
    031 * removing a property will throw an UnsupportedOperationException.
    032 *
    033 * @since 2.12.0
    034 */
    035public class JakartaServletRequestConfiguration extends BaseWebConfiguration {
    036
    037    /** Stores the wrapped request. */
    038    protected ServletRequest request;
    039
    040    /**
    041     * Create a ServletRequestConfiguration using the request parameters.
    042     *
    043     * @param request the servlet request
    044     */
    045    public JakartaServletRequestConfiguration(final ServletRequest request) {
    046        this.request = Objects.requireNonNull(request, "config");
    047    }
    048
    049    @Override
    050    protected Iterator<String> getKeysInternal() {
    051        // According to the documentation of getParameterMap(), keys are Strings.
    052        final Map<String, ?> parameterMap = request.getParameterMap();
    053        return parameterMap.keySet().iterator();
    054    }
    055
    056    @Override
    057    protected Object getPropertyInternal(final String key) {
    058        final String[] values = request.getParameterValues(key);
    059
    060        if (values == null || values.length == 0) {
    061            return null;
    062        }
    063        if (values.length == 1) {
    064            return handleDelimiters(values[0]);
    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            final Object val = handleDelimiters(value);
    070            if (val instanceof Collection) {
    071                result.addAll((Collection<?>) val);
    072            } else {
    073                result.add(val);
    074            }
    075        }
    076        return result;
    077    }
    078}