Search in sources :

Example 1 with DefaultIoFilterChainBuilder

use of org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder in project Openfire by igniterealtime.

the class MINAConnectionAcceptor method start.

/**
     * Starts this acceptor by binding the socket acceptor. When the acceptor is already started, a warning will be
     * logged and the method invocation is otherwise ignored.
     */
@Override
public synchronized void start() {
    if (socketAcceptor != null) {
        Log.warn("Unable to start acceptor (it is already started!)");
        return;
    }
    try {
        // Configure the thread pool that is to be used.
        final int initialSize = (configuration.getMaxThreadPoolSize() / 4) + 1;
        final ExecutorFilter executorFilter = new ExecutorFilter(initialSize, configuration.getMaxThreadPoolSize(), 60, TimeUnit.SECONDS);
        final ThreadPoolExecutor eventExecutor = (ThreadPoolExecutor) executorFilter.getExecutor();
        final ThreadFactory threadFactory = new NamedThreadFactory(name + "-thread-", eventExecutor.getThreadFactory(), true, null);
        eventExecutor.setThreadFactory(threadFactory);
        // Construct a new socket acceptor, and configure it.
        socketAcceptor = buildSocketAcceptor();
        if (JMXManager.isEnabled()) {
            configureJMX(socketAcceptor, name);
        }
        final DefaultIoFilterChainBuilder filterChain = socketAcceptor.getFilterChain();
        filterChain.addFirst(ConnectionManagerImpl.EXECUTOR_FILTER_NAME, executorFilter);
        // Add the XMPP codec filter
        filterChain.addAfter(ConnectionManagerImpl.EXECUTOR_FILTER_NAME, ConnectionManagerImpl.XMPP_CODEC_FILTER_NAME, new ProtocolCodecFilter(new XMPPCodecFactory()));
        // Kill sessions whose outgoing queues keep growing and fail to send traffic
        filterChain.addAfter(ConnectionManagerImpl.XMPP_CODEC_FILTER_NAME, ConnectionManagerImpl.CAPACITY_FILTER_NAME, new StalledSessionsFilter());
        // Ports can be configured to start connections in SSL (as opposed to upgrade a non-encrypted socket to an encrypted one, typically using StartTLS)
        if (configuration.getTlsPolicy() == Connection.TLSPolicy.legacyMode) {
            final SslFilter sslFilter = encryptionArtifactFactory.createServerModeSslFilter();
            filterChain.addAfter(ConnectionManagerImpl.EXECUTOR_FILTER_NAME, ConnectionManagerImpl.TLS_FILTER_NAME, sslFilter);
        }
        // Throttle sessions who send data too fast
        if (configuration.getMaxBufferSize() > 0) {
            socketAcceptor.getSessionConfig().setMaxReadBufferSize(configuration.getMaxBufferSize());
            Log.debug("Throttling read buffer for connections to max={} bytes", configuration.getMaxBufferSize());
        }
        // Start accepting connections
        socketAcceptor.setHandler(connectionHandler);
        socketAcceptor.bind(new InetSocketAddress(configuration.getBindAddress(), configuration.getPort()));
    } catch (Exception e) {
        System.err.println("Error starting " + configuration.getPort() + ": " + e.getMessage());
        Log.error("Error starting: " + configuration.getPort(), e);
        // Reset for future use.
        if (socketAcceptor != null) {
            try {
                socketAcceptor.unbind();
            } finally {
                socketAcceptor = null;
            }
        }
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) NamedThreadFactory(org.jivesoftware.util.NamedThreadFactory) SslFilter(org.apache.mina.filter.ssl.SslFilter) NamedThreadFactory(org.jivesoftware.util.NamedThreadFactory) InetSocketAddress(java.net.InetSocketAddress) ExecutorFilter(org.apache.mina.filter.executor.ExecutorFilter) DefaultIoFilterChainBuilder(org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder) StalledSessionsFilter(org.jivesoftware.openfire.net.StalledSessionsFilter) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) MalformedObjectNameException(javax.management.MalformedObjectNameException) JMException(javax.management.JMException)

Example 2 with DefaultIoFilterChainBuilder

use of org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder in project wildfly by wildfly.

the class LdapsInitializer method init.

public static IoFilterChainBuilder init(LdapServer server, TcpTransport transport) throws LdapException {
    SSLContext sslCtx;
    try {
        // Initialize the SSLContext to work with our key managers.
        sslCtx = SSLContext.getInstance("TLS");
        sslCtx.init(server.getKeyManagerFactory().getKeyManagers(), new TrustManager[] { new TrustAndStoreTrustManager() }, new SecureRandom());
    } catch (Exception e) {
        throw new LdapException(I18n.err(I18n.ERR_683), e);
    }
    DefaultIoFilterChainBuilder chain = new DefaultIoFilterChainBuilder();
    SslFilter sslFilter = new SslFilter(sslCtx);
    List<String> cipherSuites = transport.getCipherSuite();
    if ((cipherSuites != null) && !cipherSuites.isEmpty()) {
        sslFilter.setEnabledCipherSuites(cipherSuites.toArray(new String[cipherSuites.size()]));
    }
    sslFilter.setWantClientAuth(true);
    // The protocols
    List<String> enabledProtocols = transport.getEnabledProtocols();
    if ((enabledProtocols != null) && !enabledProtocols.isEmpty()) {
        sslFilter.setEnabledProtocols(enabledProtocols.toArray(new String[enabledProtocols.size()]));
    } else {
        // Be sure we disable SSLV3
        sslFilter.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" });
    }
    // The remaining SSL parameters
    sslFilter.setNeedClientAuth(transport.isNeedClientAuth());
    //sslFilter.setWantClientAuth(transport.isWantClientAuth());
    chain.addLast("sslFilter", sslFilter);
    return chain;
}
Also used : SslFilter(org.apache.mina.filter.ssl.SslFilter) TrustAndStoreTrustManager(org.jboss.as.test.manualmode.security.TrustAndStoreTrustManager) SecureRandom(java.security.SecureRandom) DefaultIoFilterChainBuilder(org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder) SSLContext(javax.net.ssl.SSLContext) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 3 with DefaultIoFilterChainBuilder

use of org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder in project Openfire by igniterealtime.

the class DebuggerPlugin method addFilterToChain.

protected void addFilterToChain(final SocketAcceptor acceptor, final String filterName, final IoFilter filter) {
    if (acceptor == null) {
        Log.debug("Not adding filter '{}' to acceptor that is null.", filterName);
        return;
    }
    final DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
    if (chain.contains(COMPRESSION_FILTER_NAME)) {
        Log.debug("Adding filter '{}' as the first filter after the compression filter in acceptor {}", filterName, acceptor);
        chain.addAfter(COMPRESSION_FILTER_NAME, filterName, filter);
    } else if (chain.contains(TLS_FILTER_NAME)) {
        Log.debug("Adding filter '{}' as the first filter after the TLS filter in acceptor {}", filterName, acceptor);
        chain.addAfter(TLS_FILTER_NAME, filterName, filter);
    } else {
        Log.debug("Adding filter '{}' as the last filter in acceptor {}", filterName, acceptor);
        chain.addLast(filterName, filter);
    }
}
Also used : DefaultIoFilterChainBuilder(org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder)

Example 4 with DefaultIoFilterChainBuilder

use of org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder in project zm-mailbox by Zimbra.

the class NioServer method start.

/**
     * Starts the server. Binds the server port and starts the connection
     * handler. Optionally adds an SSLFilter if ssl is enabled.
     *
     * @throws IOException if an I/O error occured while starting the server
     */
@Override
public void start() {
    ServerConfig sc = getConfig();
    DefaultIoFilterChainBuilder fc = acceptor.getFilterChain();
    if (sc.isSslEnabled()) {
        fc.addFirst("ssl", newSSLFilter());
    }
    fc.addLast("executer", executorFilter);
    fc.addLast("logger", new NioLoggingFilter(this, false));
    fc.addLast("codec", new ProtocolCodecFilter(getProtocolCodecFactory()));
    for (IoFilter filter : FILTERS.get(getClass())) {
        // insert custom filters
        fc.addLast(filter.getClass().getName(), filter);
    }
    acceptor.getSessionConfig().setBothIdleTime(sc.getMaxIdleTime());
    acceptor.getSessionConfig().setWriteTimeout(sc.getWriteTimeout());
    acceptor.setHandler(new NioHandlerDispatcher(this));
    try {
        acceptor.bind();
    } catch (Throwable e) {
        Zimbra.halt(getName() + " failed to start", e);
    }
    getLog().info("Starting %s on %s", getName(), acceptor.getLocalAddress());
}
Also used : DefaultIoFilterChainBuilder(org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder) IoFilter(org.apache.mina.core.filterchain.IoFilter) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter)

Aggregations

DefaultIoFilterChainBuilder (org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder)4 ProtocolCodecFilter (org.apache.mina.filter.codec.ProtocolCodecFilter)2 SslFilter (org.apache.mina.filter.ssl.SslFilter)2 InetSocketAddress (java.net.InetSocketAddress)1 SecureRandom (java.security.SecureRandom)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 JMException (javax.management.JMException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 SSLContext (javax.net.ssl.SSLContext)1 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)1 IoFilter (org.apache.mina.core.filterchain.IoFilter)1 ExecutorFilter (org.apache.mina.filter.executor.ExecutorFilter)1 TrustAndStoreTrustManager (org.jboss.as.test.manualmode.security.TrustAndStoreTrustManager)1 StalledSessionsFilter (org.jivesoftware.openfire.net.StalledSessionsFilter)1 NamedThreadFactory (org.jivesoftware.util.NamedThreadFactory)1