1 28 29 package com.caucho.server.connection; 30 31 import com.caucho.server.session.SessionManager; 32 import com.caucho.server.webapp.WebApp; 33 import com.caucho.util.FreeList; 34 import com.caucho.util.L10N; 35 import com.caucho.vfs.ReadStream; 36 37 import javax.servlet.ServletException ; 38 import javax.servlet.ServletRequest ; 39 import javax.servlet.ServletRequestWrapper ; 40 import javax.servlet.http.Cookie ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import javax.servlet.http.HttpSession ; 44 import java.io.IOException ; 45 import java.util.HashMap ; 46 47 51 public class RequestAdapter extends RequestWrapper 52 implements CauchoRequest { 53 static final L10N L = new L10N(RequestAdapter.class); 54 55 static final int MAX_DEPTH = 64; 56 57 public static String REQUEST_URI = "javax.servlet.include.request_uri"; 58 public static String CONTEXT_PATH = "javax.servlet.include.context_path"; 59 public static String SERVLET_PATH = "javax.servlet.include.servlet_path"; 60 public static String PATH_INFO = "javax.servlet.include.path_info"; 61 public static String QUERY_STRING = "javax.servlet.include.query_string"; 62 63 public static String STATUS_CODE = "javax.servlet.error.status_code"; 64 public static String EXCEPTION_TYPE = "javax.servlet.error.exception_type"; 65 public static String MESSAGE = "javax.servlet.error.message"; 66 public static String EXCEPTION = "javax.servlet.error.exception"; 67 public static String ERROR_URI = "javax.servlet.error.request_uri"; 68 public static String SERVLET_NAME = "javax.servlet.error.servlet_name"; 69 70 public static String JSP_EXCEPTION = "javax.servlet.jsp.jspException"; 71 72 public static String SHUTDOWN = "com.caucho.shutdown"; 73 74 private static final FreeList<RequestAdapter> _freeList = 75 new FreeList<RequestAdapter>(16); 76 77 private WebApp _webApp; 79 private HttpServletResponse _response; 80 81 private HashMap <String ,String > _roleMap; 82 83 protected RequestAdapter() 84 { 85 super(null); 86 } 87 88 protected RequestAdapter(HttpServletRequest request, WebApp app) 89 { 90 super(request); 91 92 _webApp = app; 93 } 94 95 98 public static RequestAdapter create(HttpServletRequest request, 99 WebApp app) 100 { 101 RequestAdapter reqAdapt = _freeList.allocate(); 102 103 if (reqAdapt == null) 104 return new RequestAdapter(request, app); 105 else { 106 reqAdapt.setRequest(request); 107 reqAdapt._webApp = app; 108 109 return reqAdapt; 110 } 111 } 112 113 116 public static RequestAdapter create() 117 { 118 RequestAdapter reqAdapt = _freeList.allocate(); 119 120 if (reqAdapt != null) 121 return reqAdapt; 122 else 123 return new RequestAdapter(); 124 } 125 126 public void init(HttpServletRequest request, 127 HttpServletResponse response, 128 WebApp app) 129 throws ServletException 130 { 131 setRequest(request); 132 133 _response = response; 134 _webApp = app; 135 136 if (request == this || 137 request instanceof CauchoRequest && 138 ((CauchoRequest) request).getRequestDepth(0) > MAX_DEPTH) { 139 throw new ServletException (L.l("too many servlet includes `{0}'", 140 request.getRequestURI())); 141 } 142 } 143 144 public boolean isTop() 145 { 146 return false; 147 } 148 149 public void setWebApp(WebApp app) 150 { 151 _webApp = app; 152 } 153 154 protected HttpServletResponse getResponse() 155 { 156 return _response; 157 } 158 159 public void setResponse(CauchoResponse response) 160 { 161 _response = response; 162 } 163 164 167 public ReadStream getStream() throws IOException 168 { 169 if (getRequest() instanceof CauchoRequest) 170 return ((CauchoRequest) getRequest()).getStream(); 171 else 172 return null; 173 } 174 175 178 public String getPageURI() 179 { 180 String uri = (String ) getAttribute(REQUEST_URI); 181 182 if (uri != null) 183 return uri; 184 else 185 return getRequestURI(); 186 } 187 188 public static String getPageURI(HttpServletRequest request) 189 { 190 String uri = (String ) request.getAttribute(REQUEST_URI); 191 192 if (uri != null) 193 return uri; 194 else 195 return request.getRequestURI(); 196 } 197 198 public String getPageContextPath() 199 { 200 String contextPath = (String ) getAttribute(CONTEXT_PATH); 201 202 if (contextPath != null) 203 return contextPath; 204 else 205 return getContextPath(); 206 } 207 208 public static String getPageContextPath(HttpServletRequest request) 209 { 210 String contextPath = (String ) request.getAttribute(CONTEXT_PATH); 211 212 if (contextPath != null) 213 return contextPath; 214 else 215 return request.getContextPath(); 216 } 217 218 222 public String getPageServletPath() 223 { 224 String servletPath = (String ) getAttribute(SERVLET_PATH); 225 226 if (servletPath != null) 227 return servletPath; 228 else 229 return getServletPath(); 230 } 231 232 236 public static String getPageServletPath(HttpServletRequest request) 237 { 238 String servletPath = (String ) request.getAttribute(SERVLET_PATH); 239 240 if (servletPath != null) 241 return servletPath; 242 else 243 return request.getServletPath(); 244 } 245 246 250 public String getPagePathInfo() 251 { 252 String uri = (String ) getAttribute(REQUEST_URI); 253 254 if (uri != null) 255 return (String ) getAttribute(PATH_INFO); 256 else 257 return getPathInfo(); 258 } 259 260 264 public static String getPagePathInfo(HttpServletRequest request) 265 { 266 String uri = (String ) request.getAttribute(REQUEST_URI); 267 268 if (uri != null) 269 return (String ) request.getAttribute(PATH_INFO); 270 else 271 return request.getPathInfo(); 272 } 273 274 278 public String getPageQueryString() 279 { 280 String uri = (String ) getAttribute(REQUEST_URI); 281 282 if (uri != null) 283 return (String ) getAttribute(QUERY_STRING); 284 else 285 return getQueryString(); 286 } 287 288 292 public static String getPageQueryString(HttpServletRequest request) 293 { 294 String uri = (String ) request.getAttribute(REQUEST_URI); 295 296 if (uri != null) 297 return (String ) request.getAttribute(QUERY_STRING); 298 else 299 return request.getQueryString(); 300 } 301 302 public int getRequestDepth(int depth) 303 { 304 if (depth > MAX_DEPTH) 305 throw new RuntimeException (L.l("too many request dispatchers")); 306 307 ServletRequest req = getRequest(); 308 while (req != null) { 309 if (req instanceof CauchoRequest) 310 return ((CauchoRequest) req).getRequestDepth(depth + 1); 311 else if (req instanceof ServletRequestWrapper ) { 312 ServletRequestWrapper reqWrap = (ServletRequestWrapper ) req; 313 314 req = reqWrap.getRequest(); 315 } 316 else 317 break; 318 } 319 320 return depth + 2; 321 } 322 323 public void setHeader(String key, String value) 324 { 325 } 326 327 public WebApp getWebApp() 328 { 329 return _webApp; 330 } 331 332 public void setVaryCookie(String cookie) 333 { 334 336 if (getRequest() instanceof CauchoRequest) 337 ((CauchoRequest) getRequest()).setVaryCookie(cookie); 338 } 339 340 public String getVaryCookie() 341 { 342 344 if (getRequest() instanceof CauchoRequest) 345 return ((CauchoRequest) getRequest()).getVaryCookie(); 346 else 347 return null; 348 } 349 350 public boolean getVaryCookies() 351 { 352 354 if (getRequest() instanceof CauchoRequest) 355 return ((CauchoRequest) getRequest()).getVaryCookies(); 356 else 357 return false; 358 } 359 360 public void setHasCookie() 361 { 362 364 if (getRequest() instanceof CauchoRequest) 365 ((CauchoRequest) getRequest()).setHasCookie(); 366 } 367 368 public boolean getHasCookie() 369 { 370 372 if (getRequest() instanceof CauchoRequest) 373 return ((CauchoRequest) getRequest()).getHasCookie(); 374 else 375 return false; 376 } 377 378 public HttpSession getSession(boolean create) 379 { 380 SessionManager manager = getSessionManager(); 381 382 setVaryCookie(getCookieName(manager)); 383 384 HttpSession session = super.getSession(create); 385 386 if (session != null) 387 setHasCookie(); 388 389 return session; 390 } 391 392 public String getRequestedSessionId() 393 { 394 SessionManager manager = getSessionManager(); 395 396 setVaryCookie(getCookieName(manager)); 397 398 String id = super.getRequestedSessionId(); 399 400 if (id != null) 401 setHasCookie(); 402 403 return id; 404 } 405 406 public boolean isRequestedSessionIdValid() 407 { 408 SessionManager manager = getSessionManager(); 409 410 setVaryCookie(getCookieName(manager)); 411 412 boolean isValid = super.isRequestedSessionIdValid(); 413 414 if (isValid) 415 setHasCookie(); 416 417 return isValid; 418 } 419 420 public boolean isRequestedSessionIdFromCookie() 421 { 422 SessionManager manager = getSessionManager(); 423 424 setVaryCookie(getCookieName(manager)); 425 426 boolean isValid = super.isRequestedSessionIdFromCookie(); 427 if (isValid) 428 setHasCookie(); 429 430 return isValid; 431 } 432 433 public boolean isRequestedSessionIdFromURL() 434 { 435 SessionManager manager = getSessionManager(); 436 437 setVaryCookie(getCookieName(manager)); 438 439 boolean isValid = super.isRequestedSessionIdFromURL(); 440 441 if (isValid) 442 setHasCookie(); 443 444 return isValid; 445 } 446 447 protected final SessionManager getSessionManager() 448 { 449 WebApp app = getWebApp(); 450 if (app != null) 451 return app.getSessionManager(); 452 else 453 return null; 454 } 455 456 protected final String getCookieName(SessionManager manager) 457 { 458 if (isSecure()) 459 return manager.getCookieName(); 460 else 461 return manager.getSSLCookieName(); 462 } 463 464 public Cookie []getCookies() 465 { 466 setVaryCookie(null); 468 469 Cookie []cookies = super.getCookies(); 470 if (cookies != null && cookies.length > 0) 471 setHasCookie(); 472 473 return cookies; 474 } 475 476 public Cookie getCookie(String name) 477 { 478 setVaryCookie(name); 480 481 if (getRequest() instanceof CauchoRequest) 482 return ((CauchoRequest) getRequest()).getCookie(name); 483 484 Cookie []cookies = super.getCookies(); 485 for (int i = 0; i < cookies.length; i++) { 486 if (cookies[i].getName().equals(name)) { 487 setHasCookie(); 488 return cookies[i]; 489 } 490 } 491 492 return null; 493 } 494 495 public void killKeepalive() 496 { 497 } 498 499 public boolean allowKeepalive() 500 { 501 return true; 502 } 503 504 507 public HashMap <String ,String > setRoleMap(HashMap <String ,String > map) 508 { 509 HashMap <String ,String > oldMap = _roleMap; 510 _roleMap = map; 511 512 return oldMap; 513 } 514 515 518 public boolean isUserInRole(String role) 519 { 520 if (_roleMap != null) { 521 String newRole = _roleMap.get(role); 522 523 if (newRole != null) 524 role = newRole; 525 } 526 527 return super.isUserInRole(role); 528 } 529 530 public boolean authenticate() 531 throws ServletException , IOException 532 { 533 return true; 534 } 535 536 539 public static void free(RequestAdapter reqAdapt) 540 { 541 reqAdapt.free(); 542 543 _freeList.free(reqAdapt); 544 } 545 546 549 protected void free() 550 { 551 super.free(); 552 553 _webApp = null; 554 _response = null; 555 } 556 } 557 | Popular Tags |