org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer Java Examples

The following examples show how to use org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer. 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: Application.java    From rest-utils with Apache License 2.0 7 votes vote down vote up
final Handler configureWebSocketHandler() throws ServletException {
  final ServletContextHandler webSocketContext =
          new ServletContextHandler(ServletContextHandler.SESSIONS);
  webSocketContext.setContextPath(
          config.getString(RestConfig.WEBSOCKET_PATH_PREFIX_CONFIG)
  );

  configureSecurityHandler(webSocketContext);

  ServerContainer container =
          WebSocketServerContainerInitializer.configureContext(webSocketContext);
  registerWebSocketEndpoints(container);

  configureWebSocketPostResourceHandling(webSocketContext);
  applyCustomConfiguration(webSocketContext, WEBSOCKET_SERVLET_INITIALIZERS_CLASSES_CONFIG);

  return webSocketContext;
}
 
Example #2
Source File: HttpMain.java    From samples with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  Server server = new Server();
  ServerConnector connector = new ServerConnector(server);
  connector.setPort(PORT);
  server.addConnector(connector);

  ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
  context.setContextPath("/");
  context.addServlet(HelloServlet.class, "/graphql");
  server.setHandler(context);

  WebSocketServerContainerInitializer.configure(context, (servletContext, serverContainer) -> {
    serverContainer.addEndpoint(ServerEndpointConfig.Builder
        .create(SubscriptionEndpoint.class, "/subscriptions")
        .configurator(new GraphQLWSEndpointConfigurer())
        .build());
  });

  server.setHandler(context);

  server.start();
  server.dump(System.err);
  server.join();
}
 
Example #3
Source File: Demo_JettyWebSocketServer.java    From haxademic with MIT License 6 votes vote down vote up
protected void firstFrame() {
	server = new Server();
	
	ServerConnector connector = new ServerConnector(server);
	connector.setPort(8787);
	server.addConnector(connector);

	ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
	context.setContextPath("/websocket");
	server.setHandler(context);

	try {
		ServerContainer wscontainer = WebSocketServerContainerInitializer.initialize(context);
		wscontainer.addEndpoint(WebsocketEndpoint.class);
		synchronized (server) {
			server.start();
		}
		
	} catch (Throwable t) {
		t.printStackTrace(System.err);
	}
}
 
Example #4
Source File: ServerStarter.java    From jetty-web-sockets-jsr356 with Apache License 2.0 6 votes vote down vote up
public static void main( String[] args ) throws Exception {
    Server server = new Server(8080);

    // Create the 'root' Spring application context
    final ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    context.addEventListener(new ContextLoaderListener());
    context.setInitParameter("contextClass",AnnotationConfigWebApplicationContext.class.getName());
    context.setInitParameter("contextConfigLocation",AppConfig.class.getName());

    // Create default servlet (servlet api required)
    // The name of DefaultServlet should be set to 'defualt'.
    final ServletHolder defaultHolder = new ServletHolder( "default", DefaultServlet.class );
    defaultHolder.setInitParameter( "resourceBase", System.getProperty("user.dir") );
    context.addServlet( defaultHolder, "/" );

    server.setHandler(context);
    WebSocketServerContainerInitializer.configureContext(context);

    server.start();
    server.join();	
}
 
Example #5
Source File: Launcher.java    From electron-java-app with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    log.info("Server starting...");

    var server = new Server(8080);

    var context = new WebAppContext();
    context.setBaseResource(findWebRoot());
    context.addServlet(VaadinServlet.class, "/*");
    context.setContextPath("/");
    context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
            ".*vaadin/.*\\.jar|.*/classes/.*");
    context.setConfigurationDiscovered(true);
    context.getServletContext().setExtendedListenerTypes(true);
    context.addEventListener(new ServletContextListeners());
    server.setHandler(context);

    var sessions = new SessionHandler();
    context.setSessionHandler(sessions);

    var classList = Configuration.ClassList.setServerDefault(server);
    classList.addBefore(JettyWebXmlConfiguration.class.getName(), AnnotationConfiguration.class.getName());
    WebSocketServerContainerInitializer.initialize(context); // fixes IllegalStateException: Unable to configure jsr356 at that stage. ServerContainer is null

    try {
        server.start();
        server.join();
    } catch (Exception e) {
        log.error("Server error:\n", e);
    }

    log.info("Server stopped");
}
 
Example #6
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 #7
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 #8
Source File: WebSocketServer.java    From product-cep with Apache License 2.0 5 votes vote down vote up
public void start(int port) {
    server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(port);
    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 #9
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.setHost(host);
    connector.setPort(port);
    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 #10
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 #11
Source File: DevServer.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void main(String[] args) {

        // 初始化 WebAppContext
        System.out.println("[INFO] Application loading");
        WebAppContext webAppContext = new WebAppContext();
        webAppContext.setContextPath(CONTEXT);
        webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH);
        webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH);
        webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH);

        // 初始化 server
        Server server = new Server(PORT);
        server.setHandler(webAppContext);
        supportJspAndSetTldJarNames(server, TLD_JAR_NAMES);

        System.out.println("[INFO] Application loaded");

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

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

            System.out.println("[HINT] Don't forget to set -XX:MaxPermSize=128m");
            System.out.println("Server running at http://localhost:" + PORT + CONTEXT);
            System.out.println("[HINT] Hit Enter to reload the application quickly");

            server.start();
            // server.join();

            // 等待用户输入回车重载应用
            while (true) {
                char c = (char) System.in.read();
                if (c == '\n') {
                    DevServer.reloadWebAppContext(server);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }