View Javadoc

1   /*
2    * Copyright 2004-2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.configuration.web;
18  
19  import java.util.Iterator;
20  import java.util.Arrays;
21  import javax.servlet.ServletRequest;
22  
23  import org.apache.commons.collections.iterators.EnumerationIterator;
24  
25  /***
26   * A configuration wrapper to read the parameters of a servlet request. This
27   * configuration is read only, adding or removing a property will throw an
28   * UnsupportedOperationException.
29   *
30   * @author <a href="mailto:ebourg@apache.org">Emmanuel Bourg</a>
31   * @version $Revision$, $Date: 2005-10-12 21:01:43 +0200 (Wed, 12 Oct 2005) $
32   * @since 1.1
33   */
34  public class ServletRequestConfiguration extends BaseWebConfiguration
35  {
36      /*** Stores the wrapped request.*/
37      protected ServletRequest request;
38  
39      /***
40       * Create a ServletRequestConfiguration using the request parameters.
41       *
42       * @param request the servlet request
43       */
44      public ServletRequestConfiguration(ServletRequest request)
45      {
46          this.request = request;
47      }
48  
49      public Object getProperty(String key)
50      {
51          String[] values = request.getParameterValues(key);
52  
53          if (values == null || values.length == 0)
54          {
55              return null;
56          }
57          else if (values.length == 1)
58          {
59              return values[0];
60          }
61          else
62          {
63              return Arrays.asList(values);
64          }
65      }
66  
67      public Iterator getKeys()
68      {
69          return new EnumerationIterator(request.getParameterNames());
70      }
71  }