1 27 28 package com.adventnet.adaptors.html; 29 30 import java.io.File ; 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.Properties ; 34 import java.util.Vector ; 35 import java.net.URL ; 36 37 import com.adventnet.agent.logging.Log; 38 import com.adventnet.agent.logging.LogFactory; 39 import com.adventnet.agent.utilities.xml.XMLNode; 40 import com.adventnet.agent.utilities.xml.XMLDataReader; 41 import com.adventnet.agent.utilities.xml.XMLDataWriter; 42 43 import org.mortbay.util.*; 44 import org.mortbay.jetty.*; 45 import org.mortbay.http.*; 46 47 52 public class JettyHtmlServer implements HttpServerInterface 53 { 54 private static final int defaultPortNumber = 8030; 55 56 private String configFileName ="conf/http/etc/JettyConfig.xml"; 57 private String jarConfig = "conf/http/etc/JarConfig.xml"; 58 59 private Properties props = null; 60 private boolean authentication = false; 61 private int port = defaultPortNumber; 62 private boolean isStarted = false; 63 private boolean sslSupport = false; 64 private boolean isJarFile = false; 65 private boolean fromSetPort = false; 66 67 private Server server = null; 68 69 private Log log = null; 70 71 74 public JettyHtmlServer() 75 { 76 this(defaultPortNumber, false, false, false); 77 } 78 79 90 public JettyHtmlServer(int port, boolean authentication) 91 { 92 this(port, authentication, false, false); 93 } 94 95 109 public JettyHtmlServer(int port, boolean authentication, boolean sslSupport) 110 { 111 this(port, authentication, sslSupport, false); 112 } 113 114 130 public JettyHtmlServer(int port, boolean authentication, 131 boolean sslSupport, boolean isJarFile) 132 { 133 log = getLogObject(); 134 this.authentication = authentication; 135 this.sslSupport = sslSupport; 136 this.port = port; 137 this.isJarFile = isJarFile; 138 139 if(!isJarFile) 140 { 141 XMLDataReader xmld = 142 new XMLDataReader(HtmlAdaptor.getParentDirectory() + File.separator 143 + configFileName ); 144 XMLNode rootNode = xmld.getRootNode(); 145 146 Vector vec = rootNode.getChildNodes(); 147 changePort(rootNode, (new Integer (port)).toString()); 148 149 changeSSL(rootNode, sslSupport); 150 changeAgentDir(rootNode, HtmlAdaptor.getParentDirectory()); 151 152 XMLDataWriter xw = new XMLDataWriter(HtmlAdaptor.getParentDirectory() + File.separator + configFileName ,rootNode); 153 } 154 } 155 156 161 public void setConfigFileName(String configFileName) 162 { 163 this.configFileName = configFileName; 164 165 try 166 { 167 restartHttpServer(); 168 } 169 catch (Exception ex) 170 { 171 log.error("Exception", ex); 172 } 173 } 174 175 180 public String getConfigFileName() 181 { 182 return this.configFileName; 183 } 184 185 191 public void setConfigJarFile(String jarConfig) 192 { 193 this.jarConfig = jarConfig; 194 195 try 196 { 197 restartHttpServer(); 198 } 199 catch(Exception ex) 200 { 201 log.error("Exception", ex); 202 } 203 } 204 205 212 public String getConfigJarFile() 213 { 214 return this.jarConfig; 215 } 216 217 224 public Integer getPort() 225 { 226 return new Integer (port); 227 } 228 229 236 public void setPort(Integer port) throws Exception 237 { 238 setPort(port.intValue()); 239 } 240 241 248 public void setPort(int port) 249 { 250 if(port < 0 || port > 65535) 251 { 252 return; 253 } 254 255 if(port != this.port) 256 { 257 this.port = port; 258 259 if(!isJarFile) 260 { 261 XMLDataReader xmld = 262 new XMLDataReader(HtmlAdaptor.getParentDirectory() 263 + File.separator + configFileName ); 264 265 XMLNode rootNode = xmld.getRootNode(); 266 267 changePort(rootNode, String.valueOf(port)); 268 269 XMLDataWriter xw = 270 new XMLDataWriter(HtmlAdaptor.getParentDirectory() 271 + File.separator + configFileName, rootNode); 272 } 273 else 274 { 275 fromSetPort = true; 276 } 277 278 log.info("applying the new port number by restarting the Http server"); 280 281 try 282 { 283 restartHttpServer(); 284 log.info("successfully changed to new port number"); 285 } 286 catch(Exception ex) 287 { 288 log.error("Exception while trying to change server port number", ex); 289 log.fatal("Failed to change to new port number"); 290 } 291 } 292 } 293 294 302 public void setAuthentication(boolean auth) 303 { 304 this.authentication = false; 306 } 307 308 315 public boolean isAuthentication() 316 { 317 return this.authentication; 318 } 319 320 331 public void addUser(String userName, String passwd) 332 { 333 } 335 336 344 public void setSslSupport(boolean sslSupport) 345 { 346 this.sslSupport = false; 348 } 349 350 357 public boolean isSslSupport() 358 { 359 return this.sslSupport; 360 } 361 362 369 public void startHttpServer() throws Exception 370 { 371 System.setProperty("LOG_FILE", "logs/agent.log"); 372 System.setProperty("LOG_CLASSES", "org.mortbay.util.WriterLogSink"); 373 374 if(!isJarFile) 375 { 376 server = new Server(HtmlAdaptor.getParentDirectory()+ 377 File.separator+configFileName); 378 } 379 else 380 { 381 URL url = this.getClass().getResource("/"+jarConfig); 382 server = new Server(url); 383 if((sslSupport) || (fromSetPort)) 384 { 385 Collection lisCol = server.getListeners(); 387 if(!(lisCol.isEmpty())){ 388 Iterator iter = lisCol.iterator(); 389 if(iter.hasNext()) 390 server.removeListener((HttpListener)iter.next()); 391 } 392 393 if(!sslSupport) 394 { 395 SocketListener socLis = new SocketListener(new InetAddrPort(this.port)); 396 server.addListener(socLis); 397 } 398 } 399 } 400 401 server.start(); 402 isStarted = true; 403 } 404 405 410 public void stopHttpServer() throws Exception 411 { 412 server.stop(); 413 isStarted = false; 414 } 415 416 421 public void restartHttpServer() throws Exception 422 { 423 try 424 { 425 stopHttpServer(); 426 } 427 catch(Exception ee) 428 { 429 throw new Exception ("Exception while stopping WebServer in restart"); 430 } 431 432 try 433 { 434 startHttpServer(); 435 } 436 catch(Exception eee) 437 { 438 throw new Exception ("Exception while starting WebServer in restart"); 439 } 440 } 441 442 void changeAgentDir(XMLNode node, String agentDir) 443 { 444 if (node.getNodeName().equals("Call") 445 && node.getAttributeList().get("name").equals("addContext")) 446 { 447 XMLNode setNode = (XMLNode)node.getChildNodes().elementAt(1); 448 449 if (setNode.getAttributeList().get("name").equals("ResourceBase")) 450 { 451 XMLNode sysNode = (XMLNode)setNode.getChildNodes().elementAt(0); 452 sysNode.getAttributeList().put("default", agentDir+"/conf/http/ssi"); 453 return; 454 } 455 456 } 457 458 Vector childVec = node.getChildNodes(); 459 if(childVec != null) 460 { 461 int len = childVec.size(); 462 463 for(int i = 0; i <len; i++) 464 { 465 changeAgentDir((XMLNode)childVec.elementAt(i), agentDir); 466 } 467 } 468 } 469 470 private boolean changeSSL(XMLNode node, boolean flag) 471 { 472 if (node.getNodeName().equals("Call") 473 && node.getAttributeList().get("name").equals("addListener")) 474 { 475 XMLNode argNode = (XMLNode)node.getChildNodes().elementAt(0); 476 XMLNode newNode = (XMLNode)argNode.getChildNodes().elementAt(0); 477 478 if(flag) 479 { 480 newNode.getAttributeList().put("class", "org.mortbay.http.SunJsseListener"); 481 482 XMLNode setNode = null; 483 484 Vector vec= newNode.getChildNodes(); 485 int size = vec.size(); 486 boolean keyStore = true; 487 boolean keyPassword = true; 488 boolean password = true; 489 490 String keyStoreStr = null; 491 String keyPasswordStr = null; 492 String passwordStr = null; 493 494 XMLDataReader myReader = 495 new XMLDataReader(HtmlAdaptor.getParentDirectory() + File.separator 496 + "etc" + File.separator + "HtmlAdaptorSSLConf.xml"); 497 XMLNode myConfigNode = myReader.getRootNode(); 498 Vector configChilds = myConfigNode.getChildNodes(); 499 int configSize = configChilds.size(); 500 for (int j = 0; j < configSize; j++) 501 { 502 XMLNode configChild = (XMLNode)configChilds.get(j); 503 if (configChild.getNodeName().equals("Store") && 504 configChild.getAttribute("name").equals("keyStore")) 505 { 506 Vector properties = configChild.getChildNodes(); 507 int propsSize = properties.size(); 508 for (int k = 0; k < propsSize; k++) 509 { 510 XMLNode prop = (XMLNode)properties.get(k); 511 String value = (String )prop.getAttribute("name"); 512 if (value.equals("Name")) 513 { 514 keyStoreStr = (String )prop.getAttribute("value"); 515 } 516 else if (value.equals("Password")) 517 { 518 passwordStr = (String )prop.getAttribute("value"); 519 } 520 else if (value.equals("Keypassword")) 521 { 522 keyPasswordStr = (String )prop.getAttribute("value"); 523 } 524 525 if (keyPasswordStr != null && 526 passwordStr != null && 527 keyStoreStr != null) 528 { 529 break; 530 } 531 } 532 } 533 } 534 535 Vector newNodeChilds = newNode.getChildNodes(); 536 int newNodeChildSize = newNodeChilds.size(); 537 for (int i = 0; i < newNodeChildSize; i++) 538 { 539 XMLNode newNodeChild = (XMLNode)newNodeChilds.get(i); 540 if (newNodeChild.getNodeName().equals("Set")) 541 { 542 if (newNodeChild.getAttribute("name").equals("Keystore")) 543 { 544 newNodeChild.removeFromParent(); 545 } 546 else if (newNodeChild.getAttribute("name").equals("Password")) 547 { 548 newNodeChild.removeFromParent(); 549 } 550 else if (newNodeChild.getAttribute("name").equals("KeyPassword")) 551 { 552 newNodeChild.removeFromParent(); 553 } 554 else if (newNodeChild.getAttribute("name").equals("ConfigFileName")) 555 { 556 newNodeChild.removeFromParent(); 557 } 558 } 559 } 560 561 XMLNode addNode = new XMLNode(); 562 addNode.setNodeType(XMLNode.ELEMENT); 563 addNode.setNodeName("Set"); 564 565 addNode.setAttribute("name", "Keystore"); 566 addNode.setNodeValue("etc/adventnetkey"); 567 newNode.addChildNode(addNode); 568 569 addNode = new XMLNode(); 570 addNode.setNodeType(XMLNode.ELEMENT); 571 addNode.setNodeName("Set"); 572 573 addNode.setAttribute("name", "KeyPassword"); 574 addNode.setNodeValue("adventnet"); 575 newNode.addChildNode(addNode); 576 577 addNode = new XMLNode(); 578 addNode.setNodeType(XMLNode.ELEMENT); 579 addNode.setNodeName("Set"); 580 581 addNode.setAttribute("name", "Password"); 582 addNode.setNodeValue("adventnet"); 583 if (password) 584 { 585 newNode.addChildNode(addNode); 586 } 587 return true; 588 } 589 else 590 { 591 if (newNode.getAttributeList().get("class").equals("org.mortbay.http.SunJsseListener")) 592 { 593 newNode.getAttributeList().put("class", "org.mortbay.http.SocketListener"); 594 595 XMLNode setNode = null; 596 for (int i=0; i<newNode.getChildNodes().size(); i++) 597 { 598 setNode = (XMLNode)newNode.getChildNodes().elementAt(i); 599 600 if (setNode.getAttributeList().get("name").equals("KeyPassword") || 601 setNode.getAttributeList().get("name").equals("Keystore") || 602 setNode.getAttributeList().get("name").equals("Password")) 603 { 604 setNode.removeFromParent(); 605 } 607 } 608 } 609 } 610 } 611 612 Vector childVec = node.getChildNodes(); 613 614 if(childVec != null) 615 { 616 int len = childVec.size(); 617 for (int i = 0; i <len; i++) 618 { 619 if (changeSSL((XMLNode)childVec.elementAt(i), flag)) 620 { 621 return true; 622 } 623 } 624 } 625 626 return false; 627 } 628 629 private void changePort(XMLNode node, String port) 631 { 632 if (node.getNodeName().equals("Call") 633 && node.getAttributeList().get("name").equals("addListener")) 634 { 635 XMLNode argNode = (XMLNode)node.getChildNodes().elementAt(0); 636 XMLNode newNode = (XMLNode)argNode.getChildNodes().elementAt(0); 637 638 XMLNode setNode = null; 639 640 for (int i=0; i<newNode.getChildNodes().size(); i++) 641 { 642 setNode = (XMLNode)newNode.getChildNodes().elementAt(i); 643 644 if (setNode.getAttributeList().get("name").equals("Port")) 645 { 646 setNode.setNodeValue(port); 647 return; 648 } 649 } 650 } 651 652 Vector childVec = node.getChildNodes(); 653 if(childVec != null) 654 { 655 int len = childVec.size(); 656 657 for (int i = 0; i <len; i++) 658 { 659 changePort((XMLNode)childVec.elementAt(i),port); 660 } 661 } 662 } 663 664 private Log getLogObject() 665 { 666 try 667 { 668 log = LogFactory.getInstance("HTML"); 669 } 670 catch(InstantiationException ie) 671 { 672 } 674 675 return log; 676 } 677 } | Popular Tags |