Java Code Examples for javax.websocket.server.ServerContainer#addEndpoint()

The following examples show how to use javax.websocket.server.ServerContainer#addEndpoint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestWsRemoteEndpointImplServer.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);

    ServerContainer sc = (ServerContainer) sce.getServletContext().getAttribute(
            Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);

    List<Class<? extends Encoder>> encoders = new ArrayList<Class<? extends Encoder>>();
    encoders.add(Bug58624Encoder.class);
    ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(
            Bug58624Endpoint.class, PATH).encoders(encoders).build();

    try {
        sc.addEndpoint(sec);
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: TesterEndpointConfig.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);

    ServerContainer sc = (ServerContainer) sce.getServletContext().getAttribute(
            Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);

    try {
        ServerEndpointConfig sec = getServerEndpointConfig();
        if (sec == null) {
            sc.addEndpoint(getEndpointClass());
        } else {
            sc.addEndpoint(sec);
        }
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: TestCloseBug58624.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@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(
            Bug58624ServerEndpoint.class, PATH).build();

    try {
        sc.addEndpoint(sec);
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
}
 
Example 4
Source File: TestCloseBug58624.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@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(
            Bug58624ServerEndpoint.class, PATH).build();

    try {
        sc.addEndpoint(sec);
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: TesterEchoServer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);
    ServerContainer sc =
            (ServerContainer) sce.getServletContext().getAttribute(
                    Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
    try {
        sc.addEndpoint(Async.class);
        sc.addEndpoint(Basic.class);
        sc.addEndpoint(BasicLimitLow.class);
        sc.addEndpoint(BasicLimitHigh.class);
        sc.addEndpoint(RootEcho.class);
    } catch (DeploymentException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 6
Source File: WebSocketListener.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    ServerContainer container = (ServerContainer) sce.getServletContext().getAttribute(SERVER_CONTAINER);
    if (container != null) {
        for (Class<?> endpointClass : serverEndpointClasses) {
            try {
                LOGGER.debug("Publishing WebSocket server endpoint {}", endpointClass.getCanonicalName());
                container.addEndpoint(endpointClass);
            } catch (DeploymentException e) {
                throw SeedException.wrap(e, WebErrorCode.CANNOT_PUBLISH_WEBSOCKET_ENDPOINT)
                        .put("class", endpointClass);
            }
        }
    } else {
        LOGGER.info("No WebSocket implementation found, WebSocket support disabled");
    }
}
 
Example 7
Source File: TestWsRemoteEndpointImplServer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);

    ServerContainer sc = (ServerContainer) sce.getServletContext().getAttribute(
            Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);

    List<Class<? extends Encoder>> encoders = new ArrayList<Class<? extends Encoder>>();
    encoders.add(Bug58624Encoder.class);
    ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(
            Bug58624Endpoint.class, PATH).encoders(encoders).build();

    try {
        sc.addEndpoint(sec);
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: TestClose.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@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(
            getEndpointClass(), PATH).build();

    try {
        sc.addEndpoint(sec);
    } catch (DeploymentException e) {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: TesterEchoServer.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);
    ServerContainer sc =
            (ServerContainer) sce.getServletContext().getAttribute(
                    Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
    try {
        sc.addEndpoint(Async.class);
        sc.addEndpoint(Basic.class);
        sc.addEndpoint(BasicLimitLow.class);
        sc.addEndpoint(BasicLimitHigh.class);
        sc.addEndpoint(WriterError.class);
        sc.addEndpoint(RootEcho.class);
    } catch (DeploymentException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 10
Source File: ServerEndpointExporter.java    From java-technology-stack with MIT License 6 votes vote down vote up
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);
	}
}
 
Example 11
Source File: TestWsServerContainer.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 12
Source File: StartupListener.java    From mdw with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent contextEvent) {
    ServletContext servletContext = contextEvent.getServletContext();
    mdwMain = new MdwMain();
    String container = "Tomcat"; // TODO
    if (ApplicationContext.isSpringBoot()) {
        ServerContainer serverContainer = (ServerContainer)servletContext.getAttribute("javax.websocket.server.ServerContainer");
        try {
            serverContainer.addEndpoint(WebSocketMessenger.class);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        mdwMain.startup(container, SpringBootApplication.getBootDir().toString(), servletContext.getContextPath());
    }
    else {
        mdwMain.startup(container, servletContext.getRealPath("/"), servletContext.getContextPath());
        new InitialRequest().submit();
    }
}
 
Example 13
Source File: AddEndpointServlet.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public void init(ServletConfig c) throws ServletException {
    String websocketPath = "/foo";
    ServerEndpointConfig config = ServerEndpointConfig.Builder.create(ProgramaticEndpoint.class, websocketPath).build();
    ServerContainer serverContainer = (ServerContainer) c.getServletContext().getAttribute("javax.websocket.server.ServerContainer");
    try {
        serverContainer.addEndpoint(config);
    } catch (DeploymentException ex) {
        throw new ServletException("Error deploying websocket endpoint:", ex);
    }
}
 
Example 14
Source File: TestWsSubprotocols.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);
    ServerContainer sc = (ServerContainer) sce.getServletContext()
            .getAttribute(Constants.
                    SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
    try {
        sc.addEndpoint(SubProtocolsEndpoint.class);
    } catch (DeploymentException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 15
Source File: WicketServerEndpointConfigRegister.java    From wicket-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
	ServletContext container = sce.getServletContext();

	final ServerContainer serverContainer = (ServerContainer) container
			.getAttribute(SERVER_CONTAINER_ATTRIBUTE);
	try {
		serverContainer.addEndpoint(new WicketServerEndpointConfig());
	} catch (DeploymentException e) {
		throw new WicketSpringBootException(e);
	}
}
 
Example 16
Source File: TerminalThread.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public void run() {
  ServerConnector connector = new ServerConnector(jettyServer);
  connector.setPort(port);
  jettyServer.addConnector(connector);

  ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
  context.setContextPath("/terminal/");

  // We look for a file, as ClassLoader.getResource() is not
  // designed to look for directories (we resolve the directory later)
  ClassLoader clazz = TerminalThread.class.getClassLoader();
  URL url = clazz.getResource("html");
  if (url == null) {
    throw new RuntimeException("Unable to find resource directory");
  }

  ResourceHandler resourceHandler = new ResourceHandler();
  // Resolve file to directory
  String webRootUri = url.toExternalForm();
  LOGGER.info("WebRoot is " + webRootUri);
  // debug
  // webRootUri = "/home/hadoop/zeppelin-current/interpreter/sh";
  resourceHandler.setResourceBase(webRootUri);

  HandlerCollection handlers = new HandlerCollection(context, resourceHandler);
  jettyServer.setHandler(handlers);

  try {
    ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
    container.addEndpoint(TerminalSocket.class);
    jettyServer.start();
    jettyServer.join();
  } catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
  }
}
 
Example 17
Source File: WebSocketServer.java    From product-cep with Apache License 2.0 5 votes vote down vote up
public void start(int port, String host) {
    server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(port);
    connector.setHost(host);
    server.addConnector(connector);

    // Setup the basic application "context" for this application at "/"
    // This is also known as the handler tree (in jetty speak)
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);

    try {
        // Initialize javax.websocket layer
        ServerContainer wscontainer = WebSocketServerContainerInitializer.configureContext(context);

        // Add WebSocket endpoint to javax.websocket layer
        wscontainer.addEndpoint(EventSocket.class);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    server.start();
                    server.join();
                } catch (Exception e) {
                    log.error(e);
                }
            }
        }).start();

    } catch (Throwable t) {
        log.error(t);
    }
}
 
Example 18
Source File: WebSocketServerEcho.java    From quarks with Apache License 2.0 5 votes vote down vote up
public void start(URI endpointURI, boolean needClientAuth) {
    curEndpointURI = endpointURI;
    curNeedClientAuth = needClientAuth;

    System.out.println(svrName+" "+endpointURI + " needClientAuth="+needClientAuth);

    server = createServer(endpointURI, needClientAuth);
    connector = (ServerConnector)server.getConnectors()[0];

    // Setup the basic application "context" for this application at "/"
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);
    
    try {
        // Initialize javax.websocket layer
        ServerContainer wscontainer = WebSocketServerContainerInitializer.configureContext(context);

        // Add WebSocket endpoint to javax.websocket layer
        wscontainer.addEndpoint(this.getClass());

        // System.setProperty("javax.net.debug", "ssl"); // or "all"; "help" for full list

        server.start();
        System.out.println(svrName+" started "+connector);
        // server.dump(System.err);            
    }
    catch (Exception e) {
        throw new RuntimeException("start", e);
    }
}
 
Example 19
Source File: TestEncodingDecoding.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);
    ServerContainer sc =
            (ServerContainer) sce.getServletContext().getAttribute(
                    org.apache.tomcat.websocket.server.Constants.
                    SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
    try {
        sc.addEndpoint(new ServerEndpointConfig() {
            @Override
            public Map<String, Object> getUserProperties() {
                return Collections.emptyMap();
            }
            @Override
            public List<Class<? extends Encoder>> getEncoders() {
                List<Class<? extends Encoder>> encoders = new ArrayList<Class<? extends Encoder>>(2);
                encoders.add(MsgStringEncoder.class);
                encoders.add(MsgByteEncoder.class);
                return encoders;
            }
            @Override
            public List<Class<? extends Decoder>> getDecoders() {
                List<Class<? extends Decoder>> decoders = new ArrayList<Class<? extends Decoder>>(2);
                decoders.add(MsgStringDecoder.class);
                decoders.add(MsgByteDecoder.class);
                return decoders;
            }
            @Override
            public List<String> getSubprotocols() {
                return Collections.emptyList();
            }
            @Override
            public String getPath() {
                return PATH_PROGRAMMATIC_EP;
            }
            @Override
            public List<Extension> getExtensions() {
                return Collections.emptyList();
            }
            @Override
            public Class<?> getEndpointClass() {
                return ProgrammaticEndpoint.class;
            }
            @Override
            public Configurator getConfigurator() {
                return new ServerEndpointConfig.Configurator() {
                };
            }
        });
    } catch (DeploymentException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 20
Source File: TestEncodingDecoding.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    super.contextInitialized(sce);
    ServerContainer sc =
            (ServerContainer) sce.getServletContext().getAttribute(
                    org.apache.tomcat.websocket.server.Constants.
                    SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
    try {
        sc.addEndpoint(new ServerEndpointConfig() {
            @Override
            public Map<String, Object> getUserProperties() {
                return Collections.emptyMap();
            }
            @Override
            public List<Class<? extends Encoder>> getEncoders() {
                List<Class<? extends Encoder>> encoders = new ArrayList<>(2);
                encoders.add(MsgStringEncoder.class);
                encoders.add(MsgByteEncoder.class);
                return encoders;
            }
            @Override
            public List<Class<? extends Decoder>> getDecoders() {
                List<Class<? extends Decoder>> decoders = new ArrayList<>(2);
                decoders.add(MsgStringDecoder.class);
                decoders.add(MsgByteDecoder.class);
                return decoders;
            }
            @Override
            public List<String> getSubprotocols() {
                return Collections.emptyList();
            }
            @Override
            public String getPath() {
                return PATH_PROGRAMMATIC_EP;
            }
            @Override
            public List<Extension> getExtensions() {
                return Collections.emptyList();
            }
            @Override
            public Class<?> getEndpointClass() {
                return ProgrammaticEndpoint.class;
            }
            @Override
            public Configurator getConfigurator() {
                return new ServerEndpointConfig.Configurator() {
                };
            }
        });
    } catch (DeploymentException e) {
        throw new IllegalStateException(e);
    }
}