Skip to content

Commit

Permalink
OF-2615: Avoid direct usage of ConnectionManagerImpl
Browse files Browse the repository at this point in the history
Openfire’s ConnectionManager module has one implementation: ConnectionManagerImpl. The two are often used interchangably.

Where possible, Openfire should use the interface, rather than the implemention. This can be easily achieved by defining often-used methods in the interface definition. If these methods are that important, that should be reason enough to define them in the interface contract.

The benefit of this approach is that mocking a ConnectionManager becomes easier.
  • Loading branch information
guusdk committed Jun 27, 2023
1 parent 219963d commit aa6a417
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2004-2008 Jive Software, 2022 Ignite Realtime Foundation. All rights reserved.
* Copyright (C) 2004-2008 Jive Software, 2022-2023 Ignite Realtime Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,11 @@

package org.jivesoftware.openfire;

import org.jivesoftware.openfire.spi.ConnectionListener;
import org.jivesoftware.openfire.spi.ConnectionType;

import java.util.Set;

/**
* Coordinates connections (accept, read, termination) on the server.
*
Expand Down Expand Up @@ -113,4 +116,33 @@ public interface ConnectionManager {
* @param port a port number.
*/
void setPort(ConnectionType type, boolean startInSslMode, int port);

/**
* Returns all connection listeners.
*
* @return All connection listeners (never null).
*/
Set<ConnectionListener> getListeners();

/**
* Returns al connection listeners for the provided type.
*
* @param type The connection type for which a listener is to be configured.
* @return The connection listener (never null).
*/
Set<ConnectionListener> getListeners( ConnectionType type );

/**
* Returns a connection listener.
*
* The #startInSslMode parameter is used to distinguish between listeners that expect to receive SSL encrypted data
* immediately, as opposed to connections that initially accept plain text data (the latter are typically subject to
* StartTLS for in-band encryption configuration). When for a particular connection type only one of these options
* is implemented, the parameter value is ignored.
*
* @param type The connection type for which a listener is to be configured.
* @param startInSslMode true when the listener to be configured is in legacy SSL mode, otherwise false.
* @return The connection listener (never null).
*/
ConnectionListener getListener(ConnectionType type, boolean startInSslMode);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2004-2008 Jive Software, 2022 Ignite Realtime Foundation. All rights reserved.
* Copyright (C) 2004-2008 Jive Software, 2022-2023 Ignite Realtime Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,13 +32,13 @@
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.*;
import org.jivesoftware.openfire.ConnectionManager;
import org.jivesoftware.admin.AuthCheckFilter;
import org.jivesoftware.openfire.JMXManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.keystore.CertificateStore;
import org.jivesoftware.openfire.keystore.IdentityStore;
import org.jivesoftware.openfire.spi.ConnectionConfiguration;
import org.jivesoftware.openfire.spi.ConnectionManagerImpl;
import org.jivesoftware.openfire.spi.ConnectionType;
import org.jivesoftware.openfire.spi.EncryptionArtifactFactory;
import org.jivesoftware.util.*;
Expand Down Expand Up @@ -209,7 +209,7 @@ protected void startup() {
Log.warn( "Admin console: Using certificates but they are not valid for the hosted domain" );
}

final ConnectionManagerImpl connectionManager = ( (ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager() );
final ConnectionManager connectionManager = XMPPServer.getInstance().getConnectionManager();
final ConnectionConfiguration configuration = connectionManager.getListener( ConnectionType.WEBADMIN, true ).generateConnectionConfiguration();
final SslContextFactory sslContextFactory = new EncryptionArtifactFactory( configuration ).getSslContextFactory();

Expand Down Expand Up @@ -254,7 +254,7 @@ protected void startup() {

try {
adminServer.start(); // excludes initialised

if(XMPPServer.getInstance().isSetupMode()) {
AuthCheckFilter.loadSetupExcludes();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2008 Jive Software, Ignite Realtime Foundation 2022. All rights reserved.
* Copyright (C) 2005-2008 Jive Software, Ignite Realtime Foundation 2022-2023. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,12 +33,12 @@
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;
import org.jivesoftware.openfire.Connection;
import org.jivesoftware.openfire.ConnectionManager;
import org.jivesoftware.openfire.JMXManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.keystore.CertificateStore;
import org.jivesoftware.openfire.keystore.IdentityStore;
import org.jivesoftware.openfire.spi.ConnectionConfiguration;
import org.jivesoftware.openfire.spi.ConnectionManagerImpl;
import org.jivesoftware.openfire.spi.ConnectionType;
import org.jivesoftware.openfire.spi.EncryptionArtifactFactory;
import org.jivesoftware.openfire.websocket.OpenfireWebSocketServlet;
Expand Down Expand Up @@ -477,7 +477,7 @@ private Connector createSSLConnector( final Server httpBindServer ) {
Log.warn("HTTP binding: Using certificates but they are not valid for the hosted domain");
}

final ConnectionManagerImpl connectionManager = ((ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager());
final ConnectionManager connectionManager = XMPPServer.getInstance().getConnectionManager();
final ConnectionConfiguration configuration = connectionManager.getListener( ConnectionType.BOSH_C2S, true ).generateConnectionConfiguration();
final SslContextFactory sslContextFactory = new EncryptionArtifactFactory(configuration).getSslContextFactory();

Expand Down Expand Up @@ -696,7 +696,7 @@ protected Handler createWebsocketHandler()

// NOTE: enabled by default
private boolean isHttpCompressionEnabled() {
final ConnectionManagerImpl connectionManager = ((ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager());
final ConnectionManager connectionManager = XMPPServer.getInstance().getConnectionManager();
final ConnectionConfiguration configuration = connectionManager.getListener( ConnectionType.BOSH_C2S, true ).generateConnectionConfiguration();
return configuration.getCompressionPolicy() == null || configuration.getCompressionPolicy().equals( Connection.CompressionPolicy.optional );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.dom4j.io.XMPPPacketReader;
import org.jivesoftware.openfire.PacketDeliverer;
import org.jivesoftware.openfire.SessionPacketRouter;
import org.jivesoftware.openfire.StreamID;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.*;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.multiplex.UnknownStanzaException;
import org.jivesoftware.openfire.net.MXParser;
import org.jivesoftware.openfire.net.SASLAuthentication;
import org.jivesoftware.openfire.net.VirtualConnection;
import org.jivesoftware.openfire.session.LocalClientSession;
import org.jivesoftware.openfire.spi.ConnectionConfiguration;
import org.jivesoftware.openfire.spi.ConnectionManagerImpl;
import org.jivesoftware.openfire.spi.ConnectionType;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.SystemProperty;
Expand Down Expand Up @@ -1312,7 +1308,7 @@ public Optional<String> getCipherSuiteName() {
@Override
public ConnectionConfiguration getConfiguration() {
if (configuration == null) {
final ConnectionManagerImpl connectionManager = ((ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager());
final ConnectionManager connectionManager = XMPPServer.getInstance().getConnectionManager();
configuration = connectionManager.getListener( connectionType, true ).generateConnectionConfiguration();
}
return configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022 Ignite Realtime Foundation. All rights reserved.
* Copyright (C) 2022-2023 Ignite Realtime Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,10 +16,10 @@

package org.jivesoftware.openfire.keystore;

import org.jivesoftware.openfire.ConnectionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.BasicModule;
import org.jivesoftware.openfire.spi.ConnectionListener;
import org.jivesoftware.openfire.spi.ConnectionManagerImpl;
import org.jivesoftware.openfire.spi.ConnectionType;
import org.jivesoftware.util.CollectionUtils;
import org.jivesoftware.util.JiveGlobals;
Expand Down Expand Up @@ -187,7 +187,7 @@ public void replaceIdentityStore( ConnectionType type, CertificateStoreConfigura
}

// Update all connection listeners that were using the old configuration.
final ConnectionManagerImpl connectionManager = ((ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager());
final ConnectionManager connectionManager = XMPPServer.getInstance().getConnectionManager();
for ( ConnectionListener connectionListener : connectionManager.getListeners( type ) ) {
try {
connectionListener.setIdentityStoreConfiguration( configuration );
Expand Down Expand Up @@ -242,7 +242,7 @@ public void replaceTrustStore( ConnectionType type, CertificateStoreConfiguratio
}

// Update all connection listeners that were using the old configuration.
final ConnectionManagerImpl connectionManager = ((ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager());
final ConnectionManager connectionManager = XMPPServer.getInstance().getConnectionManager();
for ( ConnectionListener connectionListener : connectionManager.getListeners( type ) ) {
try {
connectionListener.setTrustStoreConfiguration( configuration );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2008 Jive Software, 2022 Ignite Realtime Foundation. All rights reserved.
* Copyright (C) 2005-2008 Jive Software, 2022-2023 Ignite Realtime Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,12 +17,12 @@
package org.jivesoftware.openfire.multiplex;

import org.dom4j.Element;
import org.jivesoftware.openfire.ConnectionManager;
import org.jivesoftware.openfire.StreamID;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.net.VirtualConnection;
import org.jivesoftware.openfire.session.ConnectionMultiplexerSession;
import org.jivesoftware.openfire.spi.ConnectionConfiguration;
import org.jivesoftware.openfire.spi.ConnectionManagerImpl;
import org.jivesoftware.openfire.spi.ConnectionType;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Packet;
Expand Down Expand Up @@ -131,7 +131,7 @@ public ConnectionConfiguration getConfiguration()
{
// Here, a client-to-server configuration is mocked. It is likely not used, as actual connection handling takes
// place at the connection manager.
final ConnectionManagerImpl connectionManager = ((ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager());
final ConnectionManager connectionManager = XMPPServer.getInstance().getConnectionManager();
return connectionManager.getListener( ConnectionType.SOCKET_C2S, true ).generateConnectionConfiguration();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2008 Jive Software, 2022 Ignite Realtime Foundation. All rights reserved.
* Copyright (C) 2005-2008 Jive Software, 2022-2023 Ignite Realtime Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,6 @@
import org.jivesoftware.openfire.session.LocalSession;
import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.openfire.spi.ConnectionConfiguration;
import org.jivesoftware.openfire.spi.ConnectionManagerImpl;
import org.jivesoftware.openfire.spi.ConnectionType;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.LocaleUtils;
Expand Down Expand Up @@ -227,7 +226,7 @@ public ConnectionConfiguration getConfiguration()
// This is an ugly hack to get backwards compatibility with the pre-MINA era. As this implementation is being
// removed (it is marked as deprecated - at the time of writing, it is only used for S2S). The ugly hack: assume
// S2S:
final ConnectionManagerImpl connectionManager = ((ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager());
final ConnectionManager connectionManager = XMPPServer.getInstance().getConnectionManager();
return connectionManager.getListener( ConnectionType.SOCKET_S2S, false ).generateConnectionConfiguration();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,17 @@

package org.jivesoftware.openfire.server;

import java.io.*;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.XMPPPacketReader;
import org.jivesoftware.openfire.*;
import org.jivesoftware.openfire.auth.AuthFactory;
import org.jivesoftware.openfire.event.ServerSessionEventDispatcher;
import org.jivesoftware.openfire.net.*;
import org.jivesoftware.openfire.session.*;
import org.jivesoftware.openfire.spi.BasicStreamIDFactory;
import org.jivesoftware.openfire.spi.ConnectionManagerImpl;
import org.jivesoftware.openfire.spi.ConnectionType;
import org.jivesoftware.openfire.event.ServerSessionEventDispatcher;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.StringUtils;
import org.jivesoftware.util.cache.Cache;
Expand All @@ -51,6 +42,13 @@

import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.*;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
* Implementation of the Server Dialback method as defined by the RFC3920.
Expand Down Expand Up @@ -658,7 +656,7 @@ private VerifyResult sendVerifyKey(String key, StreamID streamID, String recipie

VerifyResult result = VerifyResult.error;

final ConnectionManagerImpl connectionManager = ( (ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager() );
final ConnectionManager connectionManager = XMPPServer.getInstance().getConnectionManager();
final TLSStreamHandler tlsStreamHandler = new TLSStreamHandler( socket, connectionManager.getListener( ConnectionType.SOCKET_S2S, directTLS ).generateConnectionConfiguration(), true );

if ( directTLS ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@

import org.dom4j.Element;
import org.dom4j.io.XMPPPacketReader;
import org.jivesoftware.openfire.Connection;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.StreamID;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.*;
import org.jivesoftware.openfire.auth.AuthFactory;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.multiplex.ConnectionMultiplexerManager;
import org.jivesoftware.openfire.multiplex.MultiplexerPacketDeliverer;
import org.jivesoftware.openfire.net.SASLAuthentication;
import org.jivesoftware.openfire.spi.ConnectionConfiguration;
import org.jivesoftware.openfire.spi.ConnectionManagerImpl;
import org.jivesoftware.openfire.spi.ConnectionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -261,7 +257,7 @@ public boolean authenticate(String digest) {
*/
private void sendClientOptions() {

final ConnectionManagerImpl connectionManager = ((ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager());
final ConnectionManager connectionManager = XMPPServer.getInstance().getConnectionManager();
final ConnectionConfiguration configuration = connectionManager.getListener( ConnectionType.SOCKET_C2S, false ).generateConnectionConfiguration();

IQ options = new IQ(IQ.Type.set);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Ignite Realtime Foundation. All rights reserved.
* Copyright 2019-2023 Ignite Realtime Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.jivesoftware.openfire.Connection;
import org.jivesoftware.openfire.ConnectionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.keystore.CertificateStoreConfiguration;
import org.jivesoftware.openfire.net.SocketConnection;
Expand Down Expand Up @@ -95,7 +96,7 @@ public class ConnectionListener


ConnectionListener getConnectionListener( ConnectionType type ) {
ConnectionManagerImpl connectionManager = ((ConnectionManagerImpl) XMPPServer.getInstance().getConnectionManager());
ConnectionManager connectionManager = XMPPServer.getInstance().getConnectionManager();
return connectionManager.getListener( type, getTLSPolicy().equals( Connection.TLSPolicy.legacyMode ) );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2008 Jive Software, 2022 Ignite Realtime Foundation. All rights reserved.
* Copyright (C) 2005-2008 Jive Software, 2022-2023 Ignite Realtime Foundation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -409,6 +409,7 @@ public InetAddress getAdminConsoleListenAddress() throws UnknownHostException
*
* @return All connection listeners (never null).
*/
@Override
public Set<ConnectionListener> getListeners() {
final Set<ConnectionListener> listeners = new LinkedHashSet<>();
listeners.add( clientListener );
Expand Down Expand Up @@ -438,6 +439,7 @@ public Set<ConnectionListener> getListeners() {
* @param startInSslMode true when the listener to be configured is in legacy SSL mode, otherwise false.
* @return The connection listener (never null).
*/
@Override
public ConnectionListener getListener( ConnectionType type, boolean startInSslMode )
{
switch ( type )
Expand Down Expand Up @@ -493,6 +495,7 @@ public ConnectionListener getListener( ConnectionType type, boolean startInSslMo
* @param type The connection type for which a listener is to be configured.
* @return The connection listener (never null).
*/
@Override
public Set<ConnectionListener> getListeners( ConnectionType type )
{
final Set<ConnectionListener> result = new HashSet<>();
Expand Down
Loading

0 comments on commit aa6a417

Please sign in to comment.