コード例 #1
0
ファイル: HttpServer.java プロジェクト: alanfgates/hive
  void initializeWebServer(Builder b) {
    // Create the thread pool for the web server to handle HTTP requests
    QueuedThreadPool threadPool = new QueuedThreadPool();
    if (b.maxThreads > 0) {
      threadPool.setMaxThreads(b.maxThreads);
    }
    threadPool.setDaemon(true);
    threadPool.setName(b.name + "-web");
    webServer.setThreadPool(threadPool);

    // Create the channel connector for the web server
    Connector connector = createChannelConnector(threadPool.getMaxThreads(), b);
    connector.setHost(b.host);
    connector.setPort(b.port);
    webServer.addConnector(connector);

    RewriteHandler rwHandler = new RewriteHandler();
    rwHandler.setRewriteRequestURI(true);
    rwHandler.setRewritePathInfo(false);

    RewriteRegexRule rootRule = new RewriteRegexRule();
    rootRule.setRegex("^/$");
    rootRule.setReplacement(b.contextRootRewriteTarget);
    rootRule.setTerminating(true);

    rwHandler.addRule(rootRule);
    rwHandler.setHandler(webAppContext);

    // Configure web application contexts for the web server
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.addHandler(rwHandler);
    webServer.setHandler(contexts);

    addServlet("jmx", "/jmx", JMXJsonServlet.class);
    addServlet("conf", "/conf", ConfServlet.class);
    addServlet("stacks", "/stacks", StackServlet.class);

    for (Pair<String, Class<? extends HttpServlet>> p : b.servlets) {
      addServlet(p.getFirst(), "/" + p.getFirst(), p.getSecond());
    }

    ServletContextHandler staticCtx = new ServletContextHandler(contexts, "/static");
    staticCtx.setResourceBase(appDir + "/static");
    staticCtx.addServlet(DefaultServlet.class, "/*");
    staticCtx.setDisplayName("static");

    String logDir = getLogDir(b.conf);
    if (logDir != null) {
      ServletContextHandler logCtx = new ServletContextHandler(contexts, "/logs");
      setContextAttributes(logCtx.getServletContext(), b.contextAttrs);
      logCtx.addServlet(AdminAuthorizedServlet.class, "/*");
      logCtx.setResourceBase(logDir);
      logCtx.setDisplayName("logs");
    }
  }
コード例 #2
0
  public static void main(String[] args) {
    GTFSDB.InitializeDB();
    Datasource ds = Datasource.getInstance();
    ds.getConnection();

    Server server = new Server(8080);
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    context.setResourceBase(BASE_RESOURCE);

    server.setHandler(context);

    context.addServlet(GetFeedJSON.class, "/getFeed");
    context.addServlet(DefaultServlet.class, "/");

    ServletHolder jerseyServlet =
        context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/api/*");
    jerseyServlet.setInitOrder(1);
    jerseyServlet.setInitParameter(
        "jersey.config.server.provider.classnames",
        "org.glassfish.jersey.moxy.json.MoxyJsonFeature");
    jerseyServlet.setInitParameter(
        "jersey.config.server.provider.packages", "edu.usf.cutr.gtfsrtvalidator.api.resource");

    try {
      server.start();
      server.join();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #3
0
  private ServletContextHandler dumb(final String path) {
    final File srcGit = remoteRepository.getRepository().getDirectory();
    final URI base = srcGit.getParentFile().toURI();

    ServletContextHandler ctx = server.addContext(path);
    ctx.setResourceBase(base.toString());
    ctx.addServlet(DefaultServlet.class, "/");
    return ctx;
  }
コード例 #4
0
 private static ServletContextHandler getServletContextHandler(WebApplicationContext context)
     throws IOException {
   ServletContextHandler contextHandler = new ServletContextHandler();
   contextHandler.setErrorHandler(null);
   contextHandler.setContextPath(CONTEXT_PATH);
   contextHandler.addServlet(new ServletHolder(new DispatcherServlet(context)), MAPPING_URL);
   contextHandler.addEventListener(new ContextLoaderListener(context));
   contextHandler.setResourceBase("webapp");
   return contextHandler;
 }
コード例 #5
0
ファイル: RascalTutor.java プロジェクト: pombredanne/rascal
  private ServletContextHandler getTutorHandler() throws IOException {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setAttribute("RascalEvaluator", eval);
    context.addServlet(new ServletHolder(new TutorDefaultHttpServlet()), "/");
    context.addServlet(new ServletHolder(new Show()), "/show");
    context.addServlet(new ServletHolder(new ValidateExam()), "/validateExam");
    context.addServlet(new ServletHolder(new Validate()), "/validate");
    context.addServlet(new ServletHolder(new Eval()), "/eval");
    context.addServlet(new ServletHolder(new Edit()), "/edit");
    context.addServlet(new ServletHolder(new Save()), "/save");
    context.addServlet(new ServletHolder(new Compile()), "/compile");

    URI baseURI = getResolverRegistry().getResourceURI(URIUtil.rootScheme(BASE_SCHEME));

    System.err.println("resourceBase = " + baseURI);
    String resourceBase = baseURI.toASCIIString();
    context.setResourceBase(resourceBase);
    context.setAttribute("ResourceBase", resourceBase);

    String welcome[] = {BASE_SCHEME + ":///index.html"};
    context.setWelcomeFiles(welcome);

    return context;
  }
コード例 #6
0
  public ServerMain(String configDir) throws Exception {

    config = Util.getConfig(configDir);
    // make sure we have all the correct dirs and files now
    ensureInstall();

    logger.info("Freeboard starting....");

    // do we have a USB drive connected?
    logger.info("USB drive " + Util.getUSBFile());

    // create a new Camel Main so we can easily start Camel
    Main main = new Main();

    // enable hangup support which mean we detect when the JVM terminates, and stop Camel graceful
    main.enableHangupSupport();

    NavDataWebSocketRoute route = new NavDataWebSocketRoute(config);
    // must do this early!
    CamelContextFactory.setContext(route);
    // web socket on port 9090
    logger.info("  Websocket port:" + config.getProperty(Constants.WEBSOCKET_PORT));
    route.setPort(Integer.valueOf(config.getProperty(Constants.WEBSOCKET_PORT)));

    // are we running demo?
    logger.info("  Serial url:" + config.getProperty(Constants.SERIAL_URL));
    route.setSerialUrl(config.getProperty(Constants.SERIAL_URL));

    // add our routes to Camel
    main.addRouteBuilder(route);

    Connector connector = new SelectChannelConnector();
    logger.info("  Webserver http port:" + config.getProperty(Constants.HTTP_PORT));
    connector.setPort(Integer.valueOf(config.getProperty(Constants.HTTP_PORT)));

    // virtual hosts
    String virtualHosts = config.getProperty(Constants.VIRTUAL_URL);
    server = new Server();
    server.addConnector(connector);

    // serve mapcache
    ServletContextHandler mapContext = new ServletContextHandler();
    logger.info("  Mapcache url:" + config.getProperty(Constants.MAPCACHE));
    mapContext.setContextPath(config.getProperty(Constants.MAPCACHE));
    logger.info("  Mapcache resource:" + config.getProperty(Constants.MAPCACHE_RESOURCE));
    mapContext.setResourceBase(config.getProperty(Constants.MAPCACHE_RESOURCE));
    ServletHolder mapHolder = mapContext.addServlet(DefaultServlet.class, "/*");
    mapHolder.setInitParameter("cacheControl", "max-age=3600,public");

    // serve tracks
    ServletContextHandler trackContext = new ServletContextHandler();
    logger.info("  Tracks url:" + config.getProperty(Constants.TRACKS));
    trackContext.setContextPath(config.getProperty(Constants.TRACKS));
    logger.info("  Tracks resource:" + config.getProperty(Constants.TRACKS_RESOURCE));
    trackContext.setResourceBase(config.getProperty(Constants.TRACKS_RESOURCE));
    trackContext.addServlet(DefaultServlet.class, "/*");

    if (StringUtils.isNotBlank(virtualHosts)) {
      mapContext.setVirtualHosts(virtualHosts.split(","));
      trackContext.setVirtualHosts(virtualHosts.split(","));
    }
    HandlerList handlers = new HandlerList();
    handlers.addHandler(mapContext);
    handlers.addHandler(trackContext);

    // serve freeboard
    WebAppContext wac = new WebAppContext();
    logger.info("  Freeboard resource:" + config.getProperty(Constants.FREEBOARD_RESOURCE));
    wac.setWar(config.getProperty(Constants.FREEBOARD_RESOURCE));
    wac.setDefaultsDescriptor(
        config.getProperty(Constants.FREEBOARD_RESOURCE) + "WEB-INF/webdefault.xml");

    wac.setDescriptor(config.getProperty(Constants.FREEBOARD_RESOURCE) + "WEB-INF/web.xml");
    logger.info("  Freeboard url:" + config.getProperty(Constants.FREEBOARD_URL));
    wac.setContextPath(config.getProperty(Constants.FREEBOARD_URL));
    wac.setServer(server);
    wac.setParentLoaderPriority(true);
    wac.setVirtualHosts(null);
    if (StringUtils.isNotBlank(virtualHosts)) {
      wac.setVirtualHosts(virtualHosts.split(","));
    }
    handlers.addHandler(wac);

    server.setHandler(handlers);
    server.start();

    // and run, which keeps blocking until we terminate the JVM (or stop CamelContext)
    main.run();

    // so now shutdown serial reader and server

    route.stopSerial();
    server.stop();
    System.exit(0);
  }