1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.talika.tarsis.filters.validator;
24
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletRequestWrapper;
33
34 /**
35 * Provides a convenient implementation of the <code>HttpServletRequest</code>
36 * interface that allows us transport all parameters from a historic request.<br>
37 * Part of experimental extended validator filter. Work in progress.
38 *
39 * @author Jose M. Palomar
40 * @version $Revision: 132 $
41 * @todo finish extended validator architecture.
42 */
43 public final class HistoricRequestWrapper extends HttpServletRequestWrapper {
44
45
46 /**
47 * Request parameters.
48 */
49 private Map parameters;
50
51
52 /**
53 * Creates a new <code>HistoricRequestWrapper</code>.
54 *
55 * @param request HttpServletRequest client's original request.
56 * @param parameters Map parameters map.
57 * @param attributes Map attributes map.
58 */
59 public HistoricRequestWrapper(HttpServletRequest request, Map parameters, Map attributes) {
60 super(request);
61
62 this.parameters = parameters;
63
64 Iterator attsIterator = attributes.keySet().iterator();
65 while (attsIterator.hasNext()) {
66
67 String attName = (String) attsIterator.next();
68 Object attValue = attributes.get(attName);
69
70 setAttribute(attName, attValue);
71
72 }
73
74 }
75
76 /**
77 * Returns the value of a request parameter as a <code>String</code>, or
78 * <code>null</code> if the parameter does not exist. Request parameters are
79 * extra information sent with the request.
80 *
81 * @param name String a <code>String</code> specifying the name of the parameter.
82 * @return String a <code>String</code> representing the single value of the
83 * parameter.
84 * @see javax.servlet.ServletRequest#getParameter(String)
85 */
86 public String getParameter(String name) {
87 String[] multiValue = (String[]) parameters.get(name);
88 if ((multiValue != null) && (multiValue.length > 0)) {
89 return multiValue[0];
90 }
91
92 return null;
93 }
94
95 /**
96 * Returns an array of <code>String</code> objects containing all of the values
97 * the given request parameter has, or <code>null</code> if the parameter does
98 * not exist.
99 *
100 * @param name String a <code>String</code> specifying the name of the parameter.
101 * @return String[] an array of <code>String</code> objects containing the
102 * parameter's values.
103 * @see javax.servlet.ServletRequest#getParameterValues(String)
104 */
105 public String[] getParameterValues(String name) {
106 return (String[]) parameters.get(name);
107 }
108
109 /**
110 * Returns an <code>Enumeration</code> of <code>String</code> objects containing
111 * the names of the parameters contained in this request.
112 *
113 * @return Enumeration an <code>Enumeration</code> of <code>String</code> objects,
114 * each <code>String</code> containing the name of a request parameter; or an
115 * empty <code>Enumeration</code> if the request has no parameters.
116 * @see javax.servlet.ServletRequest#getParameterNames()
117 */
118 public Enumeration getParameterNames() {
119 return Collections.enumeration(getParameterMap().keySet());
120 }
121
122 /**
123 * Returns a <code>Map</code> of the parameters of this request.
124 *
125 * @return Map an immutable <code>Map</code> containing parameter names as keys
126 * and parameter values as map values. The keys in the parameter map are of type
127 * <code>String</code>. The values in the parameter map are of type
128 * <code>String</code> array.
129 * @see javax.servlet.ServletRequest#getParameterMap()
130 */
131 public Map getParameterMap() {
132 HashMap parameterMap = new HashMap();
133 parameterMap.putAll(this.parameters);
134
135 return Collections.unmodifiableMap(parameterMap);
136 }
137
138 }