1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 import org.apache.commons.configuration.AbstractConfiguration;
25
26 /***
27 * A configuration wrapper to read the parameters of a servlet request. This
28 * configuration is read only, adding or removing a property will throw an
29 * UnsupportedOperationException.
30 *
31 * @author <a href="mailto:ebourg@apache.org">Emmanuel Bourg</a>
32 * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 2005) $
33 * @since 1.1
34 */
35 public class ServletRequestConfiguration extends AbstractConfiguration
36 {
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 /***
68 * <p><strong>This operation is not supported and will throw an
69 * UnsupportedOperationException.</strong></p>
70 *
71 * @throws UnsupportedOperationException
72 */
73 protected void addPropertyDirect(String key, Object obj)
74 {
75 throw new UnsupportedOperationException("Read only configuration");
76 }
77
78 public boolean isEmpty()
79 {
80 return !getKeys().hasNext();
81 }
82
83 public boolean containsKey(String key)
84 {
85 return getProperty(key) != null;
86 }
87
88 /***
89 * <p><strong>This operation is not supported and will throw an
90 * UnsupportedOperationException.</strong></p>
91 *
92 * @throws UnsupportedOperationException
93 */
94 public void clearProperty(String key)
95 {
96 throw new UnsupportedOperationException("Read only configuration");
97 }
98
99 public Iterator getKeys()
100 {
101 return new EnumerationIterator(request.getParameterNames());
102 }
103 }