1 //======================================================================== 2 //$Id: WebAppContext.java,v 1.5 2005/11/16 22:02:45 gregwilkins Exp $ 3 //Copyright 2004-2006 Mort Bay Consulting Pty. Ltd. 4 //------------------------------------------------------------------------ 5 //Licensed under the Apache License, Version 2.0 (the "License"); 6 //you may not use this file except in compliance with the License. 7 //You may obtain a copy of the License at 8 //http://www.apache.org/licenses/LICENSE-2.0 9 //Unless required by applicable law or agreed to in writing, software 10 //distributed under the License is distributed on an "AS IS" BASIS, 11 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 //See the License for the specific language governing permissions and 13 //limitations under the License. 14 //======================================================================== 15 16 package org.mortbay.jetty.servlet; 17 18 import javax.servlet.RequestDispatcher; 19 import javax.servlet.ServletContext; 20 21 import org.mortbay.jetty.HandlerContainer; 22 import org.mortbay.jetty.handler.ContextHandler; 23 import org.mortbay.jetty.handler.ErrorHandler; 24 import org.mortbay.jetty.security.SecurityHandler; 25 import org.mortbay.log.Log; 26 import org.mortbay.util.URIUtil; 27 28 29 /* ------------------------------------------------------------ */ 30 /** Servlet Context. 31 * This conveniance extention to the ContextHandler allows for 32 * simple construction of a context with ServletHandler and optionally 33 * session and security handlers, et.<pre> 34 * new ServletContext("/context",Context.SESSIONS|Context.NO_SECURITY); 35 * </pre> 36 * <p/> 37 * This class should have been called ServletContext, but this would have 38 * cause confusion with {@link ServletContext}. 39 */ 40 public class Context extends ContextHandler 41 { 42 public final static int SESSIONS=1; 43 public final static int SECURITY=2; 44 public final static int NO_SESSIONS=0; 45 public final static int NO_SECURITY=0; 46 47 protected SecurityHandler _securityHandler; 48 protected ServletHandler _servletHandler; 49 protected SessionHandler _sessionHandler; 50 51 /* ------------------------------------------------------------ */ Context()52 public Context() 53 { 54 this(null,null,null,null,null); 55 } 56 57 /* ------------------------------------------------------------ */ Context(int options)58 public Context(int options) 59 { 60 this(null,null,options); 61 } 62 63 /* ------------------------------------------------------------ */ Context(HandlerContainer parent, String contextPath)64 public Context(HandlerContainer parent, String contextPath) 65 { 66 this(parent,contextPath,null,null,null,null); 67 } 68 69 /* ------------------------------------------------------------ */ Context(HandlerContainer parent, String contextPath, int options)70 public Context(HandlerContainer parent, String contextPath, int options) 71 { 72 this(parent,contextPath,((options&SESSIONS)!=0)?new SessionHandler():null,((options&SECURITY)!=0)?new SecurityHandler():null,null,null); 73 } 74 75 /* ------------------------------------------------------------ */ Context(HandlerContainer parent, String contextPath, boolean sessions, boolean security)76 public Context(HandlerContainer parent, String contextPath, boolean sessions, boolean security) 77 { 78 this(parent,contextPath,(sessions?SESSIONS:0)|(security?SECURITY:0)); 79 } 80 81 /* ------------------------------------------------------------ */ Context(HandlerContainer parent, SessionHandler sessionHandler,SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)82 public Context(HandlerContainer parent, SessionHandler sessionHandler,SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler) 83 { 84 this(parent,null,sessionHandler,securityHandler,servletHandler,errorHandler); 85 } 86 87 /* ------------------------------------------------------------ */ Context(HandlerContainer parent, String contextPath, SessionHandler sessionHandler,SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler)88 public Context(HandlerContainer parent, String contextPath, SessionHandler sessionHandler,SecurityHandler securityHandler, ServletHandler servletHandler, ErrorHandler errorHandler) 89 { 90 super((ContextHandler.SContext)null); 91 _scontext = new SContext(); 92 _sessionHandler = sessionHandler; 93 _securityHandler = securityHandler; 94 _servletHandler = servletHandler!=null?servletHandler:new ServletHandler(); 95 96 if (_sessionHandler!=null) 97 { 98 setHandler(_sessionHandler); 99 100 if (securityHandler!=null) 101 { 102 _sessionHandler.setHandler(_securityHandler); 103 _securityHandler.setHandler(_servletHandler); 104 } 105 else 106 { 107 _sessionHandler.setHandler(_servletHandler); 108 } 109 } 110 else if (_securityHandler!=null) 111 { 112 setHandler(_securityHandler); 113 _securityHandler.setHandler(_servletHandler); 114 } 115 else 116 { 117 setHandler(_servletHandler); 118 } 119 120 if (errorHandler!=null) 121 setErrorHandler(errorHandler); 122 123 if (contextPath!=null) 124 setContextPath(contextPath); 125 126 if (parent!=null) 127 parent.addHandler(this); 128 } 129 130 /* ------------------------------------------------------------ */ 131 /** 132 * @see org.mortbay.jetty.handler.ContextHandler#startContext() 133 */ startContext()134 protected void startContext() throws Exception 135 { 136 super.startContext(); 137 138 // OK to Initialize servlet handler now 139 if (_servletHandler != null && _servletHandler.isStarted()) 140 _servletHandler.initialize(); 141 } 142 143 /* ------------------------------------------------------------ */ 144 /** 145 * @return Returns the securityHandler. 146 */ getSecurityHandler()147 public SecurityHandler getSecurityHandler() 148 { 149 return _securityHandler; 150 } 151 152 /* ------------------------------------------------------------ */ 153 /** 154 * @return Returns the servletHandler. 155 */ getServletHandler()156 public ServletHandler getServletHandler() 157 { 158 return _servletHandler; 159 } 160 161 /* ------------------------------------------------------------ */ 162 /** 163 * @return Returns the sessionHandler. 164 */ getSessionHandler()165 public SessionHandler getSessionHandler() 166 { 167 return _sessionHandler; 168 } 169 170 /* ------------------------------------------------------------ */ 171 /** conveniance method to add a servlet. 172 */ addServlet(String className,String pathSpec)173 public ServletHolder addServlet(String className,String pathSpec) 174 { 175 return _servletHandler.addServletWithMapping(className, pathSpec); 176 } 177 178 /* ------------------------------------------------------------ */ 179 /** conveniance method to add a servlet. 180 */ addServlet(Class servlet,String pathSpec)181 public ServletHolder addServlet(Class servlet,String pathSpec) 182 { 183 return _servletHandler.addServletWithMapping(servlet.getName(), pathSpec); 184 } 185 186 /* ------------------------------------------------------------ */ 187 /** conveniance method to add a servlet. 188 */ addServlet(ServletHolder servlet,String pathSpec)189 public void addServlet(ServletHolder servlet,String pathSpec) 190 { 191 _servletHandler.addServletWithMapping(servlet, pathSpec); 192 } 193 194 /* ------------------------------------------------------------ */ 195 /** conveniance method to add a filter 196 */ addFilter(FilterHolder holder,String pathSpec,int dispatches)197 public void addFilter(FilterHolder holder,String pathSpec,int dispatches) 198 { 199 _servletHandler.addFilterWithMapping(holder,pathSpec,dispatches); 200 } 201 202 /* ------------------------------------------------------------ */ 203 /** conveniance method to add a filter 204 */ addFilter(Class filterClass,String pathSpec,int dispatches)205 public FilterHolder addFilter(Class filterClass,String pathSpec,int dispatches) 206 { 207 return _servletHandler.addFilterWithMapping(filterClass,pathSpec,dispatches); 208 } 209 210 /* ------------------------------------------------------------ */ 211 /** conveniance method to add a filter 212 */ addFilter(String filterClass,String pathSpec,int dispatches)213 public FilterHolder addFilter(String filterClass,String pathSpec,int dispatches) 214 { 215 return _servletHandler.addFilterWithMapping(filterClass,pathSpec,dispatches); 216 } 217 218 219 220 /* ------------------------------------------------------------ */ 221 /** 222 * @param sessionHandler The sessionHandler to set. 223 */ setSessionHandler(SessionHandler sessionHandler)224 public void setSessionHandler(SessionHandler sessionHandler) 225 { 226 if (_sessionHandler==sessionHandler) 227 return; 228 229 if (_sessionHandler!=null) 230 _sessionHandler.setHandler(null); 231 232 _sessionHandler = sessionHandler; 233 234 setHandler(_sessionHandler); 235 236 if (_securityHandler!=null) 237 _sessionHandler.setHandler(_securityHandler); 238 else if (_servletHandler!=null) 239 _sessionHandler.setHandler(_servletHandler); 240 241 242 } 243 244 /* ------------------------------------------------------------ */ 245 /** 246 * @param securityHandler The {@link SecurityHandler} to set on this context. 247 */ setSecurityHandler(SecurityHandler securityHandler)248 public void setSecurityHandler(SecurityHandler securityHandler) 249 { 250 if(_securityHandler==securityHandler) 251 return; 252 253 if (_securityHandler!=null) 254 _securityHandler.setHandler(null); 255 256 _securityHandler = securityHandler; 257 258 if (_securityHandler==null) 259 { 260 if (_sessionHandler!=null) 261 _sessionHandler.setHandler(_servletHandler); 262 else 263 setHandler(_servletHandler); 264 } 265 else 266 { 267 if (_sessionHandler!=null) 268 _sessionHandler.setHandler(_securityHandler); 269 else 270 setHandler(_securityHandler); 271 272 if (_servletHandler!=null) 273 _securityHandler.setHandler(_servletHandler); 274 } 275 } 276 277 /* ------------------------------------------------------------ */ 278 /** 279 * @param servletHandler The servletHandler to set. 280 */ setServletHandler(ServletHandler servletHandler)281 public void setServletHandler(ServletHandler servletHandler) 282 { 283 if (_servletHandler==servletHandler) 284 return; 285 286 _servletHandler = servletHandler; 287 288 if (_securityHandler!=null) 289 _securityHandler.setHandler(_servletHandler); 290 else if (_sessionHandler!=null) 291 _sessionHandler.setHandler(_servletHandler); 292 else 293 setHandler(_servletHandler); 294 295 } 296 297 /* ------------------------------------------------------------ */ 298 public class SContext extends ContextHandler.SContext 299 { 300 301 /* ------------------------------------------------------------ */ 302 /* 303 * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String) 304 */ getNamedDispatcher(String name)305 public RequestDispatcher getNamedDispatcher(String name) 306 { 307 ContextHandler context=org.mortbay.jetty.servlet.Context.this; 308 if (_servletHandler==null || _servletHandler.getServlet(name)==null) 309 return null; 310 return new Dispatcher(context, name); 311 } 312 313 /* ------------------------------------------------------------ */ 314 /* 315 * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String) 316 */ getRequestDispatcher(String uriInContext)317 public RequestDispatcher getRequestDispatcher(String uriInContext) 318 { 319 if (uriInContext == null) 320 return null; 321 322 if (!uriInContext.startsWith("/")) 323 return null; 324 325 try 326 { 327 String query=null; 328 int q=0; 329 if ((q=uriInContext.indexOf('?'))>0) 330 { 331 query=uriInContext.substring(q+1); 332 uriInContext=uriInContext.substring(0,q); 333 } 334 if ((q=uriInContext.indexOf(';'))>0) 335 uriInContext=uriInContext.substring(0,q); 336 337 String pathInContext=URIUtil.canonicalPath(URIUtil.decodePath(uriInContext)); 338 String uri=URIUtil.addPaths(getContextPath(), uriInContext); 339 ContextHandler context=org.mortbay.jetty.servlet.Context.this; 340 return new Dispatcher(context,uri, pathInContext, query); 341 } 342 catch(Exception e) 343 { 344 Log.ignore(e); 345 } 346 return null; 347 } 348 } 349 350 } 351