org.eclipse.jetty.server.AbstractConnectionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jetty-server Show documentation
Show all versions of jetty-server Show documentation
The core jetty server artifact.
The newest version!
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.server;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jetty.io.AbstractConnection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.ArrayUtil;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;
/**
* Provides the common handling for {@link ConnectionFactory} implementations.
*/
@ManagedObject
public abstract class AbstractConnectionFactory extends ContainerLifeCycle implements ConnectionFactory
{
private final String _protocol;
private final List _protocols;
private int _inputBufferSize = IO.DEFAULT_BUFFER_SIZE;
protected AbstractConnectionFactory(String protocol)
{
_protocol = protocol;
_protocols = List.of(protocol);
}
protected AbstractConnectionFactory(String... protocols)
{
_protocol = protocols[0];
_protocols = List.of(protocols);
}
@Override
@ManagedAttribute(value = "The protocol name", readonly = true)
public String getProtocol()
{
return _protocol;
}
@Override
public List getProtocols()
{
return _protocols;
}
@ManagedAttribute("The buffer size used to read from the network")
public int getInputBufferSize()
{
return _inputBufferSize;
}
public void setInputBufferSize(int size)
{
_inputBufferSize = size;
}
protected String findNextProtocol(Connector connector)
{
return findNextProtocol(connector, getProtocol());
}
protected static String findNextProtocol(Connector connector, String currentProtocol)
{
String nextProtocol = null;
for (Iterator it = connector.getProtocols().iterator(); it.hasNext(); )
{
String protocol = it.next();
if (currentProtocol.equalsIgnoreCase(protocol))
{
nextProtocol = it.hasNext() ? it.next() : null;
break;
}
}
return nextProtocol;
}
protected T configure(T connection, Connector connector, EndPoint endPoint)
{
// Add Connection.Listeners from Connector.
connector.getEventListeners().forEach(connection::addEventListener);
// Add Connection.Listeners from this factory.
getEventListeners().forEach(connection::addEventListener);
connection.setInputBufferSize(getInputBufferSize());
return connection;
}
@Override
public String toString()
{
return String.format("%s@%x%s", this.getClass().getSimpleName(), hashCode(), getProtocols());
}
public static ConnectionFactory[] getFactories(SslContextFactory.Server sslContextFactory, ConnectionFactory... factories)
{
factories = ArrayUtil.removeNulls(factories);
if (sslContextFactory == null)
return factories;
for (ConnectionFactory factory : factories)
{
if (factory instanceof HttpConfiguration.ConnectionFactory)
{
HttpConfiguration config = ((HttpConfiguration.ConnectionFactory)factory).getHttpConfiguration();
if (config.getCustomizer(SecureRequestCustomizer.class) == null)
config.addCustomizer(new SecureRequestCustomizer());
}
}
return ArrayUtil.prependToArray(new SslConnectionFactory(sslContextFactory, factories[0].getProtocol()), factories, ConnectionFactory.class);
}
}