1 29 30 package com.caucho.server.dispatch; 31 32 import com.caucho.log.Log; 33 import com.caucho.make.DependencyContainer; 34 import com.caucho.server.webapp.WebApp; 35 import com.caucho.util.L10N; 36 import com.caucho.vfs.Depend; 37 import com.caucho.vfs.Path; 38 39 import javax.servlet.FilterChain ; 40 import javax.servlet.ServletContext ; 41 import javax.servlet.ServletException ; 42 import java.io.InputStream ; 43 import java.util.ArrayList ; 44 import java.util.HashMap ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 51 public class ServletMapper { 52 static final Logger log = Log.open(ServletMapper.class); 53 static final L10N L = new L10N(ServletMapper.class); 54 55 private ServletContext _servletContext; 56 57 private ServletManager _servletManager; 58 59 private UrlMap<String > _servletMap = new UrlMap<String >(); 60 61 private ArrayList <String > _welcomeFileList = new ArrayList <String >(); 62 63 private HashMap <String ,ServletMapping> _regexpMap 64 = new HashMap <String ,ServletMapping>(); 65 66 private ArrayList <String > _ignorePatterns = new ArrayList <String >(); 67 68 private String _defaultServlet; 69 70 73 public void setServletContext(ServletContext servletContext) 74 { 75 _servletContext = servletContext; 76 } 77 78 81 public ServletContext getServletContext() 82 { 83 return _servletContext; 84 } 85 86 89 public ServletManager getServletManager() 90 { 91 return _servletManager; 92 } 93 94 97 public void setServletManager(ServletManager manager) 98 { 99 _servletManager = manager; 100 } 101 102 105 public void addUrlRegexp(String regexp, ServletMapping mapping) 106 throws ServletException 107 { 108 _servletMap.addRegexp(regexp, regexp); 109 _regexpMap.put(regexp, mapping); 110 } 111 112 115 void addUrlMapping(String urlPattern, 116 String servletName, 117 ServletMapping mapping) 118 throws ServletException 119 { 120 try { 121 if (servletName == null) { 122 throw new ServletConfigException(L.l("servlet needs a servlet-name.")); 123 } 124 else if (servletName.equals("invoker")) { 125 } 127 else if (servletName.equals("plugin_match") || 128 servletName.equals("plugin-match")) { 129 } 131 else if (servletName.equals("plugin_ignore") || 132 servletName.equals("plugin-ignore")) { 133 if (urlPattern != null) 134 _ignorePatterns.add(urlPattern); 135 136 return; 137 } 138 else if (_servletManager.getServlet(servletName) == null) 139 throw new ServletConfigException(L.l("`{0}' is an unknown servlet-name. servlet-mapping requires that the named servlet be defined in a <servlet> configuration before the <servlet-mapping>.", servletName)); 140 141 if ("/".equals(urlPattern)) { 142 _defaultServlet = servletName; 143 } 144 else if (mapping.isStrictMapping()) { 145 _servletMap.addStrictMap(urlPattern, null, servletName); 146 } 147 else 148 _servletMap.addMap(urlPattern, servletName); 149 150 log.config("servlet-mapping " + urlPattern + " -> " + servletName); 151 } catch (ServletException e) { 152 throw e; 153 } catch (Exception e) { 154 throw new ServletException (e); 155 } 156 } 157 158 161 177 178 181 public void setDefaultServlet(String servletName) 182 throws ServletException 183 { 184 _defaultServlet = servletName; 185 } 186 187 190 public void addWelcomeFile(String fileName) 191 { 192 _welcomeFileList.add(fileName); 193 } 194 195 198 public void setWelcomeFileList(ArrayList <String > list) 199 { 200 _welcomeFileList.clear(); 201 _welcomeFileList.addAll(list); 202 } 203 204 public FilterChain mapServlet(ServletInvocation invocation) 205 throws ServletException 206 { 207 String contextURI = invocation.getContextURI(); 208 209 String servletName = null; 210 ArrayList <String > vars = new ArrayList <String >(); 211 212 invocation.setClassLoader(Thread.currentThread().getContextClassLoader()); 213 214 if (_servletMap != null) { 215 servletName = _servletMap.map(contextURI, vars); 216 217 ServletMapping servletRegexp = _regexpMap.get(servletName); 218 219 if (servletRegexp != null) { 220 servletName = servletRegexp.initRegexp(_servletContext, 221 _servletManager, 222 vars); 223 } 224 } 225 226 if (servletName == null) { 227 try { 228 InputStream is; 229 is = _servletContext.getResourceAsStream(contextURI); 230 231 if (is != null) { 232 is.close(); 233 234 servletName = _defaultServlet; 235 } 236 } catch (Exception e) { 237 } 238 } 239 240 if (servletName == null) { 241 for (int i = 0; i < _welcomeFileList.size(); i++) { 242 String file = _welcomeFileList.get(i); 243 244 try { 245 String welcomeURI; 246 247 if (contextURI.endsWith("/")) 248 welcomeURI = contextURI + file; 249 else 250 welcomeURI = contextURI + '/' + file; 251 252 InputStream is; 253 is = _servletContext.getResourceAsStream(welcomeURI); 254 255 if (is != null) 256 is.close(); 257 258 if (is == null) { 259 } 260 else if (! contextURI.endsWith("/") && 261 ! (invocation instanceof SubInvocation)) { 262 String contextPath = invocation.getContextPath(); 263 264 return new RedirectFilterChain(contextPath + contextURI + "/"); 265 } 266 else { 267 servletName = _servletMap.map(welcomeURI, vars); 268 269 if (servletName != null || _defaultServlet != null) { 270 contextURI = welcomeURI; 271 if (invocation instanceof Invocation) { 272 Invocation inv = (Invocation) invocation; 273 274 inv.setContextURI(contextURI); 275 inv.setRawURI(inv.getRawURI() + file); 276 } 277 break; 278 } 279 } 280 } catch (Throwable e) { 281 } 282 } 283 } 284 285 if (servletName == null) { 286 servletName = _defaultServlet; 287 vars.clear(); 288 vars.add(contextURI); 289 290 addWelcomeFileDependency(invocation); 291 } 292 293 if (servletName == null) { 294 log.fine(L.l("no default servlet defined for URL: '{0}'", contextURI)); 295 296 return new ErrorFilterChain(404); 297 } 298 299 String servletPath = vars.get(0); 300 301 invocation.setServletPath(servletPath); 302 303 if (servletPath.length() < contextURI.length()) 304 invocation.setPathInfo(contextURI.substring(servletPath.length())); 305 else 306 invocation.setPathInfo(null); 307 308 ServletMapping regexp = _regexpMap.get(servletName); 309 310 if (regexp != null) 311 servletName = regexp.initRegexp(_servletContext, _servletManager, vars); 312 313 if (servletName.equals("invoker")) 314 servletName = handleInvoker(invocation); 315 316 invocation.setServletName(servletName); 317 318 if (log.isLoggable(Level.FINE)) 319 log.fine("invoke (uri:" + contextURI + " -> " + servletName + ")"); 320 321 ServletConfigImpl config = _servletManager.getServlet(servletName); 322 323 if (config != null) 324 invocation.setSecurityRoleMap(config.getRoleMap()); 325 326 FilterChain chain = _servletManager.createServletChain(servletName); 327 328 if (chain instanceof PageFilterChain) { 329 PageFilterChain pageChain = (PageFilterChain) chain; 330 331 chain = PrecompilePageFilterChain.create(invocation, pageChain); 332 } 333 334 return chain; 335 } 336 337 private void addWelcomeFileDependency(ServletInvocation servletInvocation) 338 { 339 if (! (servletInvocation instanceof Invocation)) 340 return; 341 342 Invocation invocation = (Invocation) servletInvocation; 343 344 String contextURI = invocation.getContextURI(); 345 346 DependencyContainer dependencyList = new DependencyContainer(); 347 348 WebApp app = (WebApp) _servletContext; 349 350 for (int i = 0; i < _welcomeFileList.size(); i++) { 351 String file = _welcomeFileList.get(i); 352 353 String realPath = app.getRealPath(contextURI + "/" + file); 354 355 Path path = app.getAppDir().lookup(realPath); 356 357 dependencyList.add(new Depend(path)); 358 } 359 360 dependencyList.clearModified(); 361 362 invocation.setDependency(dependencyList); 363 } 364 365 private String handleInvoker(ServletInvocation invocation) 366 throws ServletException 367 { 368 String tail; 369 370 if (invocation.getPathInfo() != null) 371 tail = invocation.getPathInfo(); 372 else 373 tail = invocation.getServletPath(); 374 375 if (! tail.startsWith("/")) { 378 throw new ServletException ("expected '/' starting " + 379 " sp:" + invocation.getServletPath() + 380 " pi:" + invocation.getPathInfo() + 381 " sn:invocation" + invocation); 382 } 383 384 int next = tail.indexOf('/', 1); 385 String servletName; 386 387 if (next < 0) 388 servletName = tail.substring(1); 389 else 390 servletName = tail.substring(1, next); 391 392 if (servletName.startsWith("com.caucho")) { 394 throw new ServletConfigException(L.l("servlet `{0}' forbidden from invoker. com.caucho.* classes must be defined explicitly in a <servlet> declaration.", 395 servletName)); 396 } 397 else if (servletName.equals("")) { 398 throw new ServletConfigException(L.l("invoker needs a servlet name in URL `{0}'.", 399 invocation.getContextURI())); 400 } 401 402 addServlet(servletName); 403 404 String servletPath = invocation.getServletPath(); 405 if (invocation.getPathInfo() == null) { 406 } 407 else if (next < 0) { 408 invocation.setServletPath(servletPath + tail); 409 invocation.setPathInfo(null); 410 } 411 else if (next < tail.length()) { 412 413 invocation.setServletPath(servletPath + tail.substring(0, next)); 414 invocation.setPathInfo(tail.substring(next)); 415 } 416 else { 417 invocation.setServletPath(servletPath + tail); 418 invocation.setPathInfo(null); 419 } 420 421 return servletName; 422 } 423 424 public String getServletPattern(String uri) 425 { 426 ArrayList <String > vars = new ArrayList <String >(); 427 428 Object value = null; 429 430 if (_servletMap != null) 431 value = _servletMap.map(uri, vars); 432 433 if (value == null) 434 return null; 435 else 436 return uri; 437 } 438 439 442 public ArrayList <String > getURLPatterns() 443 { 444 ArrayList <String > patterns = _servletMap.getURLPatterns(); 445 446 return patterns; 447 } 448 449 452 public ArrayList <String > getIgnorePatterns() 453 { 454 return _ignorePatterns; 455 } 456 457 private void addServlet(String servletName) 458 throws ServletException 459 { 460 if (_servletManager.getServlet(servletName) != null) 461 return; 462 463 ServletConfigImpl config = new ServletConfigImpl(); 464 config.setServletContext(_servletContext); 465 config.setServletName(servletName); 466 467 try { 468 config.setServletClass(servletName); 469 } catch (Exception e) { 470 throw new ServletException (e); 471 } 472 473 config.init(); 474 475 _servletManager.addServlet(config); 476 } 477 478 public void destroy() 479 { 480 _servletManager.destroy(); 481 } 482 } 483 | Popular Tags |