org.eclipse.jetty.server.handler.ResourceHandler Java Examples

The following examples show how to use org.eclipse.jetty.server.handler.ResourceHandler. 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 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 #3
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 #4
Source File: GraphQlServer.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  // Embedded Jetty server
  Server server = new Server(HTTP_PORT);
  ResourceHandler resourceHandler = new ResourceHandler();
  resourceHandler.setWelcomeFiles(new String[] {"index.html"});
  resourceHandler.setDirectoriesListed(true);
  // resource base is relative to the WORKSPACE file
  resourceHandler.setResourceBase("./src/main/resources");
  HandlerList handlerList = new HandlerList();
  handlerList.setHandlers(
      new Handler[] {resourceHandler, new GraphQlHandler(), new DefaultHandler()});
  server.setHandler(handlerList);
  server.start();
  logger.info("Server running on port " + HTTP_PORT);
  server.join();
}
 
Example #5
Source File: ForbiddenWordUtilsTest.java    From es with Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
public void testFetch() throws Exception {
    String input = "12test32";
    Assert.assertFalse(ForbiddenWordUtils.containsForbiddenWord(input));

    ForbiddenWordUtils.setForbiddenWordFetchURL("http://localhost:10090/forbidden-test.txt");
    ForbiddenWordUtils.setReloadInterval(500);
    ForbiddenWordUtils.initRemoteFetch();

    Server server = new Server(10090);
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setBaseResource(Resource.newClassPathResource("."));
    server.setHandler(resourceHandler);
    server.start();

    Thread.sleep(1500);

    Assert.assertTrue(ForbiddenWordUtils.containsForbiddenWord(input));

    server.stop();
}
 
Example #6
Source File: HttpMain.java    From graphql-java-http-example with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    //
    // This example uses Jetty as an embedded HTTP server
    Server server = new Server(PORT);
    //
    // In Jetty, handlers are how your get called backed on a request
    HttpMain main_handler = new HttpMain();

    // this allows us to server our index.html and GraphIQL JS code
    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(false);
    resource_handler.setWelcomeFiles(new String[]{"index.html"});
    resource_handler.setResourceBase("./src/main/resources/httpmain");

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

    server.start();

    server.join();
}
 
Example #7
Source File: CodeGenBugTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testHelloWorldExternalBindingFile() throws Exception {
    Server server = new Server(0);
    try {
        ResourceHandler reshandler = new ResourceHandler();
        reshandler.setResourceBase(getLocation("/wsdl2java_wsdl/"));
        // this is the only handler we're supposed to need, so we don't need to
        // 'add' it.
        server.setHandler(reshandler);
        server.start();
        Thread.sleep(250); //give network connector a little time to spin up
        int port = ((NetworkConnector)server.getConnectors()[0]).getLocalPort();
        env.put(ToolConstants.CFG_WSDLURL, "http://localhost:"
            + port + "/hello_world.wsdl");
        env.put(ToolConstants.CFG_BINDING, "http://localhost:"
            + port + "/remote-hello_world_binding.xsd");
        processor.setContext(env);
        processor.execute();
        reshandler.stop();
    } finally {
        server.stop();
        server.destroy();
    }

}
 
Example #8
Source File: Application.java    From vk-java-sdk with MIT License 6 votes vote down vote up
private static void initServer(Properties properties) throws Exception {
    Integer port = Integer.valueOf(properties.getProperty("server.port"));
    String host = properties.getProperty("server.host");

    Integer clientId = Integer.valueOf(properties.getProperty("client.id"));
    String clientSecret = properties.getProperty("client.secret");

    HandlerCollection handlers = new HandlerCollection();

    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setWelcomeFiles(new String[]{"index.html"});
    resourceHandler.setResourceBase(Application.class.getResource("/static").getPath());

    VkApiClient vk = new VkApiClient(new HttpTransportClient());
    handlers.setHandlers(new Handler[]{resourceHandler, new RequestHandler(vk, clientId, clientSecret, host)});

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

    server.start();
    server.join();
}
 
Example #9
Source File: Application.java    From vk-java-sdk with MIT License 6 votes vote down vote up
private static void initServer(Properties properties) throws Exception {
    Integer port = Integer.valueOf(properties.getProperty("server.port"));
    String host = properties.getProperty("server.host");

    Integer clientId = Integer.valueOf(properties.getProperty("client.id"));
    String clientSecret = properties.getProperty("client.secret");

    HandlerCollection handlers = new HandlerCollection();

    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setWelcomeFiles(new String[]{"index.html"});
    resourceHandler.setResourceBase(Application.class.getResource("/static").getPath());

    VkApiClient vk = new VkApiClient(new HttpTransportClient());
    handlers.setHandlers(new Handler[]{resourceHandler, new RequestHandler(vk, clientId, clientSecret, host)});

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

    server.start();
    server.join();
}
 
Example #10
Source File: WebServer.java    From sparkler with Apache License 2.0 6 votes vote down vote up
public WebServer(int port, String resRoot){
    super(port);
    LOG.info("Port:{}, Resources Root:{}", port, resRoot);
    ResourceHandler rh0 = new ResourceHandler();
    ContextHandler context0 = new ContextHandler();
    context0.setContextPath("/res/*");
    context0.setResourceBase(resRoot);
    context0.setHandler(rh0);

    //ServletHandler context1 = new ServletHandler();
    //this.setHandler(context1);

    ServletContextHandler context1 =  new ServletContextHandler();
    context1.addServlet(TestSlaveServlet.class, "/slavesite/*");

    // Create a ContextHandlerCollection and set the context handlers to it.
    // This will let jetty process urls against the declared contexts in
    // order to match up content.
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.setHandlers(new Handler[] { context1, context0});

    this.setHandler(contexts);
}
 
Example #11
Source File: JettyAppServer.java    From selenium with Apache License 2.0 6 votes vote down vote up
protected ServletContextHandler addResourceHandler(String contextPath, Path resourceBase) {
  ServletContextHandler context = new ServletContextHandler();

  ResourceHandler staticResource = new ResourceHandler();
  staticResource.setDirectoriesListed(true);
  staticResource.setWelcomeFiles(new String[] { "index.html" });
  staticResource.setResourceBase(resourceBase.toAbsolutePath().toString());
  MimeTypes mimeTypes = new MimeTypes();
  mimeTypes.addMimeMapping("appcache", "text/cache-manifest");
  staticResource.setMimeTypes(mimeTypes);

  context.setContextPath(contextPath);
  context.setAliasChecks(Arrays.asList(new ApproveAliases(), new AllowSymLinkAliasChecker()));

  HandlerList allHandlers = new HandlerList();
  allHandlers.addHandler(staticResource);
  allHandlers.addHandler(context.getHandler());
  context.setHandler(allHandlers);

  handlers.addHandler(context);

  return context;
}
 
Example #12
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 #13
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 #14
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 #15
Source File: EmbeddedServer.java    From jaxrs with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	URI baseUri = UriBuilder.fromUri("http://localhost").port(SERVER_PORT)
			.build();
	ResourceConfig config = new ResourceConfig(Calculator.class);
	Server server = JettyHttpContainerFactory.createServer(baseUri, config,
			false);

	ContextHandler contextHandler = new ContextHandler("/rest");
	contextHandler.setHandler(server.getHandler());
	
	ProtectionDomain protectionDomain = EmbeddedServer.class
			.getProtectionDomain();
	URL location = protectionDomain.getCodeSource().getLocation();
	
	ResourceHandler resourceHandler = new ResourceHandler();
	resourceHandler.setWelcomeFiles(new String[] { "index.html" });
	resourceHandler.setResourceBase(location.toExternalForm());
	System.out.println(location.toExternalForm());
	HandlerCollection handlerCollection = new HandlerCollection();
	handlerCollection.setHandlers(new Handler[] { resourceHandler,
			contextHandler, new DefaultHandler() });
	server.setHandler(handlerCollection);
	server.start();
	server.join();
}
 
Example #16
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 #17
Source File: AtlasAPIV2ServerEmulator.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void createServer() throws Exception {
    server = new Server();
    final ContextHandlerCollection handlerCollection = new ContextHandlerCollection();

    final ServletContextHandler staticContext = new ServletContextHandler();
    staticContext.setContextPath("/");

    final ServletContextHandler atlasApiV2Context = new ServletContextHandler();
    atlasApiV2Context.setContextPath("/api/atlas/v2/");

    handlerCollection.setHandlers(new Handler[]{staticContext, atlasApiV2Context});

    server.setHandler(handlerCollection);

    final ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setBaseResource(Resource.newClassPathResource("public", false, false));
    staticContext.setHandler(resourceHandler);

    final ServletHandler servletHandler = new ServletHandler();
    atlasApiV2Context.insertHandler(servletHandler);

    httpConnector = new ServerConnector(server);
    httpConnector.setPort(21000);

    server.setConnectors(new Connector[]{httpConnector});

    servletHandler.addServletWithMapping(TypeDefsServlet.class, "/types/typedefs/");
    servletHandler.addServletWithMapping(EntityBulkServlet.class, "/entity/bulk/");
    servletHandler.addServletWithMapping(EntityGuidServlet.class, "/entity/guid/*");
    servletHandler.addServletWithMapping(SearchByUniqueAttributeServlet.class, "/entity/uniqueAttribute/type/*");
    servletHandler.addServletWithMapping(SearchBasicServlet.class, "/search/basic/");
    servletHandler.addServletWithMapping(LineageServlet.class, "/debug/lineage/");

    notificationServerEmulator = new AtlasNotificationServerEmulator();
}
 
Example #18
Source File: RemoteRepositoryConnectivityCheckTest.java    From archiva with Apache License 2.0 5 votes vote down vote up
protected Server buildStaticServer( Path path )
{
    Server repoServer = new Server(  );

    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed( true );
    resourceHandler.setWelcomeFiles( new String[]{ "index.html" } );
    resourceHandler.setResourceBase( path.toAbsolutePath().toString() );

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

    return repoServer;
}
 
Example #19
Source File: AdminWebServer.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private ResourceHandler buildStaticResourceHandler() {
  ResourceHandler staticResourceHandler = new ResourceHandler();
  staticResourceHandler.setDirectoriesListed(true);
  staticResourceHandler.setWelcomeFiles(new String[] { "index.html" });

  String staticDir = getClass().getClassLoader().getResource("static").toExternalForm();

  staticResourceHandler.setResourceBase(staticDir);
  return staticResourceHandler;
}
 
Example #20
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 #21
Source File: Starter.java    From jumbune with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void start() {

		Server server = new Server(9080);
		ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
		servletContextHandler.setContextPath("/");
		servletContextHandler.setResourceBase("src/main/webapp");

		final String webAppDirectory = JumbuneInfo.getHome() + "modules/webapp";
		final ResourceHandler resHandler = new ResourceHandler();
		resHandler.setResourceBase(webAppDirectory);
		final ContextHandler ctx = new ContextHandler("/");
		ctx.setHandler(resHandler);
		servletContextHandler.setSessionHandler(new SessionHandler());

		ServletHolder servletHolder = servletContextHandler.addServlet(ServletContainer.class, "/apis/*");
		servletHolder.setInitOrder(0);
		servletHolder.setAsyncSupported(true);
		servletHolder.setInitParameter("jersey.config.server.provider.packages", "org.jumbune.web.services");
		servletHolder.setInitParameter("jersey.config.server.provider.classnames",
				"org.glassfish.jersey.media.multipart.MultiPartFeature");

		try {
			server.insertHandler(servletContextHandler);
			server.insertHandler(resHandler);
			server.start();
			server.join();
		} catch (Exception e) {
			LOGGER.error("Error occurred while starting Jetty", e);
			System.exit(1);
		}
	}
 
Example #22
Source File: Main.java    From selenium with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  Flags flags = new Flags();
  JCommander.newBuilder().addObject(flags).build().parse(args);

  Server server = new Server();
  ServerConnector connector = new ServerConnector(server);
  connector.setPort(flags.port);
  server.setConnectors(new Connector[] {connector });

  HandlerList handlers = new HandlerList();

  ContextHandler context = new ContextHandler();
  context.setContextPath("/tests");
  ResourceHandler testHandler = new ResourceHandler();
  testHandler.setBaseResource(Resource.newClassPathResource("/tests"));
  testHandler.setDirectoriesListed(true);
  context.setHandler(testHandler);
  handlers.addHandler(context);

  ContextHandler coreContext = new ContextHandler();
  coreContext.setContextPath("/core");
  ResourceHandler coreHandler = new ResourceHandler();
  coreHandler.setBaseResource(Resource.newClassPathResource("/core"));
  coreContext.setHandler(coreHandler);
  handlers.addHandler(coreContext);

  ServletContextHandler driverContext = new ServletContextHandler();
  driverContext.setContextPath("/");
  driverContext.addServlet(WebDriverServlet.class, "/wd/hub/*");
  handlers.addHandler(driverContext);

  server.setHandler(handlers);
  server.start();
}
 
Example #23
Source File: TestSupport.java    From p3-batchrefine with Apache License 2.0 5 votes vote down vote up
private int startTransformServer() throws Exception {
    URL transforms = EngineTest.class.getClassLoader().getResource(
            "transforms");
    if (transforms == null) {
        Assert.fail();
    }

    int port = findFreePort();

    Server fileServer = new Server(port);

    ResourceHandler handler = new ResourceHandler();
    MimeTypes mimeTypes = handler.getMimeTypes();
    mimeTypes.addMimeMapping("json", "application/json; charset=UTF-8");

    handler.setDirectoriesListed(true);
    handler.setBaseResource(JarResource.newResource(transforms));

    HandlerList handlers = new HandlerList();
    handlers.addHandler(handler);
    handlers.addHandler(new DefaultHandler());

    fileServer.setHandler(handlers);
    fileServer.start();

    return port;
}
 
Example #24
Source File: FileServer.java    From cs601 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
      Server server = new Server(8080);
      ResourceHandler resource_handler = new ResourceHandler();
      resource_handler.setDirectoriesListed(true);
resource_handler.setResourceBase(".");                    // must set root dir

      HandlerList handlers = new HandlerList();
      handlers.setHandlers(new Handler[] { resource_handler, 	  // file handler
									 new DefaultHandler() // handles 404 etc...
});
      server.setHandler(handlers);

      server.start();
      server.join();
  }
 
Example #25
Source File: HttpdForTests.java    From buck with Apache License 2.0 5 votes vote down vote up
public FileDispenserRequestHandler(Path rootDir, String urlBasePath) {
  super(urlBasePath);

  ResourceHandler resourceHandler = new ResourceHandler();
  resourceHandler.setDirectoriesListed(true);
  resourceHandler.setResourceBase(rootDir.toAbsolutePath().toString());

  setHandler(resourceHandler);
  setLogger(new StdErrLog());
}
 
Example #26
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 #27
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 #28
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 {
    int port = 8080;
    if (args.length == 1) {
        String portString = args[0];
        port = Integer.valueOf(portString);
    }

    System.out.append("Starting at port: ").append(String.valueOf(port)).append('\n');

    AccountService accountService = new AccountService();

    Servlet signin = new SignInServlet(accountService);
    Servlet signUp = new SignUpServlet(accountService);

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(signin), "/api/v1/auth/signin");
    context.addServlet(new ServletHolder(signUp), "/api/v1/auth/signup");

    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 server = new Server(port);
    server.setHandler(handlers);

    server.start();
    server.join();
}
 
Example #29
Source File: AssetsContextHandlerTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotHandleForRails4DevelopmentMode() throws IOException, ServletException {
    when(systemEnvironment.useCompressedJs()).thenReturn(false);

    String target = "/go/assets/junk";
    Request request = mock(Request.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    Request baseRequest = mock(Request.class);
    ResourceHandler resourceHandler = mock(ResourceHandler.class);
    ReflectionUtil.setField(((HandlerWrapper) handler.getHandler()).getHandler(), "resourceHandler", resourceHandler);

    handler.getHandler().handle(target, baseRequest, request, response);
    verify(resourceHandler, never()).handle(any(String.class), any(Request.class), any(HttpServletRequest.class), any(HttpServletResponse.class));
}
 
Example #30
Source File: JettyHttpServer.java    From cloudhopper-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a directory for resources such as files containing html, images, etc.
 * to be served up automatically be this server.<br>
 * CAUTION: A resource base can only be added BEFORE the server is started.
 * @param resourceBaseDir The directory containing resources (basically the
 *      web root)
 */
public void addResourceBase(String resourceBaseDir) {
    // handles resources like images, files, etc..
    logger.info("HttpServer [{}] adding resource base dir [{}]", configuration.safeGetName(), resourceBaseDir);
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase(resourceBaseDir);
    handlers.addHandler(resourceHandler);
}