1 29 30 package com.caucho.server.webapp; 31 32 import com.caucho.i18n.CharacterEncoding; 33 import com.caucho.log.Log; 34 import com.caucho.server.connection.CauchoRequest; 35 import com.caucho.server.connection.CauchoResponse; 36 import com.caucho.server.connection.Form; 37 import com.caucho.server.connection.RequestAdapter; 38 import com.caucho.server.connection.ServletInputStreamImpl; 39 import com.caucho.server.session.SessionImpl; 40 import com.caucho.server.session.SessionManager; 41 import com.caucho.util.Alarm; 42 import com.caucho.util.CharBuffer; 43 import com.caucho.util.FreeList; 44 import com.caucho.util.HashMapImpl; 45 import com.caucho.vfs.BufferedReaderAdapter; 46 import com.caucho.vfs.Encoding; 47 import com.caucho.vfs.ReadStream; 48 49 import javax.servlet.RequestDispatcher ; 50 import javax.servlet.ServletException ; 51 import javax.servlet.ServletInputStream ; 52 import javax.servlet.http.HttpServletRequest ; 53 import javax.servlet.http.HttpServletResponse ; 54 import javax.servlet.http.HttpSession ; 55 import java.io.BufferedReader ; 56 import java.io.IOException ; 57 import java.util.Collections ; 58 import java.util.Enumeration ; 59 import java.util.Map ; 60 import java.util.logging.Logger ; 61 64 class DispatchRequest extends RequestAdapter { 65 protected static final Logger log = Log.open(DispatchRequest.class); 66 67 private static final FreeList<DispatchRequest> _freeList = 68 new FreeList<DispatchRequest>(32); 69 70 private WebApp _webApp; 71 private WebApp _oldWebApp; 72 private Form _formParser; 73 private HashMapImpl<String ,String []> _form; 74 protected ReadStream _readStream; 75 protected ServletInputStreamImpl _is; 76 private BufferedReaderAdapter _bufferedReader; 78 private String _method; 79 private String _uri; 80 private String _servletPath; 81 private String _pathInfo; 82 private String _queryString; 83 private String _addedQuery; 84 private SessionImpl _session; 85 86 private String _pageUri; 87 private String _pageContextPath; 88 private String _pageServletPath; 89 private String _pagePathInfo; 90 private String _pageQueryString; 91 92 protected DispatchRequest() 93 { 94 } 95 96 99 public static DispatchRequest createDispatch() 100 { 101 DispatchRequest req = _freeList.allocate(); 102 if (req == null) 103 req = new DispatchRequest(); 104 105 return req; 106 } 107 108 void init(WebApp webApp, 109 WebApp oldWebApp, 110 HttpServletRequest request, 111 HttpServletResponse response, 112 String method, String uri, 113 String servletPath, String pathInfo, 114 String queryString, String addedQuery) 115 throws ServletException 116 { 117 super.init(request, response, webApp); 118 119 _webApp = webApp; 120 _oldWebApp = oldWebApp; 121 122 _form = null; 123 124 _readStream = null; 125 _is = null; 126 127 _bufferedReader = null; 128 129 _method = method; 130 _uri = uri; 131 _servletPath = servletPath; 132 _pathInfo = pathInfo; 133 _queryString = queryString; 134 _addedQuery = addedQuery; 135 136 _pageUri = null; 137 _pageContextPath = null; 138 _pageServletPath = null; 139 _pagePathInfo = null; 140 _pageQueryString = null; 141 142 _session = null; 143 } 144 145 void setStream(ReadStream readStream) 146 { 147 _readStream = readStream; 148 } 149 150 public WebApp getWebApp() 151 { 152 return _webApp; 153 } 154 155 public String getMethod() 156 { 157 return _method; 158 } 159 160 public String getRequestURI() 161 { 162 return _uri; 163 } 164 165 168 public StringBuffer getRequestURL() 169 { 170 StringBuffer sb = new StringBuffer (); 171 172 sb.append(getScheme()); 173 sb.append("://"); 174 sb.append(getServerName()); 175 if (getServerPort() > 0 && 176 getServerPort() != 80 && 177 getServerPort() != 443) { 178 sb.append(":"); 179 sb.append(getServerPort()); 180 } 181 sb.append(getRequestURI()); 182 183 return sb; 184 } 185 186 void setPageURI(String uri) 187 { 188 _pageUri = uri; 189 } 190 191 public String getPageURI() 192 { 193 return _pageUri; 194 } 195 196 199 public String getContextPath() 200 { 201 if (_webApp != null) 202 return _webApp.getContextPath(); 203 else 204 return "/"; 205 } 206 207 210 void setPageContextPath(String contextPath) 211 { 212 _pageContextPath = contextPath; 213 } 214 215 218 public String getPageContextPath() 219 { 220 return _pageContextPath; 221 } 222 223 public String getServletPath() 224 { 225 return _servletPath; 226 } 227 228 public String getPageServletPath() 229 { 230 return _pageServletPath; 231 } 232 233 void setPageServletPath(String servletPath) 234 { 235 _pageServletPath = servletPath; 236 } 237 238 public String getPathInfo() 239 { 240 return _pathInfo; 241 } 242 243 public String getPagePathInfo() 244 { 245 return _pagePathInfo; 246 } 247 248 void setPagePathInfo(String pathInfo) 249 { 250 _pagePathInfo = pathInfo; 251 } 252 253 public String getQueryString() 254 { 255 return _queryString; 256 } 257 258 void setPageQueryString(String queryString) 259 { 260 _pageQueryString = queryString; 261 } 262 263 public String getPageQueryString() 264 { 265 return _pageQueryString; 266 } 267 268 public Map getParameterMap() 269 { 270 if (_form == null) 271 _form = parseQuery(); 272 273 return _form; 274 } 275 276 public Enumeration getParameterNames() 277 { 278 if (_form == null) 279 _form = parseQuery(); 280 281 return Collections.enumeration(_form.keySet()); 282 } 283 284 public String []getParameterValues(String name) 285 { 286 if (_form == null) 287 _form = parseQuery(); 288 289 return _form.get(name); 290 } 291 292 public String getParameter(String name) 293 { 294 String []values = getParameterValues(name); 295 if (values == null || values.length == 0) 296 return null; 297 298 return values[0]; 299 } 300 301 private HashMapImpl<String ,String []> parseQuery() 302 { 303 HashMapImpl<String ,String []> table = new HashMapImpl<String ,String []>(); 304 305 String defaultEncoding = CharacterEncoding.getLocalEncoding(); 306 String charEncoding = getCharacterEncoding(); 307 if (charEncoding == null) 308 charEncoding = defaultEncoding; 309 String javaEncoding = Encoding.getJavaName(charEncoding); 310 311 if (_addedQuery != null) { 312 try { 313 if (_formParser == null) 314 _formParser = new Form(); 315 _formParser.parseQueryString(table, _addedQuery, javaEncoding, false); 316 } catch (Exception e) { 317 } 318 } 319 320 Enumeration en = super.getParameterNames(); 321 while (en.hasMoreElements()) { 322 String key = (String ) en.nextElement(); 323 String []oldValues = (String []) super.getParameterValues(key); 324 String []newValues = (String []) table.get(key); 325 326 if (oldValues == null) { 327 } 328 else if (newValues == null) 329 table.put(key, oldValues); 330 else { 331 String []next = new String [oldValues.length + newValues.length]; 332 System.arraycopy(newValues, 0, next, 0, newValues.length); 333 System.arraycopy(oldValues, 0, next, newValues.length, 334 oldValues.length); 335 table.put(key, next); 336 } 337 } 338 339 return table; 340 } 341 342 public String getRealPath(String path) 343 { 344 return _webApp.getRealPath(path); 345 } 346 347 public String getPathTranslated() 348 { 349 if (_pathInfo != null) 350 return getRealPath(_pathInfo); 351 else 352 return null; 353 } 354 355 public RequestDispatcher getRequestDispatcher(String path) 356 { 357 if (path.startsWith("/")) 358 return _webApp.getRequestDispatcher(path); 359 else 360 return _webApp.getRequestDispatcher(getPwd() + path); 361 } 362 363 public String getPwd() 364 { 365 CharBuffer cb = CharBuffer.allocate(); 366 367 String servletPath = getPageServletPath(); 368 if (servletPath != null) 369 cb.append(servletPath); 370 String pathInfo = getPagePathInfo(); 371 if (pathInfo != null) 372 cb.append(pathInfo); 373 374 int p = cb.lastIndexOf('/'); 375 if (p >= 0) 376 cb.setLength(p); 377 cb.append('/'); 378 379 return cb.close(); 380 } 381 382 public ReadStream getStream() 383 throws IOException 384 { 385 if (_readStream == null && getRequest() instanceof CauchoRequest) 386 return ((CauchoRequest) getRequest()).getStream(); 387 else 388 return _readStream; 389 } 390 391 public ServletInputStream getInputStream() 392 throws IOException 393 { 394 if (_readStream == null) 395 return super.getInputStream(); 396 397 if (_is == null) 398 _is = new ServletInputStreamImpl(); 399 _is.init(_readStream); 400 401 return _is; 402 } 403 404 public BufferedReader getReader() 405 throws IOException 406 { 407 if (_readStream == null) 408 return super.getReader(); 409 410 if (_bufferedReader == null) 411 _bufferedReader = new BufferedReaderAdapter(getStream()); 412 413 _bufferedReader.init(getStream()); 415 416 return _bufferedReader; 417 } 418 419 426 @Override 427 public HttpSession getSession(boolean create) 428 { 429 SessionManager manager = _webApp.getSessionManager(); 430 431 setVaryCookie(manager.getCookieName()); 432 433 if (_session != null && _session.isValid()) { 434 setHasCookie(); 435 436 return _session; 437 } 438 439 if (_webApp == _oldWebApp) { 440 HttpSession hSession = super.getSession(create); 441 if (hSession != null) 442 setHasCookie(); 443 444 return hSession; 445 } 446 else { 447 SessionImpl oldSession = _session; 448 _session = createSession(create, oldSession != null); 449 } 450 451 if (_session != null) 452 setHasCookie(); 453 454 return _session; 455 } 456 457 466 private SessionImpl createSession(boolean create, boolean hasOldSession) 467 { 468 SessionManager manager = getSessionManager(); 469 470 String id = getRequestedSessionId(); 471 472 long now = Alarm.getCurrentTime(); 473 474 SessionImpl session; 475 476 if (id != null && id.length() > 6) { 477 session = manager.getSession(id, now, create, 478 isRequestedSessionIdFromCookie()); 479 if (session == null) { 480 } 481 else if (session.isValid()) { 482 if (session != null) 483 setHasCookie(); 484 if (! session.getId().equals(id) && manager.enableSessionCookies()) { 485 HttpServletResponse response = getResponse(); 486 487 if (response instanceof CauchoResponse) 488 ((CauchoResponse) getResponse()).setSessionId(session.getId()); 489 } 490 491 return session; 492 } 493 } 494 else 495 id = null; 496 497 if (! create) 498 return null; 499 500 506 session = manager.createSession(id, now, this, 507 isRequestedSessionIdFromCookie()); 508 509 if (session != null) 510 setHasCookie(); 511 512 if (session.getId().equals(id)) 513 return session; 514 515 if (manager.enableSessionCookies()) { 516 HttpServletResponse response = getResponse(); 517 518 if (response instanceof CauchoResponse) 519 ((CauchoResponse) getResponse()).setSessionId(session.getId()); 520 } 521 522 return session; 523 } 524 525 public boolean authenticate() 526 throws ServletException , IOException 527 { 528 if (! (getRequest() instanceof CauchoRequest)) 529 return false; 530 else 531 return ((CauchoRequest) getRequest()).authenticate(); 532 } 533 534 537 public void finish() 538 throws IOException 539 { 540 SessionImpl session = _session; 541 _session = null; 542 543 if (session != null) 544 session.finish(); 545 } 546 547 550 public static void free(DispatchRequest req) 551 { 552 req.free(); 553 554 _freeList.free(req); 555 } 556 557 560 protected void free() 561 { 562 super.free(); 563 564 _session = null; 565 _webApp = null; 566 _oldWebApp = null; 567 _readStream = null; 568 569 if (_is != null) 570 _is.free(); 571 } 572 573 public String toString() 574 { 575 return "DispatchRequest[" + getRequest() + "]"; 576 } 577 } 578 | Popular Tags |