public EmbeddedJettyServer(int port, boolean useRestSession) throws Exception {
    server = new Server();
    connector = new SelectChannelConnector();
    connector.setPort(port);
    server.addConnector(connector);

    root = new WebAppContext();
    root.setContextPath("/");
    root.setResourceBase(".");
    if (useRestSession) {
      RestSessionIdManager idManager = new RestSessionIdManager();
      RestSessionManager sessionManager = new RestSessionManager();
      server.setSessionIdManager(idManager);
      sessionManager.setSessionIdManager(idManager);
      SessionHandler sessionHandler = new SessionHandler();
      sessionHandler.setSessionManager(sessionManager);
      root.setSessionHandler(sessionHandler);
      root.setClassLoader(getContextClassLoader());
    }

    server.setHandler(root);
    server.start();
    while (!server.isStarted()) {
      Thread.sleep(100);
    }
  }
  public static void main(String args[]) throws Exception {
    Server server = new Server(8080);
    // Specify the Session ID Manager
    HashSessionIdManager idmanager = new HashSessionIdManager();
    server.setSessionIdManager(idmanager);

    // Sessions are bound to a context.
    ContextHandler context = new ContextHandler("/");
    server.setHandler(context);

    // Create the SessionHandler (wrapper) to handle the sessions
    HashSessionManager manager = new HashSessionManager();
    SessionHandler sessions = new SessionHandler(manager);
    context.setHandler(sessions);
    // ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    // ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    // context.setContextPath("/");
    // server.setHandler(h);
    // server.setSessionIdManager(sessionIdManager);
    /*
           context.addServlet(new ServletHolder(new HelloServlet()),"/*");
           context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")),"/it/*");
           context.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")),"/fr/*");
    */
    Canvas c = new Canvas(800, 900);
    context.setAttribute("myCanvas", c);
    sessions.setHandler(new HttpHandler());
    server.start();
    server.join();
  }
  private Server createServer(ServletHolder servletHolder, String nodeName) {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    server.addConnector(connector);

    ServletContextHandler context =
        new ServletContextHandler(server, CONTEXT_PATH, ServletContextHandler.SESSIONS);
    context.addServlet(servletHolder, SERVLET_PATH + "/*");

    if (nodeName != null) {
      HashSessionIdManager sessionIdManager = new HashSessionIdManager();
      sessionIdManager.setWorkerName(nodeName);
      server.setSessionIdManager(sessionIdManager);
    }

    return server;
  }
  public static void createAndStartServer() {
    server = new Server(HTTP_SERVER_PORT);

    HashSessionIdManager idmanager = new HashSessionIdManager();
    server.setSessionIdManager(idmanager);

    AnnotationConfigWebApplicationContext applicationContext =
        new AnnotationConfigWebApplicationContext();
    applicationContext.register(JPAApplicationConfiguration.class);
    applicationContext.refresh();

    appContext = applicationContext;

    try {
      server.setHandler(getServletContextHandler(applicationContext));
      server.start();
    } catch (Exception e) {
      log.error("Error starting server", e);
    }
  }
Esempio n. 5
0
  public Server getJettyServer(int port, int sslPort, int maxThreads) throws IOException {

    Server server = new Server();
    HandlerCollection handlers = new HandlerCollection();
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    server.setThreadPool(new QueuedThreadPool(maxThreads));

    SslSocketConnector sslSocketConnector = null;
    if (sslPort > 0) {
      System.out.println("SSL is Starting on port " + sslPort + "...");
      sslSocketConnector = new SslSocketConnector();
      sslSocketConnector.setPort(getContainerConfig().getSSLPort());
      sslSocketConnector.setKeystore("conf/servertestkeystore");
      sslSocketConnector.setPassword(getContainerConfig().getSSLKeyPassword());
      sslSocketConnector.setKeyPassword(getContainerConfig().getSSLKeyStorePassword());
      sslSocketConnector.setTruststore("conf/servertestkeystore");
      sslSocketConnector.setTrustPassword(getContainerConfig().getSSLKeyStorePassword());
    } else if (getContainerConfig().isAcEnabled())
      logger.error("SSL MUST be configured in the gsn.xml file when Access Control is enabled !");

    AbstractConnector connector =
        new SelectChannelConnector(); // before was connector//new SocketConnector ();//using basic
                                      // connector for windows bug; Fast
                                      // option=>SelectChannelConnector
    connector.setPort(port);
    connector.setMaxIdleTime(30000);
    connector.setAcceptors(2);
    connector.setConfidentialPort(sslPort);

    if (sslSocketConnector == null) server.setConnectors(new Connector[] {connector});
    else server.setConnectors(new Connector[] {connector, sslSocketConnector});

    WebAppContext webAppContext = new WebAppContext(contexts, DEFAULT_WEB_APP_PATH, "/");

    handlers.setHandlers(new Handler[] {contexts, new DefaultHandler()});
    server.setHandler(handlers);

    Properties usernames = new Properties();
    usernames.load(new FileReader("conf/realm.properties"));
    if (!usernames.isEmpty()) {
      HashLoginService loginService = new HashLoginService();
      loginService.setName("GSNRealm");
      loginService.setConfig("conf/realm.properties");
      loginService.setRefreshInterval(10000); // re-reads the file every 10 seconds.

      Constraint constraint = new Constraint();
      constraint.setName("GSN User");
      constraint.setRoles(new String[] {"gsnuser"});
      constraint.setAuthenticate(true);

      ConstraintMapping cm = new ConstraintMapping();
      cm.setConstraint(constraint);
      cm.setPathSpec("/*");
      cm.setMethod("GET");

      ConstraintMapping cm2 = new ConstraintMapping();
      cm2.setConstraint(constraint);
      cm2.setPathSpec("/*");
      cm2.setMethod("POST");

      ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
      securityHandler.setLoginService(loginService);
      securityHandler.setConstraintMappings(new ConstraintMapping[] {cm, cm2});
      securityHandler.setAuthenticator(new BasicAuthenticator());
      webAppContext.setSecurityHandler(securityHandler);
    }

    server.setSendServerVersion(true);
    server.setStopAtShutdown(true);
    server.setSendServerVersion(false);
    server.setSessionIdManager(new HashSessionIdManager(new Random()));

    return server;
  }