Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ServletRequestConfiguration |
|
| 3.3333333333333335;3,333 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.commons.configuration.web; | |
19 | ||
20 | import java.util.ArrayList; | |
21 | import java.util.Collection; | |
22 | import java.util.Iterator; | |
23 | import java.util.List; | |
24 | import java.util.Map; | |
25 | ||
26 | import javax.servlet.ServletRequest; | |
27 | ||
28 | /** | |
29 | * A configuration wrapper to read the parameters of a servlet request. This | |
30 | * configuration is read only, adding or removing a property will throw an | |
31 | * UnsupportedOperationException. | |
32 | * | |
33 | * @author <a href="mailto:ebourg@apache.org">Emmanuel Bourg</a> | |
34 | * @version $Id: ServletRequestConfiguration.java 1211131 2011-12-06 20:57:37Z oheger $ | |
35 | * @since 1.1 | |
36 | */ | |
37 | 5 | public class ServletRequestConfiguration extends BaseWebConfiguration |
38 | { | |
39 | /** Stores the wrapped request.*/ | |
40 | protected ServletRequest request; | |
41 | ||
42 | /** | |
43 | * Create a ServletRequestConfiguration using the request parameters. | |
44 | * | |
45 | * @param request the servlet request | |
46 | */ | |
47 | public ServletRequestConfiguration(ServletRequest request) | |
48 | 12 | { |
49 | 12 | this.request = request; |
50 | 12 | } |
51 | ||
52 | public Object getProperty(String key) | |
53 | { | |
54 | 10 | String[] values = request.getParameterValues(key); |
55 | ||
56 | 10 | if (values == null || values.length == 0) |
57 | { | |
58 | 2 | return null; |
59 | } | |
60 | 8 | else if (values.length == 1) |
61 | { | |
62 | 5 | return handleDelimiters(values[0]); |
63 | } | |
64 | else | |
65 | { | |
66 | // ensure that escape characters in all list elements are removed | |
67 | 3 | List<Object> result = new ArrayList<Object>(values.length); |
68 | 10 | for (int i = 0; i < values.length; i++) |
69 | { | |
70 | 7 | Object val = handleDelimiters(values[i]); |
71 | 7 | if (val instanceof Collection) |
72 | { | |
73 | 0 | result.addAll((Collection<?>) val); |
74 | } | |
75 | else | |
76 | { | |
77 | 7 | result.add(val); |
78 | } | |
79 | } | |
80 | 3 | return result; |
81 | } | |
82 | } | |
83 | ||
84 | public Iterator<String> getKeys() | |
85 | { | |
86 | // According to the documentation of getParameterMap(), keys are Strings. | |
87 | @SuppressWarnings("unchecked") | |
88 | 3 | Map<String, ?> parameterMap = request.getParameterMap(); |
89 | 3 | return parameterMap.keySet().iterator(); |
90 | } | |
91 | } |