View Javadoc

1   /*
2    * $Id: LoginRequestWrapper.java 128 2004-11-06 10:24:36Z 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.security;
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 permits us replace client's request with original request.
37   *
38   * @author  Jose M. Palomar
39   * @version $Revision: 128 $
40   */
41  public final class LoginRequestWrapper extends HttpServletRequestWrapper {
42  
43      // Fields
44      /**
45       * Request parameters.
46       */
47      private Map parameters;
48  
49      // Constructors
50      /**
51       * Creates a new <code>LoginRequestWrapper</code> from client's request.
52       *
53       * @param request HttpServletRequest client's request.
54       * @param parameters Map original parameters map.
55       * @param attributes Map oritinal attributes map.
56       */
57      public LoginRequestWrapper(HttpServletRequest request, Map parameters, Map attributes) {
58          super(request);
59  
60          this.parameters = parameters;
61  
62          Iterator attsIterator = attributes.keySet().iterator();
63          while (attsIterator.hasNext()) {
64  
65              String attName = (String) attsIterator.next();
66              Object attValue = attributes.get(attName);
67  
68              setAttribute(attName, attValue);
69  
70          }
71  
72      }
73  
74      /**
75       * Returns the value of a request parameter as a <code>String</code>, or
76       * <code>null</code> if the parameter does not exist. Request parameters are
77       * extra information sent with the request.
78       *
79       * @param name String a <code>String</code> specifying the name of the parameter.
80       * @return String a <code>String</code> representing the single value of the
81       * parameter.
82       * @see javax.servlet.ServletRequest#getParameter(String)
83       */
84      public String getParameter(String name) {
85          String[] multiValue =  (String[]) parameters.get(name);
86          if ((multiValue != null) && (multiValue.length > 0)) {
87              return multiValue[0];
88          }
89  
90          return null;
91      }
92  
93      /**
94       * Returns an array of <code>String</code> objects containing all of the values
95       * the given request parameter has, or <code>null</code> if the parameter does
96       * not exist.
97       *
98       * @param name String a <code>String</code> specifying the name of the parameter.
99       * @return String[] an array of <code>String</code> objects containing the
100      * parameter's values.
101      * @see javax.servlet.ServletRequest#getParameterValues(String)
102      */
103     public String[] getParameterValues(String name) {
104         return (String[]) parameters.get(name);
105     }
106 
107     /**
108      * Returns an <code>Enumeration</code> of <code>String</code> objects containing
109      * the names of the parameters contained in this request.
110      *
111      * @return Enumeration an <code>Enumeration</code> of <code>String</code> objects,
112      * each <code>String</code> containing the name of a request parameter; or an
113      * empty <code>Enumeration</code> if the request has no parameters.
114      * @see javax.servlet.ServletRequest#getParameterNames()
115      */
116     public Enumeration getParameterNames() {
117         return Collections.enumeration(getParameterMap().keySet());
118     }
119 
120     /**
121      * Returns a <code>Map</code> of the parameters of this request.
122      *
123      * @return Map an immutable <code>Map</code> containing parameter names as keys
124      * and parameter values as map values. The keys in the parameter map are of type
125      * <code>String</code>. The values in the parameter map are of type
126      * <code>String</code> array.
127      * @see javax.servlet.ServletRequest#getParameterMap()
128      */
129     public Map getParameterMap() {
130         HashMap parameterMap = new HashMap();
131         parameterMap.putAll(this.parameters);
132 
133         return Collections.unmodifiableMap(parameterMap);
134     }
135 
136 }