Search in sources :

Example 6 with ProtocolHandler

use of org.apache.coyote.ProtocolHandler in project tomcat by apache.

the class ConnectorStoreAppender method getPropertyKeys.

/**
 * Get all properties from Connector and current ProtocolHandler.
 *
 * @param bean The connector
 * @return List of Connector property names
 * @throws IntrospectionException Error introspecting connector
 */
protected List<String> getPropertyKeys(Connector bean) throws IntrospectionException {
    List<String> propertyKeys = new ArrayList<>();
    // Acquire the list of properties for this bean
    ProtocolHandler protocolHandler = bean.getProtocolHandler();
    // Acquire the list of properties for this bean
    PropertyDescriptor[] descriptors = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
    if (descriptors == null) {
        descriptors = new PropertyDescriptor[0];
    }
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor instanceof IndexedPropertyDescriptor) {
            // Indexed properties are not persisted
            continue;
        }
        if (!isPersistable(descriptor.getPropertyType()) || (descriptor.getReadMethod() == null) || (descriptor.getWriteMethod() == null)) {
            // Must be a read-write primitive or String
            continue;
        }
        if ("protocol".equals(descriptor.getName()) || "protocolHandlerClassName".equals(descriptor.getName())) {
            continue;
        }
        propertyKeys.add(descriptor.getName());
    }
    // Add the properties of the protocol handler
    descriptors = Introspector.getBeanInfo(protocolHandler.getClass()).getPropertyDescriptors();
    if (descriptors == null) {
        descriptors = new PropertyDescriptor[0];
    }
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor instanceof IndexedPropertyDescriptor) {
            // Indexed properties are not persisted
            continue;
        }
        if (!isPersistable(descriptor.getPropertyType()) || (descriptor.getReadMethod() == null) || (descriptor.getWriteMethod() == null)) {
            // Must be a read-write primitive or String
            continue;
        }
        String key = descriptor.getName();
        if (!Connector.INTERNAL_EXECUTOR_NAME.equals(bean.getExecutorName()) && internalExecutorAttributes.contains(key)) {
            continue;
        }
        if (replacements.get(key) != null) {
            key = replacements.get(key);
        }
        if (!propertyKeys.contains(key)) {
            propertyKeys.add(key);
        }
    }
    // Add the properties for the socket
    final String socketName = "socket.";
    descriptors = Introspector.getBeanInfo(SocketProperties.class).getPropertyDescriptors();
    if (descriptors == null) {
        descriptors = new PropertyDescriptor[0];
    }
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor instanceof IndexedPropertyDescriptor) {
            // Indexed properties are not persisted
            continue;
        }
        if (!isPersistable(descriptor.getPropertyType()) || (descriptor.getReadMethod() == null) || (descriptor.getWriteMethod() == null)) {
            // Must be a read-write primitive or String
            continue;
        }
        String key = descriptor.getName();
        if (replacements.get(key) != null) {
            key = replacements.get(key);
        }
        if (!propertyKeys.contains(key)) {
            // Add socket.[original name] if this is not a property
            // that could be set elsewhere
            propertyKeys.add(socketName + descriptor.getName());
        }
    }
    return propertyKeys;
}
Also used : ProtocolHandler(org.apache.coyote.ProtocolHandler) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor)

Example 7 with ProtocolHandler

use of org.apache.coyote.ProtocolHandler in project tomcat by apache.

the class ThreadLocalLeakPreventionListener method stopIdleThreads.

/**
 * Updates each ThreadPoolExecutor with the current time, which is the time
 * when a context is being stopped.
 *
 * @param context
 *            the context being stopped, used to discover all the Connectors
 *            of its parent Service.
 */
private void stopIdleThreads(Context context) {
    if (serverStopping) {
        return;
    }
    if (!(context instanceof StandardContext) || !((StandardContext) context).getRenewThreadsWhenStoppingContext()) {
        log.debug("Not renewing threads when the context is stopping. " + "It is not configured to do it.");
        return;
    }
    Engine engine = (Engine) context.getParent().getParent();
    Service service = engine.getService();
    Connector[] connectors = service.findConnectors();
    if (connectors != null) {
        for (Connector connector : connectors) {
            ProtocolHandler handler = connector.getProtocolHandler();
            Executor executor = null;
            if (handler != null) {
                executor = handler.getExecutor();
            }
            if (executor instanceof ThreadPoolExecutor) {
                ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
                threadPoolExecutor.contextStopping();
            } else if (executor instanceof StandardThreadExecutor) {
                StandardThreadExecutor stdThreadExecutor = (StandardThreadExecutor) executor;
                stdThreadExecutor.contextStopping();
            }
        }
    }
}
Also used : ProtocolHandler(org.apache.coyote.ProtocolHandler) Connector(org.apache.catalina.connector.Connector) Executor(java.util.concurrent.Executor) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) Service(org.apache.catalina.Service) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) Engine(org.apache.catalina.Engine)

Example 8 with ProtocolHandler

use of org.apache.coyote.ProtocolHandler in project tomcat by apache.

the class TestCustomSslTrustManager method doTestCustomTrustManager.

private void doTestCustomTrustManager(TrustType trustType) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    TesterSupport.configureClientCertContext(tomcat);
    Connector connector = tomcat.getConnector();
    TesterSupport.configureSSLImplementation(tomcat, sslImplementationName);
    if (needApr) {
        AprLifecycleListener listener = new AprLifecycleListener();
        Assume.assumeTrue(AprLifecycleListener.isAprAvailable());
        StandardServer server = (StandardServer) tomcat.getServer();
        server.addLifecycleListener(listener);
    }
    // Override the defaults
    ProtocolHandler handler = connector.getProtocolHandler();
    if (handler instanceof AbstractHttp11JsseProtocol) {
        connector.findSslHostConfigs()[0].setTruststoreFile(null);
    } else {
        // Unexpected
        Assert.fail("Unexpected handler type");
    }
    if (trustType.equals(TrustType.ALL)) {
        connector.findSslHostConfigs()[0].setTrustManagerClassName("org.apache.tomcat.util.net.TesterSupport$TrustAllCerts");
    } else if (trustType.equals(TrustType.CA)) {
        connector.findSslHostConfigs()[0].setTrustManagerClassName("org.apache.tomcat.util.net.TesterSupport$SequentialTrustManager");
    }
    // Start Tomcat
    tomcat.start();
    TesterSupport.configureClientSsl();
    // Unprotected resource
    ByteChunk res = getUrl("https://localhost:" + getPort() + "/unprotected");
    Assert.assertEquals("OK", res.toString());
    // Protected resource
    res.recycle();
    int rc = -1;
    try {
        rc = getUrl("https://localhost:" + getPort() + "/protected", res, null, null);
    } catch (SocketException | SSLException e) {
        if (!trustType.equals(TrustType.NONE)) {
            Assert.fail(e.getMessage());
            e.printStackTrace();
        }
    }
    if (trustType.equals(TrustType.CA)) {
        if (log.isDebugEnabled()) {
            int count = TesterSupport.getLastClientAuthRequestedIssuerCount();
            log.debug("Last client KeyManager usage: " + TesterSupport.getLastClientAuthKeyManagerUsage() + ", " + count + " requested Issuers, first one: " + (count > 0 ? TesterSupport.getLastClientAuthRequestedIssuer(0).getName() : "NONE"));
            log.debug("Expected requested Issuer: " + TesterSupport.getClientAuthExpectedIssuer());
        }
        Assert.assertTrue("Checking requested client issuer against " + TesterSupport.getClientAuthExpectedIssuer(), TesterSupport.checkLastClientAuthRequestedIssuers());
    }
    if (trustType.equals(TrustType.NONE)) {
        Assert.assertTrue(rc != 200);
        Assert.assertNull(res.toString());
    } else {
        Assert.assertEquals(200, rc);
        Assert.assertEquals("OK-" + TesterSupport.ROLE, res.toString());
    }
}
Also used : ProtocolHandler(org.apache.coyote.ProtocolHandler) Connector(org.apache.catalina.connector.Connector) AbstractHttp11JsseProtocol(org.apache.coyote.http11.AbstractHttp11JsseProtocol) SocketException(java.net.SocketException) Tomcat(org.apache.catalina.startup.Tomcat) AprLifecycleListener(org.apache.catalina.core.AprLifecycleListener) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) StandardServer(org.apache.catalina.core.StandardServer) SSLException(javax.net.ssl.SSLException)

Example 9 with ProtocolHandler

use of org.apache.coyote.ProtocolHandler in project spring-boot by spring-projects.

the class TomcatWebServerFactoryCustomizer method customizeMaxKeepAliveRequests.

private void customizeMaxKeepAliveRequests(ConfigurableTomcatWebServerFactory factory, int maxKeepAliveRequests) {
    factory.addConnectorCustomizers((connector) -> {
        ProtocolHandler handler = connector.getProtocolHandler();
        if (handler instanceof AbstractHttp11Protocol) {
            AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
            protocol.setMaxKeepAliveRequests(maxKeepAliveRequests);
        }
    });
}
Also used : ProtocolHandler(org.apache.coyote.ProtocolHandler) AbstractHttp11Protocol(org.apache.coyote.http11.AbstractHttp11Protocol)

Example 10 with ProtocolHandler

use of org.apache.coyote.ProtocolHandler in project spring-boot by spring-projects.

the class TomcatWebServerFactoryCustomizer method customizeKeepAliveTimeout.

private void customizeKeepAliveTimeout(ConfigurableTomcatWebServerFactory factory, Duration keepAliveTimeout) {
    factory.addConnectorCustomizers((connector) -> {
        ProtocolHandler handler = connector.getProtocolHandler();
        if (handler instanceof AbstractProtocol) {
            AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
            protocol.setKeepAliveTimeout((int) keepAliveTimeout.toMillis());
        }
    });
}
Also used : ProtocolHandler(org.apache.coyote.ProtocolHandler) AbstractProtocol(org.apache.coyote.AbstractProtocol)

Aggregations

ProtocolHandler (org.apache.coyote.ProtocolHandler)27 AbstractProtocol (org.apache.coyote.AbstractProtocol)8 Connector (org.apache.catalina.connector.Connector)7 AbstractHttp11Protocol (org.apache.coyote.http11.AbstractHttp11Protocol)7 Tomcat (org.apache.catalina.startup.Tomcat)5 AbstractHttp11JsseProtocol (org.apache.coyote.http11.AbstractHttp11JsseProtocol)5 SocketException (java.net.SocketException)4 Service (org.apache.catalina.Service)4 SSLException (javax.net.ssl.SSLException)3 File (java.io.File)2 Executor (java.util.concurrent.Executor)2 ObjectName (javax.management.ObjectName)2 Context (org.apache.catalina.Context)2 Engine (org.apache.catalina.Engine)2 Http11AprProtocol (org.apache.coyote.http11.Http11AprProtocol)2 Http11NioProtocol (org.apache.coyote.http11.Http11NioProtocol)2 Http11Protocol (org.apache.coyote.http11.Http11Protocol)2 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)2 ThreadPoolExecutor (org.apache.tomcat.util.threads.ThreadPoolExecutor)2 Test (org.junit.jupiter.api.Test)2