| 1 17 18 19 package org.apache.catalina.connector; 20 21 import java.lang.reflect.Method ; 22 import java.net.URLEncoder ; 23 import java.util.HashMap ; 24 25 import javax.management.MBeanRegistration ; 26 import javax.management.MBeanServer ; 27 import javax.management.MalformedObjectNameException ; 28 import javax.management.ObjectName ; 29 30 import org.apache.catalina.Container; 31 import org.apache.catalina.Lifecycle; 32 import org.apache.catalina.LifecycleException; 33 import org.apache.catalina.LifecycleListener; 34 import org.apache.catalina.Service; 35 import org.apache.catalina.core.StandardEngine; 36 import org.apache.catalina.util.LifecycleSupport; 37 import org.apache.catalina.util.StringManager; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.apache.coyote.Adapter; 41 import org.apache.coyote.ProtocolHandler; 42 import org.apache.tomcat.util.IntrospectionUtils; 43 import org.apache.tomcat.util.http.mapper.Mapper; 44 import org.apache.tomcat.util.modeler.Registry; 45 46 47 54 55 56 public class Connector 57 implements Lifecycle, MBeanRegistration  58 { 59 private static Log log = LogFactory.getLog(Connector.class); 60 61 62 65 public static final boolean RECYCLE_FACADES = 66 Boolean.valueOf(System.getProperty("org.apache.catalina.connector.RECYCLE_FACADES", "false")).booleanValue(); 67 68 69 71 72 public Connector() 73 throws Exception { 74 this(null); 75 } 76 77 public Connector(String protocol) 78 throws Exception { 79 setProtocol(protocol); 80 try { 82 Class clazz = Class.forName(protocolHandlerClassName); 83 this.protocolHandler = (ProtocolHandler) clazz.newInstance(); 84 } catch (Exception e) { 85 log.error 86 (sm.getString 87 ("coyoteConnector.protocolHandlerInstantiationFailed", e)); 88 } 89 } 90 91 92 94 95 98 protected Service service = null; 99 100 101 104 protected boolean allowTrace = false; 105 106 107 110 protected Container container = null; 111 112 113 116 protected boolean emptySessionPath = false; 117 118 119 122 protected boolean enableLookups = false; 123 124 125 128 protected boolean xpoweredBy = false; 129 130 131 134 protected static final String info = 135 "org.apache.catalina.connector.Connector/2.1"; 136 137 138 141 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 142 143 144 147 protected int port = 0; 148 149 150 156 protected String proxyName = null; 157 158 159 165 protected int proxyPort = 0; 166 167 168 171 protected int redirectPort = 443; 172 173 174 178 protected String scheme = "http"; 179 180 181 185 protected boolean secure = false; 186 187 188 191 protected StringManager sm = 192 StringManager.getManager(Constants.Package); 193 194 195 199 protected int maxPostSize = 2 * 1024 * 1024; 200 201 202 206 protected int maxSavePostSize = 4 * 1024; 207 208 209 212 protected boolean initialized = false; 213 214 215 218 protected boolean started = false; 219 220 221 224 protected boolean stopped = false; 225 226 229 protected boolean useIPVHosts = false; 230 231 234 protected Thread thread = null; 235 236 237 241 protected String protocolHandlerClassName = 242 "org.apache.coyote.http11.Http11Protocol"; 243 244 245 248 protected ProtocolHandler protocolHandler = null; 249 250 251 254 protected Adapter adapter = null; 255 256 257 260 protected Mapper mapper = new Mapper(); 261 262 263 266 protected MapperListener mapperListener = new MapperListener(mapper); 267 268 269 272 protected String URIEncoding = null; 273 274 275 278 protected boolean useBodyEncodingForURI = false; 279 280 281 protected static HashMap replacements = new HashMap (); 282 static { 283 replacements.put("acceptCount", "backlog"); 284 replacements.put("connectionLinger", "soLinger"); 285 replacements.put("connectionTimeout", "soTimeout"); 286 replacements.put("connectionUploadTimeout", "timeout"); 287 replacements.put("clientAuth", "clientauth"); 288 replacements.put("keystoreFile", "keystore"); 289 replacements.put("randomFile", "randomfile"); 290 replacements.put("rootFile", "rootfile"); 291 replacements.put("keystorePass", "keypass"); 292 replacements.put("keystoreType", "keytype"); 293 replacements.put("sslProtocol", "protocol"); 294 replacements.put("sslProtocols", "protocols"); 295 } 296 297 298 300 301 304 public Object getProperty(String name) { 305 String repl = name; 306 if (replacements.get(name) != null) { 307 repl = (String ) replacements.get(name); 308 } 309 return IntrospectionUtils.getProperty(protocolHandler, repl); 310 } 311 312 313 316 public void setProperty(String name, String value) { 317 String repl = name; 318 if (replacements.get(name) != null) { 319 repl = (String ) replacements.get(name); 320 } 321 IntrospectionUtils.setProperty(protocolHandler, repl, value); 322 } 323 324 325 328 public Object getAttribute(String name) { 329 return getProperty(name); 330 } 331 332 333 336 public void setAttribute(String name, Object value) { 337 setProperty(name, String.valueOf(value)); 338 } 339 340 341 344 public void removeProperty(String name) { 345 } 348 349 350 353 public Service getService() { 354 355 return (this.service); 356 357 } 358 359 360 365 public void setService(Service service) { 366 367 this.service = service; 368 370 } 371 372 373 376 public boolean getAllowTrace() { 377 378 return (this.allowTrace); 379 380 } 381 382 383 388 public void setAllowTrace(boolean allowTrace) { 389 390 this.allowTrace = allowTrace; 391 setProperty("allowTrace", String.valueOf(allowTrace)); 392 393 } 394 395 398 public boolean isAvailable() { 399 400 return (started); 401 402 } 403 404 405 410 public int getBufferSize() { 411 return 2048; 412 } 413 414 420 public void setBufferSize(int bufferSize) { 421 } 422 423 424 428 public Container getContainer() { 429 if( container==null ) { 430 findContainer(); 432 } 433 return (container); 434 435 } 436 437 438 444 public void setContainer(Container container) { 445 446 this.container = container; 447 448 } 449 450 451 454 public boolean getEmptySessionPath() { 455 456 return (this.emptySessionPath); 457 458 } 459 460 461 466 public void setEmptySessionPath(boolean emptySessionPath) { 467 468 this.emptySessionPath = emptySessionPath; 469 setProperty("emptySessionPath", String.valueOf(emptySessionPath)); 470 471 } 472 473 474 477 public boolean getEnableLookups() { 478 479 return (this.enableLookups); 480 481 } 482 483 484 489 public void setEnableLookups(boolean enableLookups) { 490 491 this.enableLookups = enableLookups; 492 setProperty("enableLookups", String.valueOf(enableLookups)); 493 494 } 495 496 497 500 public String getInfo() { 501 502 return (info); 503 504 } 505 506 507 510 public Mapper getMapper() { 511 512 return (mapper); 513 514 } 515 516 517 521 public int getMaxPostSize() { 522 523 return (maxPostSize); 524 525 } 526 527 528 535 public void setMaxPostSize(int maxPostSize) { 536 537 this.maxPostSize = maxPostSize; 538 } 539 540 541 545 public int getMaxSavePostSize() { 546 547 return (maxSavePostSize); 548 549 } 550 551 552 559 public void setMaxSavePostSize(int maxSavePostSize) { 560 561 this.maxSavePostSize = maxSavePostSize; 562 setProperty("maxSavePostSize", String.valueOf(maxSavePostSize)); 563 } 564 565 566 569 public int getPort() { 570 571 return (this.port); 572 573 } 574 575 576 581 public void setPort(int port) { 582 583 this.port = port; 584 setProperty("port", String.valueOf(port)); 585 586 } 587 588 589 592 public String getProtocol() { 593 594 if ("org.apache.coyote.http11.Http11Protocol".equals 595 (getProtocolHandlerClassName()) 596 || "org.apache.coyote.http11.Http11AprProtocol".equals 597 (getProtocolHandlerClassName())) { 598 return "HTTP/1.1"; 599 } else if ("org.apache.coyote.ajp.AjpProtocol".equals 600 (getProtocolHandlerClassName()) 601 || "org.apache.coyote.ajp.AjpAprProtocol".equals 602 (getProtocolHandlerClassName())) { 603 return "AJP/1.3"; 604 } 605 return getProtocolHandlerClassName(); 606 607 } 608 609 610 615 public void setProtocol(String protocol) { 616 617 boolean apr = false; 619 try { 620 String methodName = "initialize"; 621 Class paramTypes[] = new Class [1]; 622 paramTypes[0] = String .class; 623 Object paramValues[] = new Object [1]; 624 paramValues[0] = null; 625 Method method = Class.forName("org.apache.tomcat.jni.Library") 626 .getMethod(methodName, paramTypes); 627 method.invoke(null, paramValues); 628 apr = true; 629 } catch (Throwable t) { 630 } 632 633 if (apr) { 634 if ("HTTP/1.1".equals(protocol)) { 635 setProtocolHandlerClassName 636 ("org.apache.coyote.http11.Http11AprProtocol"); 637 } else if ("AJP/1.3".equals(protocol)) { 638 setProtocolHandlerClassName 639 ("org.apache.coyote.ajp.AjpAprProtocol"); 640 } else if (protocol != null) { 641 setProtocolHandlerClassName(protocol); 642 } else { 643 setProtocolHandlerClassName 644 ("org.apache.coyote.http11.Http11AprProtocol"); 645 } 646 } else { 647 if ("HTTP/1.1".equals(protocol)) { 648 setProtocolHandlerClassName 649 ("org.apache.coyote.http11.Http11Protocol"); 650 } else if ("AJP/1.3".equals(protocol)) { 651 setProtocolHandlerClassName 652 ("org.apache.coyote.ajp.AjpProtocol"); 653 } else if (protocol != null) { 654 setProtocolHandlerClassName(protocol); 655 } 656 } 657 658 } 659 660 661 664 public String getProtocolHandlerClassName() { 665 666 return (this.protocolHandlerClassName); 667 668 } 669 670 671 677 public void setProtocolHandlerClassName(String protocolHandlerClassName) { 678 679 this.protocolHandlerClassName = protocolHandlerClassName; 680 681 } 682 683 684 687 public ProtocolHandler getProtocolHandler() { 688 689 return (this.protocolHandler); 690 691 } 692 693 694 697 public String getProxyName() { 698 699 return (this.proxyName); 700 701 } 702 703 704 709 public void setProxyName(String proxyName) { 710 711 if(proxyName != null && proxyName.length() > 0) { 712 this.proxyName = proxyName; 713 setProperty("proxyName", proxyName); 714 } else { 715 this.proxyName = null; 716 removeProperty("proxyName"); 717 } 718 719 } 720 721 722 725 public int getProxyPort() { 726 727 return (this.proxyPort); 728 729 } 730 731 732 737 public void setProxyPort(int proxyPort) { 738 739 this.proxyPort = proxyPort; 740 setProperty("proxyPort", String.valueOf(proxyPort)); 741 742 } 743 744 745 750 public int getRedirectPort() { 751 752 return (this.redirectPort); 753 754 } 755 756 757 762 public void setRedirectPort(int redirectPort) { 763 764 this.redirectPort = redirectPort; 765 setProperty("redirectPort", String.valueOf(redirectPort)); 766 767 } 768 769 770 774 public String getScheme() { 775 776 return (this.scheme); 777 778 } 779 780 781 787 public void setScheme(String scheme) { 788 789 this.scheme = scheme; 790 791 } 792 793 794 798 public boolean getSecure() { 799 800 return (this.secure); 801 802 } 803 804 805 811 public void setSecure(boolean secure) { 812 813 this.secure = secure; 814 setProperty("secure", Boolean.toString(secure)); 815 } 816 817 820 public String getURIEncoding() { 821 822 return (this.URIEncoding); 823 824 } 825 826 827 832 public void setURIEncoding(String URIEncoding) { 833 834 this.URIEncoding = URIEncoding; 835 setProperty("uRIEncoding", URIEncoding); 836 837 } 838 839 840 843 public boolean getUseBodyEncodingForURI() { 844 845 return (this.useBodyEncodingForURI); 846 847 } 848 849 850 855 public void setUseBodyEncodingForURI(boolean useBodyEncodingForURI) { 856 857 this.useBodyEncodingForURI = useBodyEncodingForURI; 858 setProperty 859 ("useBodyEncodingForURI", String.valueOf(useBodyEncodingForURI)); 860 861 } 862 863 864 871 public boolean getXpoweredBy() { 872 return xpoweredBy; 873 } 874 875 876 884 public void setXpoweredBy(boolean xpoweredBy) { 885 this.xpoweredBy = xpoweredBy; 886 setProperty("xpoweredBy", String.valueOf(xpoweredBy)); 887 } 888 889 895 public void setUseIPVHosts(boolean useIPVHosts) { 896 this.useIPVHosts = useIPVHosts; 897 setProperty("useIPVHosts", String.valueOf(useIPVHosts)); 898 } 899 900 903 public boolean getUseIPVHosts() { 904 return useIPVHosts; 905 } 906 907 909 910 914 public Request createRequest() { 915 916 Request request = new Request(); 917 request.setConnector(this); 918 return (request); 919 920 } 921 922 923 927 public Response createResponse() { 928 929 Response response = new Response(); 930 response.setConnector(this); 931 return (response); 932 933 } 934 935 936 938 939 944 public void addLifecycleListener(LifecycleListener listener) { 945 946 lifecycle.addLifecycleListener(listener); 947 948 } 949 950 951 955 public LifecycleListener[] findLifecycleListeners() { 956 957 return lifecycle.findLifecycleListeners(); 958 959 } 960 961 962 967 public void removeLifecycleListener(LifecycleListener listener) { 968 969 lifecycle.removeLifecycleListener(listener); 970 971 } 972 973 974 protected ObjectName createObjectName(String domain, String type) 975 throws MalformedObjectNameException { 976 String encodedAddr = null; 977 if (getProperty("address") != null) { 978 encodedAddr = URLEncoder.encode(getProperty("address").toString()); 979 } 980 String addSuffix = (getProperty("address") == null) ? "" : ",address=" 981 + encodedAddr; 982 ObjectName _oname = new ObjectName (domain + ":type=" + type + ",port=" 983 + getPort() + addSuffix); 984 return _oname; 985 } 986 987 990 public void initialize() 991 throws LifecycleException 992 { 993 if (initialized) { 994 if(log.isInfoEnabled()) 995 log.info(sm.getString("coyoteConnector.alreadyInitialized")); 996 return; 997 } 998 999 this.initialized = true; 1000 1001 if( oname == null && (container instanceof StandardEngine)) { 1002 try { 1003 StandardEngine cb=(StandardEngine)container; 1005 oname = createObjectName(cb.getName(), "Connector"); 1006 Registry.getRegistry(null, null) 1007 .registerComponent(this, oname, null); 1008 controller=oname; 1009 } catch (Exception e) { 1010 log.error( "Error registering connector ", e); 1011 } 1012 if(log.isDebugEnabled()) 1013 log.debug("Creating name for connector " + oname); 1014 } 1015 1016 adapter = new CoyoteAdapter(this); 1018 protocolHandler.setAdapter(adapter); 1019 1020 IntrospectionUtils.setProperty(protocolHandler, "jkHome", 1021 System.getProperty("catalina.base")); 1022 1023 try { 1024 protocolHandler.init(); 1025 } catch (Exception e) { 1026 throw new LifecycleException 1027 (sm.getString 1028 ("coyoteConnector.protocolHandlerInitializationFailed", e)); 1029 } 1030 } 1031 1032 1033 1036 public void pause() 1037 throws LifecycleException { 1038 try { 1039 protocolHandler.pause(); 1040 } catch (Exception e) { 1041 log.error(sm.getString 1042 ("coyoteConnector.protocolHandlerPauseFailed"), e); 1043 } 1044 } 1045 1046 1047 1050 public void resume() 1051 throws LifecycleException { 1052 try {
|