1 16 package org.mortbay.jetty.servlet; 17 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.Collections ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import javax.servlet.RequestDispatcher ; 34 import javax.servlet.Servlet ; 35 import javax.servlet.ServletContext ; 36 import javax.servlet.ServletException ; 37 import javax.servlet.UnavailableException ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import javax.servlet.http.HttpSession ; 41 42 import org.apache.commons.logging.Log; 43 import org.mortbay.log.LogFactory; 44 import org.mortbay.http.EOFException; 45 import org.mortbay.http.HttpContext; 46 import org.mortbay.http.HttpException; 47 import org.mortbay.http.HttpFields; 48 import org.mortbay.http.HttpHandler; 49 import org.mortbay.http.HttpRequest; 50 import org.mortbay.http.HttpResponse; 51 import org.mortbay.http.PathMap; 52 import org.mortbay.http.Version; 53 import org.mortbay.util.ByteArrayISO8859Writer; 54 import org.mortbay.util.Container; 55 import org.mortbay.util.LogSupport; 56 import org.mortbay.util.MultiException; 57 import org.mortbay.util.Resource; 58 import org.mortbay.util.URI; 59 60 61 62 78 public class ServletHandler extends Container implements HttpHandler 79 { 80 private static Log log = LogFactory.getLog(ServletHandler.class); 81 82 83 public static final String __DEFAULT_SERVLET="default"; 84 public static final String __J_S_CONTEXT_TEMPDIR="javax.servlet.context.tempdir"; 85 public static final String __J_S_ERROR_EXCEPTION="javax.servlet.error.exception"; 86 public static final String __J_S_ERROR_EXCEPTION_TYPE="javax.servlet.error.exception_type"; 87 public static final String __J_S_ERROR_MESSAGE="javax.servlet.error.message"; 88 public static final String __J_S_ERROR_REQUEST_URI="javax.servlet.error.request_uri"; 89 public static final String __J_S_ERROR_SERVLET_NAME="javax.servlet.error.servlet_name"; 90 public static final String __J_S_ERROR_STATUS_CODE="javax.servlet.error.status_code"; 91 92 93 private static final boolean __Slosh2Slash=File.separatorChar=='\\'; 94 private static String __AllowString="GET, HEAD, POST, OPTIONS, TRACE"; 95 96 97 98 private boolean _usingCookies=true; 99 private boolean _autoInitializeServlets=true; 100 private String _name; 101 102 103 protected PathMap _servletMap=new PathMap(); 104 protected Map _nameMap=new HashMap (); 105 protected Map _attributes=new HashMap (3); 106 protected String _formLoginPage; 107 protected String _formErrorPage; 108 protected SessionManager _sessionManager; 109 110 protected transient Context _context; 111 protected transient ClassLoader _loader; 112 protected transient Log _contextLog; 113 protected transient HttpContext _httpContext; 114 115 116 118 public ServletHandler() 119 { 120 _context=new Context (); 121 } 122 123 124 public void setName(String name) 125 { 126 _name=name; 127 } 128 129 130 public String getName() 131 { 132 if (_name==null) 133 { 134 _name=this.getClass().getName(); 135 if (!log.isDebugEnabled()) 136 _name=_name.substring(_name.lastIndexOf('.')+1); 137 } 138 return _name; 139 } 140 141 142 public HttpContext getHttpContext() 143 { 144 return _httpContext; 145 } 146 147 148 public void initialize(HttpContext context) 149 { 150 SessionManager sessionManager=getSessionManager(); 151 152 153 if (_httpContext!=null&& _httpContext!=context) 154 throw new IllegalStateException ("Can't initialize handler for different context"); 155 _httpContext=context; 156 157 sessionManager.initialize(this); 158 } 159 160 161 public void formAuthInit(String formLoginPage, 162 String formErrorPage) 163 { 164 _formLoginPage=formLoginPage; 165 _formErrorPage=formErrorPage; 166 } 167 168 169 public void setSessionManager(SessionManager sm) 170 { 171 if (isStarted()) 172 throw new IllegalStateException ("Started"); 173 174 int mii=0; 175 boolean setMii=false; 176 177 if ( _sessionManager!=null) 178 { 179 mii=_sessionManager.getMaxInactiveInterval(); 180 setMii=true; 181 if (getHttpContext()!=null) 182 _sessionManager.initialize(null); 183 removeComponent(_sessionManager); 184 } 185 186 _sessionManager=sm; 187 188 if (_sessionManager!=null) 189 { 190 if (getHttpContext()!=null) 191 _sessionManager.initialize(this); 192 if (setMii) 193 _sessionManager.setMaxInactiveInterval(mii); 194 addComponent(_sessionManager); 195 } 196 197 _sessionManager=sm; 198 } 199 200 201 public SessionManager getSessionManager() 202 { 203 if (_sessionManager==null) 204 { 205 _sessionManager = new HashSessionManager(); 206 addComponent(_sessionManager); 207 } 208 return _sessionManager; 209 } 210 211 212 public ServletContext getServletContext() { return _context; } 213 214 215 public PathMap getServletMap() { return _servletMap; } 216 217 218 public boolean isUsingCookies() { return _usingCookies; } 219 220 221 224 public void setDynamicServletPathSpec(String dynamicServletPathSpec) 225 { 226 log.warn("setDynamicServletPathSpec is Deprecated."); 227 } 228 229 230 233 public void setDynamicInitParams(Map initParams) 234 { 235 log.warn("setDynamicInitParams is Deprecated."); 236 } 237 238 239 242 public void setServeDynamicSystemServlets(boolean b) 243 { 244 log.warn("setServeDynamicSystemServlets is Deprecated."); 245 } 246 247 248 public ClassLoader getClassLoader() 249 { 250 return _loader; 251 } 252 253 254 257 public void setUsingCookies(boolean uc) 258 { 259 _usingCookies=uc; 260 } 261 262 263 public ServletHolder newServletHolder(String name, 264 String servletClass, 265 String forcedPath) 266 { 267 if (_nameMap.containsKey(name)) 268 throw new IllegalArgumentException ("Named servlet already exists: "+name); 269 270 ServletHolder holder = new ServletHolder(this,name,servletClass,forcedPath); 271 addServletHolder(holder); 272 273 return holder; 274 } 275 276 277 public ServletHolder newServletHolder(String name, 278 String servletClass) 279 { 280 return newServletHolder(name,servletClass,null); 281 } 282 283 284 public ServletHolder getServletHolder(String name) 285 { 286 return (ServletHolder)_nameMap.get(name); 287 } 288 289 290 296 public ServletHolder mapPathToServlet(String pathSpec, 297 String servletName) 298 { 299 ServletHolder holder =(ServletHolder)_nameMap.get(servletName); 300 301 if (!pathSpec.startsWith("/") && !pathSpec.startsWith("*")) 302 { 303 log.warn("pathSpec should start with '/' or '*' : "+pathSpec); 304 pathSpec="/"+pathSpec; 305 } 306 307 if (holder==null) 308 throw new IllegalArgumentException ("Unknown servlet: "+servletName); 309 _servletMap.put(pathSpec,holder); 310 return holder; 311 } 312 313 314 323 public ServletHolder addServlet(String name, 324 String pathSpec, 325 String servletClass, 326 String forcedPath) 327 { 328 ServletHolder holder = getServletHolder(name); 329 if (holder==null) 330 holder = newServletHolder(name,servletClass,forcedPath); 331 mapPathToServlet(pathSpec,name); 332 if (isStarted() && !holder.isStarted()) 333 { 334 try{holder.start();} 335 catch(Exception e){log.warn(LogSupport.EXCEPTION,e);} 336 } 337 return holder; 338 } 339 340 341 347 public ServletHolder addServlet(String name, 348 String pathSpec, 349 String servletClass) 350 { 351 return addServlet(name,pathSpec,servletClass,null); 352 } 353 354 355 356 362 public ServletHolder addServlet(String pathSpec, 363 String servletClass) 364 { 365 return addServlet(servletClass,pathSpec,servletClass,null); 366 } 367 368 369 373 public void addServletHolder(ServletHolder holder) 374 { 375 ServletHolder existing = (ServletHolder) 376 _nameMap.get(holder.getName()); 377 if (existing==null) 378 _nameMap.put(holder.getName(),holder); 379 else if (existing!=holder) 380 throw new IllegalArgumentException ("Holder already exists for name: "+holder.getName()); 381 addComponent(holder); 382 } 383 384 385 public boolean isAutoInitializeServlets() 386 { 387 return _autoInitializeServlets; 388 } 389 390 391 public void setAutoInitializeServlets(boolean b) 392 { 393 _autoInitializeServlets=b; 394 } 395 396 397 protected synchronized void doStart() 398 throws Exception 399 { 400 if (isStarted()) 401 return; 402 403 _contextLog = LogFactory.getLog("org.mortbay.jetty.context."+getHttpContext().getHttpContextName()); 404 405 if (_contextLog==null) 406 _contextLog=log; 407 408 if (_sessionManager!=null) 409 _sessionManager.start(); 410 411 _loader=getHttpContext().getClassLoader(); 413 414 if (_autoInitializeServlets) 415 initializeServlets(); 416 } 417 418 419 422 public ServletHolder[] getServlets() 423 { 424 HashSet holder_set = new HashSet (_nameMap.size()); 426 holder_set.addAll(_nameMap.values()); 427 ServletHolder holders [] = (ServletHolder []) 428 holder_set.toArray(new ServletHolder [holder_set.size()]); 429 java.util.Arrays.sort (holders); 430 return holders; 431 } 432 433 434 437 public void initializeServlets() 438 throws Exception 439 { 440 MultiException mx = new MultiException(); 441 442 ServletHolder[] holders = getServlets(); 444 for (int i=0; i<holders.length; i++) 445 { 446 try{holders[i].start();} 447 catch(Exception e) 448 { 449 log.debug(LogSupport.EXCEPTION,e); 450 mx.add(e); 451 } 452 } 453 mx.ifExceptionThrow(); 454 } 455 456 457 protected synchronized void doStop() 458 throws Exception 459 { 460 ServletHolder[] holders = getServlets(); 462 463 for (int i=holders.length; i-->0;) 465 { 466 try 467 { 468 if (holders[i].isStarted()) 469 holders[i].stop(); 470 } 471 catch(Exception e){log.warn(LogSupport.EXCEPTION,e);} 472 } 473 474 _sessionManager.stop(); 476 _attributes.clear(); 477 _loader=null; 478 } 479 480 481 public HttpSession getHttpSession(String id) 482 { 483 return _sessionManager.getHttpSession(id); 484 } 485 486 487 public HttpSession newHttpSession(HttpServletRequest request) 488 { 489 return _sessionManager.newHttpSession(request); 490 } 491 492 493 497 public void setSessionInactiveInterval(int seconds) 498 { 499 _sessionManager.setMaxInactiveInterval(seconds); 500 } 501 502 503 510 public void handle(String pathInContext, 511 String pathParams, 512 HttpRequest httpRequest, 513 HttpResponse httpResponse) 514 throws IOException 515 { 516 if (!isStarted()) 517 return; 518 519 if (HttpRequest.__TRACE.equals(httpRequest.getMethod())) 521 { 522 handleTrace(httpRequest,httpResponse); 523 return; 524 } 525 526 ServletHttpRequest request = (ServletHttpRequest) httpRequest.getWrapper(); 528 ServletHttpResponse response = (ServletHttpResponse) httpResponse.getWrapper(); 529 if (request==null) 530 { 531 request = new ServletHttpRequest(this,pathInContext,httpRequest); 533 response = new ServletHttpResponse(request,httpResponse); 534 httpRequest.setWrapper(request); 535 httpResponse.setWrapper(response); 536 } 537 else 538 { 539 request.recycle(this,pathInContext); 540 response.recycle(); 541 } 542 543 Map.Entry servlet=getHolderEntry(pathInContext); 545 ServletHolder servletHolder=servlet==null?null:(ServletHolder)servlet.getValue(); 546 if(log.isDebugEnabled())log.debug("servlet="+servlet); 547 548 try 549 { 550 if (servlet!=null) 552 { 553 String servletPathSpec=(String )servlet.getKey(); 554 request.setServletPaths(PathMap.pathMatch(servletPathSpec,pathInContext), 555 PathMap.pathInfo(servletPathSpec,pathInContext), 556 servletHolder); 557 } 558 559 request.setRequestedSessionId(pathParams); 561 HttpSession session=request.getSession(false); 562 if (session!=null) 563 ((SessionManager.Session)session).access(); 564 if(log.isDebugEnabled())log.debug("session="+session); 565 566 if (servletHolder!=null) 568 dispatch(pathInContext,request,response,servletHolder, Dispatcher.__REQUEST); 569 } 570 catch(Exception e) 571 { 572 log.debug(LogSupport.EXCEPTION,e); 573 574 Throwable th=e; 575 while (th instanceof ServletException ) 576 { 577 log.warn(LogSupport.EXCEPTION, th); 578 Throwable cause=((ServletException )th).getRootCause(); 579 if (cause==th || cause==null) 580 break; 581 th=cause; 582 } 583 584 if (th instanceof HttpException) 585 throw (HttpException)th; 586 if (th instanceof EOFException) 587 throw (IOException )th; 588 else if (log.isDebugEnabled() || !( th instanceof java.io.IOException )) 589 { 590 if (_contextLog!=null) 591 { 592 if (th instanceof RuntimeException ) 593 _contextLog.error(httpRequest.getURI()+": ",th); 594 else 595 _contextLog.warn(httpRequest.getURI()+": ",th); 596 } 597 598 if(log.isDebugEnabled()) 599 { 600 if (th instanceof RuntimeException ) 601 log.error(httpRequest.getURI()+": ",th); 602 else 603 log.warn(httpRequest.getURI()+": ",th); 604 log.debug(httpRequest); 605 } 606 } 607 608 httpResponse.getHttpConnection().forceClose(); 609 if (!httpResponse.isCommitted()) 610 { 611 request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION_TYPE,th.getClass()); 612 request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION,th); 613 if (th instanceof UnavailableException ) 614 { 615 UnavailableException ue = (UnavailableException )th; 616 if (ue.isPermanent()) 617 response.sendError(HttpResponse.__404_Not_Found,e.getMessage()); 618 else 619 response.sendError(HttpResponse.__503_Service_Unavailable,e.getMessage()); 620 } 621 else 622 response.sendError(HttpResponse.__500_Internal_Server_Error,e.getMessage()); 623 } 624 else 625 if(log.isDebugEnabled())log.debug("Response already committed for handling "+th); 626 } 627 catch(Error e) 628 { 629 log.warn("Error for "+httpRequest.getURI(),e); 630 if(log.isDebugEnabled())log.debug(httpRequest); 631 632 httpResponse.getHttpConnection().forceClose(); 633 if (!httpResponse.isCommitted()) 634 { 635 request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION_TYPE,e.getClass()); 636 request.setAttribute(ServletHandler.__J_S_ERROR_EXCEPTION,e); 637 response.sendError(HttpResponse.__500_Internal_Server_Error, 638 e.getMessage()); 639 } 640 else 641 if(log.isDebugEnabled())log.debug("Response already committed for handling ",e); 642 } 643 finally 644 { 645 if (servletHolder!=null && response!=null) 646 { 647 response.complete(); 648 } 649 } 650 } 651 652 653 668 protected void dispatch(String pathInContext, 669 HttpServletRequest request, 670 HttpServletResponse response, 671 ServletHolder servletHolder, 672 int type) 673 throws ServletException , 674 UnavailableException , 675 IOException 676 { 677 servletHolder.handle(request,response); 678 } 679 680 681 682 686 public Map.Entry getHolderEntry(String pathInContext) 687 { 688 return _servletMap.getMatch(pathInContext); 689 } 690 691 692 693 public Set getResourcePaths(String uriInContext) 694 { 695 try 696 { 697 uriInContext=URI.canonicalPath(uriInContext); 698 if (uriInContext==null) 699 return Collections.EMPTY_SET; 700 Resource resource=getHttpContext().getResource(uriInContext); 701 if (resource==null || !resource.isDirectory()) 702 return Collections.EMPTY_SET; 703 String [] contents=resource.list(); 704 if (contents==null || contents.length==0) 705 return Collections.EMPTY_SET; 706 HashSet set = new HashSet (contents.length*2); 707 for (int i=0;i<contents.length;i++) 708 set.add(URI.addPaths(uriInContext,contents[i])); 709 return set; 710 } 711 catch(Exception e) 712 { 713 e.printStackTrace(); 714 LogSupport.ignore(log,e); 715 } 716 717 return Collections.EMPTY_SET; 718 } 719 720 721 722 728 public URL getResource(String uriInContext) 729 throws MalformedURLException 730 { 731 if (uriInContext==null || !uriInContext.startsWith("/")) 732 throw new MalformedURLException (uriInContext); 733 734 try{ 735 Resource resource = getHttpContext().getResource(uriInContext); 736 if (resource!=null && resource.exists()) 737 return resource.getURL(); 738 } 739 catch(IllegalArgumentException e) 740 { 741 LogSupport.ignore(log,e); 742 } 743 catch(MalformedURLException e) 744 { 745 throw e; 746 } 747 catch(IOException e) 748 { 749 log.warn(LogSupport.EXCEPTION,e); 750 } 751 return null; 752 } 753 754 755 public InputStream getResourceAsStream(String uriInContext) 756 { 757 if (uriInContext==null || !uriInContext.startsWith("/")) 758 return null; 759 try 760 { 761 Resource resource = getHttpContext().getResource(uriInContext); 762 if (resource!=null) 763 return resource.getInputStream(); 764 765 uriInContext=URI.canonicalPath(uriInContext); 766 URL url = getResource(uriInContext); 767 if (url!=null) 768 return url.openStream(); 769 } 770 catch(IOException e) {LogSupport.ignore(log,e);} 771 return null; 772 } 773 774 775 public String getRealPath(String path) 776 { 777 if(log.isDebugEnabled())log.debug("getRealPath of "+path+" in "+this); 778 779 if (__Slosh2Slash) 780 path=path.replace('\\','/'); 781 path=URI.canonicalPath(path); 782 if (path==null) 783 return null; 784 785 Resource baseResource=getHttpContext().getBaseResource(); 786 if (baseResource==null ) 787 return null; 788 789 try{ 790 Resource resource = baseResource.addPath(path); 791 File file = resource.getFile(); 792 793 return (file==null)?null:(file.getAbsolutePath()); 794 } 795 catch(IOException e) 796 { 797 log.warn(LogSupport.EXCEPTION,e); 798 return null; 799 } 800 } 801 802 803 public RequestDispatcher getRequestDispatcher(String uriInContext) 804 { 805 if (uriInContext == null) 806 return null; 807 808 if (!uriInContext.startsWith("/")) 809 return null; 810 811 try 812 { 813 String query=null; 814 int q=0; 815 if ((q=uriInContext.indexOf('?'))>0) 816 { 817 query=uriInContext.substring(q+1); 818 uriInContext=uriInContext.substring(0,q); 819 } 820 if ((q=uriInContext.indexOf(';'))>0) 821 uriInContext=uriInContext.substring(0,q); 822 823 String pathInContext=URI.canonicalPath(URI.decodePath(uriInContext)); 824 Map.Entry entry=getHolderEntry(pathInContext); 825 if (entry!=null) 826 return new Dispatcher(ServletHandler.this, 827 uriInContext, 828 pathInContext, 829 query, 830 entry); 831 } 832 catch(Exception e) 833 { 834 LogSupport.ignore(log,e); 835 } 836 return null; 837 } 838 839 840 845 public RequestDispatcher getNamedDispatcher(String name) 846 { 847 if (name == null || name.length()==0) 848 name=__DEFAULT_SERVLET; 849 850 try { return new Dispatcher(ServletHandler.this,name); } 851 catch(Exception e) {LogSupport.ignore(log,e);} 852 853 return null; 854 } 855 856 857 858 859 protected void notFound(HttpServletRequest request, 860 HttpServletResponse response) 861 throws IOException 862 { 863 if(log.isDebugEnabled())log.debug("Not Found "+request.getRequestURI()); 864 String method=request.getMethod(); 865 866 if (method.equals(HttpRequest.__GET) || 868 method.equals(HttpRequest.__HEAD) || 869 method.equals(HttpRequest.__POST)) 870 { 871 response.sendError(HttpResponse.__404_Not_Found); 872 } 873 else if (method.equals(HttpRequest.__TRACE)) 874 handleTrace(request,response); 875 else if (method.equals(HttpRequest.__OPTIONS)) 876 handleOptions(request,response); 877 else 878 { 879 response.setHeader(HttpFields.__Allow,__AllowString); 881 response.sendError(HttpResponse.__405_Method_Not_Allowed); 882 } 883 } 884 885 886 protected void handleTrace(HttpServletRequest request, 887 HttpServletResponse response) 888 throws IOException 889 { 890 response.setHeader(HttpFields.__ContentType, 891 HttpFields.__MessageHttp); 892 OutputStream out = response.getOutputStream(); 893 ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(); 894 writer.write(request.toString()); 895 writer.flush(); 896 response.setIntHeader(HttpFields.__ContentLength,writer.size()); 897 writer.writeTo(out); 898 out.flush(); 899 } 900 901 902 protected void handleOptions(HttpServletRequest request, 903 HttpServletResponse response) 904 throws IOException 905 { 906 if ("*".equals(request.getRequestURI())) 908 { 909 response.setIntHeader(HttpFields.__ContentLength,0); 911 response.setHeader(HttpFields.__Allow,__AllowString); 912 response.flushBuffer(); 913 } 914 else 915 response.sendError(HttpResponse.__404_Not_Found); 916 } 917 918 919 public String getErrorPage(int status,ServletHttpRequest request) 920 { 921 return null; 922 } 923 924 925 930 protected Object getContextAttribute(String name) 931 { 932 if (ServletHandler.__J_S_CONTEXT_TEMPDIR.equals(name)) 933 { 934 Object t = getHttpContext().getAttribute(ServletHandler.__J_S_CONTEXT_TEMPDIR); 936 937 if (t instanceof File ) 938 return (File )t; 939 940 return getHttpContext().getTempDirectory(); 941 } 942 943 if (_attributes.containsKey(name)) 944 return _attributes.get(name); 945 return getHttpContext().getAttribute(name); 946 } 947 948 949 952 protected Enumeration getContextAttributeNames() 953 { 954 if (_attributes.size()==0) 955 return getHttpContext().getAttributeNames(); 956 HashSet set=new HashSet (_attributes.keySet()); 957 Enumeration e=getHttpContext().getAttributeNames(); 958 while(e.hasMoreElements()) 959 set.add(e.nextElement()); 960 return Collections.enumeration(set); 961 } 962 963 964 967 protected void setContextAttribute(String name, Object value) 968 { 969 _attributes.put(name,value); 970 } 971 972 973 976 protected void removeContextAttribute(String name) 977 { 978 _attributes.remove(name); 979 } 980 981 982 983 public void handleTrace(HttpRequest request, 984 HttpResponse response) 985 throws IOException 986 { 987 boolean trace=getHttpContext().getHttpServer().getTrace(); 988 989 response.setField(HttpFields.__ContentType, 991 HttpFields.__MessageHttp); 992 if (trace) 993 { 994 OutputStream out = response.getOutputStream(); 995 ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(); 996 writer.write(request.toString()); 997 writer.flush(); 998 response.setIntField(HttpFields.__ContentLength,writer.size()); 999 writer.writeTo(out); 1000 out.flush(); 1001 } 1002 request.setHandled(true); 1003 } 1004 1005 1006 public void destroy() 1007 { 1008 Iterator iter = _nameMap.values().iterator(); 1009 while (iter.hasNext()) 1010 { 1011 Object sh=iter.next(); 1012 iter.remove(); 1013 removeComponent(sh); 1014 } 1015 1016 if (_sessionManager!=null) 1017 removeComponent(_sessionManager); 1018 _sessionManager=null; 1019 _context=null; 1020 super.destroy(); 1021 } 1022 1023 1024 protected void finalize() throws Throwable 1025 { 1026 destroy(); 1027 } 1028 1029 1030 1031 1032 class Context implements ServletContext 1033 { 1034 1035 1036 ServletHandler getServletHandler() 1037 { 1038 return ServletHandler.this; 1039 } 1040 1041 1042 public ServletContext getContext(String uri) 1043 { 1044 ServletHandler handler= (ServletHandler) 1045 getHttpContext().getHttpServer() 1046 .findHandler(org.mortbay.jetty.servlet.ServletHandler.class, 1047 uri, 1048 getHttpContext().getVirtualHosts()); 1049 if (handler!=null) 1050 return handler.getServletContext(); 1051 return null; 1052 } 1053 1054 1055 public int getMajorVersion() 1056 { 1057 return 2; 1058 } 1059 1060 1061 public int getMinorVersion() 1062 { 1063 return 4; 1064 } 1065 1066 1067 public String getMimeType(String file) 1068 { 1069 return getHttpContext().getMimeByExtension(file); 1070 } 1071 1072 1073 public Set getResourcePaths(String uriInContext) 1074 { 1075 return ServletHandler.this.getResourcePaths(uriInContext); 1076 } 1077 1078 1079 public URL getResource(String uriInContext) 1080 throws MalformedURLException 1081 { 1082 return ServletHandler.this.getResource(uriInContext); 1083 } 1084 1085 1086 public InputStream getResourceAsStream(String uriInContext) 1087 { 1088 return ServletHandler.this.getResourceAsStream(uriInContext); 1089 } 1090 1091 1092 public String getRealPath(String path) 1093 { 1094 return ServletHandler.this.getRealPath(path); 1095 } 1096 1097 1098 public RequestDispatcher getRequestDispatcher(String uriInContext) 1099 { 1100 return ServletHandler.this.getRequestDispatcher(uriInContext); 1101 } 1102 1103 1104 public RequestDispatcher getNamedDispatcher(String name) 1105 { 1106 return ServletHandler.this.getNamedDispatcher(name); 1107 } 1108 1109 1110 1113 public Servlet getServlet(String name) 1114 { 1115 return null; 1116 } 1117 1118 1119 1122 public Enumeration getServlets() 1123 { 1124 return Collections.enumeration(Collections.EMPTY_LIST); 1125 } 1126 1127 1128 1131 public Enumeration getServletNames() 1132 { 1133 return Collections.enumeration(Collections.EMPTY_LIST); 1134 } 1135 1136 1137 1143 public void log(String msg) 1144 { 1145 _contextLog.info(msg); 1146 } 1147 1148 1149 1154 public void log(Exception e, String msg) 1155 { 1156 _contextLog.warn(msg,e); 1157 } 1158 1159 1160 public void log(String msg, Throwable th) 1161 { 1162 _contextLog.warn(msg,th); 1163 } 1164 1165 1166 public String getServerInfo() 1167 { 1168 return Version.getImplVersion(); 1169 } 1170 1171 1172 1173 1178 public String getInitParameter(String param) 1179 { 1180 return getHttpContext().getInitParameter(param); 1181 } 1182 1183 1184 1188 public Enumeration getInitParameterNames() 1189 { 1190 return getHttpContext().getInitParameterNames(); 1191 } 1192 1193 1194 1195 1200 public Object getAttribute(String name) 1201 { 1202 return getContextAttribute(name); 1203 } 1204 1205 1206 1209 public Enumeration getAttributeNames() 1210 { 1211 return getContextAttributeNames(); 1212 } 1213 1214 1215 1220 public void setAttribute(String name, Object value) 1221 { 1222 setContextAttribute(name, value); 1223 } 1224 1225 1226 1230 public void removeAttribute(String name) 1231 { 1232 removeContextAttribute(name); 1233 } 1234 1235 1236 public String getServletContextName() 1237 { 1238 if (getHttpContext() instanceof WebApplicationContext) 1239 return ((WebApplicationContext)getHttpContext()).getDisplayName(); 1240 return null; 1241 } 1242 1243 1244 public String toString() 1245 { 1246 return "ServletContext["+getHttpContext()+"]"; 1247 } 1248 } 1249} 1250 | Popular Tags |