1 16 17 package org.mortbay.jetty.servlet; 18 19 import java.io.BufferedReader ; 20 import java.io.File ; 21 import java.io.InputStreamReader ; 22 import java.io.UnsupportedEncodingException ; 23 import java.security.Principal ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Enumeration ; 27 import java.util.List ; 28 import java.util.Locale ; 29 import java.util.Map ; 30 31 import javax.servlet.RequestDispatcher ; 32 import javax.servlet.ServletInputStream ; 33 import javax.servlet.ServletRequest ; 34 import javax.servlet.ServletRequestWrapper ; 35 import javax.servlet.http.Cookie ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpSession ; 38 39 import org.apache.commons.logging.Log; 40 import org.mortbay.log.LogFactory; 41 import org.mortbay.http.HttpConnection; 42 import org.mortbay.http.HttpFields; 43 import org.mortbay.http.HttpInputStream; 44 import org.mortbay.http.HttpRequest; 45 import org.mortbay.http.SecurityConstraint; 46 import org.mortbay.util.LazyList; 47 import org.mortbay.util.LogSupport; 48 import org.mortbay.util.Resource; 49 import org.mortbay.util.StringUtil; 50 import org.mortbay.util.URI; 51 52 53 54 65 public class ServletHttpRequest 66 implements HttpServletRequest 67 { 68 private static Log log = LogFactory.getLog(ServletHttpRequest.class); 69 70 71 public static final String 72 __SESSIONID_NOT_CHECKED = "not checked", 73 __SESSIONID_URL = "url", 74 __SESSIONID_COOKIE = "cookie", 75 __SESSIONID_NONE = "none"; 76 77 private static final Enumeration __emptyEnum = 78 Collections.enumeration(Collections.EMPTY_LIST); 79 private static final Collection __defaultLocale = 80 Collections.singleton(Locale.getDefault()); 81 82 private ServletHandler _servletHandler; 83 private HttpRequest _httpRequest; 84 private ServletHttpResponse _servletHttpResponse; 85 86 private String _contextPath=null; 87 private String _servletPath=null; 88 private String _pathInfo=null; 89 private String _query=null; 90 private String _pathTranslated=null; 91 private String _requestedSessionId=null; 92 private HttpSession _session=null; 93 private String _sessionIdState=__SESSIONID_NOT_CHECKED; 94 private ServletIn _in =null; 95 private BufferedReader _reader=null; 96 private int _inputState=0; 97 private ServletHolder _servletHolder; 98 private String _pathInContext; 99 100 101 103 public ServletHttpRequest(ServletHandler servletHandler, 104 String pathInContext, 105 HttpRequest request) 106 { 107 _servletHandler=servletHandler; 108 _pathInContext=pathInContext; 109 _contextPath=_servletHandler.getHttpContext().getContextPath(); 110 if (_contextPath.length()<=1) 111 _contextPath=""; 112 113 _httpRequest=request; 114 } 115 116 117 void recycle(ServletHandler servletHandler,String pathInContext) 118 { 119 _servletHandler=servletHandler; 120 _pathInContext=pathInContext; 121 _servletPath=null; 122 _pathInfo=null; 123 _query=null; 124 _pathTranslated=null; 125 _requestedSessionId=null; 126 _session=null; 127 _sessionIdState=__SESSIONID_NOT_CHECKED; 128 _in=null; 129 _reader=null; 130 _inputState=0; 131 _servletHolder=null; 132 133 if (servletHandler!=null) 134 _contextPath=_servletHandler.getHttpContext().getContextPath(); 135 if (_contextPath!=null&&_contextPath.length()<=1) 136 _contextPath=""; 137 } 138 139 140 141 ServletHandler getServletHandler() 142 { 143 return _servletHandler; 144 } 145 146 147 void setServletHandler(ServletHandler servletHandler) 148 { 149 _servletHandler=servletHandler; 150 } 151 152 153 159 void setServletPaths(String servletPath, 160 String pathInfo, 161 ServletHolder holder) 162 { 163 _servletPath=servletPath; 164 _pathInfo=pathInfo; 165 _servletHolder=holder; 166 } 167 168 169 ServletHolder getServletHolder() 170 { 171 return _servletHolder; 172 } 173 174 175 String getPathInContext() 176 { 177 return _pathInContext; 178 } 179 180 181 HttpRequest getHttpRequest() 182 { 183 return _httpRequest; 184 } 185 186 187 public ServletHttpResponse getServletHttpResponse() 188 { 189 return _servletHttpResponse; 190 } 191 192 193 void setServletHttpResponse(ServletHttpResponse response) 194 { 195 _servletHttpResponse = response; 196 } 197 198 199 public Locale getLocale() 200 { 201 Enumeration enm = _httpRequest.getFieldValues(HttpFields.__AcceptLanguage, 202 HttpFields.__separators); 203 204 if (enm == null || !enm.hasMoreElements()) 206 return Locale.getDefault(); 207 208 List acceptLanguage = HttpFields.qualityList(enm); 210 if (acceptLanguage.size()==0) 211 return Locale.getDefault(); 212 213 int size=acceptLanguage.size(); 214 215 for (int i=0; i<size; i++) 217 { 218 String language = (String )acceptLanguage.get(i); 219 language=HttpFields.valueParameters(language,null); 220 String country = ""; 221 int dash = language.indexOf('-'); 222 if (dash > -1) 223 { 224 country = language.substring(dash + 1).trim(); 225 language = language.substring(0,dash).trim(); 226 } 227 return new Locale (language,country); 228 } 229 230 return Locale.getDefault(); 231 } 232 233 234 public Enumeration getLocales() 235 { 236 Enumeration enm = _httpRequest.getFieldValues(HttpFields.__AcceptLanguage, 237 HttpFields.__separators); 238 239 if (enm == null || !enm.hasMoreElements()) 241 return Collections.enumeration(__defaultLocale); 242 243 List acceptLanguage = HttpFields.qualityList(enm); 245 246 if (acceptLanguage.size()==0) 247 return 248 Collections.enumeration(__defaultLocale); 249 250 Object langs = null; 251 int size=acceptLanguage.size(); 252 253 for (int i=0; i<size; i++) 255 { 256 String language = (String )acceptLanguage.get(i); 257 language=HttpFields.valueParameters(language,null); 258 String country = ""; 259 int dash = language.indexOf('-'); 260 if (dash > -1) 261 { 262 country = language.substring(dash + 1).trim(); 263 language = language.substring(0,dash).trim(); 264 } 265 langs=LazyList.ensureSize(langs,size); 266 langs=LazyList.add(langs,new Locale (language,country)); 267 } 268 269 if (LazyList.size(langs)==0) 270 return Collections.enumeration(__defaultLocale); 271 272 return Collections.enumeration(LazyList.getList(langs)); 273 } 274 275 276 public boolean isSecure() 277 { 278 return _httpRequest.isConfidential(); 279 } 280 281 282 public Cookie [] getCookies() 283 { 284 Cookie [] cookies = _httpRequest.getCookies(); 285 if (cookies.length==0) 286 return null; 287 return cookies; 288 } 289 290 291 public long getDateHeader(String name) 292 { 293 return _httpRequest.getDateField(name); 294 } 295 296 297 public Enumeration getHeaderNames() 298 { 299 return _httpRequest.getFieldNames(); 300 } 301 302 303 public String getHeader(String name) 304 { 305 return _httpRequest.getField(name); 306 } 307 308 309 public Enumeration getHeaders(String s) 310 { 311 Enumeration enm=_httpRequest.getFieldValues(s); 312 if (enm==null) 313 return __emptyEnum; 314 return enm; 315 } 316 317 318 public int getIntHeader(String name) 319 throws NumberFormatException 320 { 321 return _httpRequest.getIntField(name); 322 } 323 324 325 public String getMethod() 326 { 327 return _httpRequest.getMethod(); 328 } 329 330 331 public String getContextPath() 332 { 333 return _contextPath; 334 } 335 336 337 public String getPathInfo() 338 { 339 if (_servletPath==null) 340 return null; 341 return _pathInfo; 342 } 343 344 345 public String getPathTranslated() 346 { 347 if (_pathInfo==null || _pathInfo.length()==0) 348 return null; 349 if (_pathTranslated==null) 350 { 351 Resource resource = 352 _servletHandler.getHttpContext().getBaseResource(); 353 354 if (resource==null) 355 return null; 356 357 try 358 { 359 resource=resource.addPath(_pathInfo); 360 File file = resource.getFile(); 361 if (file==null) 362 return null; 363 _pathTranslated=file.getAbsolutePath(); 364 } 365 catch(Exception e) 366 { 367 log.debug(LogSupport.EXCEPTION,e); 368 } 369 } 370 371 return _pathTranslated; 372 } 373 374 375 public String getQueryString() 376 { 377 if (_query==null) 378 _query =_httpRequest.getQuery(); 379 return _query; 380 } 381 382 383 public String getAuthType() 384 { 385 String at= _httpRequest.getAuthType(); 386 if (at==SecurityConstraint.__BASIC_AUTH) 387 return HttpServletRequest.BASIC_AUTH; 388 if (at==SecurityConstraint.__FORM_AUTH) 389 return HttpServletRequest.FORM_AUTH; 390 if (at==SecurityConstraint.__DIGEST_AUTH) 391 return HttpServletRequest.DIGEST_AUTH; 392 if (at==SecurityConstraint.__CERT_AUTH) 393 return HttpServletRequest.CLIENT_CERT_AUTH; 394 if (at==SecurityConstraint.__CERT_AUTH2) 395 return HttpServletRequest.CLIENT_CERT_AUTH; 396 return at; 397 } 398 399 400 public String getRemoteUser() 401 { 402 return _httpRequest.getAuthUser(); 403 } 404 405 406 public boolean isUserInRole(String role) 407 { 408 if (_servletHolder!=null) 409 role=_servletHolder.getUserRoleLink(role); 410 return _httpRequest.isUserInRole(role); 411 } 412 413 414 public Principal getUserPrincipal() 415 { 416 return _httpRequest.getUserPrincipal(); 417 } 418 419 420 void setRequestedSessionId(String pathParams) 421 { 422 _requestedSessionId=null; 423 424 if (_servletHandler.isUsingCookies()) 426 { 427 Cookie [] cookies=_httpRequest.getCookies(); 428 if (cookies!=null && cookies.length>0) 429 { 430 for (int i=0;i<cookies.length;i++) 431 { 432 if (SessionManager.__SessionCookie.equalsIgnoreCase(cookies[i].getName())) 433 { 434 if (_requestedSessionId!=null) 435 { 436 SessionManager manager = _servletHandler.getSessionManager(); 440 if (manager!=null && manager.getHttpSession(_requestedSessionId)!=null) 441 break; 442 log.debug("multiple session cookies"); 443 } 444 445 _requestedSessionId=cookies[i].getValue(); 446 _sessionIdState = __SESSIONID_COOKIE; 447 if(log.isDebugEnabled())log.debug("Got Session "+_requestedSessionId+" from cookie"); 448 } 449 } 450 } 451 } 452 453 if (pathParams!=null && pathParams.startsWith(SessionManager.__SessionURL)) 455 { 456 String id = 457 pathParams.substring(SessionManager.__SessionURL.length()+1); 458 if(log.isDebugEnabled())log.debug("Got Session "+id+" from URL"); 459 460 if (_requestedSessionId==null) 461 { 462 _requestedSessionId=id; 463 _sessionIdState = __SESSIONID_URL; 464 } 465 else if (!id.equals(_requestedSessionId)) 466 log.debug("Mismatched session IDs"); 467 } 468 469 if (_requestedSessionId == null) 470 _sessionIdState = __SESSIONID_NONE; 471 } 472 473 474 public String getRequestedSessionId() 475 { 476 return _requestedSessionId; 477 } 478 479 480 public String getRequestURI() 481 { 482 return _httpRequest.getEncodedPath(); 483 } 484 485 486 public StringBuffer getRequestURL() 487 { 488 StringBuffer buf = _httpRequest.getRootURL(); 489 buf.append(getRequestURI()); 490 return buf; 491 } 492 493 494 public String getServletPath() 495 { 496 if (_servletPath==null) 497 return _pathInContext; 498 return _servletPath; 499 } 500 501 502 public HttpSession getSession(boolean create) 503 { 504 if (_session != null && ((SessionManager.Session)_session).isValid()) 505 return _session; 506 507 _session=null; 508 509 String id = getRequestedSessionId(); 510 511 if (id != null) 512 { 513 _session=_servletHandler.getHttpSession(id); 514 if (_session == null && !create) 515 return null; 516 } 517 518 if (_session == null && create) 519 { 520 _session=newSession(); 521 } 522 523 return _session; 524 } 525 526 527 530 HttpSession newSession() 531 { 532 HttpSession session=_servletHandler.newHttpSession(this); 533 Cookie cookie=_servletHandler.getSessionManager().getSessionCookie(session,isSecure()); 534 if (cookie!=null) 535 _servletHttpResponse.getHttpResponse().addSetCookie(cookie); 536 return session; 537 } 538 539 540 public HttpSession getSession() 541 { 542 HttpSession session = getSession(true); 543 return session; 544 } 545 546 547 public boolean isRequestedSessionIdValid() 548 { 549 return _requestedSessionId != null && getSession(false) != null; 550 } 551 552 553 public boolean isRequestedSessionIdFromCookie() 554 { 555 return _sessionIdState == __SESSIONID_COOKIE; 556 } 557 558 559 public boolean isRequestedSessionIdFromURL() 560 { 561 return _sessionIdState == __SESSIONID_URL; 562 } 563 564 565 568 public boolean isRequestedSessionIdFromUrl() 569 { 570 return isRequestedSessionIdFromURL(); 571 } 572 573 574 public Enumeration getAttributeNames() 575 { 576 return _httpRequest.getAttributeNames(); 577 } 578 579 580 public Object getAttribute(String name) 581 { 582 return _httpRequest.getAttribute(name); 583 } 584 585 586 public void setAttribute(String name, Object value) 587 { 588 _httpRequest.setAttribute(name,value); 589 } 590 591 592 public void removeAttribute(String name) 593 { 594 _httpRequest.removeAttribute(name); 595 } 596 597 598 public void setCharacterEncoding(String encoding) 599 throws UnsupportedEncodingException 600 { 601 if (_inputState!=0) 602 throw new IllegalStateException ("getReader() or getInputStream() called"); 603 "".getBytes(encoding); 604 _httpRequest.setCharacterEncoding(encoding,false); 605 } 606 607 608 public String getCharacterEncoding() 609 { 610 return _httpRequest.getCharacterEncoding(); 611 } 612 613 614 public int getContentLength() 615 { 616 return _httpRequest.getContentLength(); 617 } 618 619 620 public String getContentType() 621 { 622 return _httpRequest.getContentType(); 623 } 624 625 626 public ServletInputStream getInputStream() 627 { 628 if (_inputState!=0 && _inputState!=1) 629 throw new IllegalStateException (); 630 if (_in==null) 631 _in = new ServletIn((HttpInputStream)_httpRequest.getInputStream()); 632 _inputState=1; 633 _reader=null; 634 return _in; 635 } 636 637 638 642 public Map getParameterMap() 643 { 644 return Collections.unmodifiableMap(_httpRequest.getParameterStringArrayMap()); 645 } 646 647 648 public String getParameter(String name) 649 { 650 return _httpRequest.getParameter(name); 651 } 652 653 654 public Enumeration getParameterNames() 655 { 656 return Collections.enumeration(_httpRequest.getParameterNames()); 657 } 658 659 660 public String [] getParameterValues(String name) 661 { 662 List v=_httpRequest.getParameterValues(name); 663 if (v==null) 664 return null; 665 String []a=new String [v.size()]; 666 return (String [])v.toArray(a); 667 } 668 669 670 public String getProtocol() 671 { 672 return _httpRequest.getVersion(); 673 } 674 675 676 public String getScheme() 677 { 678 return _httpRequest.getScheme(); 679 } 680 681 682 public String getServerName() 683 { 684 return _httpRequest.getHost(); 685 } 686 687 688 public int getServerPort() 689 { 690 int port = _httpRequest.getPort(); 691 if (port==0) 692 { 693 if (getScheme().equalsIgnoreCase("https")) 694 return 443; 695 return 80; 696 } 697 return port; 698 } 699 700 701 public int getRemotePort() 702 { 703 HttpConnection connection= _httpRequest.getHttpConnection(); 704 if (connection!=null) 705 return connection.getRemotePort(); 706 return 0; 707 } 708 709 710 public String getLocalName() 711 { 712 HttpConnection connection= _httpRequest.getHttpConnection(); 713 if (connection!=null) 714 return connection.getServerName(); 715 return null; 716 } 717 718 719 public String getLocalAddr() 720 { 721 HttpConnection connection= _httpRequest.getHttpConnection(); 722 if (connection!=null) 723 return connection.getServerAddr(); 724 return null; 725 } 726 727 728 public int getLocalPort() 729 { 730 HttpConnection connection= _httpRequest.getHttpConnection(); 731 if (connection!=null) 732 return connection.getServerPort(); 733 return 0; 734 } 735 736 737 public BufferedReader getReader() 738 throws UnsupportedEncodingException 739 { 740 if (_inputState!=0 && _inputState!=2) 741 throw new IllegalStateException (); 742 if (_reader==null) 743 { 744 String encoding=getCharacterEncoding(); 745 if (encoding==null) 746 encoding=StringUtil.__ISO_8859_1; 747 _reader=new BufferedReader (new InputStreamReader (getInputStream(),encoding)); 748 749 } 750 _inputState=2; 751 return _reader; 752 } 753 754 755 public String getRemoteAddr() 756 { 757 return _httpRequest.getRemoteAddr(); 758 } 759 760 761 public String getRemoteHost() 762 { 763 if (_httpRequest.getHttpConnection()==null) 764 return null; 765 return _httpRequest.getRemoteHost(); 766 } 767 768 769 773 public String getRealPath(String path) 774 { 775 return _servletHandler.getServletContext().getRealPath(path); 776 } 777 778 779 public RequestDispatcher getRequestDispatcher(String url) 780 { 781 if (url == null) 782 return null; 783 784 if (!url.startsWith("/")) 785 { 786 String relTo=URI.addPaths(_servletPath,_pathInfo); 787 int slash=relTo.lastIndexOf("/"); 788 if (slash>1) 789 relTo=relTo.substring(0,slash+1); 790 else 791 relTo="/"; 792 url=URI.addPaths(relTo,url); 793 } 794 795 return _servletHandler.getServletContext().getRequestDispatcher(url); 796 } 797 798 799 public String toString() 800 { 801 return 802 getContextPath()+"+"+getServletPath()+"+"+getPathInfo()+"\n"+ 803 _httpRequest.toString(); 804 } 805 806 807 808 816 public static ServletHttpRequest unwrap(ServletRequest request) 817 { 818 while (!(request instanceof ServletHttpRequest)) 819 { 820 if (request instanceof ServletRequestWrapper ) 821 { 822 ServletRequestWrapper wrapper = 823 (ServletRequestWrapper )request; 824 request=wrapper.getRequest(); 825 } 826 else 827 throw new IllegalArgumentException ("Does not wrap ServletHttpRequest"); 828 } 829 830 return (ServletHttpRequest)request; 831 } 832 } 833 834 835 836 837 838 839 | Popular Tags |