From d3421070cb9d594656aa15281dff45411a167252 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 9 Sep 2013 18:51:12 -0700 Subject: [PATCH] Adding back ENABLE attribute for JSR-356 --- .../jsr356/server/WebSocketConfiguration.java | 65 +++++++++++++++---- .../ServerApplicationConfigListener.java | 5 ++ 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/WebSocketConfiguration.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/WebSocketConfiguration.java index 2040ca95644..880bcec1d64 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/WebSocketConfiguration.java +++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/WebSocketConfiguration.java @@ -30,50 +30,87 @@ import org.eclipse.jetty.websocket.jsr356.server.deploy.ServerEndpointAnnotation import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter; /** - * WebSocket Server Configuration component + * WebSocket Server Configuration component. This configuration will configure a context for JSR356 Websockets if the attribute + * "org.eclipse.jetty.websocket.jsr356" is set to true. This attribute may be set on an individual context or on the server to affect all deployed contexts. */ public class WebSocketConfiguration extends AbstractConfiguration { + public static final String ENABLE = "org.eclipse.jetty.websocket.jsr356"; private static final Logger LOG = Log.getLogger(WebSocketConfiguration.class); - + + /** + * Create a ServerContainer properly, useful for embedded application use. + *

+ * Notably, the cometd3 project uses this. + * + * @param context + * the context to enable javax.websocket support filters on + * @return the ServerContainer that was created + */ public static ServerContainer configureContext(ServletContextHandler context) { + LOG.debug("Configure javax.websocket for WebApp {}",context); WebSocketUpgradeFilter filter = WebSocketUpgradeFilter.configureContext(context); - + // Create the Jetty ServerContainer implementation ServerContainer jettyContainer = new ServerContainer(filter,filter.getFactory()); context.addBean(jettyContainer); // Store a reference to the ServerContainer per javax.websocket spec 1.0 final section 6.4 Programmatic Server Deployment context.setAttribute(javax.websocket.server.ServerContainer.class.getName(),jettyContainer); - + // Store reference to DiscoveredEndpoints context.setAttribute(DiscoveredEndpoints.class.getName(),new DiscoveredEndpoints()); return jettyContainer; } + public static boolean isJSR356Context(WebAppContext context) + { + Object enable = context.getAttribute(ENABLE); + if (enable instanceof Boolean) + { + } + + enable = context.getServer().getAttribute(ENABLE); + if (enable instanceof Boolean) + { + return ((Boolean)enable).booleanValue(); + } + + return true; + } + @Override public void configure(WebAppContext context) throws Exception { - LOG.debug("Configure javax.websocket for WebApp {}",context); - WebSocketConfiguration.configureContext(context); + if (isJSR356Context(context)) + { + WebSocketConfiguration.configureContext(context); + } + else + { + LOG.debug("JSR-356 support disabled for WebApp {}",context); + } } @Override public void preConfigure(WebAppContext context) throws Exception { - boolean scanningAdded = false; - // Add the annotation scanning handlers (if annotation scanning enabled) - for (Configuration config : context.getConfigurations()) + if (isJSR356Context(context)) { - if (config instanceof AnnotationConfiguration) + boolean scanningAdded = false; + // Add the annotation scanning handlers (if annotation scanning enabled) + for (Configuration config : context.getConfigurations()) { - AnnotationConfiguration annocfg = (AnnotationConfiguration)config; - annocfg.addDiscoverableAnnotationHandler(new ServerEndpointAnnotationHandler(context)); - scanningAdded = true; + if (config instanceof AnnotationConfiguration) + { + AnnotationConfiguration annocfg = (AnnotationConfiguration)config; + annocfg.addDiscoverableAnnotationHandler(new ServerEndpointAnnotationHandler(context)); + scanningAdded = true; + } } + LOG.debug("@ServerEndpoint scanning added: {}",scanningAdded); } - LOG.debug("@ServerEndpoint scanning added: {}", scanningAdded); } } diff --git a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/ServerApplicationConfigListener.java b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/ServerApplicationConfigListener.java index 5b82dff24c3..5eebf2935f9 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/ServerApplicationConfigListener.java +++ b/jetty-websocket/javax-websocket-server-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/server/deploy/ServerApplicationConfigListener.java @@ -35,7 +35,9 @@ import javax.websocket.server.ServerEndpointConfig; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; +import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.jsr356.server.ServerContainer; +import org.eclipse.jetty.websocket.jsr356.server.WebSocketConfiguration; import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter; @HandlesTypes( @@ -47,6 +49,9 @@ public class ServerApplicationConfigListener implements ServletContainerInitiali @Override public void onStartup(Set> c, ServletContext ctx) throws ServletException { + if (!WebSocketConfiguration.isJSR356Context(WebAppContext.getCurrentWebAppContext())) + return; + WebSocketUpgradeFilter filter = (WebSocketUpgradeFilter)ctx.getAttribute(WebSocketUpgradeFilter.class.getName()); if (filter == null) {