Java Code Examples for org.eclipse.jetty.server.handler.ResourceHandler#setDirectoriesListed()

The following examples show how to use org.eclipse.jetty.server.handler.ResourceHandler#setDirectoriesListed() . 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: 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 3
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 4
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 5
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 6
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 7
Source File: StreamingReceiver.java    From kylin with Apache License 2.0 5 votes vote down vote up
private void startHttpServer() throws Exception {
    KylinConfig kylinConfig = KylinConfig.getInstanceFromEnv();
    createAndConfigHttpServer(kylinConfig);

    ContextHandlerCollection contexts = new ContextHandlerCollection();

    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/kylin");

    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    ctx.setConfigLocation("classpath:applicationContext.xml");
    ctx.refresh();
    DispatcherServlet dispatcher = new DispatcherServlet(ctx);
    context.addServlet(new ServletHolder(dispatcher), "/api/*");

    ContextHandler logContext = new ContextHandler("/kylin/logs");
    String logDir = getLogDir(kylinConfig);
    ResourceHandler logHandler = new ResourceHandler();
    logHandler.setResourceBase(logDir);
    logHandler.setDirectoriesListed(true);
    logContext.setHandler(logHandler);

    contexts.setHandlers(new Handler[] { context, logContext });
    httpServer.setHandler(contexts);
    httpServer.start();
    httpServer.join();
}
 
Example 8
Source File: ApiServer.java    From lancoder with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void run() {
	try {
		Properties jettyShutUpProperties = new Properties();
		jettyShutUpProperties.setProperty("org.eclipse.jetty.LEVEL", "WARN");
		StdErrLog.setProperties(jettyShutUpProperties);

		server = new Server(master.getConfig().getApiServerPort());

		// static resources handler
		ContextHandler ctxStatic = new ContextHandler("/");
		ResourceHandler staticHandler = new ResourceHandler();
		staticHandler.setResourceBase(this.getClass().getClassLoader().getResource(WEB_DIR).toExternalForm());
		// staticHandler.setResourceBase("src/main/web/web_resources");
		staticHandler.setDirectoriesListed(true);
		ctxStatic.setHandler(staticHandler);

		// api handler
		ContextHandler ctxApi = buildServletContextHandler();
		ctxApi.setContextPath("/api");

		ContextHandlerCollection contexts = new ContextHandlerCollection();
		contexts.setHandlers(new Handler[] { ctxStatic, ctxApi });
		server.setHandler(contexts);

		server.start();
		server.join();
	} catch (Exception e) {
		// TODO alert master api server api crashed
		e.printStackTrace();
	}
}
 
Example 9
Source File: BaleenWebApi.java    From baleen with Apache License 2.0 5 votes vote down vote up
private void installSwagger(HandlerList handlers) {
  LOGGER.debug("Adding Swagger documentation");

  final ResourceHandler resourceHandler = new ResourceHandler();
  resourceHandler.setDirectoriesListed(true); //
  resourceHandler.setResourceBase(getClass().getResource("/swagger").toExternalForm());

  ContextHandler swaggerHandler = new ContextHandler("/swagger/*");
  swaggerHandler.setHandler(resourceHandler);
  handlers.addHandler(swaggerHandler);
}
 
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: WebServer.java    From tracing-framework with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Server setupServer() throws Exception {
    // String webDir = "target/classes/webui";
    // String webDir = "src/main/resources/webui";
    String webDir = WebServer.class.getClassLoader().getResource("webui").toExternalForm();
    log.info("Base webdir is {}", webDir);

    int httpPort = ConfigFactory.load().getInt("resource-reporting.visualization.webui-port");
    log.info("Resource reporting web ui port is ", httpPort);

    // Create Jetty server
    Server server = new Server(httpPort);

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[] { "filter.html" });
    resource_handler.setResourceBase(webDir);

    WebSocketHandler wsHandler = new WebSocketHandler.Simple(PubSubProxyWebSocket.class);

    ContextHandler context = new ContextHandler();
    context.setContextPath("/ws");
    context.setHandler(wsHandler);

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

    server.setHandler(handlers);

    ClusterResources.subscribeToAll(callback);

    return server;
}
 
Example 12
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 13
Source File: HttpMain.java    From graphql-java-subscription-example with MIT License 5 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 back on a request
    ServletContextHandler servletContextHandler = new ServletContextHandler();
    servletContextHandler.setContextPath("/");

    ServletHolder stockTicker = new ServletHolder("ws-stockticker", StockTickerServlet.class);
    servletContextHandler.addServlet(stockTicker, "/stockticker");

    // 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, servletContextHandler});
    server.setHandler(handlers);

    server.start();

    server.join();
}
 
Example 14
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 15
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 16
Source File: TestServer.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
public TestServer(String protocol, String host, int port) {
    server = new Server(port);

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

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
    server.setHandler(handlers);
}
 
Example 17
Source File: TestServer.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
public TestServer(int port, File baseDirectory) {
    server = new Server(port);

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setResourceBase(baseDirectory == null ? "." : baseDirectory.getAbsolutePath());

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
    server.setHandler(handlers);
}
 
Example 18
Source File: JettyServer.java    From artifactory_ssh_proxy with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"resource", "boxing"})
public static Server newServer(int jettyPort, String jettyWebAppDir, JettyServiceSetting jettyServiceSetting) throws Exception {

    if (jettyPort == 0 || jettyWebAppDir == null) {
        throw new IllegalArgumentException("Jetty port and resource dir may not be empty");
    }

    // server setup
    Server server = new Server();
    server.addBean(new ScheduledExecutorScheduler());
    server.setDumpAfterStart(false);
    server.setDumpBeforeStop(false);
    server.setStopAtShutdown(true);


    // http://www.eclipse.org/jetty/documentation/current/embedding-jetty.html#d0e19050
    // Setup JMX
    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    server.addBean(mbContainer);


    //setup handlers according to the jetty settings
    HandlerCollection handlerCollection = new HandlerCollection();

    if (jettyServiceSetting == JettyServiceSetting.ARTIFACTORY || jettyServiceSetting == JettyServiceSetting.BOTH) {

        // The WebAppContext is the entity that controls the environment in
        // which a web application lives and breathes. In this example the
        // context path is being set to "/" so it is suitable for serving root
        // context requests and then we see it setting the location of the war.
        // A whole host of other configurations are available, ranging from
        // configuring to support annotation scanning in the webapp (through
        // PlusConfiguration) to choosing where the webapp will unpack itself.
        WebAppContext webapp = new WebAppContext();
        File warFile = new File(jettyWebAppDir + File.separator + "artifactory.war");
        webapp.setContextPath("/artifactory");
        webapp.setWar(warFile.getAbsolutePath());

        // A WebAppContext is a ContextHandler as well so it needs to be set to
        // the server so it is aware of where to send the appropriate requests.
        handlerCollection.addHandler(webapp);

    }

    if (jettyServiceSetting == JettyServiceSetting.VIP || jettyServiceSetting == JettyServiceSetting.BOTH) {

        // Serve resource files which reside in the jettyWebAppDir
        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(false);
        resourceHandler.setResourceBase(jettyWebAppDir);

        handlerCollection.addHandler(resourceHandler);
    }

    server.setHandler(handlerCollection);


    // http configuration
    HttpConfiguration http_config = new HttpConfiguration();
    http_config.setSendServerVersion(true);

    // HTTP connector
    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config));
    http.setPort(jettyPort);
    server.addConnector(http);

    // start server
    server.start();

    LOG.info("Started jetty server on port: {}, resource dir: {} ", jettyPort, jettyWebAppDir);
    return server;
}
 
Example 19
Source File: Activator.java    From slr-toolkit with Eclipse Public License 1.0 4 votes vote down vote up
public void start(BundleContext context) throws Exception {
	super.start(context);

	IWorkspace workspace = ResourcesPlugin.getWorkspace();
	String webappRoot = workspace.getRoot().getLocation().toString().concat("/webapp");
	new File(webappRoot).mkdirs();

	Activator.WEB_APP_WORKSPACE = webappRoot;

	// create the server on a free port
	Server server = new Server(0);

	// Configure the ResourceHandler. Setting the resource base indicates where the
	// files should be served out of.
	ResourceHandler resource_handler = new ResourceHandler();
	resource_handler.setDirectoriesListed(true);
	resource_handler.setResourceBase(webappRoot);

	// Add the ResourceHandler to the server.
	HandlerCollection handlers = new HandlerCollection(true);
	handlers.addHandler(resource_handler);

	server.setHandler(handlers);

	// Start server up.
	try {
		server.start();
	} catch (Exception e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	// Get the URI of the server and set it global for the
	// diagram handlers to access
	if (null != server && null != server.getURI()) {
		String localhost = server.getURI().toString();
		Activator.URL = localhost;
	}

	writeVegaFiles();

	plugin = this;
}
 
Example 20
Source File: DashBoardServer.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 3 votes vote down vote up
ContextHandler getResourceHandler() {
    ContextHandler root = new ContextHandler();
    root.setContextPath("/*");

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

    resourceHandler.setWelcomeFiles(new String[]{HOME});
    resourceHandler.setResourceBase(R_BASE);

    root.setHandler(resourceHandler);

    return root;

}