KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > http > HttpRequest


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.http;
17
18 import java.io.BufferedReader JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.lang.ref.WeakReference JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Vector JavaDoc;
27 import java.util.WeakHashMap JavaDoc;
28
29 import javax.servlet.RequestDispatcher JavaDoc;
30 import javax.servlet.ServletInputStream JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32
33 import org.apache.avalon.framework.CascadingRuntimeException;
34 import org.apache.cocoon.environment.Cookie;
35 import org.apache.cocoon.environment.Request;
36 import org.apache.cocoon.environment.Session;
37 import org.apache.cocoon.environment.http.HttpSession;
38 import org.apache.cocoon.servlet.multipart.MultipartHttpServletRequest;
39
40 /**
41  * Implements the {@link org.apache.cocoon.environment.Request} interface
42  * to provide request information in the HTTP servlets environment.
43  *
44  * @author <a HREF="mailto:giacomo@apache.org">Giacomo Pati</a>
45  * @version $Id: HttpRequest.java 202262 2005-06-28 18:02:55Z vgritsenko $
46  */

47 public final class HttpRequest implements Request JavaDoc {
48
49     /** The real HttpServletRequest object */
50     private final HttpServletRequest JavaDoc req;
51
52     /** The HttpEnvironment object */
53     private final HttpEnvironment env;
54
55     /** The character encoding of parameters */
56     private String JavaDoc form_encoding;
57
58     /** The default form encoding of the servlet container */
59     private String JavaDoc container_encoding;
60
61     /** The map to assure 1:1-mapping of server sessions and Cocoon session wrappers */
62     private static final Map JavaDoc sessions = new WeakHashMap JavaDoc();
63
64     /**
65      * Creates a HttpRequest based on a real HttpServletRequest object
66      */

67     protected HttpRequest(HttpServletRequest JavaDoc req, HttpEnvironment env) {
68         super();
69         this.req = req;
70         this.env = env;
71     }
72
73     /* (non-Javadoc)
74      * @see org.apache.cocoon.environment.Request#get(java.lang.String)
75      */

76     public Object JavaDoc get(String JavaDoc name) {
77         // if the request has been wrapped then access its method
78
if (req instanceof MultipartHttpServletRequest) {
79             return ((MultipartHttpServletRequest) req).get(name);
80         } else {
81             String JavaDoc[] values = req.getParameterValues(name);
82             if (values == null) {
83                 return null;
84             }
85             if (values.length == 1) {
86                 return values[0];
87             }
88             if (values.length > 1) {
89                 Vector JavaDoc vect = new Vector JavaDoc(values.length);
90                 for (int i = 0; i < values.length; i++) {
91                     vect.add(values[i]);
92                 }
93                 return vect;
94             }
95         }
96         return null;
97     }
98
99     /* The HttpServletRequest interface methods */
100
101     public String JavaDoc getAuthType() {
102         return this.req.getAuthType();
103     }
104
105     private Cookie[] wrappedCookies = null;
106     private Map JavaDoc wrappedCookieMap = null;
107
108     public Cookie[] getCookies() {
109         if (this.wrappedCookieMap == null) {
110             wrapCookies();
111         }
112         return this.wrappedCookies;
113     }
114
115     public Map JavaDoc getCookieMap() {
116         if (this.wrappedCookieMap == null) {
117             wrapCookies();
118         }
119         return this.wrappedCookieMap;
120     }
121
122     private synchronized void wrapCookies() {
123         this.wrappedCookieMap = new HashMap JavaDoc();
124         javax.servlet.http.Cookie JavaDoc[] cookies = this.req.getCookies();
125         if (cookies != null) {
126             this.wrappedCookies = new Cookie[cookies.length];
127             for(int i=0; i<cookies.length;i++) {
128                 HttpCookie cookie = new HttpCookie(cookies[i]);
129                 this.wrappedCookies[i] = cookie;
130                 this.wrappedCookieMap.put(cookie.getName(),cookie);
131             }
132         }
133         this.wrappedCookieMap = Collections.unmodifiableMap(this.wrappedCookieMap);
134     }
135
136     public long getDateHeader(String JavaDoc name) {
137         return this.req.getDateHeader(name);
138     }
139
140     public String JavaDoc getHeader(String JavaDoc name) {
141         return this.req.getHeader(name);
142     }
143
144     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
145         return this.req.getHeaders(name);
146     }
147
148     public Enumeration JavaDoc getHeaderNames() {
149         return this.req.getHeaderNames();
150     }
151
152     public int getIntHeader(String JavaDoc name) {
153         return this.req.getIntHeader(name);
154     }
155
156     public String JavaDoc getMethod() {
157         return this.req.getMethod();
158     }
159
160     public String JavaDoc getPathInfo() {
161         return this.req.getPathInfo();
162     }
163
164     public String JavaDoc getPathTranslated() {
165         return this.req.getPathTranslated();
166     }
167
168     public String JavaDoc getContextPath() {
169         return this.req.getContextPath();
170     }
171
172     public String JavaDoc getQueryString() {
173         return this.req.getQueryString();
174     }
175
176     public String JavaDoc getRemoteUser() {
177         return this.req.getRemoteUser();
178     }
179
180     public boolean isUserInRole(String JavaDoc role) {
181         return this.req.isUserInRole(role);
182     }
183
184     public java.security.Principal JavaDoc getUserPrincipal() {
185         return this.req.getUserPrincipal();
186     }
187
188     public String JavaDoc getRequestedSessionId() {
189         return this.req.getRequestedSessionId();
190     }
191
192     protected String JavaDoc reqURI;
193
194     public String JavaDoc getRequestURI() {
195         if (this.reqURI == null) {
196             this.reqURI = this.req.getRequestURI();
197             if ( this.reqURI.equals("/") ) {
198                 String JavaDoc s = this.req.getServletPath();
199                 final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
200                 if ( null != s ) buffer.append(s);
201                 s = this.req.getPathInfo();
202                 if ( null != s ) buffer.append(s);
203                 this.reqURI = buffer.toString();
204             }
205         }
206         return this.reqURI;
207     }
208
209     public String JavaDoc getSitemapURI() {
210         return this.env.getURI();
211     }
212
213     public String JavaDoc getSitemapURIPrefix() {
214         return this.env.getURIPrefix();
215     }
216
217     public String JavaDoc getServletPath() {
218         return this.req.getServletPath();
219     }
220
221     /**
222      * Creates a wrapper implementing {@link Session} for
223      * {@link javax.servlet.http.HttpSession}.
224      * The method must assure 1:1-mapping of
225      * {@link javax.servlet.http.HttpSession}s to Cocoon's session wrappers.
226      *
227      * @see org.apache.cocoon.environment.Request#getSession(boolean)
228      */

229     public Session getSession(boolean create) {
230         javax.servlet.http.HttpSession JavaDoc serverSession = this.req.getSession(create);
231         HttpSession session;
232         if (serverSession != null) {
233             synchronized (sessions) {
234                 // retrieve existing wrapper
235
WeakReference JavaDoc ref = (WeakReference JavaDoc)sessions.get(serverSession);
236                 if (ref == null || (session = (HttpSession)ref.get()) == null) {
237                     // create new wrapper
238
session = new HttpSession(serverSession);
239                     sessions.put(serverSession, new WeakReference JavaDoc(session));
240                 }
241             }
242         } else {
243             // invalidate
244
session = null;
245         }
246         return session;
247     }
248
249     public Session getSession() {
250         return this.getSession(true);
251     }
252
253     public boolean isRequestedSessionIdValid() {
254         return this.req.isRequestedSessionIdValid();
255     }
256
257     public boolean isRequestedSessionIdFromCookie() {
258         return this.req.isRequestedSessionIdFromCookie();
259     }
260
261     public boolean isRequestedSessionIdFromURL() {
262         return this.req.isRequestedSessionIdFromURL();
263     }
264
265     /**
266      * @deprecated As of Version 2.1 of the Java Servlet API, use
267      * {@link #isRequestedSessionIdFromURL()} instead.
268      */

269     public boolean isRequestedSessionIdFromUrl() {
270         return this.req.isRequestedSessionIdFromURL();
271     }
272
273     /* The ServletRequest interface methods */
274
275     public Object JavaDoc getAttribute(String JavaDoc name) {
276         return this.req.getAttribute(name);
277     }
278
279     public Enumeration JavaDoc getAttributeNames() {
280         return this.req.getAttributeNames();
281     }
282
283     public String JavaDoc getCharacterEncoding() {
284         if (this.form_encoding == null) {
285             return this.req.getCharacterEncoding();
286         } else {
287             return this.form_encoding;
288         }
289     }
290
291     public void setCharacterEncoding(String JavaDoc form_encoding)
292     throws java.io.UnsupportedEncodingException JavaDoc {
293         this.form_encoding = form_encoding;
294     }
295
296     /**
297      * Sets the default encoding of the servlet container.
298      */

299     public void setContainerEncoding(String JavaDoc container_encoding) {
300         this.container_encoding = container_encoding;
301     }
302
303     public int getContentLength() {
304         return this.req.getContentLength();
305     }
306
307     public String JavaDoc getContentType() {
308         return this.req.getContentType();
309     }
310
311     public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc {
312         return this.req.getInputStream();
313     }
314
315     public String JavaDoc getParameter(String JavaDoc name) {
316         String JavaDoc value = this.req.getParameter(name);
317         if (this.form_encoding == null || value == null) {
318             return value;
319         }
320         return decode(value);
321     }
322
323     private String JavaDoc decode(String JavaDoc str) {
324         if (str == null) return null;
325         try {
326             if (this.container_encoding == null)
327                 this.container_encoding = "ISO-8859-1";
328             byte[] bytes = str.getBytes(this.container_encoding);
329             return new String JavaDoc(bytes, form_encoding);
330         } catch (java.io.UnsupportedEncodingException JavaDoc uee) {
331             throw new CascadingRuntimeException("Unsupported Encoding Exception", uee);
332         }
333     }
334
335     public Enumeration JavaDoc getParameterNames() {
336         return this.req.getParameterNames();
337     }
338
339     public String JavaDoc[] getParameterValues(String JavaDoc name) {
340         String JavaDoc[] values = this.req.getParameterValues(name);
341         if (values == null) return null;
342         if (this.form_encoding == null) {
343             return values;
344         }
345         String JavaDoc[] decoded_values = new String JavaDoc[values.length];
346         for (int i = 0; i < values.length; ++i) {
347             decoded_values[i] = decode(values[i]);
348         }
349         return decoded_values;
350     }
351
352     public String JavaDoc getProtocol() {
353         return this.req.getProtocol();
354     }
355
356     public String JavaDoc getScheme() {
357         return this.req.getScheme();
358     }
359
360     public String JavaDoc getServerName() {
361         return this.req.getServerName();
362     }
363
364     public int getServerPort() {
365         return this.req.getServerPort();
366     }
367
368     public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
369         return this.req.getReader();
370     }
371
372     public String JavaDoc getRemoteAddr() {
373         return this.req.getRemoteAddr();
374     }
375
376     public String JavaDoc getRemoteHost() {
377         return this.req.getRemoteHost();
378     }
379
380     public void setAttribute(String JavaDoc name, Object JavaDoc o) {
381         this.req.setAttribute(name, o);
382     }
383
384     public void removeAttribute(String JavaDoc name) {
385         this.req.removeAttribute(name);
386     }
387
388     public Locale JavaDoc getLocale() {
389         return this.req.getLocale();
390     }
391
392     public Enumeration JavaDoc getLocales() {
393         return this.req.getLocales();
394     }
395
396     public boolean isSecure() {
397         return this.req.isSecure();
398     }
399
400     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
401         return this.req.getRequestDispatcher(path);
402     }
403
404     /**
405      * @deprecated As of Version 2.1 of the Java Servlet API, use
406      * {@link javax.servlet.ServletContext#getRealPath(java.lang.String)}instead.
407      */

408     public String JavaDoc getRealPath(String JavaDoc path) {
409         return this.req.getRealPath(path);
410     }
411 }
412
Popular Tags