コード例 #1
0
  public void handle(ClientData clientData) {
    HttpServerRequest req = clientData.getRequest();
    HttpServerResponse res = req.response;
    String requestedFileName = clientData.getPath();
    String resourceRootDir = getRootDir();

    File rootDir = new File(requestedFileName);
    if (!rootDir.exists()) {
      copyFilesToDir();
    }

    switch (requestedFileName) {
      case "/static/flashsocket/WebSocketMainInsecure.swf":
        res.sendFile(resourceRootDir + "/WebSocketMainInsecure.swf");
        break;
      case "/static/flashsocket/WebSocketMain.swf":
        res.sendFile(resourceRootDir + "/WebSocketMain.swf");
        break;
      case "/socket.io.js":
        res.sendFile(resourceRootDir + "/socket.io.js");
        break;
      case "/socket.io.min.js":
        res.sendFile(resourceRootDir + "/socket.io.min.js");
        break;
      default:
        throw new IllegalArgumentException(requestedFileName);
    }
    res.close();
  }
コード例 #2
0
  /** text/javascript text/css text/html; charset=GBK */
  @Override
  public void handle(final HttpServerRequest req) {

    final HttpServerResponse res = req.response();

    String uri = req.path().substring(1);

    if (uri.startsWith("web")) {
      uri = uri.substring(4);
    }
    if (uri.isEmpty()) {
      uri = indexPage;
    }
    String ext = Utils.getFileExtWithDot(uri);
    if (".js".equals(ext) || ".css".equals(ext) || ".html".equals(ext)) {
      int i = 0;
      if (".css".equals(ext)) {
        i = 1;
      } else {
        i = 2;
      }
      res.headers().set("content-type", ctary[i]);
      String finalFile = webRootPath.resolve(uri).toString();

      //      if(fileSystem.existsSync(finalFile)){
      //        fileSystem.readFile(finalFile, new AsyncResultHandler<Buffer>() {
      //          public void handle(AsyncResult<Buffer> ar) {
      //            if (ar.succeeded()) {
      //              res.headers().set("content-length", ar.result().length() + "");
      //              res.end(ar.result());
      //            } else {
      //              logger.error("Failed to open file", ar.cause());
      //              res.end("File Read Error!");
      //            }
      //          }
      //        });
      //      } else {
      //        res.end("File Not Found!");
      //      }

      fileSystem.open(
          finalFile,
          new AsyncResultHandler<AsyncFile>() {
            public void handle(AsyncResult<AsyncFile> ar) {
              if (ar.succeeded()) {
                AsyncFile asyncFile = ar.result();
                res.setChunked(true);
                Pump.createPump(asyncFile, res).start();
                asyncFile.endHandler(
                    new VoidHandler() {
                      public void handle() {
                        res.end();
                      }
                    });
              } else {
                logger.error("Failed to open file", ar.cause());
                res.end("File Not Found!");
              }
            }
          });
    } else {
      res.sendFile(webRootPath.resolve(uri).toString());
    }
  }