Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2015-01-30 22:13:35 +0000
committerJoakim Erdfelt2015-03-12 17:09:52 +0000
commit87246263b8ae726a61cb9cef20f5d10c1c054458 (patch)
treed6a6c8950ca999e7a37470e824d233f9351ab264 /jetty-websocket
parente213c4488ea6ae83d440c5e53920be96187d860f (diff)
downloadorg.eclipse.jetty.project-87246263b8ae726a61cb9cef20f5d10c1c054458.tar.gz
org.eclipse.jetty.project-87246263b8ae726a61cb9cef20f5d10c1c054458.tar.xz
org.eclipse.jetty.project-87246263b8ae726a61cb9cef20f5d10c1c054458.zip
453834 - CDI Support for WebSocket
+ Verifying ServletContextHandler behavior of Decorators + Marking old ServletContextHandler methods as deprecated + Updates to DecoratedObjectFactory + Removing customized weld scopes/context (for now) + Deferring JSR356 ServerContainer endpoint init till Container.doStart() to allow weld to init properly first + Removing JSR356 BasicServerEndpointConfigurator in favor of ContainerDefaultEndpointConfigurator + Only decorating server endpoints if they were created by ContainerDefaultEndpointConfigurator, all others are trusted to be complete and injected already.
Diffstat (limited to 'jetty-websocket')
-rw-r--r--jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java32
-rw-r--r--jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/DecoderFactory.java7
-rw-r--r--jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/EncoderFactory.java7
-rw-r--r--jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JettyClientContainerProvider.java10
-rw-r--r--jetty-websocket/javax-websocket-client-impl/src/test/resources/jetty-logging.properties1
-rw-r--r--jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointConfig.java3
-rw-r--r--jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/BasicServerEndpointConfig.java4
-rw-r--r--jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/BasicServerEndpointConfigurator.java57
-rw-r--r--jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ContainerDefaultConfigurator.java2
-rw-r--r--jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/JsrCreator.java9
-rw-r--r--jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ServerContainer.java70
-rw-r--r--jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/WebSocketServerContainerInitializer.java2
-rw-r--r--jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java2
-rw-r--r--jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java5
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/DecoratorsLegacyTest.java179
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/DecoratorsTest.java178
-rw-r--r--jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/SimpleServletServer.java5
-rw-r--r--jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/WebSocketServlet.java7
18 files changed, 481 insertions, 99 deletions
diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java
index eff027094b..84e668ace2 100644
--- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java
+++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/ClientContainer.java
@@ -77,6 +77,8 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
{
private static final Logger LOG = Log.getLogger(ClientContainer.class);
+ /** The delegated Container Scope */
+ private final WebSocketContainerScope scopeDelegate;
/** Tracking all primitive decoders for the container */
private final DecoderFactory decoderFactory;
/** Tracking all primitive encoders for the container */
@@ -89,8 +91,6 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
/** The jetty websocket client in use for this container */
private WebSocketClient client;
- protected DecoratedObjectFactory objectFactory;
-
public ClientContainer()
{
// This constructor is used with Standalone JSR Client usage.
@@ -102,6 +102,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
{
boolean trustAll = Boolean.getBoolean("org.eclipse.jetty.websocket.jsr356.ssl-trust-all");
+ this.scopeDelegate = scope;
client = new WebSocketClient(scope, new SslContextFactory(trustAll));
client.setEventDriverFactory(new JsrEventDriverFactory(client.getPolicy()));
SessionFactory sessionFactory = new JsrSessionFactory(this,this,client);
@@ -112,10 +113,6 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
this.decoderFactory = new DecoderFactory(this,PrimitiveDecoderMetadataSet.INSTANCE);
this.encoderFactory = new EncoderFactory(this,PrimitiveEncoderMetadataSet.INSTANCE);
- EmptyClientEndpointConfig empty = new EmptyClientEndpointConfig();
- this.decoderFactory.init(empty);
- this.encoderFactory.init(empty);
-
ShutdownThread.register(this);
}
@@ -196,7 +193,18 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
EndpointInstance instance = newClientEndpointInstance(endpoint,null);
return connect(instance,path);
}
-
+
+ @Override
+ protected void doStart() throws Exception
+ {
+ super.doStart();
+
+ // Initialize the default decoder / encoder factories
+ EmptyClientEndpointConfig empty = new EmptyClientEndpointConfig();
+ this.decoderFactory.init(empty);
+ this.encoderFactory.init(empty);
+ }
+
@Override
protected void doStop() throws Exception
{
@@ -208,7 +216,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
@Override
public ByteBufferPool getBufferPool()
{
- return client.getBufferPool();
+ return scopeDelegate.getBufferPool();
}
public WebSocketClient getClient()
@@ -297,7 +305,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
@Override
public Executor getExecutor()
{
- return client.getExecutor();
+ return scopeDelegate.getExecutor();
}
@Override
@@ -317,7 +325,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
@Override
public DecoratedObjectFactory getObjectFactory()
{
- return client.getObjectFactory();
+ return scopeDelegate.getObjectFactory();
}
/**
@@ -331,13 +339,13 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
@Override
public WebSocketPolicy getPolicy()
{
- return client.getPolicy();
+ return scopeDelegate.getPolicy();
}
@Override
public SslContextFactory getSslContextFactory()
{
- return client.getSslContextFactory();
+ return scopeDelegate.getSslContextFactory();
}
private EndpointInstance newClientEndpointInstance(Class<?> endpointClass, ClientEndpointConfig config)
diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/DecoderFactory.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/DecoderFactory.java
index 1459eda4dc..71a3af770e 100644
--- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/DecoderFactory.java
+++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/DecoderFactory.java
@@ -25,7 +25,6 @@ import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
-import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
@@ -76,7 +75,7 @@ public class DecoderFactory implements Configurable
private static final Logger LOG = Log.getLogger(DecoderFactory.class);
private final DecoderMetadataSet metadatas;
- private final DecoratedObjectFactory objectFactory;
+ private final WebSocketContainerScope containerScope;
private DecoderFactory parentFactory;
private Map<Class<?>, Wrapper> activeWrappers;
@@ -93,7 +92,7 @@ public class DecoderFactory implements Configurable
protected DecoderFactory(WebSocketContainerScope containerScope, DecoderMetadataSet metadatas, DecoderFactory parentFactory)
{
Objects.requireNonNull(containerScope,"Container Scope cannot be null");
- this.objectFactory = containerScope.getObjectFactory();
+ this.containerScope = containerScope;
this.metadatas = metadatas;
this.activeWrappers = new ConcurrentHashMap<>();
this.parentFactory = parentFactory;
@@ -186,7 +185,7 @@ public class DecoderFactory implements Configurable
Class<? extends Decoder> decoderClass = metadata.getCoderClass();
try
{
- Decoder decoder = objectFactory.createInstance(decoderClass);
+ Decoder decoder = containerScope.getObjectFactory().createInstance(decoderClass);
return new Wrapper(decoder,metadata);
}
catch (InstantiationException | IllegalAccessException e)
diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/EncoderFactory.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/EncoderFactory.java
index 4e18662b2b..d5165d45a1 100644
--- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/EncoderFactory.java
+++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/EncoderFactory.java
@@ -25,7 +25,6 @@ import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;
-import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
@@ -69,7 +68,7 @@ public class EncoderFactory implements Configurable
private static final Logger LOG = Log.getLogger(EncoderFactory.class);
private final EncoderMetadataSet metadatas;
- private final DecoratedObjectFactory objectFactory;
+ private final WebSocketContainerScope containerScope;
private EncoderFactory parentFactory;
private Map<Class<?>, Wrapper> activeWrappers;
@@ -86,7 +85,7 @@ public class EncoderFactory implements Configurable
protected EncoderFactory(WebSocketContainerScope containerScope, EncoderMetadataSet metadatas, EncoderFactory parentFactory)
{
Objects.requireNonNull(containerScope,"Container Scope cannot be null");
- this.objectFactory = containerScope.getObjectFactory();
+ this.containerScope = containerScope;
this.metadatas = metadatas;
this.activeWrappers = new ConcurrentHashMap<>();
this.parentFactory = parentFactory;
@@ -179,7 +178,7 @@ public class EncoderFactory implements Configurable
Class<? extends Encoder> encoderClass = metadata.getCoderClass();
try
{
- Encoder encoder = objectFactory.createInstance(encoderClass);
+ Encoder encoder = containerScope.getObjectFactory().createInstance(encoderClass);
return new Wrapper(encoder,metadata);
}
catch (InstantiationException | IllegalAccessException e)
diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JettyClientContainerProvider.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JettyClientContainerProvider.java
index eb043bcc46..3fafe70a3c 100644
--- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JettyClientContainerProvider.java
+++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JettyClientContainerProvider.java
@@ -22,16 +22,24 @@ import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
/**
- * Client {@link ContainerProvider} implementation
+ * Client {@link ContainerProvider} implementation.
+ * <p>
+ * Created by a {@link java.util.ServiceLoader} call in the
+ * {@link javax.websocket.ContainerProvider#getWebSocketContainer()} call.
*/
public class JettyClientContainerProvider extends ContainerProvider
{
+ /**
+ * Used by {@link ContainerProvider#getWebSocketContainer()} to get a new instance
+ * of the Client {@link WebSocketContainer}.
+ */
@Override
protected WebSocketContainer getContainer()
{
ClientContainer container = new ClientContainer();
try
{
+ // We need to start this container properly.
container.start();
return container;
}
diff --git a/jetty-websocket/javax-websocket-client-impl/src/test/resources/jetty-logging.properties b/jetty-websocket/javax-websocket-client-impl/src/test/resources/jetty-logging.properties
index d17e9c443a..3acadb6cad 100644
--- a/jetty-websocket/javax-websocket-client-impl/src/test/resources/jetty-logging.properties
+++ b/jetty-websocket/javax-websocket-client-impl/src/test/resources/jetty-logging.properties
@@ -1,5 +1,6 @@
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
org.eclipse.jetty.LEVEL=WARN
+# org.eclipse.jetty.websocket.LEVEL=INFO
# org.eclipse.jetty.websocket.LEVEL=ALL
# org.eclipse.jetty.websocket.jsr356.LEVEL=DEBUG
diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointConfig.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointConfig.java
index f6ae65b3e3..3dc14762dc 100644
--- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointConfig.java
+++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointConfig.java
@@ -122,7 +122,7 @@ public class AnnotatedServerEndpointConfig implements ServerEndpointConfig
}
else
{
- cfgr = new BasicServerEndpointConfigurator(containerScope);
+ cfgr = new ContainerDefaultConfigurator();
}
}
else
@@ -144,7 +144,6 @@ public class AnnotatedServerEndpointConfig implements ServerEndpointConfig
// Make sure all Configurators obtained are decorated
this.configurator = containerScope.getObjectFactory().decorate(cfgr);
-
}
@Override
diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/BasicServerEndpointConfig.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/BasicServerEndpointConfig.java
index 28f16ce107..1156917350 100644
--- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/BasicServerEndpointConfig.java
+++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/BasicServerEndpointConfig.java
@@ -51,7 +51,7 @@ public class BasicServerEndpointConfig implements ServerEndpointConfig
this.subprotocols = new ArrayList<>();
this.extensions = new ArrayList<>();
this.userProperties = new HashMap<>();
- this.configurator = new BasicServerEndpointConfigurator(containerScope);
+ this.configurator = new ContainerDefaultConfigurator();
}
public BasicServerEndpointConfig(WebSocketContainerScope containerScope, ServerEndpointConfig copy)
@@ -73,7 +73,7 @@ public class BasicServerEndpointConfig implements ServerEndpointConfig
}
else
{
- cfgr = new BasicServerEndpointConfigurator(containerScope);
+ cfgr = new ContainerDefaultConfigurator();
}
// Make sure all Configurators obtained are decorated
diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/BasicServerEndpointConfigurator.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/BasicServerEndpointConfigurator.java
deleted file mode 100644
index 71000af8fd..0000000000
--- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/BasicServerEndpointConfigurator.java
+++ /dev/null
@@ -1,57 +0,0 @@
-//
-// ========================================================================
-// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
-// ------------------------------------------------------------------------
-// All rights reserved. This program and the accompanying materials
-// are made available under the terms of the Eclipse Public License v1.0
-// and Apache License v2.0 which accompanies this distribution.
-//
-// The Eclipse Public License is available at
-// http://www.eclipse.org/legal/epl-v10.html
-//
-// The Apache License v2.0 is available at
-// http://www.opensource.org/licenses/apache2.0.php
-//
-// You may elect to redistribute this code under either of these licenses.
-// ========================================================================
-//
-
-package org.eclipse.jetty.websocket.jsr356.server;
-
-import org.eclipse.jetty.util.DecoratedObjectFactory;
-import org.eclipse.jetty.util.log.Log;
-import org.eclipse.jetty.util.log.Logger;
-import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
-
-/**
- * The Basic Configurator
- */
-public class BasicServerEndpointConfigurator extends ContainerDefaultConfigurator
-{
- private static final Logger LOG = Log.getLogger(BasicServerEndpointConfigurator.class);
- private final DecoratedObjectFactory objectFactory;
-
- public BasicServerEndpointConfigurator(WebSocketContainerScope containerScope)
- {
- super();
- this.objectFactory = containerScope.getObjectFactory();
- }
-
- @Override
- public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException
- {
- if (LOG.isDebugEnabled())
- {
- LOG.debug(".getEndpointInstance({})",endpointClass);
- }
-
- try
- {
- return objectFactory.createInstance(endpointClass);
- }
- catch (IllegalAccessException e)
- {
- throw new InstantiationException(String.format("%s: %s",e.getClass().getName(),e.getMessage()));
- }
- }
-} \ No newline at end of file
diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ContainerDefaultConfigurator.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ContainerDefaultConfigurator.java
index 07d8a26771..4d0168811f 100644
--- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ContainerDefaultConfigurator.java
+++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ContainerDefaultConfigurator.java
@@ -35,7 +35,7 @@ import org.eclipse.jetty.websocket.api.util.QuoteUtil;
*
* @see ServiceLoader behavior of {@link ServerEndpointConfig.Configurator}
*/
-public class ContainerDefaultConfigurator extends Configurator
+public final class ContainerDefaultConfigurator extends Configurator
{
private static final Logger LOG = Log.getLogger(ContainerDefaultConfigurator.class);
private static final String NO_SUBPROTOCOL = "";
diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/JsrCreator.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/JsrCreator.java
index 93d6659771..1d32ed3f31 100644
--- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/JsrCreator.java
+++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/JsrCreator.java
@@ -25,6 +25,7 @@ import java.util.List;
import javax.websocket.Extension;
import javax.websocket.Extension.Parameter;
import javax.websocket.server.ServerEndpointConfig;
+import javax.websocket.server.ServerEndpointConfig.Configurator;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
@@ -137,7 +138,13 @@ public class JsrCreator implements WebSocketCreator
try
{
Class<?> endpointClass = config.getEndpointClass();
- Object endpoint = config.getConfigurator().getEndpointInstance(endpointClass);
+ Configurator configr = config.getConfigurator();
+ Object endpoint = configr.getEndpointInstance(endpointClass);
+ if (configr instanceof ContainerDefaultConfigurator)
+ {
+ // default impl always decorates (custom ones do not)
+ endpoint = containerScope.getObjectFactory().decorate(endpoint);
+ }
PathSpec pathSpec = hsreq.getRequestPathSpec();
if (pathSpec instanceof WebSocketPathSpec)
{
diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ServerContainer.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ServerContainer.java
index 6955bef201..e1932d2a83 100644
--- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ServerContainer.java
+++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/ServerContainer.java
@@ -18,6 +18,8 @@
package org.eclipse.jetty.websocket.jsr356.server;
+import java.util.ArrayList;
+import java.util.List;
import java.util.concurrent.Executor;
import javax.websocket.DeploymentException;
@@ -43,16 +45,19 @@ public class ServerContainer extends ClientContainer implements javax.websocket.
private final MappedWebSocketCreator mappedCreator;
private final WebSocketServerFactory webSocketServerFactory;
+ private List<Class<?>> deferredEndpointClasses;
+ private List<ServerEndpointConfig> deferredEndpointConfigs;
public ServerContainer(MappedWebSocketCreator creator, WebSocketServerFactory factory, Executor executor)
{
- super();
+ super(factory);
this.mappedCreator = creator;
this.webSocketServerFactory = factory;
EventDriverFactory eventDriverFactory = this.webSocketServerFactory.getEventDriverFactory();
eventDriverFactory.addImplementation(new JsrServerEndpointImpl());
eventDriverFactory.addImplementation(new JsrServerExtendsEndpointImpl());
this.webSocketServerFactory.addSessionFactory(new JsrSessionFactory(this,this));
+ addBean(webSocketServerFactory);
}
public EndpointInstance newClientEndpointInstance(Object endpoint, ServerEndpointConfig config, String path)
@@ -76,11 +81,22 @@ public class ServerContainer extends ClientContainer implements javax.websocket.
@Override
public void addEndpoint(Class<?> endpointClass) throws DeploymentException
{
- ServerEndpointMetadata metadata = getServerEndpointMetadata(endpointClass,null);
- addEndpoint(metadata);
+ if (isStarted() || isStarting())
+ {
+ ServerEndpointMetadata metadata = getServerEndpointMetadata(endpointClass,null);
+ addEndpoint(metadata);
+ }
+ else
+ {
+ if (deferredEndpointClasses == null)
+ {
+ deferredEndpointClasses = new ArrayList<Class<?>>();
+ }
+ deferredEndpointClasses.add(endpointClass);
+ }
}
- public void addEndpoint(ServerEndpointMetadata metadata) throws DeploymentException
+ private void addEndpoint(ServerEndpointMetadata metadata) throws DeploymentException
{
JsrCreator creator = new JsrCreator(this,metadata,webSocketServerFactory.getExtensionFactory());
mappedCreator.addMapping(new WebSocketPathSpec(metadata.getPath()),creator);
@@ -89,12 +105,50 @@ public class ServerContainer extends ClientContainer implements javax.websocket.
@Override
public void addEndpoint(ServerEndpointConfig config) throws DeploymentException
{
- if (LOG.isDebugEnabled())
+ if (isStarted() || isStarting())
+ {
+ if (LOG.isDebugEnabled())
+ {
+ LOG.debug("addEndpoint({}) path={} endpoint={}",config,config.getPath(),config.getEndpointClass());
+ }
+ ServerEndpointMetadata metadata = getServerEndpointMetadata(config.getEndpointClass(),config);
+ addEndpoint(metadata);
+ }
+ else
+ {
+ if (deferredEndpointConfigs == null)
+ {
+ deferredEndpointConfigs = new ArrayList<ServerEndpointConfig>();
+ }
+ deferredEndpointConfigs.add(config);
+ }
+ }
+
+ @Override
+ protected void doStart() throws Exception
+ {
+ // Proceed with Normal Startup
+ super.doStart();
+
+ // Process Deferred Endpoints
+ if (deferredEndpointClasses != null)
{
- LOG.debug("addEndpoint({}) path={} endpoint={}",config,config.getPath(),config.getEndpointClass());
+ for (Class<?> endpointClass : deferredEndpointClasses)
+ {
+ addEndpoint(endpointClass);
+ }
+ }
+
+ if (deferredEndpointConfigs != null)
+ {
+ for (ServerEndpointConfig config : deferredEndpointConfigs)
+ {
+ addEndpoint(config);
+ }
}
- ServerEndpointMetadata metadata = getServerEndpointMetadata(config.getEndpointClass(),config);
- addEndpoint(metadata);
+
+ deferredEndpointClasses.clear();
+ deferredEndpointConfigs.clear();
}
public ServerEndpointMetadata getServerEndpointMetadata(final Class<?> endpoint, final ServerEndpointConfig config) throws DeploymentException
diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/WebSocketServerContainerInitializer.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/WebSocketServerContainerInitializer.java
index b389ab0f27..76bd63d1bb 100644
--- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/WebSocketServerContainerInitializer.java
+++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/WebSocketServerContainerInitializer.java
@@ -58,7 +58,6 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit
WebSocketUpgradeFilter filter = WebSocketUpgradeFilter.configureContext(context);
// Create the Jetty ServerContainer implementation
- filter.getFactory().init(context);
ServerContainer jettyContainer = new ServerContainer(filter,filter.getFactory(),context.getServer().getThreadPool());
context.addBean(jettyContainer);
@@ -79,7 +78,6 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit
WebSocketUpgradeFilter filter = WebSocketUpgradeFilter.configureContext(context);
// Create the Jetty ServerContainer implementation
- filter.getFactory().init(context);
ServerContainer jettyContainer = new ServerContainer(filter,filter.getFactory(),jettyContext.getServer().getThreadPool());
jettyContext.addBean(jettyContainer);
diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java
index 85aa4d187c..1ca3805746 100644
--- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java
+++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java
@@ -381,7 +381,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
public void init(ServletContextHandler context) throws ServletException
{
- this.objectFactory = (DecoratedObjectFactory)context.getAttribute(DecoratedObjectFactory.ATTR);
+ this.objectFactory = (DecoratedObjectFactory)context.getServletContext().getAttribute(DecoratedObjectFactory.ATTR);
if (this.objectFactory == null)
{
this.objectFactory = new DecoratedObjectFactory();
diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java
index f41e06260e..9b9b113eba 100644
--- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java
+++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java
@@ -257,7 +257,8 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter
try
{
- factory.init(config.getServletContext());
+ ServletContext ctx = config.getServletContext();
+ factory.init(ctx);
WebSocketPolicy policy = factory.getPolicy();
String max = config.getInitParameter("maxIdleTime");
@@ -291,7 +292,7 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter
key = WebSocketUpgradeFilter.class.getName();
}
- setToAttribute(config.getServletContext(), key);
+ setToAttribute(ctx, key);
factory.start();
}
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/DecoratorsLegacyTest.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/DecoratorsLegacyTest.java
new file mode 100644
index 0000000000..baf9388c68
--- /dev/null
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/DecoratorsLegacyTest.java
@@ -0,0 +1,179 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.websocket.server;
+
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import javax.servlet.ServletContext;
+
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.toolchain.test.EventQueue;
+import org.eclipse.jetty.util.DecoratedObjectFactory;
+import org.eclipse.jetty.util.Decorator;
+import org.eclipse.jetty.websocket.api.WebSocketAdapter;
+import org.eclipse.jetty.websocket.common.WebSocketFrame;
+import org.eclipse.jetty.websocket.common.frames.TextFrame;
+import org.eclipse.jetty.websocket.common.test.BlockheadClient;
+import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
+import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
+import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
+import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
+import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class DecoratorsLegacyTest
+{
+ private static class DecoratorsSocket extends WebSocketAdapter
+ {
+ private final DecoratedObjectFactory objFactory;
+
+ public DecoratorsSocket(DecoratedObjectFactory objFactory)
+ {
+ this.objFactory = objFactory;
+ }
+
+ @Override
+ public void onWebSocketText(String message)
+ {
+ StringWriter str = new StringWriter();
+ PrintWriter out = new PrintWriter(str);
+
+ if (objFactory != null)
+ {
+ out.printf("Object is a DecoratedObjectFactory%n");
+ List<Decorator> decorators = objFactory.getDecorators();
+ out.printf("Decorators.size = [%d]%n",decorators.size());
+ for (Decorator decorator : decorators)
+ {
+ out.printf(" decorator[] = %s%n",decorator.getClass().getName());
+ }
+ }
+ else
+ {
+ out.printf("DecoratedObjectFactory is NULL%n");
+ }
+
+ getRemote().sendStringByFuture(str.toString());
+ }
+ }
+
+ private static class DecoratorsCreator implements WebSocketCreator
+ {
+ @Override
+ public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
+ {
+ ServletContext servletContext = req.getHttpServletRequest().getServletContext();
+ DecoratedObjectFactory objFactory = (DecoratedObjectFactory)servletContext.getAttribute(DecoratedObjectFactory.ATTR);
+ return new DecoratorsSocket(objFactory);
+ }
+ }
+
+ public static class DecoratorsRequestServlet extends WebSocketServlet
+ {
+ private static final long serialVersionUID = 1L;
+ private final WebSocketCreator creator;
+
+ public DecoratorsRequestServlet(WebSocketCreator creator)
+ {
+ this.creator = creator;
+ }
+
+ @Override
+ public void configure(WebSocketServletFactory factory)
+ {
+ factory.setCreator(this.creator);
+ }
+ }
+
+ private static class DummyLegacyDecorator implements org.eclipse.jetty.servlet.ServletContextHandler.Decorator
+ {
+ @Override
+ public <T> T decorate(T o)
+ {
+ return o;
+ }
+
+ @Override
+ public void destroy(Object o)
+ {
+ }
+ }
+
+ private static SimpleServletServer server;
+ private static DecoratorsCreator decoratorsCreator;
+
+ @BeforeClass
+ public static void startServer() throws Exception
+ {
+ decoratorsCreator = new DecoratorsCreator();
+ server = new SimpleServletServer(new DecoratorsRequestServlet(decoratorsCreator))
+ {
+ @SuppressWarnings("deprecation")
+ @Override
+ protected void configureServletContextHandler(ServletContextHandler context)
+ {
+ // Add decorator in the legacy way
+ context.addDecorator(new DummyLegacyDecorator());
+ }
+ };
+ server.start();
+ }
+
+ @AfterClass
+ public static void stopServer()
+ {
+ server.stop();
+ }
+
+ @Test
+ public void testAccessRequestCookies() throws Exception
+ {
+ BlockheadClient client = new BlockheadClient(server.getServerUri());
+ client.setTimeout(1,TimeUnit.SECONDS);
+
+ try
+ {
+ client.connect();
+ client.sendStandardRequest();
+ client.expectUpgradeResponse();
+
+ client.write(new TextFrame().setPayload("info"));
+
+ EventQueue<WebSocketFrame> frames = client.readFrames(1,1,TimeUnit.SECONDS);
+ WebSocketFrame resp = frames.poll();
+ String textMsg = resp.getPayloadAsUTF8();
+
+ assertThat("DecoratedObjectFactory", textMsg, containsString("Object is a DecoratedObjectFactory"));
+ assertThat("decorators.size", textMsg, containsString("Decorators.size = [1]"));
+ assertThat("decorator type", textMsg, containsString("decorator[] = " + DummyLegacyDecorator.class.getName()));
+ }
+ finally
+ {
+ client.close();
+ }
+ }
+}
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/DecoratorsTest.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/DecoratorsTest.java
new file mode 100644
index 0000000000..b1944c6590
--- /dev/null
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/DecoratorsTest.java
@@ -0,0 +1,178 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.websocket.server;
+
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import javax.servlet.ServletContext;
+
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.toolchain.test.EventQueue;
+import org.eclipse.jetty.util.DecoratedObjectFactory;
+import org.eclipse.jetty.util.Decorator;
+import org.eclipse.jetty.websocket.api.WebSocketAdapter;
+import org.eclipse.jetty.websocket.common.WebSocketFrame;
+import org.eclipse.jetty.websocket.common.frames.TextFrame;
+import org.eclipse.jetty.websocket.common.test.BlockheadClient;
+import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
+import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
+import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
+import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
+import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class DecoratorsTest
+{
+ private static class DecoratorsSocket extends WebSocketAdapter
+ {
+ private final DecoratedObjectFactory objFactory;
+
+ public DecoratorsSocket(DecoratedObjectFactory objFactory)
+ {
+ this.objFactory = objFactory;
+ }
+
+ @Override
+ public void onWebSocketText(String message)
+ {
+ StringWriter str = new StringWriter();
+ PrintWriter out = new PrintWriter(str);
+
+ if (objFactory != null)
+ {
+ out.printf("Object is a DecoratedObjectFactory%n");
+ List<Decorator> decorators = objFactory.getDecorators();
+ out.printf("Decorators.size = [%d]%n",decorators.size());
+ for (Decorator decorator : decorators)
+ {
+ out.printf(" decorator[] = %s%n",decorator.getClass().getName());
+ }
+ }
+ else
+ {
+ out.printf("DecoratedObjectFactory is NULL%n");
+ }
+
+ getRemote().sendStringByFuture(str.toString());
+ }
+ }
+
+ private static class DecoratorsCreator implements WebSocketCreator
+ {
+ @Override
+ public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
+ {
+ ServletContext servletContext = req.getHttpServletRequest().getServletContext();
+ DecoratedObjectFactory objFactory = (DecoratedObjectFactory)servletContext.getAttribute(DecoratedObjectFactory.ATTR);
+ return new DecoratorsSocket(objFactory);
+ }
+ }
+
+ public static class DecoratorsRequestServlet extends WebSocketServlet
+ {
+ private static final long serialVersionUID = 1L;
+ private final WebSocketCreator creator;
+
+ public DecoratorsRequestServlet(WebSocketCreator creator)
+ {
+ this.creator = creator;
+ }
+
+ @Override
+ public void configure(WebSocketServletFactory factory)
+ {
+ factory.setCreator(this.creator);
+ }
+ }
+
+ private static class DummyUtilDecorator implements org.eclipse.jetty.util.Decorator
+ {
+ @Override
+ public <T> T decorate(T o)
+ {
+ return o;
+ }
+
+ @Override
+ public void destroy(Object o)
+ {
+ }
+ }
+
+ private static SimpleServletServer server;
+ private static DecoratorsCreator decoratorsCreator;
+
+ @BeforeClass
+ public static void startServer() throws Exception
+ {
+ decoratorsCreator = new DecoratorsCreator();
+ server = new SimpleServletServer(new DecoratorsRequestServlet(decoratorsCreator))
+ {
+ @Override
+ protected void configureServletContextHandler(ServletContextHandler context)
+ {
+ // Add decorator in the new util way
+ context.getObjectFactory().addDecorator(new DummyUtilDecorator());
+ }
+ };
+ server.start();
+ }
+
+ @AfterClass
+ public static void stopServer()
+ {
+ server.stop();
+ }
+
+ @Test
+ public void testAccessRequestCookies() throws Exception
+ {
+ BlockheadClient client = new BlockheadClient(server.getServerUri());
+ client.setTimeout(1,TimeUnit.SECONDS);
+
+ try
+ {
+ client.connect();
+ client.sendStandardRequest();
+ client.expectUpgradeResponse();
+
+ client.write(new TextFrame().setPayload("info"));
+
+ EventQueue<WebSocketFrame> frames = client.readFrames(1,1,TimeUnit.SECONDS);
+ WebSocketFrame resp = frames.poll();
+ String textMsg = resp.getPayloadAsUTF8();
+
+ assertThat("DecoratedObjectFactory", textMsg, containsString("Object is a DecoratedObjectFactory"));
+ assertThat("decorators.size", textMsg, containsString("Decorators.size = [1]"));
+ assertThat("decorator type", textMsg, containsString("decorator[] = " + DummyUtilDecorator.class.getName()));
+ }
+ finally
+ {
+ client.close();
+ }
+ }
+}
diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/SimpleServletServer.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/SimpleServletServer.java
index 585a680735..c4dc38dc3e 100644
--- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/SimpleServletServer.java
+++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/SimpleServletServer.java
@@ -114,6 +114,7 @@ public class SimpleServletServer
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
+ configureServletContextHandler(context);
server.setHandler(context);
// Serve capture servlet
@@ -138,6 +139,10 @@ public class SimpleServletServer
}
}
+ protected void configureServletContextHandler(ServletContextHandler context)
+ {
+ }
+
public void stop()
{
try
diff --git a/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/WebSocketServlet.java b/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/WebSocketServlet.java
index 106ff39cd8..3fc29c6d0f 100644
--- a/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/WebSocketServlet.java
+++ b/jetty-websocket/websocket-servlet/src/main/java/org/eclipse/jetty/websocket/servlet/WebSocketServlet.java
@@ -20,6 +20,7 @@ package org.eclipse.jetty.websocket.servlet;
import java.io.IOException;
+import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@@ -128,10 +129,12 @@ public abstract class WebSocketServlet extends HttpServlet
factory = WebSocketServletFactory.Loader.create(policy);
configure(factory);
+
+ ServletContext ctx = getServletContext();
- factory.init(getServletContext());
+ factory.init(ctx);
- getServletContext().setAttribute(WebSocketServletFactory.class.getName(),factory);
+ ctx.setAttribute(WebSocketServletFactory.class.getName(),factory);
}
catch (Exception x)
{

Back to the top