Example #1
0
  protected HttpServer createHttpServer() throws IOException {
    HttpServer server = new HttpServer();

    SocketListener listener = new SocketListener();
    listener.setPort(SysConfig.getHttpPortAsInt());
    server.addListener(listener);

    return server;
  }
Example #2
0
  protected void configureHttpServer(HttpServer server) {

    // Favicon hack

    HttpContext faviconContext = new HttpContext();
    faviconContext.setContextPath("/favicon.ico");
    server.addContext(faviconContext);

    ResourceHandler faviconHandler = new ResourceHandler();
    faviconContext.setResourceBase(SysConfig.getStaticDir().getRootPath());
    faviconContext.addHandler(faviconHandler);

    faviconContext.addHandler(new NotFoundHandler());

    // robots.txt hack

    HttpContext robotsContext = new HttpContext();
    robotsContext.setContextPath("/robots.txt");
    server.addContext(robotsContext);

    ResourceHandler robotsHandler = new ResourceHandler();
    robotsContext.setResourceBase(SysConfig.getStaticDir().getRootPath());
    robotsContext.addHandler(robotsHandler);

    robotsContext.addHandler(new NotFoundHandler());

    // Dynamic content

    HttpContext servletContext = new HttpContext();
    servletContext.setContextPath("/");
    server.addContext(servletContext);

    ServletHandler servlets = new ServletHandler();
    servletContext.addHandler(servlets);
    servlets.addServlet("/*", "org.wahlzeit.main.MainServlet");

    servletContext.addHandler(new NotFoundHandler());

    // Photos content

    HttpContext photosContext = new HttpContext();
    photosContext.setContextPath(SysConfig.getPhotosDirAsString());
    server.addContext(photosContext);

    ResourceHandler photosHandler = new ResourceHandler();
    photosContext.setResourceBase(SysConfig.getPhotosDirAsString());
    photosContext.addHandler(photosHandler);

    photosContext.addHandler(new NotFoundHandler());

    // Static content

    HttpContext staticContext = new HttpContext();
    staticContext.setContextPath(SysConfig.getStaticDir().getRootPath());
    server.addContext(staticContext);

    ResourceHandler staticHandler = new ResourceHandler();
    staticContext.setResourceBase(SysConfig.getStaticDir().getRootPath());
    staticContext.addHandler(staticHandler);

    staticContext.addHandler(new NotFoundHandler());
  }