KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > env > ServletEnvironmentRequest


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.env;
35
36 import com.icesoft.jasper.Constants;
37 import org.apache.commons.collections.iterators.IteratorEnumeration;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 import javax.servlet.http.Cookie JavaDoc;
42 import javax.servlet.http.HttpServletRequest JavaDoc;
43 import javax.servlet.http.HttpSession JavaDoc;
44 import java.util.Enumeration JavaDoc;
45 import java.util.Hashtable JavaDoc;
46 import java.util.LinkedList JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 /**
50  * A wrapper for HttpServletRequest.
51  * <p/>
52  * It is up to the user to ensure that casts to this specific type and use the
53  * specific methods if you are running in the appropriate environment. Also,
54  * since we wrap real requests, the state of those requests can get changed by
55  * the application server, so it's possible that certain calls may result in
56  * exceptions being thrown.
57  * <p/>
58  * Note: This class may not be completely finished. We have only included what
59  * we've needed to this point. Also, the proper operations of some methods may
60  * not make sense (like getting the reader or the input stream) so they simply
61  * return null and we don't call them on the underlying instance either.
62  */

63 public class ServletEnvironmentRequest
64         extends CommonEnvironmentRequest
65         implements HttpServletRequest JavaDoc {
66
67     protected static Log log =
68             LogFactory.getLog(ServletEnvironmentRequest.class);
69     private static String JavaDoc ACEGI_AUTH_CLASS = "org.acegisecurity.Authentication";
70     HttpServletRequest JavaDoc servletRequest;
71     private String JavaDoc localName;
72     private String JavaDoc localAddr;
73     private int localPort;
74     private Hashtable JavaDoc headers;
75     private Cookie JavaDoc[] cookies;
76     private String JavaDoc method;
77     private String JavaDoc pathInfo;
78     private String JavaDoc pathTranslated;
79     private String JavaDoc queryString;
80     private String JavaDoc requestURI;
81     private StringBuffer JavaDoc requestURL;
82     private String JavaDoc servletPath;
83     private HttpSession JavaDoc servletSession;
84     private boolean isRequestedSessionIdFromCookie;
85     private boolean isRequestedSessionIdFromURL;
86     private String JavaDoc characterEncoding;
87     private int contentLength;
88     private String JavaDoc contentType;
89     private String JavaDoc protocol;
90     private String JavaDoc remoteAddr;
91     private String JavaDoc remoteHost;
92     private int remotePort;
93     private AcegiAuthWrapper acegiAuthWrapper;
94     private static Class JavaDoc acegiAuthClass;
95
96     static {
97         try {
98             acegiAuthClass = Class.forName(ACEGI_AUTH_CLASS);
99             if (log.isDebugEnabled()) {
100                 log.debug("Acegi Security engaged.");
101             }
102         } catch (Throwable JavaDoc t) {
103             if (log.isDebugEnabled()) {
104                 log.debug("Acegi Security not detected.");
105             }
106         }
107     }
108     
109     public ServletEnvironmentRequest(HttpServletRequest JavaDoc req) {
110         servletRequest = req;
111
112         //Copy common data
113
authType = req.getAuthType();
114         contextPath = req.getContextPath();
115         remoteUser = req.getRemoteUser();
116         userPrincipal = req.getUserPrincipal();
117         if (null != acegiAuthClass) {
118             if (acegiAuthClass.isInstance(userPrincipal)) {
119                 acegiAuthWrapper = new AcegiAuthWrapper(userPrincipal);
120             }
121         }
122         requestedSessionId = req.getRequestedSessionId();
123         isRequestedSessionIdValid = req.isRequestedSessionIdValid();
124
125         attributes = new Hashtable JavaDoc();
126         Enumeration items = req.getAttributeNames();
127         String JavaDoc name = null;
128         while (items.hasMoreElements()) {
129             name = (String JavaDoc) items.nextElement();
130             Object JavaDoc attribute = req.getAttribute(name);
131             if ((null != name) && (null != attribute)) {
132                 attributes.put(name, attribute);
133             }
134         }
135
136         // Warning: For some reason, the various javax.include.* attributes are
137
// not available via the getAttributeNames() call. This may be limited
138
// to a Liferay issue but when the MainPortlet dispatches the call to
139
// the MainServlet, all of the javax.include.* attributes can be
140
// retrieved using request.getAttribute() but they do NOT appear in
141
// the Enumeration of names returned by getAttributeNames(). So here
142
// we manually add them to our map to ensure we can find them later.
143
String JavaDoc[] incAttrKeys = Constants.INC_CONSTANTS;
144         for (int index = 0; index < incAttrKeys.length; index++) {
145             String JavaDoc incAttrKey = incAttrKeys[index];
146             Object JavaDoc incAttrVal = req.getAttribute(incAttrKey);
147             if( incAttrVal != null ){
148                 attributes.put(incAttrKey,req.getAttribute(incAttrKey));
149             }
150         }
151
152         headers = new Hashtable JavaDoc();
153         items = req.getHeaderNames();
154         name = null;
155         while (items.hasMoreElements()) {
156             name = (String JavaDoc) items.nextElement();
157             Enumeration values = req.getHeaders(name);
158             LinkedList JavaDoc valueList = new LinkedList JavaDoc();
159             while (values.hasMoreElements()) {
160                  valueList.add(values.nextElement());
161             }
162             headers.put(name, valueList);
163         }
164
165         parameters = new Hashtable JavaDoc();
166         items = req.getParameterNames();
167         while (items.hasMoreElements()) {
168             name = (String JavaDoc) items.nextElement();
169             parameters.put(name, req.getParameterValues(name));
170         }
171
172         scheme = req.getScheme();
173         serverName = req.getServerName();
174         serverPort = req.getServerPort();
175         locale = req.getLocale();
176
177         locales = new Vector JavaDoc();
178         items = req.getLocales();
179         while (items.hasMoreElements()) {
180             locales.add(items.nextElement());
181         }
182
183         isSecure = req.isSecure();
184
185         //Copy servlet specific data
186
cookies = req.getCookies();
187         method = req.getMethod();
188         pathInfo = req.getPathInfo();
189         pathTranslated = req.getPathTranslated();
190         queryString = req.getQueryString();
191         requestURI = req.getRequestURI();
192         requestURL = req.getRequestURL();
193         servletPath = req.getServletPath();
194         servletSession = req.getSession();
195         isRequestedSessionIdFromCookie = req.isRequestedSessionIdFromCookie();
196         isRequestedSessionIdFromURL = req.isRequestedSessionIdFromURL();
197         characterEncoding = req.getCharacterEncoding();
198         contentLength = req.getContentLength();
199         contentType = req.getContentType();
200         protocol = req.getProtocol();
201         remoteAddr = req.getRemoteAddr();
202         remoteHost = req.getRemoteHost();
203
204         //Servlet 2.4 Stuff
205
// localName = req.getLocalName();
206
// localAddr = req.getLocalAddr();
207
// localPort = req.getLocalPort();
208
// remotePort = req.getRemotePort();
209

210     }
211
212
213     /**
214      * Common HttpServletRequest/PortletRequest methods
215      */

216
217     public boolean isUserInRole(String JavaDoc role) {
218         if (null != acegiAuthWrapper) {
219             return acegiAuthWrapper.isUserInRole(role);
220         }
221         return servletRequest.isUserInRole(role);
222     }
223
224
225     /**
226      * HttpServletRequest methods
227      */

228
229     public Cookie JavaDoc[] getCookies() {
230         return cookies;
231     }
232
233
234     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
235         super.setAttribute(name, value);
236         try {
237             servletRequest.setAttribute(name, value);
238         } catch (Exception JavaDoc e) {
239             //ignore because the container disposed servletRequest by now
240
}
241     }
242
243     public void removeAttribute(String JavaDoc name) {
244         super.removeAttribute(name);
245         try {
246             servletRequest.removeAttribute(name);
247         } catch (Exception JavaDoc e) {
248             //ignore because the container disposed servletRequest by now
249
}
250     }
251
252     public long getDateHeader(String JavaDoc name) {
253         String JavaDoc header = getHeader(name);
254         if (header == null) {
255             return -1;
256         }
257         //TODO
258
//Convert header string to a date
259
return -1;
260     }
261
262     public String JavaDoc getHeader(String JavaDoc name) {
263         LinkedList JavaDoc values = (LinkedList JavaDoc) headers.get(name);
264         return values == null || values.isEmpty() ?
265                null : (String JavaDoc) values.getFirst();
266     }
267
268     public Enumeration getHeaders(String JavaDoc name) {
269         LinkedList JavaDoc values = (LinkedList JavaDoc) headers.get(name);
270         return new IteratorEnumeration(values.iterator());
271     }
272
273     public Enumeration getHeaderNames() {
274         return headers.keys();
275     }
276
277     public int getIntHeader(String JavaDoc name) {
278         String JavaDoc header = getHeader(name);
279         if (header == null) {
280             return -1;
281         }
282         return Integer.parseInt(name, -1);
283     }
284
285     public String JavaDoc getMethod() {
286         return method;
287     }
288
289     public String JavaDoc getPathInfo() {
290         return pathInfo;
291     }
292
293     public String JavaDoc getPathTranslated() {
294         return pathTranslated;
295     }
296
297     public String JavaDoc getQueryString() {
298         return queryString;
299         //return (contextServletPath.substring(contextServletPath.indexOf("/")));
300
}
301
302     public String JavaDoc getRequestURI() {
303         return requestURI;
304     }
305
306     public StringBuffer JavaDoc getRequestURL() {
307         return requestURL;
308         //return new StringBuffer(contextServletPath);
309
}
310
311     public String JavaDoc getServletPath() {
312         return servletPath;
313         //return (contextServletPath.substring(contextServletPath.lastIndexOf("/")));
314
}
315
316     public HttpSession JavaDoc getSession(boolean create) {
317         return servletRequest.getSession(create);
318     }
319
320     public HttpSession JavaDoc getSession() {
321         return servletSession;
322     }
323
324     public boolean isRequestedSessionIdFromCookie() {
325         return isRequestedSessionIdFromCookie;
326     }
327
328     public boolean isRequestedSessionIdFromURL() {
329         return isRequestedSessionIdFromURL;
330     }
331
332     public boolean isRequestedSessionIdFromUrl() {
333         return isRequestedSessionIdFromURL();
334     }
335
336     public String JavaDoc getCharacterEncoding() {
337         return characterEncoding;
338     }
339
340     public void setCharacterEncoding(String JavaDoc encoding)
341             throws java.io.UnsupportedEncodingException JavaDoc {
342         characterEncoding = encoding;
343     }
344
345     public int getContentLength() {
346         return contentLength;
347     }
348
349     public String JavaDoc getContentType() {
350         return contentType;
351     }
352
353     public javax.servlet.ServletInputStream JavaDoc getInputStream()
354             throws java.io.IOException JavaDoc {
355         return null;
356     }
357
358     public String JavaDoc getProtocol() {
359         return protocol;
360 // return "javascript";
361
}
362
363     public java.io.BufferedReader JavaDoc getReader() throws java.io.IOException JavaDoc {
364         return null;
365     }
366
367     public String JavaDoc getRemoteAddr() {
368         return remoteAddr;
369     }
370
371     public String JavaDoc getRemoteHost() {
372         return remoteHost;
373     }
374
375     public javax.servlet.RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc name) {
376         return null;
377     }
378
379     /**
380      * @deprecated
381      */

382     public String JavaDoc getRealPath(String JavaDoc path) {
383         return servletRequest.getRealPath(path);
384     }
385
386     public int getRemotePort() {
387         return remotePort;
388     }
389
390     public String JavaDoc getLocalName() {
391         return localName;
392     }
393
394     public String JavaDoc getLocalAddr() {
395         return localAddr;
396     }
397
398     public int getLocalPort() {
399         return localPort;
400     }
401
402 }
403
Popular Tags