001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.env;
035:
036: import com.icesoft.jasper.Constants;
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: import javax.servlet.RequestDispatcher;
041: import javax.servlet.ServletContext;
042: import javax.servlet.ServletInputStream;
043: import javax.servlet.http.Cookie;
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpSession;
046: import java.io.BufferedReader;
047: import java.io.IOException;
048: import java.io.UnsupportedEncodingException;
049: import java.util.Collections;
050: import java.util.Enumeration;
051: import java.util.HashMap;
052: import java.util.Hashtable;
053: import java.util.List;
054: import java.util.Map;
055:
056: /**
057: * A wrapper for HttpServletRequest.
058: * <p/>
059: * It is up to the user to ensure that casts to this specific type and use the
060: * specific methods if you are running in the appropriate environment. Also,
061: * since we wrap real requests, the state of those requests can get changed by
062: * the application server, so it's possible that certain calls may result in
063: * exceptions being thrown.
064: * <p/>
065: */
066: public class ServletEnvironmentRequest extends CommonEnvironmentRequest
067: implements HttpServletRequest {
068: private static final Log log = LogFactory
069: .getLog(ServletEnvironmentRequest.class);
070: private static final String ACEGI_AUTH_CLASS = "org.acegisecurity.Authentication";
071: private static Class acegiAuthClass;
072: private HttpServletRequest request;
073: private Map headers;
074: private Cookie[] cookies;
075: private String method;
076: private String pathInfo;
077: private String pathTranslated;
078: private String queryString;
079: private String requestURI;
080: private StringBuffer requestURL;
081: private String servletPath;
082: private HttpSession servletSession;
083: private boolean isRequestedSessionIdFromCookie;
084: private boolean isRequestedSessionIdFromURL;
085: private String characterEncoding;
086: private int contentLength;
087: private String contentType;
088: private String protocol;
089: private String remoteAddr;
090: private int remotePort;
091: private String remoteHost;
092: private String localName;
093: private String localAddr;
094: private int localPort;
095: private AcegiAuthWrapper acegiAuthWrapper;
096:
097: static {
098: try {
099: acegiAuthClass = Class.forName(ACEGI_AUTH_CLASS);
100: if (log.isDebugEnabled()) {
101: log.debug("Acegi Security engaged.");
102: }
103: } catch (Throwable t) {
104: if (log.isDebugEnabled()) {
105: log.debug("Acegi Security not detected.");
106: }
107: }
108: }
109:
110: public ServletEnvironmentRequest(Object request) {
111: this .request = (HttpServletRequest) request;
112: //Copy common data
113: authType = this .request.getAuthType();
114: contextPath = this .request.getContextPath();
115: remoteUser = this .request.getRemoteUser();
116: userPrincipal = this .request.getUserPrincipal();
117: if (null != acegiAuthClass) {
118: if (acegiAuthClass.isInstance(userPrincipal)) {
119: acegiAuthWrapper = new AcegiAuthWrapper(userPrincipal);
120: }
121: }
122: requestedSessionId = this .request.getRequestedSessionId();
123: requestedSessionIdValid = this .request
124: .isRequestedSessionIdValid();
125:
126: attributes = new Hashtable();
127: Enumeration attributeNames = this .request.getAttributeNames();
128: while (attributeNames.hasMoreElements()) {
129: String name = (String) attributeNames.nextElement();
130: Object attribute = this .request.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 this.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[] incAttrKeys = Constants.INC_CONSTANTS;
144: for (int index = 0; index < incAttrKeys.length; index++) {
145: String incAttrKey = incAttrKeys[index];
146: Object incAttrVal = this .request.getAttribute(incAttrKey);
147: if (incAttrVal != null) {
148: attributes.put(incAttrKey, this .request
149: .getAttribute(incAttrKey));
150: }
151: }
152:
153: headers = new HashMap();
154: Enumeration headerNames = this .request.getHeaderNames();
155: while (headerNames.hasMoreElements()) {
156: String name = (String) headerNames.nextElement();
157: Enumeration values = this .request.getHeaders(name);
158: headers.put(name, Collections.list(values));
159: }
160:
161: parameters = new HashMap();
162: Enumeration parameterNames = this .request.getParameterNames();
163: while (parameterNames.hasMoreElements()) {
164: String name = (String) parameterNames.nextElement();
165: parameters.put(name, this .request.getParameterValues(name));
166: }
167:
168: scheme = this .request.getScheme();
169: serverName = this .request.getServerName();
170: serverPort = this .request.getServerPort();
171: locale = this .request.getLocale();
172: locales = Collections.list(this .request.getLocales());
173: secure = this .request.isSecure();
174:
175: //Copy servlet specific data
176: cookies = this .request.getCookies();
177: method = this .request.getMethod();
178: pathInfo = this .request.getPathInfo();
179: pathTranslated = this .request.getPathTranslated();
180: queryString = this .request.getQueryString();
181: requestURI = this .request.getRequestURI();
182: requestURL = this .request.getRequestURL();
183: servletPath = this .request.getServletPath();
184: servletSession = this .request.getSession();
185: isRequestedSessionIdFromCookie = this .request
186: .isRequestedSessionIdFromCookie();
187: isRequestedSessionIdFromURL = this .request
188: .isRequestedSessionIdFromURL();
189: characterEncoding = this .request.getCharacterEncoding();
190: contentLength = this .request.getContentLength();
191: contentType = this .request.getContentType();
192: protocol = this .request.getProtocol();
193: remoteAddr = this .request.getRemoteAddr();
194: remoteHost = this .request.getRemoteHost();
195: initializeServlet2point4Properties();
196: }
197:
198: private void initializeServlet2point4Properties() {
199: ServletContext context = request.getSession()
200: .getServletContext();
201: if (context.getMajorVersion() > 1
202: && context.getMinorVersion() > 3) {
203: remotePort = this .request.getRemotePort();
204: localName = this .request.getLocalName();
205: localAddr = this .request.getLocalAddr();
206: localPort = this .request.getLocalPort();
207: }
208: }
209:
210: public boolean isUserInRole(String role) {
211: if (null != acegiAuthWrapper) {
212: return acegiAuthWrapper.isUserInRole(role);
213: }
214: return request.isUserInRole(role);
215: }
216:
217: public Cookie[] getCookies() {
218: return cookies;
219: }
220:
221: public void setAttribute(String name, Object value) {
222: super .setAttribute(name, value);
223: try {
224: request.setAttribute(name, value);
225: } catch (Exception e) {
226: //ignore because the container disposed servletRequest by now
227: }
228: }
229:
230: public void removeAttribute(String name) {
231: super .removeAttribute(name);
232: try {
233: request.removeAttribute(name);
234: } catch (Exception e) {
235: //ignore because the container disposed servletRequest by now
236: }
237: }
238:
239: public long getDateHeader(String name) {
240: String header = getHeader(name);
241: if (header == null) {
242: return -1;
243: }
244: //TODO
245: //Convert header string to a date
246: return -1;
247: }
248:
249: public String getHeader(String name) {
250: List values = (List) headers.get(name);
251: return values == null || values.isEmpty() ? null
252: : (String) values.get(0);
253: }
254:
255: public Enumeration getHeaders(String name) {
256: List values = (List) headers.get(name);
257: return Collections.enumeration(values);
258: }
259:
260: public Enumeration getHeaderNames() {
261: return Collections.enumeration(headers.keySet());
262: }
263:
264: public int getIntHeader(String name) {
265: String header = getHeader(name);
266: if (header == null) {
267: return -1;
268: }
269: return Integer.parseInt(name, -1);
270: }
271:
272: public String getMethod() {
273: return method;
274: }
275:
276: public String getPathInfo() {
277: return pathInfo;
278: }
279:
280: public String getPathTranslated() {
281: return pathTranslated;
282: }
283:
284: public String getQueryString() {
285: return queryString;
286: }
287:
288: public String getRequestURI() {
289: return requestURI;
290: }
291:
292: public StringBuffer getRequestURL() {
293: return requestURL;
294: }
295:
296: public String getServletPath() {
297: return servletPath;
298: }
299:
300: public HttpSession getSession(boolean create) {
301: return request.getSession(create);
302: }
303:
304: public HttpSession getSession() {
305: return servletSession;
306: }
307:
308: public boolean isRequestedSessionIdFromCookie() {
309: return isRequestedSessionIdFromCookie;
310: }
311:
312: public boolean isRequestedSessionIdFromURL() {
313: return isRequestedSessionIdFromURL;
314: }
315:
316: public boolean isRequestedSessionIdFromUrl() {
317: return isRequestedSessionIdFromURL();
318: }
319:
320: public String getCharacterEncoding() {
321: return characterEncoding;
322: }
323:
324: public void setCharacterEncoding(String encoding)
325: throws UnsupportedEncodingException {
326: characterEncoding = encoding;
327: }
328:
329: public int getContentLength() {
330: return contentLength;
331: }
332:
333: public String getContentType() {
334: return contentType;
335: }
336:
337: public ServletInputStream getInputStream() throws IOException {
338: return request.getInputStream();
339: }
340:
341: public String getProtocol() {
342: return protocol;
343: }
344:
345: public BufferedReader getReader() throws IOException {
346: return request.getReader();
347: }
348:
349: public String getRemoteAddr() {
350: return remoteAddr;
351: }
352:
353: public String getRemoteHost() {
354: return remoteHost;
355: }
356:
357: public RequestDispatcher getRequestDispatcher(String name) {
358: return request.getRequestDispatcher(name);
359: }
360:
361: public String getRealPath(String path) {
362: return request.getRealPath(path);
363: }
364:
365: public int getRemotePort() {
366: return remotePort;
367: }
368:
369: public String getLocalName() {
370: return localName;
371: }
372:
373: public String getLocalAddr() {
374: return localAddr;
375: }
376:
377: public int getLocalPort() {
378: return localPort;
379: }
380: }
|