private void addCtx(String path, HttpHandler h) {
   HttpContext ctx = server.createContext(path, h);
   if (configuration.isWebAuthenticate()) {
     ctx.setAuthenticator(
         new BasicAuthenticator("") {
           @Override
           public boolean checkCredentials(String user, String pwd) {
             LOGGER.debug("authenticate " + user);
             return pwd.equals(users.get(user));
             // return true;
           }
         });
   }
 }
Exemple #2
0
  /**
   * Initialize this JolokiaServer with the given HttpServer. The calle is responsible for managing
   * (starting/stopping) the HttpServer.
   *
   * @param pServer server to use
   * @param pConfig configuration
   * @param pLazy whether the initialization should be done lazy or not
   */
  protected final void init(HttpServer pServer, JolokiaServerConfig pConfig, boolean pLazy) {
    config = pConfig;
    lazy = pLazy;

    // Create proper context along with handler
    final String contextPath = pConfig.getContextPath();
    jolokiaHttpHandler = new JolokiaHttpHandler(pConfig.getJolokiaConfig());
    HttpContext context = pServer.createContext(contextPath, jolokiaHttpHandler);

    // Add authentication if configured
    final Authenticator authenticator = pConfig.getAuthenticator();
    if (authenticator != null) {
      context.setAuthenticator(authenticator);
    }

    url = detectAgentUrl(pServer, pConfig, contextPath);
  }
Exemple #3
0
 private static HttpServer createHttpServer(ExecutorService execs) throws Exception {
   InetSocketAddress inetAddress = new InetSocketAddress(0);
   HttpServer testServer = HttpServer.create(inetAddress, 5);
   testServer.setExecutor(execs);
   HttpContext context = testServer.createContext("/test");
   context.setHandler(
       new HttpHandler() {
         public void handle(HttpExchange msg) {
           try {
             synchronized (lock) {
               ++s_received;
               if ((s_received % 1000) == 0) {
                 System.out.println("Received=" + s_received);
               }
             }
             String method = msg.getRequestMethod();
             if (method.equals("POST")) {
               InputStream is = msg.getRequestBody();
               byte[] buf = readFully(is);
               is.close();
               writePostReply(msg, buf);
             } else {
               System.out.println("****** METHOD not handled ***** " + method);
               System.out.println("Received=" + s_received);
             }
             synchronized (lock) {
               ++sent;
               if ((sent % 1000) == 0) {
                 System.out.println("sent=" + sent);
               }
             }
           } catch (Exception e) {
             e.printStackTrace();
           } finally {
             msg.close();
           }
         }
       });
   return testServer;
 }
Exemple #4
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());
  }