Java Code Examples for org.eclipse.jetty.server.handler.HandlerList#setHandlers()

The following examples show how to use org.eclipse.jetty.server.handler.HandlerList#setHandlers() . 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: ConfigurationServerDriver.java    From candybean with GNU Affero General Public License v3.0 7 votes vote down vote up
public static void main (String args[]) throws Exception{
	logger.info("Starting JETTY server");
    Server server = new Server(8080);
    
       ResourceHandler resourceHandler = new ResourceHandler();
       resourceHandler.setDirectoriesListed(true);
       resourceHandler.setWelcomeFiles(new String[]{ "resources/html/configure.html" });
       resourceHandler.setResourceBase(".");
       
       ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
       resourceHandler.setWelcomeFiles(new String[]{ "resources/html/configure.html" });
       resourceHandler.setResourceBase(".");
       context.addServlet(new ServletHolder(new ConfigurationServlet()),"/cfg");
       context.addServlet(new ServletHolder(new SaveConfigurationServlet()),"/cfg/save");
       context.addServlet(new ServletHolder(new LoadConfigurationServlet()),"/cfg/load");
       
       HandlerList handlers = new HandlerList();
       handlers.setHandlers(new Handler[] { resourceHandler, context });
       server.setHandler(handlers);

       server.start();
	logger.info("Configuration server started at: http://localhost:8080/cfg");
       server.join();
}
 
Example 2
Source File: Main.java    From tp_java_2015_02 with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.append("Use port as the first argument");
        System.exit(1);
    }

    String portString = args[0];
    int port = Integer.valueOf(portString);
    System.out.append("Starting at port: ").append(portString).append('\n');

    Server server = new Server(port);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(new AdminPageServlet()), AdminPageServlet.adminPageURL);

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setResourceBase("static");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resource_handler, context});
    server.setHandler(handlers);

    server.start();
    server.join();
}
 
Example 3
Source File: Main.java    From stepic_java_webserver with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    AccountService accountService = new AccountService();

    accountService.addNewUser(new UserProfile("admin"));
    accountService.addNewUser(new UserProfile("test"));

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(new UsersServlet(accountService)), "/api/v1/users");
    context.addServlet(new ServletHolder(new SessionsServlet(accountService)), "/api/v1/sessions");

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setResourceBase("public_html");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resource_handler, context});

    Server server = new Server(8080);
    server.setHandler(handlers);

    server.start();
    server.join();
}
 
Example 4
Source File: WebServerTestCase.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Starts the web server on the default {@link #PORT}.
 * The given resourceBase is used to be the ROOT directory that serves the default context.
 * <p><b>Don't forget to stop the returned HttpServer after the test</b>
 *
 * @param resourceBase the base of resources for the default context
 * @throws Exception if the test fails
 */
protected void startWebServer(final String resourceBase) throws Exception {
    if (server_ != null) {
        throw new IllegalStateException("startWebServer() can not be called twice");
    }
    final Server server = buildServer(PORT);

    final WebAppContext context = new WebAppContext();
    context.setContextPath("/");
    context.setResourceBase(resourceBase);

    final ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase(resourceBase);
    final MimeTypes mimeTypes = new MimeTypes();
    mimeTypes.addMimeMapping("js", MimeType.APPLICATION_JAVASCRIPT);
    resourceHandler.setMimeTypes(mimeTypes);

    final HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resourceHandler, context});
    server.setHandler(handlers);
    server.setHandler(resourceHandler);

    tryStart(PORT, server);
    server_ = server;
}
 
Example 5
Source File: WebServer.java    From haxademic with MIT License 6 votes vote down vote up
protected void configServer() {
       // init static web server
       WebAppContext webAppContext = new WebAppContext(wwwPath, "/");
       
       // turn off file locking! 
       // without this, we were blocked from dynamically replace static files in the web server directory at runtime
       webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
       
       // set custom & static handlers
       HandlerList handlers = new HandlerList();
       handlers.setHandlers(new Handler[] { 
   		this.handler, 
   		webAppContext 			// Jetty's built-in static asset web server. this catches any request not handled by the custom handler
       });
       server.setHandler(handlers);
}
 
Example 6
Source File: Main.java    From stepic_java_webserver with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

    context.addServlet(new ServletHolder(new WebSocketChatServlet()), "/chat");

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setResourceBase("public_html");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resource_handler, context});
    server.setHandler(handlers);

    server.start();
    server.join();
}
 
Example 7
Source File: PackageManagerCLITest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void start() throws Exception {
  server = new Server();

  connector = new ServerConnector(server);
  connector.setPort(port);
  server.addConnector(connector);
  server.setStopAtShutdown(true);

  ResourceHandler resourceHandler = new ResourceHandler();
  resourceHandler.setResourceBase(resourceDir);
  resourceHandler.setDirectoriesListed(true);

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

  server.start();
}
 
Example 8
Source File: CompletionServer.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Starts the server.
 * 
 * @param host
 *            the host to listen
 * @param port
 *            the port to listen
 * @throws Exception
 *             if the server can't be started
 */
public void start(String host, int port) throws Exception {
    server = new Server();

    @SuppressWarnings("resource")
    final ServerConnector connector = new ServerConnector(server);
    connector.setPort(port);
    connector.setHost(host);
    server.setConnectors(new Connector[] {connector });

    final ServletHandler servletHandler = new ServletHandler();
    server.setHandler(servletHandler);
    servletHandler.addServletWithMapping(AddInServlet.class, "/*");

    final HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {servletHandler, new DefaultHandler() });
    server.setHandler(handlers);

    server.start();
}
 
Example 9
Source File: Main.java    From homework_tester with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

    context.addServlet(new ServletHolder(new WebSocketChatServlet()), "/chat");

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setResourceBase("public_html");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resource_handler, context});
    server.setHandler(handlers);

    server.start();
    System.out.println("Server started!");
    server.join();
}
 
Example 10
Source File: Main.java    From tp_java_2015_02 with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

    WebSocketService webSocketService = new WebSocketServiceImpl();
    GameMechanics gameMechanics = new GameMechanicsImpl(webSocketService);
    AuthService authService = new AuthServiceImpl();

    //for chat example
    context.addServlet(new ServletHolder(new WebSocketChatServlet()), "/chat");

    //for game example
    context.addServlet(new ServletHolder(new WebSocketGameServlet(authService, gameMechanics, webSocketService)), "/gameplay");
    context.addServlet(new ServletHolder(new GameServlet(gameMechanics, authService)), "/game.html");

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setResourceBase("static");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resource_handler, context});
    server.setHandler(handlers);

    server.setHandler(handlers);

    server.start();

    //run GM in main thread
    gameMechanics.run();
}
 
Example 11
Source File: SubmarineServer.java    From submarine with Apache License 2.0 5 votes vote down vote up
private static WebAppContext setupWebAppContext(HandlerList handlers,
    SubmarineConfiguration conf) {
  WebAppContext webApp = new WebAppContext();
  webApp.setContextPath("/");
  File warPath = new File(conf.getString(SubmarineConfVars.ConfVars.WORKBENCH_WEB_WAR));
  LOG.info("workbench web war file path is {}.",
      conf.getString(SubmarineConfVars.ConfVars.WORKBENCH_WEB_WAR));
  if (warPath.isDirectory()) {
    // Development mode, read from FS
    webApp.setResourceBase(warPath.getPath());
    webApp.setParentLoaderPriority(true);
  } else {
    // use packaged WAR
    webApp.setWar(warPath.getAbsolutePath());
    File warTempDirectory = new File("webapps");
    warTempDirectory.mkdir();
    webApp.setTempDirectory(warTempDirectory);
  }

  webApp.addServlet(new ServletHolder(new DefaultServlet()), "/");
  // When requesting the workbench page, the content of index.html needs to be returned,
  // otherwise a 404 error will be displayed
  // NOTE: If you modify the workbench directory in the front-end URL,
  // you need to modify the `/workbench/*` here.
  webApp.addServlet(new ServletHolder(RefreshServlet.class), "/user/*");
  webApp.addServlet(new ServletHolder(RefreshServlet.class), "/workbench/*");

  handlers.setHandlers(new Handler[] { webApp });

  return webApp;
}
 
Example 12
Source File: Commands.java    From allure2 with Apache License 2.0 5 votes vote down vote up
/**
 * Set up Jetty server to serve Allure Report.
 */
protected Server setUpServer(final String host, final int port, final Path reportDirectory) {
    final Server server = Objects.isNull(host)
            ? new Server(port)
            : new Server(new InetSocketAddress(host, port));
    final ResourceHandler handler = new ResourceHandler();
    handler.setRedirectWelcome(true);
    handler.setDirectoriesListed(true);
    handler.setResourceBase(reportDirectory.toAbsolutePath().toString());
    final HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{handler, new DefaultHandler()});
    server.setStopAtShutdown(true);
    server.setHandler(handlers);
    return server;
}
 
Example 13
Source File: WebServer.java    From pf4j-update with Apache License 2.0 5 votes vote down vote up
public void start() throws Exception {
    server = new Server(port);
    server.setStopAtShutdown(true);

    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase(resourceBase);
    resourceHandler.setDirectoriesListed(true);

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

    server.start();
}
 
Example 14
Source File: Main.java    From stepic_java_webserver with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        logger.error("Use port as the first argument");
        System.exit(1);
    }

    String portString = args[0];
    int port = Integer.valueOf(portString);

    logger.info("Starting at http://127.0.0.1:" + portString);

    AccountServerI accountServer = new AccountServer(1);

    AccountServerControllerMBean serverStatistics = new AccountServerController(accountServer);
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = new ObjectName("ServerManager:type=AccountServerController");
    mbs.registerMBean(serverStatistics, name);

    Server server = new Server(port);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(new HomePageServlet(accountServer)), HomePageServlet.PAGE_URL);

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setResourceBase("static");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resource_handler, context});
    server.setHandler(handlers);

    server.start();
    logger.info("Server started");

    server.join();
}
 
Example 15
Source File: Main.java    From tp_java_2015_02 with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        logger.error("Use port as the first argument");
        System.exit(1);
    }

    String portString = args[0];
    int port = Integer.valueOf(portString);

    logger.info("Starting at http://127.0.0.1:" + portString);

    AccountServerI accountServer = new AccountServer(1);

    AccountServerControllerMBean serverStatistics = new AccountServerController(accountServer);
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = new ObjectName("ServerManager:type=AccountServerController");
    mbs.registerMBean(serverStatistics, name);

    Server server = new Server(port);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(new HomePageServlet(accountServer)), HomePageServlet.PAGE_URL);

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setResourceBase("static");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resource_handler, context});
    server.setHandler(handlers);

    server.start();
    logger.info("Server started");

    server.join();
}
 
Example 16
Source File: ReportOpen.java    From allure1 with Apache License 2.0 5 votes vote down vote up
/**
 * Set up server for report directory.
 */
private Server setUpServer() {
    Server server = new Server(port);
    ResourceHandler handler = new ResourceHandler();
    handler.setDirectoriesListed(true);
    handler.setWelcomeFiles(new String[]{"index.html"});
    handler.setResourceBase(getReportDirectoryPath().toAbsolutePath().toString());
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{handler, new DefaultHandler()});
    server.setStopAtShutdown(true);
    server.setHandler(handlers);
    return server;
}
 
Example 17
Source File: FileServer.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates an http server on a random port, serving the <code>dir</code>
 * directory (relative to the current project).
 * 
 * @param dir
 * @throws IOException
 */
public FileServer(String dir) throws IOException {
	server = new Server(0);
	ResourceHandler resourceHandler = new ResourceHandler();
	Path base = ProjectUtils.getProjectDirectory().resolve(dir);
	resourceHandler.setResourceBase(base.toUri().toString());
	resourceHandler.setDirectoriesListed(true);
       HandlerList handlers = new HandlerList();
       handlers.setHandlers(new Handler[] { resourceHandler, new DefaultHandler() });
       server.setHandler(handlers);
}
 
Example 18
Source File: Main.java    From homework_tester with MIT License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    int port = 8080;

    logger.info("Starting at http://127.0.0.1:" + port);

    AccountServer accountServer = new AccountServerImpl();

    createMBean(accountServer);

    Server server = new Server(port);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(new AdminPageServlet(accountServer)), AdminPageServlet.PAGE_URL);

    ResourceHandler resource_handler = new ResourceHandler();

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resource_handler, context});
    server.setHandler(handlers);

    server.start();
    logger.info("Server started");

    server.join();
}
 
Example 19
Source File: Main.java    From homework_tester with MIT License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    int port = 8080;

    logger.info("Starting at http://127.0.0.1:" + port);

    ResourceServer resourceServer = new ResourceServerImpl();

    createMBean(resourceServer);

    Server server = new Server(port);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(new ResourcePageServlet(resourceServer)), ResourcePageServlet.PAGE_URL);

    ResourceHandler resource_handler = new ResourceHandler();

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resource_handler, context});
    server.setHandler(handlers);

    server.start();
    logger.info("Server started");

    server.join();
}
 
Example 20
Source File: CitizenIntelligenceAgencyServer.java    From cia with Apache License 2.0 4 votes vote down vote up
/**
 * Inits the.
 *
 * @throws Exception the exception
 */
public final void init() throws Exception {
	initialised = true;
	server = new Server();
	Security.addProvider(new BouncyCastleProvider());
	// Setup JMX
	final MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
	server.addBean(mbContainer);

	final org.eclipse.jetty.webapp.Configurations classlist = org.eclipse.jetty.webapp.Configurations.setServerDefault(server);
			
	final HttpConfiguration http_config = new HttpConfiguration();
	http_config.setSecureScheme("https");
	http_config.setSecurePort(28443);
	http_config.setSendServerVersion(false);

	final HttpConfiguration https_config = new HttpConfiguration(http_config);
	https_config.addCustomizer(new SecureRequestCustomizer());

	final SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
	sslContextFactory.setKeyStoreType("PKCS12");
	sslContextFactory.setKeyStorePath("target/keystore.p12");
	sslContextFactory.setTrustStorePath("target/keystore.p12");
	sslContextFactory.setTrustStoreType("PKCS12");

	sslContextFactory.setKeyStorePassword("changeit");
	sslContextFactory.setTrustStorePassword("changeit");
	sslContextFactory.setKeyManagerPassword("changeit");
	sslContextFactory.setCertAlias("jetty");
	sslContextFactory.setIncludeCipherSuites("TLS_AES_256_GCM_SHA384", "TLS_CHACHA20_POLY1305_SHA256",
			"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
			"TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256");
	sslContextFactory.setExcludeProtocols("SSL", "SSLv2", "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1");
	sslContextFactory.setIncludeProtocols("TLSv1.2", "TLSv1.3");

	final ServerConnector sslConnector = new ServerConnector(server,
			new SslConnectionFactory(sslContextFactory, "http/1.1"), new HttpConnectionFactory(https_config),
			new HTTP2CServerConnectionFactory(https_config));
	sslConnector.setPort(PORT);

	server.setConnectors(new ServerConnector[] { sslConnector });
	final WebAppContext handler = new WebAppContext("src/main/webapp", "/");
	handler.setExtraClasspath("target/classes");
	handler.setParentLoaderPriority(true);
	handler.setConfigurationDiscovered(true);
	handler.setClassLoader(Thread.currentThread().getContextClassLoader());
	final HandlerList handlers = new HandlerList();

	handlers.setHandlers(new Handler[] { handler, new DefaultHandler() });

	server.setHandler(handlers);
}