Пример #1
0
  /**
   * handle a http request and serve a file
   *
   * @param request the http request
   * @param response the http response
   */
  @Override
  public void handle(Request request, Response response) {
    try {
      PrintStream body = response.getPrintStream();
      long time = System.currentTimeMillis();
      String msg = "";
      String home = System.getProperty("user.dir");
      String contentType = "text/plain";
      Path path = request.getPath();
      if (path.getPath().substring(path.getPath().length() - 3).equals(".js"))
        contentType = "application/javascript";
      if (path.getPath().substring(path.getPath().length() - 3).equals("css"))
        contentType = "text/css";

      response.set("Content-Type", contentType);
      response.set("Server", "ServerLyzer/1.0 (Simple 4.0)");
      response.setDate("Date", time);
      response.setDate("Last-Modified", time);
      String filename = wwwroot + path.getPath();

      String content;
      if (ServerLyzer.wwwroot == null) {
        content = new Scanner(new File(filename)).useDelimiter("\\Z").next();
        /*
        * UPdated because the code below gives :/home/pehlivanz/workspace/Pagelyzer/target/Pagelyzer-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/js/bomlib.js
        * for the jars
         URL resourceUrl = getClass().getResource(filename);
         content = new Scanner(new File(resourceUrl.toURI().getPath().toString())).useDelimiter("\\Z").next();
         */
      } else {
        content = new Scanner(new File(filename)).useDelimiter("\\Z").next();
      }
      body.print(content);

      body.println(msg);
      body.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }