1 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 ; 42 import javax.servlet.http.HttpServletRequest ; 43 import javax.servlet.http.HttpSession ; 44 import java.util.Enumeration ; 45 import java.util.Hashtable ; 46 import java.util.LinkedList ; 47 import java.util.Vector ; 48 49 63 public class ServletEnvironmentRequest 64 extends CommonEnvironmentRequest 65 implements HttpServletRequest { 66 67 protected static Log log = 68 LogFactory.getLog(ServletEnvironmentRequest.class); 69 private static String ACEGI_AUTH_CLASS = "org.acegisecurity.Authentication"; 70 HttpServletRequest servletRequest; 71 private String localName; 72 private String localAddr; 73 private int localPort; 74 private Hashtable headers; 75 private Cookie [] cookies; 76 private String method; 77 private String pathInfo; 78 private String pathTranslated; 79 private String queryString; 80 private String requestURI; 81 private StringBuffer requestURL; 82 private String servletPath; 83 private HttpSession servletSession; 84 private boolean isRequestedSessionIdFromCookie; 85 private boolean isRequestedSessionIdFromURL; 86 private String characterEncoding; 87 private int contentLength; 88 private String contentType; 89 private String protocol; 90 private String remoteAddr; 91 private String remoteHost; 92 private int remotePort; 93 private AcegiAuthWrapper acegiAuthWrapper; 94 private static Class 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 t) { 103 if (log.isDebugEnabled()) { 104 log.debug("Acegi Security not detected."); 105 } 106 } 107 } 108 109 public ServletEnvironmentRequest(HttpServletRequest req) { 110 servletRequest = req; 111 112 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 (); 126 Enumeration items = req.getAttributeNames(); 127 String name = null; 128 while (items.hasMoreElements()) { 129 name = (String ) items.nextElement(); 130 Object attribute = req.getAttribute(name); 131 if ((null != name) && (null != attribute)) { 132 attributes.put(name, attribute); 133 } 134 } 135 136 String [] incAttrKeys = Constants.INC_CONSTANTS; 144 for (int index = 0; index < incAttrKeys.length; index++) { 145 String incAttrKey = incAttrKeys[index]; 146 Object incAttrVal = req.getAttribute(incAttrKey); 147 if( incAttrVal != null ){ 148 attributes.put(incAttrKey,req.getAttribute(incAttrKey)); 149 } 150 } 151 152 headers = new Hashtable (); 153 items = req.getHeaderNames(); 154 name = null; 155 while (items.hasMoreElements()) { 156 name = (String ) items.nextElement(); 157 Enumeration values = req.getHeaders(name); 158 LinkedList valueList = new LinkedList (); 159 while (values.hasMoreElements()) { 160 valueList.add(values.nextElement()); 161 } 162 headers.put(name, valueList); 163 } 164 165 parameters = new Hashtable (); 166 items = req.getParameterNames(); 167 while (items.hasMoreElements()) { 168 name = (String ) 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 (); 178 items = req.getLocales(); 179 while (items.hasMoreElements()) { 180 locales.add(items.nextElement()); 181 } 182 183 isSecure = req.isSecure(); 184 185 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 210 } 211 212 213 216 217 public boolean isUserInRole(String role) { 218 if (null != acegiAuthWrapper) { 219 return acegiAuthWrapper.isUserInRole(role); 220 } 221 return servletRequest.isUserInRole(role); 222 } 223 224 225 228 229 public Cookie [] getCookies() { 230 return cookies; 231 } 232 233 234 public void setAttribute(String name, Object value) { 235 super.setAttribute(name, value); 236 try { 237 servletRequest.setAttribute(name, value); 238 } catch (Exception e) { 239 } 241 } 242 243 public void removeAttribute(String name) { 244 super.removeAttribute(name); 245 try { 246 servletRequest.removeAttribute(name); 247 } catch (Exception e) { 248 } 250 } 251 252 public long getDateHeader(String name) { 253 String header = getHeader(name); 254 if (header == null) { 255 return -1; 256 } 257 return -1; 260 } 261 262 public String getHeader(String name) { 263 LinkedList values = (LinkedList ) headers.get(name); 264 return values == null || values.isEmpty() ? 265 null : (String ) values.getFirst(); 266 } 267 268 public Enumeration getHeaders(String name) { 269 LinkedList values = (LinkedList ) 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 name) { 278 String header = getHeader(name); 279 if (header == null) { 280 return -1; 281 } 282 return Integer.parseInt(name, -1); 283 } 284 285 public String getMethod() { 286 return method; 287 } 288 289 public String getPathInfo() { 290 return pathInfo; 291 } 292 293 public String getPathTranslated() { 294 return pathTranslated; 295 } 296 297 public String getQueryString() { 298 return queryString; 299 } 301 302 public String getRequestURI() { 303 return requestURI; 304 } 305 306 public StringBuffer getRequestURL() { 307 return requestURL; 308 } 310 311 public String getServletPath() { 312 return servletPath; 313 } 315 316 public HttpSession getSession(boolean create) { 317 return servletRequest.getSession(create); 318 } 319 320 public HttpSession 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 getCharacterEncoding() { 337 return characterEncoding; 338 } 339 340 public void setCharacterEncoding(String encoding) 341 throws java.io.UnsupportedEncodingException { 342 characterEncoding = encoding; 343 } 344 345 public int getContentLength() { 346 return contentLength; 347 } 348 349 public String getContentType() { 350 return contentType; 351 } 352 353 public javax.servlet.ServletInputStream getInputStream() 354 throws java.io.IOException { 355 return null; 356 } 357 358 public String getProtocol() { 359 return protocol; 360 } 362 363 public java.io.BufferedReader getReader() throws java.io.IOException { 364 return null; 365 } 366 367 public String getRemoteAddr() { 368 return remoteAddr; 369 } 370 371 public String getRemoteHost() { 372 return remoteHost; 373 } 374 375 public javax.servlet.RequestDispatcher getRequestDispatcher(String name) { 376 return null; 377 } 378 379 382 public String getRealPath(String path) { 383 return servletRequest.getRealPath(path); 384 } 385 386 public int getRemotePort() { 387 return remotePort; 388 } 389 390 public String getLocalName() { 391 return localName; 392 } 393 394 public String getLocalAddr() { 395 return localAddr; 396 } 397 398 public int getLocalPort() { 399 return localPort; 400 } 401 402 } 403 | Popular Tags |