1 16 17 package org.apache.jk.config; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.StringReader ; 22 import java.util.Hashtable ; 23 import java.util.Vector ; 24 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 29 import org.apache.tomcat.util.IntrospectionUtils; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Node ; 32 import org.xml.sax.EntityResolver ; 33 import org.xml.sax.InputSource ; 34 import org.xml.sax.SAXException ; 35 36 37 58 59 76 public class WebXml2Jk { 77 String vhost=""; 78 String cpath=""; 79 String docBase; 80 String file; 81 String worker="lb"; 82 83 85 87 89 public void setHost( String vhost ) { 90 this.vhost=vhost; 91 } 92 93 95 public void setContext( String contextPath ) { 96 this.cpath=contextPath; 97 } 98 99 100 103 public void setDocBase(String docBase ) { 104 this.docBase=docBase; 105 } 106 107 115 122 127 public void setGroup(String route ) { 128 worker=route; 129 } 130 131 public static interface MappingGenerator { 133 void setWebXmlReader(WebXml2Jk wxml ); 134 135 137 void generateStart() throws IOException ; 138 139 void generateEnd() throws IOException ; 140 141 void generateServletMapping( String servlet, String url )throws IOException ; 142 void generateFilterMapping( String servlet, String url ) throws IOException ; 143 144 void generateLoginConfig( String loginPage, 145 String errPage, String authM ) throws IOException ; 146 147 void generateErrorPage( int err, String location ) throws IOException ; 148 149 void generateConstraints( Vector urls, Vector methods, Vector roles, boolean isSSL ) throws IOException ; 150 } 151 152 Node webN; 154 File jkDir; 155 156 158 public Node getWebXmlNode() { 159 return webN; 160 } 161 162 public File getJkDir() { 163 return jkDir; 164 } 165 166 168 public Vector getWellcomeFiles() { 169 Node n0=getChild( webN, "welcome-file-list" ); 170 Vector wF=new Vector (); 171 if( n0!=null ) { 172 for( Node mapN=getChild( webN, "welcome-file" ); 173 mapN != null; mapN = getNext( mapN ) ) { 174 wF.addElement( getContent(mapN)); 175 } 176 } 177 return wF; 179 } 180 181 182 void generate(MappingGenerator gen ) throws IOException { 183 gen.generateStart(); 184 log.info("Generating mappings for servlets " ); 185 for( Node mapN=getChild( webN, "servlet-mapping" ); 186 mapN != null; mapN = getNext( mapN ) ) { 187 188 String serv=getChildContent( mapN, "servlet-name"); 189 String url=getChildContent( mapN, "url-pattern"); 190 191 gen.generateServletMapping( serv, url ); 192 } 193 194 log.info("Generating mappings for filters " ); 195 for( Node mapN=getChild( webN, "filter-mapping" ); 196 mapN != null; mapN = getNext( mapN ) ) { 197 198 String filter=getChildContent( mapN, "filter-name"); 199 String url=getChildContent( mapN, "url-pattern"); 200 201 gen.generateFilterMapping( filter, url ); 202 } 203 204 205 for( Node mapN=getChild( webN, "error-page" ); 206 mapN != null; mapN = getNext( mapN ) ) { 207 String errorCode= getChildContent( mapN, "error-code" ); 208 String location= getChildContent( mapN, "location" ); 209 210 if( errorCode!=null && ! "".equals( errorCode ) ) { 211 try { 212 int err=new Integer ( errorCode ).intValue(); 213 gen.generateErrorPage( err, location ); 214 } catch( Exception ex ) { 215 log.error( "Format error " + location, ex); 216 } 217 } 218 } 219 220 Node lcN=getChild( webN, "login-config" ); 221 if( lcN!=null ) { 222 log.info("Generating mapping for login-config " ); 223 224 String authMeth=getContent( getChild( lcN, "auth-method")); 225 String realm=getContent( getChild( lcN, "realm-name")); 226 if( authMeth == null ) authMeth="BASIC"; 227 228 Node n1=getChild( lcN, "form-login-config"); 229 String loginPage= getChildContent( n1, "form-login-page"); 230 String errPage= getChildContent( n1, "form-error-page"); 231 232 if(loginPage != null) { 233 int lpos = loginPage.lastIndexOf("/"); 234 String jscurl = loginPage.substring(0,lpos+1) + "j_security_check"; 235 gen.generateLoginConfig( jscurl, errPage, authMeth ); 236 } 237 } 238 239 log.info("Generating mappings for security constraints " ); 240 for( Node mapN=getChild( webN, "security-constraint" ); 241 mapN != null; mapN = getNext( mapN )) { 242 243 Vector methods=new Vector (); 244 Vector urls=new Vector (); 245 Vector roles=new Vector (); 246 boolean isSSL=false; 247 248 Node wrcN=getChild( mapN, "web-resource-collection"); 249 for( Node uN=getChild(wrcN, "http-method"); 250 uN!=null; uN=getNext( uN )) { 251 methods.addElement( getContent( uN )); 252 } 253 for( Node uN=getChild(wrcN, "url-pattern"); 254 uN!=null; uN=getNext( uN )) { 255 urls.addElement( getContent( uN )); 256 } 257 258 Node acN=getChild( mapN, "auth-constraint"); 260 for( Node rN=getChild(acN, "role-name"); 261 rN!=null; rN=getNext( rN )) { 262 roles.addElement(getContent( rN )); 263 } 264 265 Node ucN=getChild( mapN, "user-data-constraint"); 266 String transp=getContent(getChild( ucN, "transport-guarantee")); 267 if( transp!=null ) { 268 if( "INTEGRAL".equalsIgnoreCase( transp ) || 269 "CONFIDENTIAL".equalsIgnoreCase( transp ) ) { 270 isSSL=true; 271 } 272 } 273 274 gen.generateConstraints( urls, methods, roles, isSSL ); 275 } 276 gen.generateEnd(); 277 } 278 279 281 public void execute() { 282 try { 283 if( docBase== null) { 284 log.error("No docbase - please specify the base directory of you web application ( -docBase PATH )"); 285 return; 286 } 287 if( cpath== null) { 288 log.error("No context - please specify the mount ( -context PATH )"); 289 return; 290 } 291 File docbF=new File (docBase); 292 File wXmlF=new File ( docBase, "WEB-INF/web.xml"); 293 294 Document wXmlN=readXml(wXmlF); 295 if( wXmlN == null ) return; 296 297 webN = wXmlN.getDocumentElement(); 298 if( webN==null ) { 299 log.error("Can't find web-app"); 300 return; 301 } 302 303 jkDir=new File ( docbF, "WEB-INF/jk2" ); 304 jkDir.mkdirs(); 305 306 MappingGenerator generator=new GeneratorJk2(); 307 generator.setWebXmlReader( this ); 308 generate( generator ); 309 310 generator=new GeneratorJk1(); 311 generator.setWebXmlReader( this ); 312 generate( generator ); 313 314 generator=new GeneratorApache2(); 315 generator.setWebXmlReader( this ); 316 generate( generator ); 317 318 } catch( Exception ex ) { 319 ex.printStackTrace(); 320 } 321 } 322 323 324 public static void main(String args[] ) { 325 try { 326 if( args.length == 1 && 327 ( "-?".equals(args[0]) || "-h".equals( args[0])) ) { 328 System.out.println("Usage: "); 329 System.out.println(" WebXml2Jk [OPTIONS]"); 330 System.out.println(); 331 System.out.println(" -docBase DIR The location of the webapp. Required"); 332 System.out.println(" -group GROUP Group, if you have multiple tomcats with diffrent content. " ); 333 System.out.println(" The default is 'lb', and should be used in most cases"); 334 System.out.println(" -host HOSTNAME Canonical hostname - for virtual hosts"); 335 System.out.println(" -context /CPATH Context path where the app will be mounted"); 336 return; 337 } 338 339 WebXml2Jk w2jk=new WebXml2Jk(); 340 341 342 IntrospectionUtils.processArgs( w2jk, args, new String [] {}, 343 null, new Hashtable ()); 344 w2jk.execute(); 345 } catch( Exception ex ) { 346 ex.printStackTrace(); 347 } 348 349 } 350 351 private static org.apache.commons.logging.Log log= 352 org.apache.commons.logging.LogFactory.getLog( WebXml2Jk.class ); 353 354 355 357 359 public static String getContent(Node n ) { 360 if( n==null ) return null; 361 Node n1=n.getFirstChild(); 362 364 String s1=n1.getNodeValue(); 365 return s1.trim(); 366 } 367 368 370 public static Node getChild( Node parent, String name ) { 371 if( parent==null ) return null; 372 Node first=parent.getFirstChild(); 373 if( first==null ) return null; 374 for (Node node = first; node != null; 375 node = node.getNextSibling()) { 376 if( name.equals( node.getNodeName() ) ) { 378 return node; 379 } 380 } 381 return null; 382 } 383 384 386 public static String getChildContent( Node parent, String name ) { 387 Node first=parent.getFirstChild(); 388 if( first==null ) return null; 389 for (Node node = first; node != null; 390 node = node.getNextSibling()) { 391 if( name.equals( node.getNodeName() ) ) { 393 return getContent( node ); 394 } 395 } 396 return null; 397 } 398 399 401 public static Node getNext( Node current ) { 402 Node first=current.getNextSibling(); 403 String name=current.getNodeName(); 404 if( first==null ) return null; 405 for (Node node = first; node != null; 406 node = node.getNextSibling()) { 407 if( name.equals( node.getNodeName() ) ) { 409 return node; 410 } 411 } 412 return null; 413 } 414 415 public static class NullResolver implements EntityResolver { 416 public InputSource resolveEntity (String publicId, 417 String systemId) 418 throws SAXException , IOException 419 { 420 if (log.isDebugEnabled()) 421 log.debug("ResolveEntity: " + publicId + " " + systemId); 422 return new InputSource (new StringReader ("")); 423 } 424 } 425 426 public static Document readXml(File xmlF) 427 throws SAXException , IOException , ParserConfigurationException 428 { 429 if( ! xmlF.exists() ) { 430 log.error("No xml file " + xmlF ); 431 return null; 432 } 433 DocumentBuilderFactory dbf = 434 DocumentBuilderFactory.newInstance(); 435 436 dbf.setValidating(false); 437 dbf.setIgnoringComments(false); 438 dbf.setIgnoringElementContentWhitespace(true); 439 442 DocumentBuilder db = null; 443 db = dbf.newDocumentBuilder(); 444 db.setEntityResolver( new NullResolver() ); 445 446 448 Document doc = db.parse(xmlF); 449 return doc; 450 } 451 452 } 453 | Popular Tags |