Skip to content

Commit

Permalink
OF-2717: Allow plugins to register/unregister web context
Browse files Browse the repository at this point in the history
The embedded webserver (Jetty) that powers the HTTP-Bind (BOSH) and Websocket endpoints can be used by plugins, for them to add web content of their own.

To do so, a plugin can dynamically add a 'handler' when it gets loaded in Openfire (that handler should be removed again when the plugin gets unloaded).

Due to changes to the Jetty API (which got updated from 10 to 12, using EE8), this mechanism was broken.

Prior to the upgrade, a `org.eclipse.jetty.server.handler.HandlerList` was used to maintain the collection of plugin-provided contexts. After upgrading to Jetty 12, this was replaced with a `org.eclipse.jetty.server.handler.ContextHandlerCollection`. This was part of 99fe5f9#diff-55709739e79c9b3141bb106d4e4cf428d2bc54e46acf59450c3d9e8cf988385b

The methods used by plugins to add and remove 'handlers' were not modified in that change. The 'handler' passed to them remained of type `org.eclipse.jetty.server.Handler`. This compiles fine, but seems to be a problem.

When plugins (which are stand-alone projects with their own source code repositories) get updated to use Jetty 12 instead of 10, their representation of their web-context switches from `org.eclipse.jetty.webapp.WebAppContext` to the Jetty 12 EE8 `org.eclipse.jetty.ee8.webapp.WebAppContext`. The former is compatible with the old implementation in Openfire, but the latter is not.

This commit adds new methods to register and unregister (Jetty 12 EE8) web contexts.

I considered leaving the original methods (possibly marked as being deprecated) in hopes of providing backwards compatibility. However old plugins that are compiled against Jetty 10 won't find the Jetty classes that they need in Openfire anyway (as Jetty is a 'provided' dependency, which now got updated), causing ClassCastExceptions to be thrown. That makes retaining the original add/remove methods (for backwards compatibility) rather pointless.
  • Loading branch information
guusdk committed Nov 15, 2024
1 parent e032592 commit fe6c616
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
import org.apache.tomcat.InstanceManager;
import org.apache.tomcat.SimpleInstanceManager;
import org.eclipse.jetty.ee8.apache.jsp.JettyJasperInitializer;
import org.eclipse.jetty.ee8.nested.ContextHandler;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.ForwardedRequestCustomizer;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.Handler.Sequence;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
Expand Down Expand Up @@ -757,12 +766,12 @@ protected ServletContextHandler createStaticContentHandler()
}

/**
* Adds a Jetty handler to be added to the embedded web server that is used to expose BOSH (HTTP-bind)
* functionality.
* Adds a Jetty handler to be added to the embedded web server that is used to expose Openfire's public
* web-bindings (eg: BOSH / HTTP-bind and websocket).
*
* @param handler The handler (cannot be null).
*/
public void addJettyHandler( Handler handler )
public void addJettyHandler(final ContextHandler handler)
{
if ( handler == null )
{
Expand All @@ -785,17 +794,14 @@ public void addJettyHandler( Handler handler )
}

/**
* Removes a Jetty handler to be added to the embedded web server that is used to expose BOSH (HTTP-bind)
* functionality.
*
* Removing a handler, even when null, or non-existing, might have side-effects as introduced by the Jetty
* implementation. At the time of writing, Jetty will re
* Removes a Jetty handler to be added to the embedded web server that is used to expose Openfire's public
* web-bindings (eg: BOSH / HTTP-bind and websocket).
*
* @param handler The handler (should not be null).
*/
public void removeJettyHandler( Handler handler )
public void removeJettyHandler(final ContextHandler handler)
{
extensionHandlers.removeHandler( handler );
extensionHandlers.removeHandler( handler.get() );
if ( handler.isStarted() )
{
try
Expand All @@ -807,7 +813,6 @@ public void removeJettyHandler( Handler handler )
Log.warn( "Unable to stop the handler that was removed: {}", handler, e );
}
}

}

private void doEnableHttpBind(boolean shouldEnable) {
Expand Down

0 comments on commit fe6c616

Please sign in to comment.