View Javadoc

1   /*
2    * $Id: HistoricRequestWrapper.java 132 2004-11-28 13:35:12Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      // Fields
46      /**
47       * Request parameters.
48       */
49      private Map parameters;
50  
51      // Constructors
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 }