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
018 package org.apache.commons.configuration.web;
019
020 import java.util.ArrayList;
021 import java.util.Collection;
022 import java.util.Iterator;
023 import java.util.List;
024
025 import javax.servlet.ServletRequest;
026
027 import org.apache.commons.collections.iterators.EnumerationIterator;
028
029 /**
030 * A configuration wrapper to read the parameters of a servlet request. This
031 * configuration is read only, adding or removing a property will throw an
032 * UnsupportedOperationException.
033 *
034 * @author <a href="mailto:ebourg@apache.org">Emmanuel Bourg</a>
035 * @version $Revision: 515306 $, $Date: 2007-03-06 22:15:00 +0100 (Di, 06. Mrz 2007) $
036 * @since 1.1
037 */
038 public class ServletRequestConfiguration extends BaseWebConfiguration
039 {
040 /** Stores the wrapped request.*/
041 protected ServletRequest request;
042
043 /**
044 * Create a ServletRequestConfiguration using the request parameters.
045 *
046 * @param request the servlet request
047 */
048 public ServletRequestConfiguration(ServletRequest request)
049 {
050 this.request = request;
051 }
052
053 public Object getProperty(String key)
054 {
055 String[] values = request.getParameterValues(key);
056
057 if (values == null || values.length == 0)
058 {
059 return null;
060 }
061 else if (values.length == 1)
062 {
063 return handleDelimiters(values[0]);
064 }
065 else
066 {
067 // ensure that escape characters in all list elements are removed
068 List result = new ArrayList(values.length);
069 for (int i = 0; i < values.length; i++)
070 {
071 Object val = handleDelimiters(values[i]);
072 if (val instanceof Collection)
073 {
074 result.addAll((Collection) val);
075 }
076 else
077 {
078 result.add(val);
079 }
080 }
081 return result;
082 }
083 }
084
085 public Iterator getKeys()
086 {
087 return new EnumerationIterator(request.getParameterNames());
088 }
089 }