public synchronized Resource match(Request request, Response response) {
    Path path = request.getPath();
    String target = path.getPath();
    Resource resource = cache.get(target);

    if (resource == null) {
      resource = match(request, target);

      if (resource != null) {
        cache.put(target, resource);
      }
    }
    return resource;
  }
示例#2
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();
    }
  }
  @Override
  public void handle(Request req, Response res) {
    try {
      Path p = req.getPath();
      Query qry = req.getQuery();
      PrintStream ps = res.getPrintStream();
      long time = System.currentTimeMillis();
      String service;

      if (p.getSegments().length >= 1) {
        service = p.getSegments()[0];
      } else {
        service = "";
      }

      res.setValue("Content-Type", "text/html");
      res.setValue("Server", "SLPeripherals/1.0 (Simple 4.0)");
      res.setDate("Date", time);
      res.setDate("Last-Modified", time);

      // System.out.println("request" + url);

      try {
        if (Integer.valueOf(service) != null) {
          int i = Integer.valueOf(service);

          if (services.get(i) != null) {
            // put an event and wait for response

            IComputerAccess ica = (IComputerAccess) ((Object[]) services.get(i))[0];
            TileEntityHTTPD hd = (TileEntityHTTPD) ((Object[]) services.get(i))[1];

            hd.reqsw.add(ps);
            int idx = hd.reqsw.lastIndexOf(ps);

            // reqs.add("{bl}");
            // int idx=reqs.lastIndexOf("{bl}");

            Map<String, String> cookies = new HashMap<String, String>();

            for (Cookie c : res.getCookies()) {
              cookies.put(c.getName(), c.getValue());
            }

            ica.queueEvent("http_server_request", new Object[] {idx, p.getPath(1), qry, cookies});

            return;

            /*
             *
             * for (int ij = 1; ij <= 10; ij++) { Thread.sleep(100);
             *
             * if (! reqs.get(idx).equals("{bl}")) { // request
             * done. ps.print(reqs.get(idx)); ps.close(); return; }
             *
             * }
             *
             * // request timed out
             *
             * reqs.remove(idx);
             *
             *
             *
             * ps.print(
             * "<hr>The computer did not respond after 1 second, request timed out."
             * ); ps.close(); return;
             */

          } else {
            ps.print(
                "SLP HTTP Server (based on the Simple Framework)<br>"
                    + "The service "
                    + i
                    + " is not online.");
          }
        }
      } catch (Exception e) {
        ps.print(
            "SLP HTTP Server (based on the Simple Framework)<br>"
                + "Please go to a service by typing the computer id then a slash, example:<br>"
                + "http://localhost/4/<br>"
                + "would go to Computer ID 4's service. - The IDs are assigned by ComputerCraft.<br>"
                + "(please note 'localhost' depends on your setup. You may have to use a port number too. See the current URL.).<br>(exception encountered)");
      }

      ps.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }