Skip to content

Commit

Permalink
Issue #6106 - always decorate javax.websocket Configurators
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Apr 6, 2021
1 parent a1e5227 commit f858aa6
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.MultiException;
import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.websocket.core.Behavior;
Expand Down Expand Up @@ -291,7 +292,12 @@ public void requestComplete()
headers(headers -> headers.add(HttpHeader.SEC_WEBSOCKET_EXTENSIONS, extensionString));

// Notify the listener which may change the headers directly.
notifyUpgradeListeners((listener) -> listener.onHandshakeRequest(this));
Exception listenerError = notifyUpgradeListeners((listener) -> listener.onHandshakeRequest(this));
if (listenerError != null)
{
abort(listenerError);
return;
}

// Check if extensions were set in the headers from the upgrade listener.
String extsAfterListener = String.join(",", getHeaders().getCSV(HttpHeader.SEC_WEBSOCKET_EXTENSIONS, true));
Expand All @@ -306,8 +312,9 @@ public void requestComplete()
}
}

private void notifyUpgradeListeners(Consumer<UpgradeListener> action)
private Exception notifyUpgradeListeners(Consumer<UpgradeListener> action)
{
MultiException multiException = null;
for (UpgradeListener listener : upgradeListeners)
{
try
Expand All @@ -317,8 +324,13 @@ private void notifyUpgradeListeners(Consumer<UpgradeListener> action)
catch (Throwable t)
{
LOG.info("Exception while invoking listener {}", listener, t);
if (multiException == null)
multiException = new MultiException();
multiException.add(t);
}
}

return multiException;
}

public void upgrade(HttpResponse response, EndPoint endPoint)
Expand Down Expand Up @@ -437,7 +449,9 @@ else if (values.length == 1)
WebSocketConnection wsConnection = new WebSocketConnection(endPoint, httpClient.getExecutor(), httpClient.getScheduler(), bufferPool, coreSession);
wsClient.getEventListeners().forEach(wsConnection::addEventListener);
coreSession.setWebSocketConnection(wsConnection);
notifyUpgradeListeners((listener) -> listener.onHandshakeResponse(this, response));
Exception listenerError = notifyUpgradeListeners((listener) -> listener.onHandshakeResponse(this, response));
if (listenerError != null)
throw new WebSocketException("onHandshakeResponse error", listenerError);

// Now swap out the connection
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
import javax.websocket.ClientEndpoint;
import javax.websocket.ClientEndpointConfig;

import org.eclipse.jetty.websocket.core.WebSocketComponents;
import org.eclipse.jetty.websocket.core.exception.InvalidWebSocketException;
import org.eclipse.jetty.websocket.javax.common.ClientEndpointConfigWrapper;

public class AnnotatedClientEndpointConfig extends ClientEndpointConfigWrapper
{
public AnnotatedClientEndpointConfig(ClientEndpoint anno)
public AnnotatedClientEndpointConfig(ClientEndpoint anno, WebSocketComponents components)
{
Configurator configurator;
try
{
configurator = anno.configurator().getDeclaredConstructor().newInstance();
configurator = components.getObjectFactory().createInstance(anno.configurator());
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,16 @@ public Session connectToServer(final Class<?> annotatedEndpointClass, final URI
@Override
public Session connectToServer(final Endpoint endpoint, final ClientEndpointConfig providedConfig, final URI path) throws DeploymentException, IOException
{
ClientEndpointConfig config = providedConfig;
if (config == null)
ClientEndpointConfig config;
if (providedConfig == null)
{
config = new BasicClientEndpointConfig();
}
else
{
config = providedConfig;
components.getObjectFactory().decorate(providedConfig.getConfigurator());
}

ConfiguredEndpoint instance = new ConfiguredEndpoint(endpoint, config);
return connect(instance, path);
Expand All @@ -240,6 +247,7 @@ public Session connectToServer(final Endpoint endpoint, final ClientEndpointConf
@Override
public Session connectToServer(Object endpoint, URI path) throws DeploymentException, IOException
{
// The Configurator will be decorated when it is created in the getAnnotatedConfig method.
ClientEndpointConfig config = getAnnotatedConfig(endpoint);
ConfiguredEndpoint instance = new ConfiguredEndpoint(endpoint, config);
return connect(instance, path);
Expand Down Expand Up @@ -275,7 +283,7 @@ private ClientEndpointConfig getAnnotatedConfig(Object endpoint) throws Deployme
if (anno == null)
throw new DeploymentException("Could not get ClientEndpoint annotation for " + endpoint.getClass().getName());

return new AnnotatedClientEndpointConfig(anno);
return new AnnotatedClientEndpointConfig(anno, components);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ public AnnotatedServerEndpointConfig(JavaxWebSocketContainer containerScope, Cla
else
path = anno.value();

// Make sure all Configurators obtained are decorated.
ServerEndpointConfig.Configurator rawConfigurator = getConfigurator(baseServerConfig, anno);
ServerEndpointConfig.Configurator configurator = containerScope.getObjectFactory().decorate(rawConfigurator);
ServerEndpointConfig.Configurator configurator = getConfigurator(baseServerConfig, anno, containerScope);

// Build a ServerEndpointConfig with the Javax API builder to wrap.
ServerEndpointConfig endpointConfig = ServerEndpointConfig.Builder.create(endpointClass, path)
Expand All @@ -90,7 +88,7 @@ public AnnotatedServerEndpointConfig(JavaxWebSocketContainer containerScope, Cla
init(endpointConfig);
}

private static Configurator getConfigurator(ServerEndpointConfig baseServerConfig, ServerEndpoint anno) throws DeploymentException
private static Configurator getConfigurator(ServerEndpointConfig baseServerConfig, ServerEndpoint anno, JavaxWebSocketContainer containerScope) throws DeploymentException
{
Configurator ret = null;

Expand All @@ -115,7 +113,7 @@ private static Configurator getConfigurator(ServerEndpointConfig baseServerConfi
// Instantiate the provided configurator
try
{
return anno.configurator().getConstructor().newInstance();
return containerScope.getObjectFactory().createInstance(anno.configurator());
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ public void addEndpoint(ServerEndpointConfig providedConfig) throws DeploymentEx

if (isStarted() || isStarting())
{
// Decorate the provided Configurator.
components.getObjectFactory().decorate(providedConfig.getConfigurator());

// If we have annotations merge the annotated ServerEndpointConfig with the provided one.
Class<?> endpointClass = providedConfig.getEndpointClass();
ServerEndpoint anno = endpointClass.getAnnotation(ServerEndpoint.class);
Expand Down
Loading

0 comments on commit f858aa6

Please sign in to comment.