KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > web > ServletRequestConfiguration


1 /*
2  * Copyright 2004 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 JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import javax.servlet.ServletRequest JavaDoc;
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 JavaDoc request;
38
39     /**
40      * Create a ServletRequestConfiguration using the request parameters.
41      *
42      * @param request the servlet request
43      */

44     public ServletRequestConfiguration(ServletRequest JavaDoc request)
45     {
46         this.request = request;
47     }
48
49     public Object JavaDoc getProperty(String JavaDoc key)
50     {
51         String JavaDoc 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 JavaDoc key, Object JavaDoc obj)
74     {
75         throw new UnsupportedOperationException JavaDoc("Read only configuration");
76     }
77
78     public boolean isEmpty()
79     {
80         return !getKeys().hasNext();
81     }
82
83     public boolean containsKey(String JavaDoc 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 JavaDoc key)
95     {
96         throw new UnsupportedOperationException JavaDoc("Read only configuration");
97     }
98
99     public Iterator JavaDoc getKeys()
100     {
101         return new EnumerationIterator(request.getParameterNames());
102     }
103 }
104
Popular Tags