KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > wrapper > RequestWrapper


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.environment.wrapper;
17
18 import java.security.Principal JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.apache.cocoon.environment.Cookie;
27 import org.apache.cocoon.environment.Environment;
28 import org.apache.cocoon.environment.Request;
29 import org.apache.cocoon.environment.Session;
30
31 /**
32  * This is a wrapper class for the <code>Request</code> object.
33  * It has the same properties except that the url and the parameters
34  * are different.
35  *
36  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
37  * @version $Id: RequestWrapper.java 202262 2005-06-28 18:02:55Z vgritsenko $
38  */

39 public final class RequestWrapper implements Request {
40
41     /** The real {@link Request} object */
42     private final Request req;
43
44     /** The query string */
45     private String JavaDoc queryString;
46
47     /** The request parameters */
48     private final RequestParameters parameters ;
49
50     /** The environment */
51     private final Environment environment;
52
53     /** raw mode? **/
54     private final boolean rawMode;
55
56     /** The request uri */
57     private String JavaDoc requestURI;
58
59     /**
60      * Constructor
61      */

62     public RequestWrapper(Request request,
63                           String JavaDoc requestURI,
64                           String JavaDoc queryString,
65                           Environment env) {
66         this(request, requestURI, queryString, env, false);
67     }
68
69     /**
70      * Constructor
71      */

72     public RequestWrapper(Request request,
73                           String JavaDoc requestURI,
74                           String JavaDoc queryString,
75                           Environment env,
76                           boolean rawMode) {
77         this.environment = env;
78         this.req = request;
79         this.queryString = queryString;
80         this.parameters = new RequestParameters(queryString);
81         this.rawMode = rawMode;
82         if (this.req.getQueryString() != null && this.rawMode == false) {
83             if (this.queryString == null)
84                 this.queryString = this.req.getQueryString();
85             else
86                 this.queryString += '&' + this.req.getQueryString();
87         }
88         this.requestURI = this.req.getRequestURI();
89     }
90
91     public Object JavaDoc get(String JavaDoc name) {
92         return this.req.get(name);
93     }
94
95     public Object JavaDoc getAttribute(String JavaDoc name) {
96         return this.req.getAttribute(name);
97     }
98
99     public Enumeration JavaDoc getAttributeNames() {
100         return this.req.getAttributeNames();
101     }
102
103     public String JavaDoc getCharacterEncoding() {
104         return this.req.getCharacterEncoding();
105     }
106
107     public void setCharacterEncoding(String JavaDoc enc)
108     throws java.io.UnsupportedEncodingException JavaDoc {
109         this.req.setCharacterEncoding(enc);
110     }
111
112     public int getContentLength() {
113         return this.req.getContentLength();
114     }
115
116     public String JavaDoc getContentType() {
117         return this.req.getContentType();
118     }
119
120     public String JavaDoc getParameter(String JavaDoc name) {
121         String JavaDoc value = this.parameters.getParameter(name);
122         if (value == null && this.rawMode == false)
123             return this.req.getParameter(name);
124         else
125             return value;
126     }
127
128     public Enumeration JavaDoc getParameterNames() {
129         if ( this.rawMode == false ) {
130             // put all parameter names into a set
131
Set JavaDoc parameterNames = new HashSet JavaDoc();
132             Enumeration JavaDoc names = this.parameters.getParameterNames();
133             while (names.hasMoreElements()) {
134                 parameterNames.add(names.nextElement());
135             }
136             names = this.req.getParameterNames();
137             while (names.hasMoreElements()) {
138                 parameterNames.add(names.nextElement());
139             }
140             return new EnumerationFromIterator(parameterNames.iterator());
141         } else {
142             return this.parameters.getParameterNames();
143         }
144     }
145
146     static final class EnumerationFromIterator implements Enumeration JavaDoc {
147         private Iterator JavaDoc iter;
148         EnumerationFromIterator(Iterator JavaDoc iter) {
149             this.iter = iter;
150         }
151
152         public boolean hasMoreElements() {
153             return iter.hasNext();
154         }
155         public Object JavaDoc nextElement() { return iter.next(); }
156     }
157
158     public String JavaDoc[] getParameterValues(String JavaDoc name) {
159         if ( this.rawMode == false) {
160             String JavaDoc[] values = this.parameters.getParameterValues(name);
161             String JavaDoc[] inherited = this.req.getParameterValues(name);
162             if (inherited == null) return values;
163             if (values == null) return inherited;
164             String JavaDoc[] allValues = new String JavaDoc[values.length + inherited.length];
165             System.arraycopy(values, 0, allValues, 0, values.length);
166             System.arraycopy(inherited, 0, allValues, values.length, inherited.length);
167             return allValues;
168         } else {
169             return this.parameters.getParameterValues(name);
170         }
171     }
172
173     public String JavaDoc getProtocol() {
174         return this.req.getProtocol();
175     }
176
177     public String JavaDoc getScheme() {
178         return this.req.getScheme();
179     }
180
181     public String JavaDoc getServerName() {
182         return this.req.getServerName();
183     }
184
185     public int getServerPort() {
186         return this.req.getServerPort();
187     }
188
189     public String JavaDoc getRemoteAddr() {
190         return this.req.getRemoteAddr();
191     }
192
193     public String JavaDoc getRemoteHost() {
194         return this.req.getRemoteHost();
195     }
196
197     public void setAttribute(String JavaDoc name, Object JavaDoc o) {
198         this.req.setAttribute(name, o);
199     }
200
201     /**
202      * Remove one attriube
203      */

204     public void removeAttribute(String JavaDoc name) {
205         this.req.removeAttribute(name);
206     }
207
208     public Locale JavaDoc getLocale() {
209         return this.req.getLocale();
210     }
211
212     public Enumeration JavaDoc getLocales() {
213         return this.req.getLocales();
214     }
215
216     public boolean isSecure() {
217         return this.req.isSecure();
218     }
219
220     public Cookie[] getCookies() {
221         return this.req.getCookies();
222     }
223
224     public Map JavaDoc getCookieMap() {
225         return this.req.getCookieMap();
226     }
227
228     public long getDateHeader(String JavaDoc name) {
229         return this.req.getDateHeader(name);
230     }
231
232     public String JavaDoc getHeader(String JavaDoc name) {
233         return this.req.getHeader(name);
234     }
235
236     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
237         return this.req.getHeaders(name);
238     }
239
240     public Enumeration JavaDoc getHeaderNames() {
241         return this.req.getHeaderNames();
242     }
243
244     public String JavaDoc getMethod() {
245         return this.req.getMethod();
246     }
247
248     public String JavaDoc getPathInfo() {
249         return this.req.getPathInfo();
250     }
251
252     public String JavaDoc getPathTranslated() {
253         return this.req.getPathTranslated();
254     }
255
256     public String JavaDoc getContextPath() {
257         return this.req.getContextPath();
258     }
259
260     public String JavaDoc getQueryString() {
261         return this.queryString;
262     }
263
264     public String JavaDoc getRemoteUser() {
265         return this.req.getRemoteUser();
266     }
267
268     public String JavaDoc getRequestedSessionId() {
269         return this.req.getRequestedSessionId();
270     }
271
272     public String JavaDoc getRequestURI() {
273         return this.requestURI;
274     }
275
276     public String JavaDoc getSitemapURI() {
277         return this.environment.getURI();
278     }
279
280     public String JavaDoc getSitemapURIPrefix() {
281         return this.environment.getURIPrefix();
282     }
283
284     public String JavaDoc getServletPath() {
285         return this.req.getServletPath();
286     }
287
288     public Session getSession(boolean create) {
289         return this.req.getSession(create);
290     }
291
292     public Session getSession() {
293         return this.req.getSession();
294     }
295
296     public boolean isRequestedSessionIdValid() {
297         return this.req.isRequestedSessionIdValid();
298     }
299
300     public boolean isRequestedSessionIdFromCookie() {
301         return this.req.isRequestedSessionIdFromCookie();
302     }
303
304     public boolean isRequestedSessionIdFromURL() {
305         return this.req.isRequestedSessionIdFromURL();
306     }
307
308     public boolean isRequestedSessionIdFromUrl() {
309         return this.req.isRequestedSessionIdFromURL();
310     }
311
312     public Principal JavaDoc getUserPrincipal() {
313         return this.req.getUserPrincipal();
314     }
315
316     public boolean isUserInRole(String JavaDoc role) {
317         return this.req.isUserInRole(role);
318     }
319
320     public String JavaDoc getAuthType() {
321         return this.req.getAuthType();
322     }
323
324     public void setRequestURI(String JavaDoc prefix, String JavaDoc uri) {
325         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(this.getContextPath());
326         buffer.append('/');
327         buffer.append(prefix);
328         buffer.append(uri);
329         this.requestURI = buffer.toString();
330     }
331 }
332
Popular Tags