@Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { super.modifyHandshake(this, request, response); }
private void initServerContainer(ServerContainer serverContainer) { if (this.asyncSendTimeout != null) { serverContainer.setAsyncSendTimeout(this.asyncSendTimeout); } if (this.maxSessionIdleTimeout != null) { serverContainer.setDefaultMaxSessionIdleTimeout(this.maxSessionIdleTimeout); } if (this.maxTextMessageBufferSize != null) { serverContainer.setDefaultMaxTextMessageBufferSize(this.maxTextMessageBufferSize); } if (this.maxBinaryMessageBufferSize != null) { serverContainer.setDefaultMaxBinaryMessageBufferSize(this.maxBinaryMessageBufferSize); } }
public class ServletAwareConfig extends ServerEndpointConfig.Configurator { @Override public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) { HttpSession httpSession = (HttpSession) request.getHttpSession(); config.getUserProperties().put("httpSession", httpSession); } }
@ServerEndpoint("/echo") private static class PerConnectionEchoEndpoint { @SuppressWarnings("unused") private final EchoService service; @Autowired public PerConnectionEchoEndpoint(EchoService service) { this.service = service; } }
private void registerEndpoint(ServerEndpointConfig endpointConfig) { ServerContainer serverContainer = getServerContainer(); Assert.state(serverContainer != null, "No ServerContainer set"); try { if (logger.isDebugEnabled()) { logger.debug("Registering ServerEndpointConfig: " + endpointConfig); } serverContainer.addEndpoint(endpointConfig); } catch (DeploymentException ex) { throw new IllegalStateException("Failed to register ServerEndpointConfig: " + endpointConfig, ex); } }
@Test public void endpointPerConnection() throws Exception { @SuppressWarnings("resource") ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class); ServerEndpointRegistration registration = new ServerEndpointRegistration("/path", EchoEndpoint.class); registration.setBeanFactory(context.getBeanFactory()); EchoEndpoint endpoint = registration.getConfigurator().getEndpointInstance(EchoEndpoint.class); assertNotNull(endpoint); }
@Override public List<Extension> getNegotiatedExtensions(List<Extension> installed, List<Extension> requested) { log.trace("negotiatedExtensions: installed={}, requested={}", installed, requested); // TODO: its not common to need to negotiate these outside of the container // but we could eventually expose them in the WebSocketHandshake as in getNegotiatedSubprotocol return super.getNegotiatedExtensions(installed, requested); }
@Override public boolean checkOrigin(String originHeaderValue) { log.trace("checkOrigin: {}", originHeaderValue); return super.checkOrigin(originHeaderValue); }
@Test public void servletServerContainerFactoryBeanSupport() { assertEquals(42, serverContainer.getDefaultMaxTextMessageBufferSize()); }
@ServerEndpoint("/echo") private static class EchoEndpoint { @SuppressWarnings("unused") private final EchoService service; @Autowired public EchoEndpoint(EchoService service) { this.service = service; } }
private void registerEndpoint(Class<?> endpointClass) { ServerContainer serverContainer = getServerContainer(); Assert.state(serverContainer != null, "No ServerContainer set. Most likely the server's own WebSocket ServletContainerInitializer " + "has not run yet. Was the Spring ApplicationContext refreshed through a " + "org.springframework.web.context.ContextLoaderListener, " + "i.e. after the ServletContext has been fully initialized?"); try { if (logger.isDebugEnabled()) { logger.debug("Registering @ServerEndpoint class: " + endpointClass); } serverContainer.addEndpoint(endpointClass); } catch (DeploymentException ex) { throw new IllegalStateException("Failed to register @ServerEndpoint class: " + endpointClass, ex); } }
@Override public void afterPropertiesSet() { Assert.state(this.servletContext != null, "A ServletContext is required to access the javax.websocket.server.ServerContainer instance"); this.serverContainer = (ServerContainer) this.servletContext.getAttribute( "javax.websocket.server.ServerContainer"); Assert.state(this.serverContainer != null, "Attribute 'javax.websocket.server.ServerContainer' not found in ServletContext"); if (this.asyncSendTimeout != null) { this.serverContainer.setAsyncSendTimeout(this.asyncSendTimeout); } if (this.maxSessionIdleTimeout != null) { this.serverContainer.setDefaultMaxSessionIdleTimeout(this.maxSessionIdleTimeout); } if (this.maxTextMessageBufferSize != null) { this.serverContainer.setDefaultMaxTextMessageBufferSize(this.maxTextMessageBufferSize); } if (this.maxBinaryMessageBufferSize != null) { this.serverContainer.setDefaultMaxBinaryMessageBufferSize(this.maxBinaryMessageBufferSize); } }
@Test public void endpointSingleton() throws Exception { EchoEndpoint endpoint = new EchoEndpoint(new EchoService()); ServerEndpointRegistration registration = new ServerEndpointRegistration("/path", endpoint); EchoEndpoint actual = registration.getConfigurator().getEndpointInstance(EchoEndpoint.class); assertSame(endpoint, actual); }
@Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { log.trace("modifyHandshake"); super.modifyHandshake(sec, request, response); }
@ServerEndpoint("/path") private static class AnnotatedDummyEndpoint { }
@Test public void addAnnotatedEndpointClassesWithServletContextOnly() throws Exception { this.exporter.setAnnotatedEndpointClasses(AnnotatedDummyEndpoint.class, AnnotatedDummyEndpointBean.class); this.exporter.setServletContext(this.servletContext); this.exporter.afterPropertiesSet(); this.exporter.afterSingletonsInstantiated(); verify(this.serverContainer).addEndpoint(AnnotatedDummyEndpoint.class); verify(this.serverContainer).addEndpoint(AnnotatedDummyEndpointBean.class); }
@ServerEndpoint("/path") private static class AnnotatedDummyEndpointBean { }
@Test public void addAnnotatedEndpointClassesWithExplicitServerContainerOnly() throws Exception { this.exporter.setAnnotatedEndpointClasses(AnnotatedDummyEndpoint.class, AnnotatedDummyEndpointBean.class); this.exporter.setServerContainer(this.serverContainer); this.exporter.afterPropertiesSet(); this.exporter.afterSingletonsInstantiated(); verify(this.serverContainer).addEndpoint(AnnotatedDummyEndpoint.class); verify(this.serverContainer).addEndpoint(AnnotatedDummyEndpointBean.class); }
@Component("myComponentEchoEndpoint") @ServerEndpoint("/echo") private static class ComponentEchoEndpoint { @SuppressWarnings("unused") private final EchoService service; @Autowired public ComponentEchoEndpoint(EchoService service) { this.service = service; } }
@Test public void addAnnotatedEndpointClasses() throws Exception { this.exporter.setAnnotatedEndpointClasses(AnnotatedDummyEndpoint.class); this.exporter.setApplicationContext(this.webAppContext); this.exporter.afterPropertiesSet(); this.exporter.afterSingletonsInstantiated(); verify(this.serverContainer).addEndpoint(AnnotatedDummyEndpoint.class); verify(this.serverContainer).addEndpoint(AnnotatedDummyEndpointBean.class); }