Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Licensed to the Apache Software Foundation (ASF) under one or more
    * contributor license agreements.  See the NOTICE file distributed with
    * this work for additional information regarding copyright ownership.
    * The ASF licenses this file to You under the Apache License, Version 2.0
    * (the "License"); you may not use this file except in compliance with
    * the License.  You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  package org.apache.catalina.connector;
  
  import static org.jboss.web.CatalinaMessages.MESSAGES;
  
  import java.util.HashMap;
  import java.util.Set;
  
  
Implementation of a Coyote connector for Tomcat 5.x.

Author(s):
Craig R. McClanahan
Remy Maucherat
Version:
$Revision: 2042 $ $Date: 2012-06-14 16:11:34 +0200 (Thu, 14 Jun 2012) $
  
  
  
  public class Connector
      implements LifecycleMBeanRegistration
  {

    
Alternate flag to enable recycling of facades.
  
      public static final boolean RECYCLE_FACADES =
          Boolean.valueOf(System.getProperty("org.apache.catalina.connector.RECYCLE_FACADES"
                  (.....) ? "true" : "false")).booleanValue();
  
      
      protected static final boolean X_POWERED_BY = 
          Boolean.valueOf(System.getProperty("org.apache.catalina.connector.X_POWERED_BY""false")).booleanValue();
      
      protected static final boolean ALLOW_TRACE = 
              Boolean.valueOf(System.getProperty("org.apache.catalina.connector.ALLOW_TRACE""false")).booleanValue();
  
      protected static final String URI_ENCODING = 
          System.getProperty("org.apache.catalina.connector.URI_ENCODING");
      
      protected static final boolean USE_BODY_ENCODING_FOR_QUERY_STRING = 
          Boolean.valueOf(System.getProperty("org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING""false")).booleanValue();
      
  
      // ------------------------------------------------------------ Constructor
  
  
      public Connector(String protocol)
          throws Exception {
          setProtocol(protocol);
          // Instantiate protocol handler
          try {
              Class<?> clazz = Class.forName();
              this. = (ProtocolHandlerclazz.newInstance();
          } catch (Exception e) {
              throw .protocolHandlerInstantiationFailed(e);
          }
      }
  
  
      // ----------------------------------------------------- Instance Variables
  

    
The Service we are associated with (if any).
 
     protected Service service = null;


    
Do we allow TRACE ?
 
     protected boolean allowTrace = ;


    
The Container used for processing requests received by this Connector.
 
     protected Container container = null;


    
The "enable DNS lookups" flag for this Connector.
 
     protected boolean enableLookups = false;


    
Is generation of X-Powered-By response header enabled/disabled?
 
     protected boolean xpoweredBy = ;


    
Descriptive information about this Connector implementation.
 
     protected static final String info =
         "org.apache.catalina.connector.Connector/2.1";


    
The lifecycle event support for this component.
 
     protected LifecycleSupport lifecycle = new LifecycleSupport(this);


    
The port number on which we listen for requests.
 
     protected int port = 0;


    
The server name to which we should pretend requests to this Connector were directed. This is useful when operating Tomcat behind a proxy server, so that redirects get constructed accurately. If not specified, the server name included in the Host header is used.
 
     protected String proxyName = null;


    
The server port to which we should pretent requests to this Connector were directed. This is useful when operating Tomcat behind a proxy server, so that redirects get constructed accurately. If not specified, the port number specified by the port property is used.
 
     protected int proxyPort = 0;


    
The redirect port for non-SSL to SSL redirects.
 
     protected int redirectPort = 443;


    
The request scheme that will be set on all requests received through this connector.
 
     protected String scheme = "http";


    
The secure connection flag that will be set on all requests received through this connector.
 
     protected boolean secure = false;


    
Maximum size of a POST which will be automatically parsed by the container. 2MB by default.
 
     protected int maxPostSize = 2 * 1024 * 1024;


    
Maximum size of a POST which will be saved by the container during authentication. 4kB by default
 
     protected int maxSavePostSize = 4 * 1024;


    
Has this component been initialized yet?
 
     protected boolean initialized = false;


    
Has this component been started yet?
 
     protected boolean started = false;


    
The shutdown signal to our background thread
 
     protected boolean stopped = false;

    
Flag to use IP-based virtual hosting.
 
     protected boolean useIPVHosts = 
             Boolean.valueOf(System.getProperty("org.apache.catalina.connector.USE_IP_PORT_FOR_ALIAS""false")).booleanValue();

    
The background thread.
 
     protected Thread thread = null;


    
Coyote Protocol handler class name. Defaults to the Coyote HTTP/1.1 protocolHandler.
 
     protected String protocolHandlerClassName =
         "org.apache.coyote.http11.Http11Protocol";


    
Coyote protocol handler.
 
     protected ProtocolHandler protocolHandler = null;


    
Coyote adapter.
 
     protected Adapter adapter = null;


     
URI encoding.
 
      protected String URIEncoding = ;


     
URI encoding as body.
 
      protected boolean useBodyEncodingForURI = ;

     
     
Allowed virtual hosts.
 
      protected Set<StringallowedHosts = null;
      
 
      protected static HashMap<StringStringreplacements = new HashMap<StringString>();
      static {
          .put("acceptCount""backlog");
          .put("connectionLinger""soLinger");
          .put("connectionTimeout""soTimeout");
          .put("connectionUploadTimeout""timeout");
          .put("clientAuth""clientauth");
          .put("keystoreFile""keystore");
          .put("randomFile""randomfile");
          .put("rootFile""rootfile");
          .put("keystorePass""keypass");
          .put("keystoreType""keytype");
          .put("sslProtocol""protocol");
          .put("sslProtocols""protocols");
      }
 
 
     // ------------------------------------------------------------- Properties
 

    
Return a configured property.
 
     public Object getProperty(String name) {
         String repl = name;
         if (.get(name) != null) {
             repl = (String.get(name);
         }
         return IntrospectionUtils.getProperty(repl);
     }


    
Set a configured property.
 
     public boolean setProperty(String nameString value) {
         String repl = name;
         if (.get(name) != null) {
             repl = (String.get(name);
         }
         return IntrospectionUtils.setProperty(replvalue);
     }


    
Return a configured property.
 
     public Object getAttribute(String name) {
         return getProperty(name);
     }


    
Set a configured property.
 
     public void setAttribute(String nameObject value) {
         setProperty(name, String.valueOf(value));
     }


    
Return the Service with which we are associated (if any).
 
     public Service getService() {
 
         return (this.);
 
     }


    
Set the Service with which we are associated (if any).

Parameters:
service The service that owns this Engine
 
     public void setService(Service service) {
 
         this. = service;
 
     }


    
True if the TRACE method is allowed. Default value is "false".
 
     public boolean getAllowTrace() {
 
         return (this.);
 
     }


    
Set the allowTrace flag, to disable or enable the TRACE HTTP method.

Parameters:
allowTrace The new allowTrace flag
 
     public void setAllowTrace(boolean allowTrace) {
 
         this. = allowTrace;
         setProperty("allowTrace", String.valueOf(allowTrace));
 
     }


    
Set of allowed hosts.
 
     public Set<StringgetAllowedHosts() {
 
         return ;
 
     }


    
Restrict the connector to certain hosts.
 
     public void setAllowedHosts(Set<StringallowedHosts) {
 
         this. = allowedHosts;
 
     }

   
Is this connector available for processing requests?
 
     public boolean isAvailable() {
 
         return ();
 
     }


    
Return the input buffer size for this Connector.

Deprecated:
 
     public int getBufferSize() {
         return 2048;
     }

    
Set the input buffer size for this Connector.

Deprecated:
Parameters:
bufferSize The new input buffer size.
 
     public void setBufferSize(int bufferSize) {
     }


    
Return the Container used for processing requests received by this Connector.
 
     public Container getContainer() {
         return ();
 
     }


    
Set the Container used for processing requests received by this Connector.

Parameters:
container The new Container to use
 
     public void setContainer(Container container) {
 
         this. = container;
 
     }


    
Return the "enable DNS lookups" flag.
 
     public boolean getEnableLookups() {
 
         return (this.);
 
     }


    
Set the "enable DNS lookups" flag.

Parameters:
enableLookups The new "enable DNS lookups" flag value
 
     public void setEnableLookups(boolean enableLookups) {
 
         this. = enableLookups;
         setProperty("enableLookups", String.valueOf(enableLookups));
 
     }


    
Return descriptive information about this Connector implementation.
 
     public String getInfo() {
 
         return ();
 
     }


    
Return the maximum size of a POST which will be automatically parsed by the container.
 
     public int getMaxPostSize() {
 
         return ();
 
     }


    
Set the maximum size of a POST which will be automatically parsed by the container.

Parameters:
maxPostSize The new maximum size in bytes of a POST which will be automatically parsed by the container
 
     public void setMaxPostSize(int maxPostSize) {
 
         this. = maxPostSize;
     }


    
Return the maximum size of a POST which will be saved by the container during authentication.
 
     public int getMaxSavePostSize() {
 
         return ();
 
     }


    
Set the maximum size of a POST which will be saved by the container during authentication.

Parameters:
maxSavePostSize The new maximum size in bytes of a POST which will be saved by the container during authentication.
 
     public void setMaxSavePostSize(int maxSavePostSize) {
 
         this. = maxSavePostSize;
         setProperty("maxSavePostSize", String.valueOf(maxSavePostSize));
     }


    
Return the port number on which we listen for requests.
 
     public int getPort() {
 
         return (this.);
 
     }


    
Set the port number on which we listen for requests.

Parameters:
port The new port number
 
     public void setPort(int port) {
 
         this. = port;
         setProperty("port", String.valueOf(port));
 
     }


    
Return the Coyote protocol handler in use.
 
     public String getProtocol() {
 
         if ("org.apache.coyote.http11.Http11Protocol".equals
             (getProtocolHandlerClassName())
             || "org.apache.coyote.http11.Http11AprProtocol".equals
             (getProtocolHandlerClassName())
             || "org.apache.coyote.http11.Http11NioProtocol".equals
             (getProtocolHandlerClassName())) {
             return "HTTP/1.1";
         } else if ("org.apache.coyote.ajp.AjpProtocol".equals
                    (getProtocolHandlerClassName())
                    || "org.apache.coyote.ajp.AjpAprProtocol".equals
                    (getProtocolHandlerClassName())) {
             return "AJP/1.3";
         }
         return getProtocolHandlerClassName();
 
     }
    
    
Set the Coyote protocol which will be used by the connector.

Parameters:
protocol The Coyote protocol name
 
     protected void setProtocol(String protocol) {
 
         if (AprLifecycleListener.isAprInitialized()) {
             if ("HTTP/1.1".equals(protocol) || "http".equals(protocol)) {
                 setProtocolHandlerClassName
                     ("org.apache.coyote.http11.Http11AprProtocol");
             } else if ("AJP/1.3".equals(protocol) || "ajp".equals(protocol)) {
                 setProtocolHandlerClassName
                     ("org.apache.coyote.ajp.AjpAprProtocol");
             } else if (protocol != null) {
                 setProtocolHandlerClassName(protocol);
             } else {
                 setProtocolHandlerClassName
                     ("org.apache.coyote.http11.Http11AprProtocol");
             }
         } else {
             if ("HTTP/1.1".equals(protocol) || "http".equals(protocol)) {
                 /*try {
                     Class.forName("java.nio.channels.CompletionHandler");
                     setProtocolHandlerClassName
                         ("org.apache.coyote.http11.Http11NioProtocol");
                 } catch (Exception e) {
                     // NIO 2 is not available
                     setProtocolHandlerClassName
                         ("org.apache.coyote.http11.Http11Protocol");
                     CatalinaLogger.CONNECTOR_LOGGER.usingJavaIoConnector();
                 }*/
                 setProtocolHandlerClassName
                     ("org.apache.coyote.http11.Http11Protocol");
             } else if ("AJP/1.3".equals(protocol) || "ajp".equals(protocol)) {
                 setProtocolHandlerClassName
                     ("org.apache.coyote.ajp.AjpProtocol");
             } else if (protocol != null) {
                 setProtocolHandlerClassName(protocol);
             }
         }
 
     }


    
Return the class name of the Coyote protocol handler in use.
 
     public String getProtocolHandlerClassName() {
 
         return (this.);
 
     }


    
Set the class name of the Coyote protocol handler which will be used by the connector.

Parameters:
protocolHandlerClassName The new class name
 
     public void setProtocolHandlerClassName(String protocolHandlerClassName) {
 
         this. = protocolHandlerClassName;
 
     }


    
Return the protocol handler associated with the connector.
 
     public ProtocolHandler getProtocolHandler() {
 
         return (this.);
 
     }


    
Return the proxy server name for this Connector.
 
     public String getProxyName() {
 
         return (this.);
 
     }


    
Set the proxy server name for this Connector.

Parameters:
proxyName The new proxy server name
 
     public void setProxyName(String proxyName) {
 
         if(proxyName != null && proxyName.length() > 0) {
             this. = proxyName;
             setProperty("proxyName"proxyName);
         } else {
             this. = null;
             setProperty("proxyName"null);
         }
 
     }


    
Return the proxy server port for this Connector.
 
     public int getProxyPort() {
 
         return (this.);
 
     }


    
Set the proxy server port for this Connector.

Parameters:
proxyPort The new proxy server port
 
     public void setProxyPort(int proxyPort) {
 
         this. = proxyPort;
         setProperty("proxyPort", String.valueOf(proxyPort));
 
     }


    
Return the port number to which a request should be redirected if it comes in on a non-SSL port and is subject to a security constraint with a transport guarantee that requires SSL.
 
     public int getRedirectPort() {
 
         return (this.);
 
     }


    
Set the redirect port number.

Parameters:
redirectPort The redirect port number (non-SSL to SSL)
 
     public void setRedirectPort(int redirectPort) {
 
         this. = redirectPort;
         setProperty("redirectPort", String.valueOf(redirectPort));
 
     }


    
Return the scheme that will be assigned to requests received through this connector. Default value is "http".
 
     public String getScheme() {
 
         return (this.);
 
     }


    
Set the scheme that will be assigned to requests received through this connector.

Parameters:
scheme The new scheme
 
     public void setScheme(String scheme) {
 
         this. = scheme;
 
     }


    
Return the secure connection flag that will be assigned to requests received through this connector. Default value is "false".
 
     public boolean getSecure() {
 
         return (this.);
 
     }


    
Set the secure connection flag that will be assigned to requests received through this connector.

Parameters:
secure The new secure connection flag
 
     public void setSecure(boolean secure) {
 
         this. = secure;
         setProperty("secure", Boolean.toString(secure));
     }

     
Return the character encoding to be used for the URI.
 
      public String getURIEncoding() {
 
          return (this.);
 
      }


     
Set the URI encoding to be used for the URI.

Parameters:
URIEncoding The new URI character encoding.
 
      public void setURIEncoding(String URIEncoding) {
 
          this. = URIEncoding;
          setProperty("uRIEncoding"URIEncoding);
 
      }


     
Return the true if the entity body encoding should be used for the URI.
 
      public boolean getUseBodyEncodingForURI() {
 
          return (this.);
 
      }


     
Set if the entity body encoding should be used for the URI.

Parameters:
useBodyEncodingForURI The new value for the flag.
 
      public void setUseBodyEncodingForURI(boolean useBodyEncodingForURI) {
 
          this. = useBodyEncodingForURI;
          setProperty
              ("useBodyEncodingForURI", String.valueOf(useBodyEncodingForURI));
 
      }


    
Indicates whether the generation of an X-Powered-By response header for servlet-generated responses is enabled or disabled for this Connector.

Returns:
true if generation of X-Powered-By response header is enabled, false otherwise
 
     public boolean getXpoweredBy() {
         return ;
     }


    
Indicates if the protocol handler support IO events.

Returns:
true if IO events are supported
 
     public boolean hasIoEvents() {
         return .hasIoEvents();
     }


    
Enables or disables the generation of an X-Powered-By header (with value Servlet/2.4) for all servlet-generated responses returned by this Connector.

Parameters:
xpoweredBy true if generation of X-Powered-By response header is to be enabled, false otherwise
 
     public void setXpoweredBy(boolean xpoweredBy) {
         this. = xpoweredBy;
         setProperty("xpoweredBy", String.valueOf(xpoweredBy));
     }

    
Enable the use of IP-based virtual hosting.

Parameters:
useIPVHosts true if Hosts are identified by IP, false/code> if Hosts are identified by name.
 
     public void setUseIPVHosts(boolean useIPVHosts) {
         this. = useIPVHosts;
         setProperty("useIPVHosts", String.valueOf(useIPVHosts));
     }

    
Test if IP-based virtual hosting is enabled.
 
     public boolean getUseIPVHosts() {
         return ;
     }
 
     // --------------------------------------------------------- Public Methods
 

    
Create (or allocate) and return a Request object suitable for specifying the contents of a Request to the responsible Container.
 
     public Request createRequest() {
 
         Request request = new Request();
         request.setConnector(this);
         return (request);
 
     }


    
Create (or allocate) and return a Response object suitable for receiving the contents of a Response from the responsible Container.
 
     public Response createResponse() {
 
         Response response = new Response();
         response.setConnector(this);
         return (response);
 
     }
 
 
     // ------------------------------------------------------ Lifecycle Methods
 

    
Add a lifecycle event listener to this component.

Parameters:
listener The listener to add
 
     public void addLifecycleListener(LifecycleListener listener) {
 
         .addLifecycleListener(listener);
 
     }


    
Get the lifecycle listeners associated with this lifecycle. If this Lifecycle has no listeners registered, a zero-length array is returned.
 
 
         return .findLifecycleListeners();
 
     }


    
Remove a lifecycle event listener from this component.

Parameters:
listener The listener to add
 
     public void removeLifecycleListener(LifecycleListener listener) {
 
         .removeLifecycleListener(listener);
 
     }
 
 
     protected ObjectName createObjectName(String domainString type)
             throws MalformedObjectNameException {
         String encodedAddr = null;
         if (getProperty("address") != null) {
             encodedAddr = ObjectName.quote(getProperty("address").toString());
         }
         String addSuffix = (getProperty("address") == null) ? "" : ",address="
                 + encodedAddr;
         ObjectName _oname = new ObjectName(domain + ":type=" + type + ",port="
                 + getPort() + addSuffix);
         return _oname;
     }

    
Initialize this connector (create ServerSocket here!)
 
     public void init()
         throws LifecycleException
     {
         if () {
             return;
         }
 
         this. = true;
 
         if (.....) {
             if ( == null) {
                 try {
                     // we are loaded directly, via API - and no name was given to us
                      = createObjectName(.getName(), "Connector");
                     Registry.getRegistry(nullnull)
                     .registerComponent(thisnull);
                     =;
                 } catch (Exception e) {
                     ..failedConnectorJmxRegistration(e);
                 }
             }
         }
 
         // Initializa adapter
          = new CoyoteAdapter(this);
 
         IntrospectionUtils.setProperty("jkHome",
                                        System.getProperty("catalina.base"));
 
         try {
             synchronized (Connector.class) {
                 .init();
             }
         } catch (Exception e) {
             throw new LifecycleException(.protocolHandlerInitFailed(e));
         }
     }


    
Pause the connector.
 
     public void pause()
         throws LifecycleException {
         try {
            .pause();
        } catch (Exception e) {
        }
    }


    
Pause the connector.
    public void resume()
        throws LifecycleException {
        try {
            .resume();
        } catch (Exception e) {
        }
    }


    
Begin processing requests via this Connector.

Throws:
org.apache.catalina.LifecycleException if a fatal startup error occurs
    public void start() throws LifecycleException {
        if( ! )
            init();
        // Validate and update our current state
        if ( ) {
            return;
        }
         = true;
        // We can't register earlier - the JMX registration of this happens
        // in Server.start callback
            if ( this. != null ) {
                // We are registred - register the adapter as well.
                try {
                    Registry.getRegistry(nullnull).registerComponent
                    (createObjectName(this.,"ProtocolHandler"), null);
                } catch (Exception ex) {
                    ..failedProtocolJmxRegistration(ex);
                }
            } else {
            }
        }
        try {
            .start();
        } catch (Exception e) {
            throw new LifecycleException
                (.protocolHandlerStartFailed(e));
        }
    }


    
Terminate processing requests via this Connector.

Throws:
org.apache.catalina.LifecycleException if a fatal shutdown error occurs
    public void stop() throws LifecycleException {
        // Validate and update our current state
        if (!) {
            return;
        }
         = false;
            try {
                Registry.getRegistry(nullnull).unregisterComponent
                (createObjectName(this.,"ProtocolHandler"));
            } catch (MalformedObjectNameException e) {
            }
        }
        try {
            .destroy();
        } catch (Exception e) {
            throw new LifecycleException(.protocolHandlerDestroyFailed(e));
        }
    }
    // -------------------- JMX registration  --------------------
    protected String domain;
    protected ObjectName oname;
    protected MBeanServer mserver;
    public ObjectName getController() {
        return ;
    }
    public void setController(ObjectName controller) {
        this. = controller;
    }
    public ObjectName getObjectName() {
        return ;
    }
    public String getDomain() {
        return ;
    }
    public ObjectName preRegister(MBeanServer server,
                                  ObjectName namethrows Exception {
        =name;
        =server;
        =name.getDomain();
        return name;
    }
    public void postRegister(Boolean registrationDone) {
    }
    public void preDeregister() throws Exception {
    }
    public void postDeregister() {
        try {
            if ) {
                stop();
            }
        } catchThrowable t ) {
        }
    }
    public void destroy() throws Exception {
            if!=null && == ) {
                Registry.getRegistry(nullnull).unregisterComponent();
            }
        }
        ifgetService() == null)
            return;
        getService().removeConnector(this);
    }
New to GrepCode? Check out our FAQ X