KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > util > RequestInfo


1 /*
2  * RequestInfo.java
3  *
4  * Version: $Revision: 1.5 $
5  *
6  * Date: $Date: 2005/04/20 14:23:00 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.webui.util;
41
42 import java.util.Enumeration JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.Map JavaDoc;
46
47 import javax.servlet.http.HttpServletRequest JavaDoc;
48 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
49
50 /**
51  * Stores information about an HTTP request. This is used so that the request
52  * can be replicated during a later request, once authentication has
53  * successfully occurred.
54  *
55  * @author Robert Tansley
56  * @version $Revision: 1.5 $
57  */

58 public class RequestInfo
59 {
60     /** The original parameters */
61     private Map JavaDoc originalParameterMap;
62
63     /** The original method */
64     private String JavaDoc originalMethod;
65
66     /** The original query */
67     private String JavaDoc originalQueryString;
68
69     /**
70      * Construct a request info object storing information about the given
71      * request
72      *
73      * @param request
74      * the request to get information from
75      */

76     public RequestInfo(HttpServletRequest JavaDoc request)
77     {
78         originalParameterMap = new HashMap JavaDoc(request.getParameterMap());
79         originalMethod = request.getMethod();
80         originalQueryString = request.getQueryString();
81     }
82
83     /**
84      * Wrap an incoming request to make it look like the request that the
85      * constructor was called with
86      *
87      * @param request
88      * the request to wrap
89      *
90      * @return a wrapper around the request passed into this method, wrapped so
91      * that it looks like the request passed into the constructor
92      */

93     public HttpServletRequest JavaDoc wrapRequest(HttpServletRequest JavaDoc request)
94     {
95         return new MyWrapper(request);
96     }
97
98     /**
99      * Our own flavour of HTTP request wrapper, that uses information from= this
100      * RequestInfo object
101      */

102     class MyWrapper extends HttpServletRequestWrapper JavaDoc
103     {
104         public MyWrapper(HttpServletRequest JavaDoc request)
105         {
106             super(request);
107         }
108
109         // ====== Methods below here are the wrapped methods ======
110
public String JavaDoc getParameter(String JavaDoc name)
111         {
112             String JavaDoc[] vals = (String JavaDoc[]) originalParameterMap.get(name);
113
114             if (vals == null)
115             {
116                 // Delegate to wrapped object
117
// FIXME: This is possibly a bug in Tomcat
118
return super.getParameter(name);
119             }
120             else
121             {
122                 return vals[0];
123             }
124         }
125
126         public Map JavaDoc getParameterMap()
127         {
128             return originalParameterMap;
129         }
130
131         public Enumeration JavaDoc getParameterNames()
132         {
133             Iterator JavaDoc i = originalParameterMap.keySet().iterator();
134
135             return new EnumIterator(i);
136         }
137
138         public String JavaDoc[] getParameterValues(String JavaDoc name)
139         {
140             return ((String JavaDoc[]) originalParameterMap.get(name));
141         }
142
143         public String JavaDoc getMethod()
144         {
145             return originalMethod;
146         }
147
148         public String JavaDoc getQueryString()
149         {
150             return originalQueryString;
151         }
152
153         /**
154          * This class converts an interator into an enumerator. This is done
155          * because we have the parameters as a Map (JDK 1.2 style), but for some
156          * weird reason the HttpServletRequest interface returns an Enumeration
157          * from getParameterNames() (JDK1.0 style.) JDK apparently offers no way
158          * of simply changing between the new styles.
159          */

160         class EnumIterator implements Enumeration JavaDoc
161         {
162             private Iterator JavaDoc iterator;
163
164             public EnumIterator(Iterator JavaDoc i)
165             {
166                 iterator = i;
167             }
168
169             public boolean hasMoreElements()
170             {
171                 return iterator.hasNext();
172             }
173
174             public Object JavaDoc nextElement()
175             {
176                 return iterator.next();
177             }
178         }
179     }
180 }
181
Popular Tags