@Override public void contextInitialized(ServletContextEvent sce) { ServerContainer container = (ServerContainer) sce.getServletContext().getAttribute(ServerContainer.class.getName()); try { // Should fail as there is no path associated with this endpoint container.addEndpoint(BasicEchoEndpoint.class); } catch (DeploymentException e) { throw new RuntimeException("Unable to add endpoint directly", e); } }
@Override public void contextInitialized(ServletContextEvent sce) { super.contextInitialized(sce); ServerContainer sc = (ServerContainer) sce.getServletContext() .getAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE); try { sc.addEndpoint(Endpoint.class); } catch (DeploymentException e) { throw new IllegalStateException(e); } }
public static void launchSessionEndpoint( ServletContext context, String path, String actorSystemKey) { ServerContainer container = (ServerContainer) context.getAttribute("javax.websocket.server.ServerContainer"); try { container.addEndpoint( ServerEndpointConfig.Builder.create(SessionEndpoint.class, path) .configurator(new SessionEndpointConfigurator(actorSystemKey)) .build()); } catch (DeploymentException ex) { ex.printStackTrace(); } }
@Override public void contextInitialized(ServletContextEvent sce) { ServerContainer container = (ServerContainer) sce.getServletContext().getAttribute(ServerContainer.class.getName()); try { container.addEndpoint( ServerEndpointConfig.Builder.create(PongMessageEndpoint.class, "/ping").build()); container.addEndpoint( ServerEndpointConfig.Builder.create(PongMessageEndpoint.class, "/pong").build()); } catch (DeploymentException e) { throw new RuntimeException("Unable to add endpoint via config file", e); } }
@Override public void contextInitialized(ServletContextEvent sce) { super.contextInitialized(sce); ServerContainer sc = (ServerContainer) sce.getServletContext() .getAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE); try { sc.addEndpoint(ServerEndpointConfig.Builder.create(ConstantTxEndpoint.class, PATH).build()); if (TestWsWebSocketContainer.timoutOnContainer) { sc.setAsyncSendTimeout(TIMEOUT_MS); } } catch (DeploymentException e) { throw new IllegalStateException(e); } }
@Override public void contextInitialized(ServletContextEvent sce) { super.contextInitialized(sce); ServerContainer sc = (ServerContainer) sce.getServletContext() .getAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE); ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(TesterEchoServer.Basic.class, "/{param}").build(); try { sc.addEndpoint(sec); } catch (DeploymentException e) { throw new RuntimeException(e); } }
@Test public void testWebSocketInjectionAndInterception() throws Exception { AnnotatedClient.reset(); AnnotatedEndpoint.reset(); ComponentInterceptor.resetInterceptions(); final ServerContainer serverContainer = (ServerContainer) new InitialContext().lookup(SERVER_CONTAINER_JNDI_NAME); serverContainer.connectToServer( AnnotatedClient.class, new URI( "ws", "", TestSuiteEnvironment.getServerAddress(), 8080, "/websocket/websocket/cruel", "", "")); Assert.assertEquals("Hello cruel World", AnnotatedClient.getMessage()); Assert.assertTrue("Client endpoint's injection not correct.", AnnotatedClient.injectionOK); Assert.assertTrue("Server endpoint's injection not correct.", AnnotatedEndpoint.injectionOK); Assert.assertTrue( "PostConstruct method on client endpoint instance not called.", AnnotatedClient.postConstructCalled); Assert.assertTrue( "PostConstruct method on server endpoint instance not called.", AnnotatedEndpoint.postConstructCalled); Assert.assertEquals( "AroundConstruct interceptor method not invoked for client endpoint.", "AroundConstructInterceptor#Joe#AnnotatedClient", AnnotatedClient.getName()); Assert.assertEquals( "AroundConstruct interceptor method not invoked for server endpoint.", "AroundConstructInterceptor#Joe#AnnotatedEndpoint", AnnotatedEndpoint.getName()); Assert.assertEquals(2, ComponentInterceptor.getInterceptions().size()); Assert.assertEquals("open", ComponentInterceptor.getInterceptions().get(0).getMethodName()); Assert.assertEquals("message", ComponentInterceptor.getInterceptions().get(1).getMethodName()); }
@SuppressWarnings({"unchecked", "rawtypes"}) public Task<Void> start() { // Ensuring that jersey will use singletons from the orbit container. ServiceLocator locator = Injections.createLocator(); DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class); DynamicConfiguration dc = dcs.createDynamicConfiguration(); final List<Class<?>> classes = new ArrayList<>(providers); if (container != null) { classes.addAll(container.getClasses()); for (final Class<?> c : container.getClasses()) { if (c.isAnnotationPresent(Singleton.class)) { Injections.addBinding( Injections.newFactoryBinder( new Factory() { @Override public Object provide() { return container.get(c); } @Override public void dispose(final Object instance) {} }) .to(c), dc); } } } dc.commit(); final ResourceConfig resourceConfig = new ResourceConfig(); // installing jax-rs classes known by the orbit container. for (final Class c : classes) { if (c.isAnnotationPresent(javax.ws.rs.Path.class) || c.isAnnotationPresent(javax.ws.rs.ext.Provider.class)) { resourceConfig.register(c); } } final WebAppContext webAppContext = new WebAppContext(); final ProtectionDomain protectionDomain = EmbeddedHttpServer.class.getProtectionDomain(); final URL location = protectionDomain.getCodeSource().getLocation(); logger.info(location.toExternalForm()); webAppContext.setInitParameter("useFileMappedBuffer", "false"); webAppContext.setWar(location.toExternalForm()); // this sets the default service locator to one that bridges to the orbit container. webAppContext.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, locator); webAppContext.setContextPath("/*"); webAppContext.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*"); final ContextHandler resourceContext = new ContextHandler(); ResourceHandler resourceHandler = new ResourceHandler(); resourceHandler.setDirectoriesListed(true); resourceHandler.setWelcomeFiles(new String[] {"index.html"}); resourceHandler.setBaseResource(Resource.newClassPathResource("/web")); resourceContext.setHandler(resourceHandler); resourceContext.setInitParameter("useFileMappedBuffer", "false"); final ContextHandlerCollection contexts = new ContextHandlerCollection(); contexts.setHandlers( new Handler[] { wrapHandlerWithMetrics(resourceContext, "resourceContext"), wrapHandlerWithMetrics(webAppContext, "webAppContext") }); server = new Server(port); server.setHandler(contexts); try { /// Initialize javax.websocket layer final ServerContainer serverContainer = WebSocketServerContainerInitializer.configureContext(webAppContext); for (Class c : classes) { if (c.isAnnotationPresent(ServerEndpoint.class)) { final ServerEndpoint annotation = (ServerEndpoint) c.getAnnotation(ServerEndpoint.class); final ServerEndpointConfig serverEndpointConfig = ServerEndpointConfig.Builder.create(c, annotation.value()) .configurator( new ServerEndpointConfig.Configurator() { @Override public <T> T getEndpointInstance(final Class<T> endpointClass) throws InstantiationException { return container.get(endpointClass); } }) .build(); serverContainer.addEndpoint(serverEndpointConfig); } } } catch (Exception e) { logger.error("Error starting jetty", e); throw new UncheckedException(e); } try { server.start(); } catch (Exception e) { logger.error("Error starting jetty", e); throw new UncheckedException(e); } return Task.done(); }