1 package org.mortbay.jetty.servlet; 16 17 import java.io.IOException ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.util.Enumeration ; 21 import java.util.EventListener ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import javax.servlet.UnavailableException ; 26 27 import org.apache.commons.logging.Log; 28 import org.mortbay.log.LogFactory; 29 import org.mortbay.http.Authenticator; 30 import org.mortbay.http.ClientCertAuthenticator; 31 import org.mortbay.http.SecurityConstraint; 32 import org.mortbay.util.LogSupport; 33 import org.mortbay.util.Resource; 34 import org.mortbay.xml.XmlParser; 35 36 40 public class XMLConfiguration implements WebApplicationContext.Configuration 41 { 42 private static Log log=LogFactory.getLog(XMLConfiguration.class); 43 private WebApplicationContext _context; 44 protected XmlParser xmlParser; 45 46 public XMLConfiguration() 47 { 48 xmlParser=webXmlParser(); 50 } 51 52 public static XmlParser webXmlParser() 53 { 54 XmlParser xmlParser=new XmlParser(); 55 URL dtd22=WebApplicationContext.class.getResource("/javax/servlet/resources/web-app_2_2.dtd"); 57 URL dtd23=WebApplicationContext.class.getResource("/javax/servlet/resources/web-app_2_3.dtd"); 58 URL jsp20xsd=WebApplicationContext.class.getResource("/javax/servlet/resources/jsp_2_0.xsd"); 59 URL j2ee14xsd=WebApplicationContext.class.getResource("/javax/servlet/resources/j2ee_1_4.xsd"); 60 URL webapp24xsd=WebApplicationContext.class.getResource("/javax/servlet/resources/web-app_2_4.xsd"); 61 URL schemadtd=WebApplicationContext.class.getResource("/javax/servlet/resources/XMLSchema.dtd"); 62 URL xmlxsd=WebApplicationContext.class.getResource("/javax/servlet/resources/xml.xsd"); 63 URL webserviceclient11xsd=WebApplicationContext.class.getResource("/javax/servlet/resources/j2ee_web_services_client_1_1.xsd"); 64 URL webservice11xsd=WebApplicationContext.class.getResource("/javax/servlet/resources/j2ee_web_services_1_1.xsd"); 65 URL datatypesdtd=WebApplicationContext.class.getResource("/javax/servlet/resources/datatypes.dtd"); 66 xmlParser.redirectEntity("web-app_2_2.dtd",dtd22); 67 xmlParser.redirectEntity("-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN",dtd22); 68 xmlParser.redirectEntity("web.dtd",dtd23); 69 xmlParser.redirectEntity("web-app_2_3.dtd",dtd23); 70 xmlParser.redirectEntity("-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",dtd23); 71 xmlParser.redirectEntity("XMLSchema.dtd",schemadtd); 72 xmlParser.redirectEntity("http://www.w3.org/2001/XMLSchema.dtd",schemadtd); 73 xmlParser.redirectEntity("-//W3C//DTD XMLSCHEMA 200102//EN",schemadtd); 74 xmlParser.redirectEntity("jsp_2_0.xsd",jsp20xsd); 75 xmlParser.redirectEntity("http://java.sun.com/xml/ns/j2ee/jsp_2_0.xsd",jsp20xsd); 76 xmlParser.redirectEntity("j2ee_1_4.xsd",j2ee14xsd); 77 xmlParser.redirectEntity("http://java.sun.com/xml/ns/j2ee/j2ee_1_4.xsd",j2ee14xsd); 78 xmlParser.redirectEntity("web-app_2_4.xsd",webapp24xsd); 79 xmlParser.redirectEntity("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd",webapp24xsd); 80 xmlParser.redirectEntity("xml.xsd",xmlxsd); 81 xmlParser.redirectEntity("http://www.w3.org/2001/xml.xsd",xmlxsd); 82 xmlParser.redirectEntity("datatypes.dtd",datatypesdtd); 83 xmlParser.redirectEntity("http://www.w3.org/2001/datatypes.dtd",datatypesdtd); 84 xmlParser.redirectEntity("j2ee_web_services_client_1_1.xsd",webservice11xsd); 85 xmlParser.redirectEntity("http://www.ibm.com/webservices/xsd/j2ee_web_services_1_1.xsd",webservice11xsd); 86 xmlParser.redirectEntity("http://www.ibm.com/webservices/xsd/j2ee_web_services_client_1_1.xsd",webserviceclient11xsd); 87 return xmlParser; 88 } 89 90 91 public void setWebApplicationContext (WebApplicationContext context) 92 { 93 _context = context; 94 } 95 96 97 public WebApplicationContext getWebApplicationContext() 98 { 99 return _context; 100 } 101 102 public WebApplicationHandler getWebApplicationHandler() 103 { 104 return _context.getWebApplicationHandler(); 105 } 106 107 108 115 public void configureClassPath() 116 throws Exception 117 { 118 if (_context.isStarted()) 120 { 121 if (log.isDebugEnabled()){log.debug("Cannot configure webapp after it is started");}; 122 return; 123 } 124 125 Resource webInf=_context.getWebInf(); 126 127 if (webInf != null && webInf.isDirectory()) 129 { 130 Resource classes= webInf.addPath("classes/"); 132 if (classes.exists()) 133 _context.setClassPath(classes.toString()); 134 else 135 _context.setClassPath(null); 136 137 Resource lib= webInf.addPath("lib/"); 139 _context.addClassPaths(lib); 140 } 141 } 142 143 144 public void configureDefaults() throws Exception 145 { 146 if (_context.isStarted()) 148 { 149 if (log.isDebugEnabled()){log.debug("Cannot configure webapp after it is started");}; 150 return; 151 } 152 153 getWebApplicationContext().setWelcomeFiles(null); 154 155 String defaultsDescriptor=getWebApplicationContext().getDefaultsDescriptor(); 156 if(defaultsDescriptor!=null&&defaultsDescriptor.length()>0) 157 { 158 Resource dftResource=Resource.newSystemResource(defaultsDescriptor); 159 if(dftResource==null) 160 dftResource=Resource.newResource(defaultsDescriptor); 161 XmlParser.Node defaultConfig=xmlParser.parse(dftResource.getURL()); 162 initialize(defaultConfig); 163 } 164 } 165 166 167 public void configureWebApp() throws Exception 168 { 169 if (_context.isStarted()) 171 { 172 if (log.isDebugEnabled()){log.debug("Cannot configure webapp after it is started");}; 173 return; 174 } 175 176 Resource webInf=getWebApplicationContext().getWebInf(); 177 if(webInf!=null&&webInf.isDirectory()) 179 { 180 Resource web=webInf.addPath("web.xml"); 182 if(!web.exists()) 183 { 184 log.info("No WEB-INF/web.xml in "+getWebApplicationContext().getWAR() 185 +". Serving files and default/dynamic servlets only"); 186 } 187 else 188 { 189 XmlParser.Node config=null; 190 config=xmlParser.parse(web.getURL()); 191 initialize(config); 192 } 193 } 194 } 195 196 197 198 protected void initialize(XmlParser.Node config) throws ClassNotFoundException ,UnavailableException 199 { 200 Iterator iter=config.iterator(); 201 XmlParser.Node node=null; 202 while(iter.hasNext()) 203 { 204 try 205 { 206 Object o=iter.next(); 207 if(!(o instanceof XmlParser.Node)) 208 continue; 209 node=(XmlParser.Node)o; 210 String name=node.getTag(); 211 initWebXmlElement(name,node); 212 } 213 catch(ClassNotFoundException e) 214 { 215 throw e; 216 } 217 catch(Exception e) 218 { 219 log.warn("Configuration problem at "+node,e); 220 throw new UnavailableException ("Configuration problem"); 221 } 222 } 223 } 224 225 226 234 protected void initWebXmlElement(String element,XmlParser.Node node) throws Exception 235 { 236 if("display-name".equals(element)) 237 initDisplayName(node); 238 else if("description".equals(element)) 239 {} 240 else if("context-param".equals(element)) 241 initContextParam(node); 242 else if("servlet".equals(element)) 243 initServlet(node); 244 else if("servlet-mapping".equals(element)) 245 initServletMapping(node); 246 else if("session-config".equals(element)) 247 initSessionConfig(node); 248 else if("mime-mapping".equals(element)) 249 initMimeConfig(node); 250 else if("welcome-file-list".equals(element)) 251 initWelcomeFileList(node); 252 else if("locale-encoding-mapping-list".equals(element)) 253 initLocaleEncodingList(node); 254 else if("error-page".equals(element)) 255 initErrorPage(node); 256 else if("taglib".equals(element)) 257 initTagLib(node); 258 else if("jsp-config".equals(element)) 259 initJspConfig(node); 260 else if("resource-ref".equals(element)) 261 { 262 if(log.isDebugEnabled()) 263 log.debug("No implementation: "+node); 264 } 265 else if("security-constraint".equals(element)) 266 initSecurityConstraint(node); 267 else if("login-config".equals(element)) 268 initLoginConfig(node); 269 else if("security-role".equals(element)) 270 initSecurityRole(node); 271 else if("filter".equals(element)) 272 initFilter(node); 273 else if("filter-mapping".equals(element)) 274 initFilterMapping(node); 275 else if("listener".equals(element)) 276 initListener(node); 277 else if("distributable".equals(element)) 278 initDistributable(node); 279 else 280 { 281 if(log.isDebugEnabled()) 282 { 283 log.debug("Element "+element+" not handled in "+this); 284 log.debug(node); 285 } 286 } 287 } 288 289 290 protected void initDisplayName(XmlParser.Node node) 291 { 292 getWebApplicationContext().setDisplayName(node.toString(false,true)); 293 } 294 295 296 protected void initContextParam(XmlParser.Node node) 297 { 298 String name=node.getString("param-name",false,true); 299 String value=node.getString("param-value",false,true); 300 if(log.isDebugEnabled()) 301 log.debug("ContextParam: "+name+"="+value); 302 getWebApplicationContext().setInitParameter(name,value); 303 } 304 305 306 protected void initFilter(XmlParser.Node node) throws ClassNotFoundException ,UnavailableException 307 { 308 String name=node.getString("filter-name",false,true); 309 String className=node.getString("filter-class",false,true); 310 if(className==null) 311 { 312 log.warn("Missing filter-class in "+node); 313 return; 314 } 315 if(name==null) 316 name=className; 317 FilterHolder holder=getWebApplicationHandler().defineFilter(name,className); 318 Iterator iter=node.iterator("init-param"); 319 while(iter.hasNext()) 320 { 321 XmlParser.Node paramNode=(XmlParser.Node)iter.next(); 322 String pname=paramNode.getString("param-name",false,true); 323 String pvalue=paramNode.getString("param-value",false,true); 324 holder.put(pname,pvalue); 325 } 326 } 327 328 329 protected void initFilterMapping(XmlParser.Node node) 330 { 331 String filterName=node.getString("filter-name",false,true); 332 String pathSpec=node.getString("url-pattern",false,true); 333 String servletName=node.getString("servlet-name",false,true); 334 int dispatcher=Dispatcher.__DEFAULT; 335 Iterator iter=node.iterator("dispatcher"); 336 while(iter.hasNext()) 337 { 338 String d=((XmlParser.Node)iter.next()).toString(false,true); 339 dispatcher|=Dispatcher.type(d); 340 } 341 342 FilterHolder holder=(servletName!=null) 343 ?getWebApplicationHandler().addFilterServletMapping(servletName, filterName, dispatcher) 344 :getWebApplicationHandler().addFilterPathMapping(pathSpec, filterName, dispatcher); 345 } 346 347 348 protected void initServlet(XmlParser.Node node) throws ClassNotFoundException ,UnavailableException ,IOException , 349 MalformedURLException 350 { 351 String name=node.getString("servlet-name",false,true); 352 String className=node.getString("servlet-class",false,true); 353 String jspFile=null; 354 Holder template=null; 355 if(className==null) 356 { 357 jspFile=node.getString("jsp-file",false,true); 359 if(jspFile!=null) 360 { 361 Map.Entry entry=getWebApplicationHandler().getHolderEntry(jspFile); 362 if(entry!=null) 363 { 364 template = (Holder)entry.getValue(); 365 className=template.getClassName(); 366 } 367 } 368 if(className==null) 369 { 370 log.warn("Missing servlet-class|jsp-file in "+node); 371 return; 372 } 373 } 374 if(name==null) 375 name=className; 376 ServletHolder holder=getWebApplicationHandler().newServletHolder(name,className,jspFile); 377 378 if(jspFile!=null) 380 { 381 Enumeration e=template.getInitParameterNames(); 382 383 while(e.hasMoreElements()) 384 { 385 String p=(String )e.nextElement(); 386 holder.setInitParameter(p, template.getInitParameter(p)); 387 } 388 if (holder.getInitParameter("classpath")==null) 389 holder.setInitParameter("classpath",getWebApplicationContext().getFileClassPath()); 390 } 391 392 Iterator iParamsIter=node.iterator("init-param"); 394 while(iParamsIter.hasNext()) 395 { 396 XmlParser.Node paramNode=(XmlParser.Node)iParamsIter.next(); 397 String pname=paramNode.getString("param-name",false,true); 398 String pvalue=paramNode.getString("param-value",false,true); 399 holder.put(pname,pvalue); 400 } 401 XmlParser.Node startup=node.get("load-on-startup"); 402 if(startup!=null) 403 { 404 String s=startup.toString(false,true).toLowerCase(); 405 if(s.startsWith("t")) 406 { 407 log.warn("Deprecated boolean load-on-startup. Please use integer"); 408 holder.setInitOrder(1); 409 } 410 else 411 { 412 int order=0; 413 try 414 { 415 if(s!=null&&s.trim().length()>0) 416 order=Integer.parseInt(s); 417 } 418 catch(Exception e) 419 { 420 log.warn("Cannot parse load-on-startup "+s+". Please use integer"); 421 LogSupport.ignore(log,e); 422 } 423 holder.setInitOrder(order); 424 } 425 } 426 Iterator sRefsIter=node.iterator("security-role-ref"); 427 while(sRefsIter.hasNext()) 428 { 429 XmlParser.Node securityRef=(XmlParser.Node)sRefsIter.next(); 430 String roleName=securityRef.getString("role-name",false,true); 431 String roleLink=securityRef.getString("role-link",false,true); 432 if(roleName!=null&&roleName.length()>0&&roleLink!=null&&roleLink.length()>0) 433 { 434 if(log.isDebugEnabled()) 435 log.debug("link role "+roleName+" to "+roleLink+" for "+this); 436 holder.setUserRoleLink(roleName,roleLink); 437 } 438 else 439 { 440 log.warn("Ignored invalid security-role-ref element: "+"servlet-name="+name+", "+securityRef); 441 } 442 } 443 XmlParser.Node run_as=node.get("run-as"); 444 if(run_as!=null) 445 { 446 String roleName=run_as.getString("role-name",false,true); 447 if(roleName!=null) 448 holder.setRunAs(roleName); 449 } 450 } 451 452 453 protected void initServletMapping(XmlParser.Node node) 454 { 455 String name=node.getString("servlet-name",false,true); 456 String pathSpec=node.getString("url-pattern",false,true); 457 getWebApplicationHandler().mapPathToServlet(pathSpec,name); 458 } 459 460 461 protected void initListener(XmlParser.Node node) 462 { 463 String className=node.getString("listener-class",false,true); 464 Object listener=null; 465 try 466 { 467 Class listenerClass=getWebApplicationContext().loadClass(className); 468 listener=listenerClass.newInstance(); 469 } 470 catch(Exception e) 471 { 472 log.warn("Could not instantiate listener "+className,e); 473 return; 474 } 475 if(!(listener instanceof EventListener )) 476 { 477 log.warn("Not an EventListener: "+listener); 478 return; 479 } 480 boolean known=false; 481 try 482 { 483 getWebApplicationContext().addEventListener((EventListener )listener); 484 known=true; 485 } 486 catch(Exception e) 487 { 488 LogSupport.ignore(log,e); 489 } 490 try 491 { 492 getWebApplicationHandler().addEventListener((EventListener )listener); 493 known=true; 494 } 495 catch(Exception e) 496 { 497 LogSupport.ignore(log,e); 498 } 499 if(!known) 500 log.warn("Unknown: "+listener); 501 } 502 503 504 protected void initDistributable(XmlParser.Node node) 505 { 506 WebApplicationContext wac=getWebApplicationContext(); 509 if (!wac.isDistributable()) 510 wac.setDistributable(true); 511 } 512 513 514 protected void initSessionConfig(XmlParser.Node node) 515 { 516 XmlParser.Node tNode=node.get("session-timeout"); 517 if(tNode!=null) 518 { 519 int timeout=Integer.parseInt(tNode.toString(false,true)); 520 getWebApplicationHandler().setSessionInactiveInterval(timeout*60); 521 } 522 } 523 524 525 protected void initMimeConfig(XmlParser.Node node) 526 { 527 String extension=node.getString("extension",false,true); 528 if(extension!=null&&extension.startsWith(".")) 529 extension=extension.substring(1); 530 String mimeType=node.getString("mime-type",false,true); 531 getWebApplicationContext().setMimeMapping(extension,mimeType); 532 } 533 534 535 protected void initWelcomeFileList(XmlParser.Node node) 536 { 537 Iterator iter=node.iterator("welcome-file"); 538 while(iter.hasNext()) 539 { 540 XmlParser.Node indexNode=(XmlParser.Node)iter.next(); 541 String index=indexNode.toString(false,true); 542 if(log.isDebugEnabled()) 543 log.debug("Index: "+index); 544 getWebApplicationContext().addWelcomeFile(index); 545 } 546 } 547 548 549 protected void initLocaleEncodingList(XmlParser.Node node) 550 { 551 Iterator iter=node.iterator("locale-encoding-mapping"); 552 while(iter.hasNext()) 553 { 554 XmlParser.Node mapping=(XmlParser.Node)iter.next(); 555 String locale=mapping.getString("locale",false,true); 556 String encoding=mapping.getString("encoding",false,true); 557 getWebApplicationContext().addLocaleEncoding(locale,encoding); 558 } 559 } 560 561 562 protected void initErrorPage(XmlParser.Node node) 563 { 564 String error=node.getString("error-code",false,true); 565 if(error==null||error.length()==0) 566 error=node.getString("exception-type",false,true); 567 String location=node.getString("location",false,true); 568 getWebApplicationContext().setErrorPage(error,location); 569 } 570 571 572 protected void initTagLib(XmlParser.Node node) 573 { 574 String uri=node.getString("taglib-uri",false,true); 575 String location=node.getString("taglib-location",false,true); 576 getWebApplicationContext().setResourceAlias(uri,location); 577 } 578 579 580 protected void initJspConfig(XmlParser.Node node) 581 { 582 for (int i=0;i<node.size();i++) 583 { 584 Object o=node.get(i); 585 if (o instanceof XmlParser.Node && "taglib".equals(((XmlParser.Node)o).getTag())) 586 initTagLib((XmlParser.Node)o); 587 } 588 } 589 590 591 protected void initSecurityConstraint(XmlParser.Node node) 592 { 593 try 594 { 595 SecurityConstraint scBase = new SecurityConstraint(); 596 XmlParser.Node auths = node.get("auth-constraint"); 597 if (auths != null) 598 { 599 scBase.setAuthenticate(true); 600 Iterator iter = auths.iterator("role-name"); 602 while (iter.hasNext()) 603 { 604 String role = ((XmlParser.Node) iter.next()).toString(false, true); 605 scBase.addRole(role); 606 } 607 } 608 XmlParser.Node data = node.get("user-data-constraint"); 609 if (data != null) 610 { 611 data = data.get("transport-guarantee"); 612 String guarantee = data.toString(false, true).toUpperCase(); 613 if (guarantee == null || guarantee.length() == 0 || "NONE".equals(guarantee)) 614 scBase.setDataConstraint(SecurityConstraint.DC_NONE); 615 else if ("INTEGRAL".equals(guarantee)) 616 scBase.setDataConstraint(SecurityConstraint.DC_INTEGRAL); 617 else if ("CONFIDENTIAL".equals(guarantee)) 618 scBase.setDataConstraint(SecurityConstraint.DC_CONFIDENTIAL); 619 else 620 { 621 log.warn("Unknown user-data-constraint:" + guarantee); 622 scBase.setDataConstraint(SecurityConstraint.DC_CONFIDENTIAL); 623 } 624 } 625 Iterator iter = node.iterator("web-resource-collection"); 626 while (iter.hasNext()) 627 { 628 XmlParser.Node collection = (XmlParser.Node) iter.next(); 629 String name = collection.getString("web-resource-name", false, true); 630 SecurityConstraint sc = (SecurityConstraint) scBase.clone(); 631 sc.setName(name); 632 Iterator iter2 = collection.iterator("http-method"); 633 while (iter2.hasNext()) 634 sc.addMethod(((XmlParser.Node) iter2.next()).toString(false, true)); 635 iter2 = collection.iterator("url-pattern"); 636 while (iter2.hasNext()) 637 { 638 String url = ((XmlParser.Node) iter2.next()).toString(false, true); 639 getWebApplicationContext().addSecurityConstraint(url, sc); 640 } 641 } 642 } 643 catch (CloneNotSupportedException e) 644 { 645 log.fatal(e); 646 } 647 } 648 649 650 protected void initLoginConfig(XmlParser.Node node) 651 { 652 XmlParser.Node method=node.get("auth-method"); 653 FormAuthenticator _formAuthenticator=null; 654 if(method!=null) 655 { 656 Authenticator authenticator=null; 657 String m=method.toString(false,true); 658 if(SecurityConstraint.__FORM_AUTH.equals(m)) 659 authenticator=_formAuthenticator=new FormAuthenticator(); 660 else if(SecurityConstraint.__BASIC_AUTH.equals(m)) 661 authenticator=new BasicAuthenticator(); 662 else if(SecurityConstraint.__DIGEST_AUTH.equals(m)) 663 authenticator=new DigestAuthenticator(); 664 else if(SecurityConstraint.__CERT_AUTH.equals(m)) 665 authenticator=new ClientCertAuthenticator(); 666 else if(SecurityConstraint.__CERT_AUTH2.equals(m)) 667 authenticator=new ClientCertAuthenticator(); 668 else 669 log.warn("UNKNOWN AUTH METHOD: "+m); 670 getWebApplicationContext().setAuthenticator(authenticator); 671 } 672 XmlParser.Node name=node.get("realm-name"); 673 if(name!=null) 674 getWebApplicationContext().setRealmName(name.toString(false,true)); 675 XmlParser.Node formConfig=node.get("form-login-config"); 676 if(formConfig!=null) 677 { 678 if(_formAuthenticator==null) 679 log.warn("FORM Authentication miss-configured"); 680 else 681 { 682 XmlParser.Node loginPage=formConfig.get("form-login-page"); 683 if(loginPage!=null) 684 _formAuthenticator.setLoginPage(loginPage.toString(false,true)); 685 XmlParser.Node errorPage=formConfig.get("form-error-page"); 686 if(errorPage!=null) 687 { 688 String ep=errorPage.toString(false,true); 689 _formAuthenticator.setErrorPage(ep); 690 } 691 } 692 } 693 } 694 695 696 protected void initSecurityRole(XmlParser.Node node) 697 {} 698 } 699 | Popular Tags |